1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

Merge pull request #480 from trezor/tsusanka/numpad

NumPad to enter a number from arbitrary range easily
This commit is contained in:
Tomas Susanka 2019-03-14 10:38:52 +01:00 committed by GitHub
commit edb8177f1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

47
src/trezor/ui/num_pad.py Normal file
View File

@ -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)]