2016-09-29 03:29:43 -07:00
|
|
|
from micropython import const
|
2018-01-23 05:18:13 -08:00
|
|
|
from trezor import loop, res, ui
|
2017-02-08 07:20:20 -08:00
|
|
|
from trezor.ui import Widget
|
2018-01-23 05:18:13 -08:00
|
|
|
from trezor.ui.button import BTN_ACTIVE, BTN_CLICKED, BTN_STARTED, Button
|
2017-09-25 08:23:24 -07:00
|
|
|
from trezor.ui.loader import Loader
|
2016-06-01 06:06:00 -07:00
|
|
|
|
|
|
|
CONFIRMED = const(1)
|
|
|
|
CANCELLED = const(2)
|
2017-09-26 08:05:53 -07:00
|
|
|
DEFAULT_CONFIRM = res.load(ui.ICON_CONFIRM)
|
2018-02-27 14:48:51 -08:00
|
|
|
DEFAULT_CANCEL = res.load(ui.ICON_CANCEL)
|
2016-06-01 06:06:00 -07:00
|
|
|
|
2017-10-03 02:43:56 -07:00
|
|
|
|
2017-02-08 07:20:20 -08:00
|
|
|
class ConfirmDialog(Widget):
|
2017-06-12 09:16:06 -07:00
|
|
|
|
2018-02-12 07:42:03 -08:00
|
|
|
def __init__(self, content, confirm=DEFAULT_CONFIRM, cancel=DEFAULT_CANCEL, confirm_style=ui.BTN_CONFIRM, cancel_style=ui.BTN_CANCEL):
|
2016-06-01 06:06:00 -07:00
|
|
|
self.content = content
|
2017-03-29 05:46:41 -07:00
|
|
|
if cancel is not None:
|
2018-01-23 05:18:13 -08:00
|
|
|
self.confirm = Button(
|
2018-02-12 07:42:03 -08:00
|
|
|
ui.grid(9, n_x=2), confirm, style=confirm_style)
|
2018-01-23 05:18:13 -08:00
|
|
|
self.cancel = Button(
|
2018-02-12 07:42:03 -08:00
|
|
|
ui.grid(8, n_x=2), cancel, style=cancel_style)
|
2017-03-29 05:46:41 -07:00
|
|
|
else:
|
2018-01-23 05:18:13 -08:00
|
|
|
self.confirm = Button(
|
2018-02-12 07:42:03 -08:00
|
|
|
ui.grid(4, n_x=1), confirm, style=confirm_style)
|
2018-01-11 10:44:56 -08:00
|
|
|
self.cancel = None
|
2016-06-01 06:06:00 -07:00
|
|
|
|
|
|
|
def render(self):
|
|
|
|
self.confirm.render()
|
2017-03-29 05:46:41 -07:00
|
|
|
if self.cancel is not None:
|
|
|
|
self.cancel.render()
|
2016-06-01 06:06:00 -07:00
|
|
|
|
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-03-29 05:46:41 -07:00
|
|
|
if self.cancel is not None:
|
|
|
|
if self.cancel.touch(event, pos) == BTN_CLICKED:
|
|
|
|
return CANCELLED
|
2016-06-01 06:06:00 -07:00
|
|
|
|
2017-03-22 07:15:12 -07:00
|
|
|
async def __iter__(self):
|
2017-09-16 06:00:31 -07:00
|
|
|
return await loop.wait(super().__iter__(), self.content)
|
2016-09-27 06:32:12 -07:00
|
|
|
|
|
|
|
|
2017-06-20 03:02:38 -07:00
|
|
|
_STARTED = const(-1)
|
|
|
|
_STOPPED = const(-2)
|
|
|
|
|
|
|
|
|
2017-03-21 05:15:04 -07:00
|
|
|
class HoldToConfirmDialog(Widget):
|
2017-06-12 09:16:06 -07:00
|
|
|
|
2018-01-23 05:18:13 -08:00
|
|
|
def __init__(self,
|
|
|
|
content,
|
|
|
|
hold='Hold to confirm',
|
|
|
|
button_style=ui.BTN_CONFIRM,
|
|
|
|
loader_style=ui.LDR_DEFAULT):
|
2017-06-20 03:02:38 -07:00
|
|
|
self.content = content
|
2018-01-23 05:18:13 -08:00
|
|
|
self.button = Button(ui.grid(4, n_x=1), hold, style=button_style)
|
|
|
|
self.loader = Loader(style=loader_style)
|
2016-09-27 06:32:12 -07:00
|
|
|
|
|
|
|
def render(self):
|
|
|
|
self.button.render()
|
|
|
|
|
2017-05-31 10:30:06 -07:00
|
|
|
def touch(self, event, pos):
|
2016-09-27 06:32:12 -07:00
|
|
|
button = self.button
|
2017-09-26 05:09:52 -07:00
|
|
|
was_started = button.state & BTN_STARTED and button.state & BTN_ACTIVE
|
2017-03-20 13:40:53 -07:00
|
|
|
button.touch(event, pos)
|
2017-09-26 05:09:52 -07:00
|
|
|
is_started = button.state & BTN_STARTED and button.state & BTN_ACTIVE
|
2017-06-20 03:02:38 -07:00
|
|
|
if is_started and not was_started:
|
2018-01-11 11:05:26 -08:00
|
|
|
ui.display.clear()
|
2017-06-20 03:02:38 -07:00
|
|
|
self.loader.start()
|
|
|
|
return _STARTED
|
|
|
|
if was_started and not is_started:
|
|
|
|
if self.loader.stop():
|
|
|
|
return CONFIRMED
|
|
|
|
else:
|
|
|
|
return _STOPPED
|
2016-09-27 06:32:12 -07:00
|
|
|
|
2017-03-22 07:15:12 -07:00
|
|
|
async def __iter__(self):
|
2017-06-20 03:02:38 -07:00
|
|
|
result = None
|
|
|
|
while result is None or result < 0: # _STARTED or _STOPPED
|
|
|
|
if self.loader.is_active():
|
|
|
|
content_loop = self.loader
|
|
|
|
else:
|
|
|
|
content_loop = self.content
|
|
|
|
confirm_loop = super().__iter__() # default loop (render on touch)
|
2017-09-16 06:00:31 -07:00
|
|
|
result = await loop.wait(content_loop, confirm_loop)
|
2017-06-20 03:02:38 -07:00
|
|
|
return result
|