From d2f75605551d07f28db500e4379470b5f8c41074 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Thu, 21 Feb 2019 09:33:35 +0100 Subject: [PATCH] ui: introduce numpad The numpad allows to easily enter numbers from an arbitrary range. --- src/trezor/ui/num_pad.py | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/trezor/ui/num_pad.py diff --git a/src/trezor/ui/num_pad.py b/src/trezor/ui/num_pad.py new file mode 100644 index 0000000000..e841cce2de --- /dev/null +++ b/src/trezor/ui/num_pad.py @@ -0,0 +1,47 @@ +from trezor import ui +from trezor.ui import display +from trezor.ui.button import BTN_CLICKED, Button + +ITEMS_PER_PAGE = 11 + + +def digit_area(i): + return ui.grid(i + 3) # skip the first line + + +class NumPad(ui.Widget): + def __init__(self, label: str, start: int, end: int): + """ + Generates a numpad with numbers from `start` to `end` excluding. + """ + self.label = label + self.start = start + self.end = end + self.page = 0 + + self._generate_buttons() + + def render(self): + for btn in self.buttons: + btn.render() + + # header label + display.text_center(ui.WIDTH // 2, 36, self.label, ui.BOLD, ui.GREY, ui.BG) + + def touch(self, event, pos): + for btn in self.buttons: + if btn.touch(event, pos) == BTN_CLICKED: + if "+" in btn.content: + self.page += 1 + self._generate_buttons() + else: + return btn.content + + def _generate_buttons(self): + display.clear() # we need to clear old buttons + start = self.start + (ITEMS_PER_PAGE + 1) * self.page - self.page + end = min(self.end, (ITEMS_PER_PAGE + 1) * (self.page + 1) - self.page) + digits = list(range(start, end)) + if len(digits) == ITEMS_PER_PAGE: + digits.append(str(end) + "+") + self.buttons = [Button(digit_area(i), str(d)) for i, d in enumerate(digits)]