From 847691798b6b91f717034344ba4a86e62f64e8fe Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 1 Jun 2020 12:07:57 +0200 Subject: [PATCH] core: simplify homescreen and lockscreen implementations --- core/src/apps/homescreen/__init__.py | 20 +++-------------- core/src/apps/homescreen/lockscreen.py | 31 +++----------------------- core/src/trezor/loop.py | 4 ++++ core/src/trezor/ui/__init__.py | 7 ++++-- 4 files changed, 15 insertions(+), 47 deletions(-) diff --git a/core/src/apps/homescreen/__init__.py b/core/src/apps/homescreen/__init__.py index 0ffdd59d3..8a914da4b 100644 --- a/core/src/apps/homescreen/__init__.py +++ b/core/src/apps/homescreen/__init__.py @@ -1,11 +1,10 @@ import storage.device -from trezor import io, res, ui - -if False: - from typing import Any, Coroutine +from trezor import loop, res, ui class HomescreenBase(ui.Layout): + RENDER_SLEEP = loop.SLEEP_FOREVER + def __init__(self) -> None: self.repaint = True @@ -17,16 +16,3 @@ class HomescreenBase(ui.Layout): def render_homescreen(self) -> None: ui.display.avatar(48, 48 - 10, self.image, ui.WHITE, ui.BLACK) ui.display.text_center(ui.WIDTH // 2, 220, self.label, ui.BOLD, ui.FG, ui.BG) - - def dispatch(self, event: int, x: int, y: int) -> None: - if event is ui.RENDER and self.repaint: - self.repaint = False - self.on_render() - elif event is io.TOUCH_END: - self.on_touch_end(x, y) - - def __iter__(self) -> Coroutine[Any, Any, ui.ResultValue]: - # called whenever `await homescreen` is invoked. - # we want to repaint once after that and then never again - self.repaint = True - return super().__iter__() diff --git a/core/src/apps/homescreen/lockscreen.py b/core/src/apps/homescreen/lockscreen.py index 3a923872a..87f0fb8fc 100644 --- a/core/src/apps/homescreen/lockscreen.py +++ b/core/src/apps/homescreen/lockscreen.py @@ -1,4 +1,4 @@ -from trezor import loop, res, ui, wire +from trezor import res, ui, wire from . import HomescreenBase @@ -20,6 +20,8 @@ async def lockscreen() -> None: class Lockscreen(HomescreenBase): + BACKLIGHT_LEVEL = ui.BACKLIGHT_LOW + def __init__( self, lock_label: str = "Locked", tap_label: str = "Tap to unlock" ) -> None: @@ -39,33 +41,6 @@ class Lockscreen(HomescreenBase): ) ui.display.icon(45, 202, res.load(ui.ICON_CLICK), ui.TITLE_GREY, ui.BG) - def handle_rendering(self) -> loop.Task: # type: ignore - """Task that is rendering the layout in a busy loop. - - Copy-pasted from ui.Layout.handle_rendering with modification to set the - backlight to a lower level while lockscreen is on, and a longer sleep because - we never do any redrawing.""" - # Before the first render, we dim the display. - ui.backlight_fade(ui.BACKLIGHT_DIM) - # Clear the screen of any leftovers, make sure everything is marked for - # repaint (we can be running the same layout instance multiple times) - # and paint it. - ui.display.clear() - self.on_render() - - if __debug__ and self.should_notify_layout_change: - from apps.debug import notify_layout_change - - self.should_notify_layout_change = False - notify_layout_change(self) - - ui.refresh() - ui.backlight_fade(ui.BACKLIGHT_LOW) - # long sleep - sleep = loop.sleep(1000 * 1000 * 1000) - while True: - yield sleep - def on_render(self) -> None: self.render_homescreen() self.render_lock() diff --git a/core/src/trezor/loop.py b/core/src/trezor/loop.py index e6f122dad..0f680cb33 100644 --- a/core/src/trezor/loop.py +++ b/core/src/trezor/loop.py @@ -222,6 +222,10 @@ class Syscall: pass +SLEEP_FOREVER = Syscall() +"""Tasks awaiting `SLEEP_FOREVER` will never be resumed.""" + + class sleep(Syscall): """ Pause current task and resume it after given delay. Although the delay is diff --git a/core/src/trezor/ui/__init__.py b/core/src/trezor/ui/__init__.py index e42c85cb1..748d5013e 100644 --- a/core/src/trezor/ui/__init__.py +++ b/core/src/trezor/ui/__init__.py @@ -305,6 +305,9 @@ class Layout(Component): raised, usually from some of the child components. """ + BACKLIGHT_LEVEL = style.BACKLIGHT_NORMAL + RENDER_SLEEP = loop.sleep(_RENDER_DELAY_US) # type: loop.Syscall + async def __iter__(self) -> ResultValue: """ Run the layout and wait until it completes. Returns the result value. @@ -381,8 +384,8 @@ class Layout(Component): # rendering everything synchronously, so refresh it manually and turn # the brightness on again. refresh() - backlight_fade(style.BACKLIGHT_NORMAL) - sleep = loop.sleep(_RENDER_DELAY_US) + backlight_fade(self.BACKLIGHT_LEVEL) + sleep = self.RENDER_SLEEP while True: # Wait for a couple of ms and render the layout again. Because # components use re-paint marking, they do not really draw on the