1from utime import ticks_ms
2import gc
3
4ITEM_CNT = 200
5
6def draw_event_cb(e):
7    obj = e.get_target()
8    dsc = lv.obj_draw_part_dsc_t.__cast__(e.get_param())
9    # If the cells are drawn...
10    if dsc.part == lv.PART.ITEMS:
11        chk = obj.has_cell_ctrl(dsc.id, 0, lv.table.CELL_CTRL.CUSTOM_1)
12
13        rect_dsc = lv.draw_rect_dsc_t()
14        rect_dsc.init()
15
16        if chk:
17            rect_dsc.bg_color = lv.theme_get_color_primary(obj)
18        else:
19            rect_dsc.bg_color = lv.palette_lighten(lv.PALETTE.GREY, 2)
20
21        rect_dsc.radius = lv.RADIUS.CIRCLE
22
23        sw_area = lv.area_t()
24        sw_area.x1 = dsc.draw_area.x2 - 50
25        sw_area.x2 = sw_area.x1 + 40
26        sw_area.y1 = dsc.draw_area.y1 + dsc.draw_area.get_height() // 2 - 10
27        sw_area.y2 = sw_area.y1 + 20
28        dsc.draw_ctx.rect(rect_dsc, sw_area)
29
30        rect_dsc.bg_color = lv.color_white()
31
32        if chk:
33            sw_area.x2 -= 2
34            sw_area.x1 = sw_area.x2 - 16
35        else:
36            sw_area.x1 += 2
37            sw_area.x2 = sw_area.x1 + 16
38        sw_area.y1 += 2
39        sw_area.y2 -= 2
40        dsc.draw_ctx.rect(rect_dsc, sw_area)
41
42def change_event_cb(e):
43    obj = e.get_target()
44    row = lv.C_Pointer()
45    col = lv.C_Pointer()
46    table.get_selected_cell(row, col)
47    # print("row: ",row.uint_val)
48
49    chk = table.has_cell_ctrl(row.uint_val, 0, lv.table.CELL_CTRL.CUSTOM_1)
50    if chk:
51        table.clear_cell_ctrl(row.uint_val, 0, lv.table.CELL_CTRL.CUSTOM_1)
52    else:
53        table.add_cell_ctrl(row.uint_val, 0, lv.table.CELL_CTRL.CUSTOM_1)
54
55#
56# A very light-weighted list created from table
57#
58
59# Measure memory usage
60gc.enable()
61gc.collect()
62mem_free = gc.mem_free()
63print("mem_free: ", mem_free)
64t = ticks_ms()
65print("ticks: ", t)
66table = lv.table(lv.scr_act())
67
68# Set a smaller height to the table. It'll make it scrollable
69table.set_size(150, 200)
70
71table.set_col_width(0, 150)
72table.set_row_cnt(ITEM_CNT)  # Not required but avoids a lot of memory reallocation lv_table_set_set_value
73table.set_col_cnt(1)
74
75# Don't make the cell pressed, we will draw something different in the event
76table.remove_style(None, lv.PART.ITEMS | lv.STATE.PRESSED)
77
78for i in range(ITEM_CNT):
79    table.set_cell_value(i, 0, "Item " + str(i+1))
80
81table.align(lv.ALIGN.CENTER, 0, -20)
82
83# Add an event callback to apply some custom drawing
84table.add_event_cb(draw_event_cb, lv.EVENT.DRAW_PART_END, None)
85table.add_event_cb(change_event_cb, lv.EVENT.VALUE_CHANGED, None)
86
87gc.collect()
88mem_used = mem_free - gc.mem_free()
89elaps = ticks_ms()-t
90
91label = lv.label(lv.scr_act())
92label.set_text(str(ITEM_CNT) + " items were created in " + str(elaps) + " ms\n using " + str(mem_used) + " bytes of memory")
93#label.set_text(str(ITEM_CNT) + " items were created in " + str(elaps) + " ms")
94
95label.align(lv.ALIGN.BOTTOM_MID, 0, -10)
96