1import urandom
2
3currentButton = None
4list1 = None
5
6def event_handler(evt):
7    global currentButton
8    code = evt.get_code()
9    obj = evt.get_target()
10    if code == lv.EVENT.CLICKED:
11        if currentButton == obj:
12            currentButton = None
13        else:
14            currentButton = obj
15        parent = obj.get_parent()
16        for i in range( parent.get_child_cnt()):
17            child = parent.get_child(i)
18            if child == currentButton:
19                child.add_state(lv.STATE.CHECKED)
20            else:
21                child.clear_state(lv.STATE.CHECKED)
22
23def event_handler_top(evt):
24    global currentButton
25    code = evt.get_code()
26    obj = evt.get_target()
27    if code == lv.EVENT.CLICKED:
28        if currentButton == None:
29            return
30        currentButton.move_background()
31        currentButton.scroll_to_view( lv.ANIM.ON)
32
33def event_handler_up(evt):
34    global currentButton
35    code = evt.get_code()
36    obj = evt.get_target()
37    if code == lv.EVENT.CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
38        if currentButton == None:
39            return
40        index = currentButton.get_index()
41        if index <= 0:
42            return
43        currentButton.move_to_index(index - 1)
44        currentButton.scroll_to_view(lv.ANIM.ON)
45
46def event_handler_center(evt):
47    global currentButton
48    code = evt.get_code()
49    obj = evt.get_target()
50    if code == lv.EVENT.CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
51        if currentButton == None:
52            return
53        parent = currentButton.get_parent()
54        pos = parent.get_child_cnt() // 2
55        currentButton.move_to_index(pos)
56        currentButton.scroll_to_view(lv.ANIM.ON)
57
58def event_handler_dn(evt):
59    global currentButton
60    code = evt.get_code()
61    obj = evt.get_target()
62    if code == lv.EVENT.CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
63        if currentButton == None:
64            return
65        index = currentButton.get_index()
66        currentButton.move_to_index(index + 1)
67        currentButton.scroll_to_view(lv.ANIM.ON)
68
69def event_handler_bottom(evt):
70    global currentButton
71    code = evt.get_code()
72    obj = evt.get_target()
73    if code == lv.EVENT.CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
74        if currentButton == None:
75            return
76        currentButton.move_foreground()
77        currentButton.scroll_to_view(lv.ANIM.ON)
78
79def event_handler_swap(evt):
80    global currentButton
81    global list1
82    code = evt.get_code()
83    obj = evt.get_target()
84    if code == lv.EVENT.CLICKED:
85        cnt = list1.get_child_cnt()
86        for i in range(100):
87            if cnt > 1:
88                obj = list1.get_child(urandom.getrandbits(32) % cnt )
89                obj.move_to_index(urandom.getrandbits(32) % cnt)
90        if currentButton != None:
91            currentButton.scroll_to_view(lv.ANIM.ON)
92
93#Create a list with buttons that can be sorted
94list1 = lv.list(lv.scr_act())
95list1.set_size(lv.pct(60), lv.pct(100))
96list1.set_style_pad_row( 5, 0)
97
98for i in range(15):
99    btn = lv.btn(list1)
100    btn.set_width(lv.pct(100))
101    btn.add_event_cb( event_handler, lv.EVENT.CLICKED, None)
102    lab = lv.label(btn)
103    lab.set_text("Item " + str(i))
104
105#Select the first button by default
106currentButton = list1.get_child(0)
107currentButton.add_state(lv.STATE.CHECKED)
108
109#Create a second list with up and down buttons
110list2 = lv.list(lv.scr_act())
111list2.set_size(lv.pct(40), lv.pct(100))
112list2.align(lv.ALIGN.TOP_RIGHT, 0, 0)
113list2.set_flex_flow(lv.FLEX_FLOW.COLUMN)
114
115btn = list2.add_btn(None, "Top")
116btn.add_event_cb(event_handler_top, lv.EVENT.ALL, None)
117lv.group_remove_obj(btn)
118
119btn = list2.add_btn(lv.SYMBOL.UP, "Up")
120btn.add_event_cb(event_handler_up, lv.EVENT.ALL, None)
121lv.group_remove_obj(btn)
122
123btn = list2.add_btn(lv.SYMBOL.LEFT, "Center")
124btn.add_event_cb(event_handler_center, lv.EVENT.ALL, None)
125lv.group_remove_obj(btn)
126
127btn = list2.add_btn(lv.SYMBOL.DOWN, "Down")
128btn.add_event_cb(event_handler_dn, lv.EVENT.ALL, None)
129lv.group_remove_obj(btn)
130
131btn = list2.add_btn(None, "Bottom")
132btn.add_event_cb(event_handler_bottom, lv.EVENT.ALL, None)
133lv.group_remove_obj(btn)
134
135btn = list2.add_btn(lv.SYMBOL.SHUFFLE, "Shuffle")
136btn.add_event_cb(event_handler_swap, lv.EVENT.ALL, None)
137lv.group_remove_obj(btn)
138