core: simplify homescreen and lockscreen implementations

pull/971/head
matejcik 4 years ago committed by matejcik
parent 35a0ae1d18
commit 847691798b

@ -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__()

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

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

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

Loading…
Cancel
Save