2 import integration.libtui as tui
3 from integration.libtui import ColorScope, TuiColor, Alignment, EventType
5 def create_buttons(main_ctx, btn_defs, sizes = "*,*"):
6 size_defs = ",".join(['*'] * len(btn_defs))
8 layout = tui.FlexLinearLayout(main_ctx, "buttons", size_defs)
9 layout.orientation(tui.FlexLinearLayout.LANDSCAPE)
10 layout.set_size(*(sizes.split(',')[:2]))
11 layout.set_padding(1, 1, 1, 1)
12 layout.set_alignment(Alignment.CENTER | Alignment.BOT)
14 for i, btn_def in enumerate(btn_defs):
15 but1 = tui.TuiButton(main_ctx, "b1")
16 but1.set_text(btn_def["text"])
17 but1.set_click_callback(btn_def["onclick"])
18 but1.set_alignment(Alignment.CENTER)
20 layout.set_cell(i, but1)
24 def create_title(ctx, title):
25 _t = tui.TuiLabel(ctx, "label")
27 _t.set_local_pos(1, 0)
28 _t.set_alignment(Alignment.TOP | Alignment.CENTER)
33 class ListView(tui.TuiObject):
34 def __init__(self, context, id):
35 super().__init__(context, id)
37 self.__create_layout()
39 self.__sel_changed = None
42 def __create_layout(self):
43 hint_moveup = tui.TuiLabel(self._context, "movup")
44 hint_moveup.override_color(ColorScope.HINT)
45 hint_moveup.set_text("^^^ - MORE")
46 hint_moveup.set_visbility(False)
47 hint_moveup.set_alignment(Alignment.TOP)
49 hint_movedown = tui.TuiLabel(self._context, "movdown")
50 hint_movedown.override_color(ColorScope.HINT)
51 hint_movedown.set_text("vvv - MORE")
52 hint_movedown.set_visbility(False)
53 hint_movedown.set_alignment(Alignment.BOT)
55 list_ = tui.SimpleList(self._context, "list")
56 list_.set_size("*", "*")
57 list_.set_alignment(Alignment.CENTER | Alignment.TOP)
59 list_.set_onselected_cb(self._on_selected)
60 list_.set_onselection_change_cb(self._on_sel_changed)
62 scroll = tui.TuiScrollable(self._context, "scroll")
63 scroll.set_size("*", "*")
64 scroll.set_alignment(Alignment.CENTER)
65 scroll.set_content(list_)
67 layout = tui.FlexLinearLayout(
68 self._context, f"main_layout", "2,*,2")
69 layout.set_size("*", "*")
70 layout.set_alignment(Alignment.CENTER)
71 layout.orientation(tui.FlexLinearLayout.PORTRAIT)
72 layout.set_parent(self)
74 layout.set_cell(0, hint_moveup)
75 layout.set_cell(1, scroll)
76 layout.set_cell(2, hint_movedown)
78 self.__hint_up = hint_moveup
79 self.__hint_down = hint_movedown
81 self.__scroll = scroll
82 self.__layout = layout
84 def add_item(self, item):
85 self.__list.add_item(item)
93 more_above = not self.__scroll.reached_top()
94 more_below = not self.__scroll.reached_last()
95 self.__hint_up.set_visbility(more_above)
96 self.__hint_down.set_visbility(more_below)
98 self.__layout.on_draw()
102 self.__layout.on_layout()
104 def _on_sel_changed(self, listv, prev, new):
105 h = self.__scroll._size.y()
106 self.__scroll.set_scrollY((new + 1) // h * h)
108 if self.__sel_changed:
109 self.__sel_changed(listv, prev, new)
111 def _on_selected(self, listv, index, item):
113 self.__sel(listv, index, item)
115 def set_onselected_cb(self, cb):
118 def set_onselect_changed_cb(self, cb):
119 self.__sel_changed = cb
121 class Dialogue(tui.TuiContext):
126 def __init__(self, session, title = "", content = "", input=False,
127 ok_btn = "OK", no_btn = "No", abort_btn = None):
128 super().__init__(session)
131 { "text": ok_btn, "onclick": lambda x: self._ok_onclick() }
137 "onclick": lambda x: self._no_onclick()
142 "onclick": lambda x: self._abort_onclick()
145 self.__title_txt = title
146 self.__status = Dialogue.Pending
147 self.__content = content
148 self.__input_dialog = input
151 self.set_size("70", "0.5*")
152 self.set_alignment(Alignment.CENTER)
154 def set_content(self, content):
155 self.__content = content
157 def set_input_dialogue(self, yes):
158 self.__input_dialog = yes
161 self.__create_layout(self.__title_txt)
163 def _handle_key_event(self, key):
167 super()._handle_key_event(key)
170 def _ok_onclick(self):
171 self.__status = Dialogue.Yes
174 def _no_onclick(self):
175 self.__status = Dialogue.No
178 def _abort_onclick(self):
179 self.__status = Dialogue.Abort
182 def __create_layout(self, title):
183 panel = tui.TuiPanel(self, "panel")
184 layout = tui.FlexLinearLayout(self, "layout", "*,3")
185 btn_grp = create_buttons(self, self.__btns)
186 t = create_title(self, title)
187 content = self.__create_content()
190 self.__layout = layout
193 panel._dyn_size.set(self._dyn_size)
194 panel._local_pos.set(self._local_pos)
195 panel.set_alignment(self._align)
196 panel.drop_shadow(1, 2)
199 layout.orientation(tui.FlexLinearLayout.PORTRAIT)
200 layout.set_size("*", "*")
201 layout.set_padding(4, 1, 1, 1)
203 t.set_alignment(Alignment.CENTER | Alignment.TOP)
205 layout.set_cell(0, content)
206 layout.set_cell(1, btn_grp)
213 def __create_content(self):
215 if isinstance(self.__content, str):
216 text = tui.TuiTextBlock(self, "tb")
217 text.set_size("0.6*", "0.5*")
218 text.set_alignment(Alignment.CENTER)
219 text.set_text(self.__content)
220 elif self.__content is not None:
221 return self.__content
223 if not self.__input_dialog:
224 self.set_size(h = "20")
227 tb = tui.TuiTextBox(self, "input")
228 tb.set_size("0.5*", "3")
229 tb.set_alignment(Alignment.CENTER)
232 layout = tui.FlexLinearLayout(self, "layout", "*,5")
233 layout.orientation(tui.FlexLinearLayout.PORTRAIT)
234 layout.set_size("*", "*")
235 layout.set_cell(0, text)
236 layout.set_cell(1, tb)
239 self.set_size(h = "10")
241 self.set_curser_mode(1)
248 self.session().pop_context()
253 def show(self, title=None):
255 self.__title.set_text(title)
256 self.session().push_context(self)
259 def show_dialog(session, title, text):
260 dia = Dialogue(session, title=title, content=text, no_btn=None)