parent
10e5ec6135
commit
a4039f9459
@ -1,55 +1,59 @@
|
||||
from micropython import const
|
||||
|
||||
from trezor import ui
|
||||
from trezor.ui import text
|
||||
from trezor import res, ui
|
||||
from trezor.ui.text import TEXT_HEADER_HEIGHT, TEXT_LINE_HEIGHT
|
||||
|
||||
_CHECKLIST_MAX_LINES = const(5)
|
||||
_CHECKLIST_OFFSET_X = const(24)
|
||||
_CHECKLIST_OFFSET_X_ICON = const(0)
|
||||
|
||||
|
||||
class Checklist(ui.Control):
|
||||
def __init__(self, title, icon):
|
||||
self.title = title
|
||||
self.icon = icon
|
||||
self.choices = []
|
||||
self.words = []
|
||||
self.items = []
|
||||
self.active = 0
|
||||
self.repaint = False
|
||||
self.repaint = True
|
||||
|
||||
def add(self, choice):
|
||||
self.choices.append(choice)
|
||||
self.items.append(choice)
|
||||
|
||||
def select(self, active):
|
||||
self.active = active
|
||||
|
||||
def process(self):
|
||||
w = self.words
|
||||
w.clear()
|
||||
for index, choice in enumerate(self.choices):
|
||||
if index < self.active:
|
||||
w.append(ui.BOLD)
|
||||
w.append(ui.GREEN)
|
||||
elif index == self.active:
|
||||
w.append(ui.BOLD)
|
||||
w.append(ui.FG)
|
||||
else: # index > self.active
|
||||
w.append(ui.NORMAL)
|
||||
w.append(ui.GREY)
|
||||
if isinstance(choice, str):
|
||||
w.append(choice)
|
||||
else: # choice is iterable
|
||||
w.extend(choice)
|
||||
w.append(text.BR)
|
||||
self.words = w
|
||||
self.repaint = True
|
||||
|
||||
def on_render(self):
|
||||
if self.repaint:
|
||||
ui.header(self.title, self.icon)
|
||||
text.render_text(
|
||||
self.words,
|
||||
new_lines=False, # we are adding line breaks manually
|
||||
max_lines=_CHECKLIST_MAX_LINES,
|
||||
offset_x=_CHECKLIST_OFFSET_X,
|
||||
)
|
||||
self.render_items()
|
||||
self.repaint = False
|
||||
|
||||
def render_items(self):
|
||||
offset_x = _CHECKLIST_OFFSET_X
|
||||
offset_y = TEXT_HEADER_HEIGHT + TEXT_LINE_HEIGHT
|
||||
bg = ui.BG
|
||||
|
||||
for index, item in enumerate(self.items):
|
||||
# compute font and color
|
||||
if index < self.active:
|
||||
font = ui.BOLD
|
||||
fg = ui.GREEN
|
||||
elif index == self.active:
|
||||
font = ui.BOLD
|
||||
fg = ui.FG
|
||||
else: # index > self.active
|
||||
font = ui.NORMAL
|
||||
fg = ui.GREY
|
||||
|
||||
# render item icon
|
||||
icon = res.load(ui.ICON_CONFIRM)
|
||||
ui.display.icon(0, offset_y - 14, icon, fg, bg)
|
||||
|
||||
# render item text
|
||||
if isinstance(item, str):
|
||||
ui.display.text(offset_x, offset_y, item, font, fg, bg)
|
||||
offset_y += TEXT_LINE_HEIGHT
|
||||
else:
|
||||
for line in item:
|
||||
ui.display.text(offset_x, offset_y, line, font, fg, bg)
|
||||
offset_y += TEXT_LINE_HEIGHT
|
||||
|
Loading…
Reference in new issue