2016-09-29 03:29:43 -07:00
|
|
|
from micropython import const
|
|
|
|
from trezor import loop
|
2017-02-08 07:20:20 -08:00
|
|
|
from trezor.ui import Widget
|
2016-09-27 06:32:12 -07:00
|
|
|
from .button import Button, BTN_CLICKED, BTN_STARTED
|
2016-06-01 06:06:00 -07:00
|
|
|
from .button import CONFIRM_BUTTON, CONFIRM_BUTTON_ACTIVE
|
|
|
|
from .button import CANCEL_BUTTON, CANCEL_BUTTON_ACTIVE
|
2016-09-27 07:08:02 -07:00
|
|
|
from .loader import Loader
|
2016-06-01 06:06:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
CONFIRMED = const(1)
|
|
|
|
CANCELLED = const(2)
|
|
|
|
|
|
|
|
|
2017-02-08 07:20:20 -08:00
|
|
|
class ConfirmDialog(Widget):
|
2016-06-01 06:06:00 -07:00
|
|
|
|
|
|
|
def __init__(self, content=None, confirm='Confirm', cancel='Cancel'):
|
|
|
|
self.content = content
|
2016-06-07 04:40:05 -07:00
|
|
|
self.confirm = Button((121, 240 - 48, 119, 48), confirm,
|
2016-06-01 06:06:00 -07:00
|
|
|
normal_style=CONFIRM_BUTTON,
|
|
|
|
active_style=CONFIRM_BUTTON_ACTIVE)
|
2016-06-07 04:40:05 -07:00
|
|
|
self.cancel = Button((0, 240 - 48, 119, 48), cancel,
|
2016-06-01 06:06:00 -07:00
|
|
|
normal_style=CANCEL_BUTTON,
|
|
|
|
active_style=CANCEL_BUTTON_ACTIVE)
|
|
|
|
|
|
|
|
def render(self):
|
|
|
|
self.confirm.render()
|
|
|
|
self.cancel.render()
|
|
|
|
|
2017-02-08 07:20:20 -08:00
|
|
|
def touch(self, event, pos):
|
|
|
|
if self.confirm.touch(event, pos) == BTN_CLICKED:
|
2016-06-01 06:06:00 -07:00
|
|
|
return CONFIRMED
|
2017-02-08 07:20:20 -08:00
|
|
|
if self.cancel.touch(event, pos) == BTN_CLICKED:
|
2016-06-01 06:06:00 -07:00
|
|
|
return CANCELLED
|
|
|
|
|
2016-09-21 08:50:11 -07:00
|
|
|
def __iter__(self):
|
2017-02-08 07:20:20 -08:00
|
|
|
yield loop.Wait((super().__iter__(), self.content))
|
2016-09-27 06:32:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
class HoldToConfirmDialog():
|
|
|
|
|
|
|
|
def __init__(self, button, content=None, *args, **kwargs):
|
|
|
|
self.button = button
|
|
|
|
self.content = content
|
|
|
|
self.loader = Loader(*args, **kwargs)
|
|
|
|
|
|
|
|
def render(self):
|
2016-10-20 06:07:56 -07:00
|
|
|
if self.loader.is_active():
|
|
|
|
self.loader.render()
|
|
|
|
elif self.content is not None:
|
|
|
|
self.content.render()
|
2016-09-27 06:32:12 -07:00
|
|
|
self.button.render()
|
|
|
|
|
|
|
|
def send(self, event, pos):
|
|
|
|
button = self.button
|
|
|
|
was_started = button.state & BTN_STARTED
|
|
|
|
button.send(event, pos)
|
|
|
|
is_started = button.state & BTN_STARTED
|
|
|
|
if is_started:
|
|
|
|
if not was_started:
|
|
|
|
self.loader.start()
|
|
|
|
else:
|
|
|
|
if was_started:
|
|
|
|
if self.loader.stop():
|
|
|
|
return CONFIRMED
|
2016-09-27 07:08:02 -07:00
|
|
|
if self.content is not None:
|
|
|
|
return self.content.send(event, pos)
|
2016-09-27 06:32:12 -07:00
|
|
|
|
|
|
|
async def __iter__(self):
|
2017-01-20 05:05:32 -08:00
|
|
|
return await loop.Wait([self._render_loop(),
|
|
|
|
self._event_loop()])
|
2016-09-27 06:32:12 -07:00
|
|
|
|
|
|
|
def _render_loop(self):
|
|
|
|
RENDER_DELAY = const(1000000 // 60)
|
|
|
|
while True:
|
|
|
|
self.render()
|
|
|
|
yield loop.Sleep(RENDER_DELAY)
|
|
|
|
|
|
|
|
def _event_loop(self):
|
|
|
|
while True:
|
|
|
|
event, *pos = yield loop.Select(loop.TOUCH)
|
|
|
|
result = self.send(event, pos)
|
|
|
|
if result is not None:
|
|
|
|
return result
|