mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 12:00:59 +00:00
WIP: more UI experiments, now with 100% more OOP
This commit is contained in:
parent
4fec964a90
commit
15a03ce994
@ -3,56 +3,12 @@ from trezor import ui
|
|||||||
from trezor.utils import unimport_func
|
from trezor.utils import unimport_func
|
||||||
|
|
||||||
|
|
||||||
def multiplex_touch_events(gens):
|
|
||||||
while True:
|
|
||||||
event = yield loop.Wait([
|
|
||||||
loop.TOUCH_START,
|
|
||||||
loop.TOUCH_MOVE,
|
|
||||||
loop.TOUCH_END,
|
|
||||||
])
|
|
||||||
for gen in gens:
|
|
||||||
gen.send(event)
|
|
||||||
|
|
||||||
|
|
||||||
def in_area(pos, area):
|
def in_area(pos, area):
|
||||||
x, y = pos
|
x, y = pos
|
||||||
ax, ay, aw, ah = area
|
ax, ay, aw, ah = area
|
||||||
return ax <= x <= ax + aw and ay <= y <= ay + ah
|
return ax <= x <= ax + aw and ay <= y <= ay + ah
|
||||||
|
|
||||||
|
|
||||||
def click_in(area, click=None, enter=None, leave=None):
|
|
||||||
while True:
|
|
||||||
e, pos = yield
|
|
||||||
if e is not loop.TOUCH_START or not in_area(pos, area):
|
|
||||||
continue
|
|
||||||
|
|
||||||
inside = True
|
|
||||||
if enter:
|
|
||||||
enter()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
e, pos = yield
|
|
||||||
if e is loop.TOUCH_MOVE:
|
|
||||||
if in_area(pos, area):
|
|
||||||
if not inside:
|
|
||||||
if enter:
|
|
||||||
enter()
|
|
||||||
inside = True
|
|
||||||
else:
|
|
||||||
if inside:
|
|
||||||
if leave:
|
|
||||||
leave()
|
|
||||||
inside = False
|
|
||||||
elif e is loop.TOUCH_END:
|
|
||||||
if in_area(pos, area):
|
|
||||||
if click:
|
|
||||||
click()
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
default_button = {
|
default_button = {
|
||||||
'bg-color': ui.WHITE,
|
'bg-color': ui.WHITE,
|
||||||
'fg-color': ui.BLACK,
|
'fg-color': ui.BLACK,
|
||||||
@ -85,90 +41,102 @@ confirm_button_active = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def render_button(area, text, style):
|
BTN_CLICKED = const(1)
|
||||||
ax, ay, aw, ah = area
|
|
||||||
tx = ax + aw // 2
|
BTN_STARTED = const(1)
|
||||||
ty = ay + ah - 5
|
BTN_ACTIVE = const(2)
|
||||||
ui.display.bar(ax, ay, aw, ah, style['bg-color'])
|
BTN_DIRTY = const(4)
|
||||||
ui.display.text_center(tx, ty, text,
|
|
||||||
style['text-style'],
|
|
||||||
style['fg-color'],
|
|
||||||
style['bg-color'])
|
|
||||||
|
|
||||||
|
|
||||||
def button_widget(area, text,
|
class Button():
|
||||||
click=None,
|
|
||||||
style=default_button,
|
|
||||||
active_style=default_button_active):
|
|
||||||
|
|
||||||
def enter():
|
def __init__(self, area, text,
|
||||||
render_button(area, text, active_style)
|
style=default_button,
|
||||||
|
active_style=default_button_active):
|
||||||
|
self.area = area
|
||||||
|
self.text = text
|
||||||
|
self.style = style
|
||||||
|
self.active_style = active_style
|
||||||
|
self.state = BTN_DIRTY
|
||||||
|
|
||||||
def leave():
|
def render(self):
|
||||||
render_button(area, text, style)
|
if not self.state & BTN_DIRTY:
|
||||||
|
return
|
||||||
|
state = self.state & ~BTN_DIRTY
|
||||||
|
style = self.active_style if state & BTN_ACTIVE else self.style
|
||||||
|
ax, ay, aw, ah = self.area
|
||||||
|
tx = ax + aw // 2
|
||||||
|
ty = ay + ah - 5
|
||||||
|
ui.display.bar(ax, ay, aw, ah, style['bg-color'])
|
||||||
|
ui.display.text_center(tx, ty, self.text,
|
||||||
|
style['text-style'],
|
||||||
|
style['fg-color'],
|
||||||
|
style['bg-color'])
|
||||||
|
self.state = state
|
||||||
|
|
||||||
def _click():
|
def progress(self, event, pos):
|
||||||
leave()
|
if event is loop.TOUCH_START:
|
||||||
click()
|
if in_area(pos, self.area):
|
||||||
|
self.state = BTN_STARTED | BTN_DIRTY | BTN_ACTIVE
|
||||||
render_button(area, text, style)
|
elif event is loop.TOUCH_MOVE and self.state & BTN_STARTED:
|
||||||
return click_in(area, _click, enter, leave)
|
if in_area(pos, self.area):
|
||||||
|
if not self.state & BTN_ACTIVE:
|
||||||
|
self.state = BTN_STARTED | BTN_DIRTY | BTN_ACTIVE
|
||||||
|
else:
|
||||||
|
if self.state & BTN_ACTIVE:
|
||||||
|
self.state = BTN_STARTED | BTN_DIRTY
|
||||||
|
elif event is loop.TOUCH_END and self.state & BTN_STARTED:
|
||||||
|
self.state = BTN_DIRTY
|
||||||
|
if in_area(pos, self.area):
|
||||||
|
return BTN_CLICKED
|
||||||
|
|
||||||
|
|
||||||
def pin_widget():
|
def digit_area(d):
|
||||||
|
|
||||||
pin = ''
|
|
||||||
width = const(80)
|
width = const(80)
|
||||||
height = const(60)
|
height = const(60)
|
||||||
margin = const(5)
|
margin = const(1)
|
||||||
|
x = ((d - 1) % 3) * width
|
||||||
|
y = ((d - 1) // 3) * height
|
||||||
|
return (
|
||||||
|
x + margin,
|
||||||
|
y + margin,
|
||||||
|
width - margin,
|
||||||
|
height - margin)
|
||||||
|
|
||||||
def digit_area(d):
|
|
||||||
x = ((d - 1) % 3) * width
|
|
||||||
y = ((d - 1) // 3) * height
|
|
||||||
return (
|
|
||||||
x + margin,
|
|
||||||
y + margin,
|
|
||||||
width - margin,
|
|
||||||
height - margin
|
|
||||||
)
|
|
||||||
|
|
||||||
def append_digit(d):
|
PIN_CONFIRMED = const(1)
|
||||||
pin += str(d)
|
PIN_CANCELLED = const(2)
|
||||||
print('PIN so far: ', pin)
|
|
||||||
|
|
||||||
def digit_widget(digit):
|
|
||||||
nonlocal pin
|
|
||||||
area = digit_area(digit)
|
|
||||||
button = button_widget(
|
|
||||||
area,
|
|
||||||
str(digit),
|
|
||||||
lambda: append_digit(digit))
|
|
||||||
return button
|
|
||||||
|
|
||||||
digits = (1, 2, 3, 4, 5, 6, 7, 8, 9)
|
class PinDialog():
|
||||||
buttons = [digit_widget(d) for d in digits]
|
|
||||||
|
|
||||||
def cancel():
|
def __init__(self):
|
||||||
raise StopIteration(None)
|
self.confirm_button = Button(
|
||||||
|
(0, 240-60, 120, 60), 'Confirm',
|
||||||
|
style=confirm_button,
|
||||||
|
active_style=confirm_button_active)
|
||||||
|
self.cancel_button = Button(
|
||||||
|
(120, 240-60, 120, 60), 'Cancel',
|
||||||
|
style=cancel_button,
|
||||||
|
active_style=cancel_button_active)
|
||||||
|
self.pin_buttons = [
|
||||||
|
Button(digit_area(d), str(d)) for d in range(1, 10)]
|
||||||
|
self.pin = ''
|
||||||
|
|
||||||
def confirm():
|
def render(self):
|
||||||
raise StopIteration(pin)
|
for b in self.pin_buttons:
|
||||||
|
b.render()
|
||||||
|
self.confirm_button.render()
|
||||||
|
self.cancel_button.render()
|
||||||
|
|
||||||
cancel_widget = button_widget((0, 240 - 60, 120, 60), 'Cancel',
|
def progress(self, event, pos):
|
||||||
click=cancel,
|
for b in self.pin_buttons:
|
||||||
style=cancel_button,
|
if b.progress(event, pos) is BTN_CLICKED:
|
||||||
active_style=cancel_button_active)
|
self.pin += b.text
|
||||||
confirm_widget = button_widget((120, 240 - 60, 120, 60), 'Confirm',
|
if self.confirm_button.progress(event, pos) is BTN_CLICKED:
|
||||||
click=confirm,
|
return PIN_CONFIRMED
|
||||||
style=confirm_button,
|
if self.cancel_button.progress(event, pos) is BTN_CLICKED:
|
||||||
active_style=confirm_button_active)
|
return PIN_CANCELLED
|
||||||
|
|
||||||
buttons.append(cancel_widget)
|
|
||||||
buttons.append(confirm_widget)
|
|
||||||
for but in buttons:
|
|
||||||
next(but)
|
|
||||||
|
|
||||||
return buttons
|
|
||||||
|
|
||||||
|
|
||||||
def layout_tap_to_confirm(address, amount, currency):
|
def layout_tap_to_confirm(address, amount, currency):
|
||||||
@ -193,17 +161,25 @@ def layout_tap_to_confirm(address, amount, currency):
|
|||||||
|
|
||||||
# animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000)
|
# animation = ui.animate_pulse(func, ui.BLACK, ui.GREY, speed=200000)
|
||||||
|
|
||||||
# button = button_widget((20, 20, 210, 60), 'HELLO WORLD!')
|
pin = PinDialog()
|
||||||
# next(button)
|
|
||||||
|
|
||||||
pin = pin_widget()
|
while True:
|
||||||
|
pin.render()
|
||||||
|
|
||||||
result = yield loop.Wait((
|
# TODO: if we simply wait for any of scalar events, we can use
|
||||||
# animation,
|
# something much more lightweight than loop.Wait
|
||||||
multiplex_touch_events(pin),
|
event, pos = yield loop.Wait([
|
||||||
))
|
loop.TOUCH_START,
|
||||||
|
loop.TOUCH_MOVE,
|
||||||
print(result)
|
loop.TOUCH_END,
|
||||||
|
])
|
||||||
|
result = pin.progress(event, pos)
|
||||||
|
if result is PIN_CONFIRMED:
|
||||||
|
print('PIN confirmed:', pin.pin)
|
||||||
|
return
|
||||||
|
elif result is PIN_CANCELLED:
|
||||||
|
print('PIN CANCELLED, go home')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
@unimport_func
|
@unimport_func
|
||||||
|
Loading…
Reference in New Issue
Block a user