1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00

core: refactor homescreen app, include lockscreen in it

This commit is contained in:
matejcik 2020-04-14 12:21:32 +02:00 committed by matejcik
parent 9197623d83
commit 04c8b2803d
4 changed files with 74 additions and 50 deletions

View File

@ -0,0 +1,45 @@
import storage.device
from trezor import io, res, ui
if False:
from typing import Any, Coroutine
class HomescreenBase(ui.Layout):
def __init__(self, lock_label = "Locked") -> None:
self.repaint = True
self.lock_label = lock_label
self.label = storage.device.get_label() or "My Trezor"
self.image = storage.device.get_homescreen() or res.load(
"apps/homescreen/res/bg.toif"
)
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 render_lock(self) -> None:
ui.display.bar_radius(40, 100, 160, 40, ui.TITLE_GREY, ui.BG, 4)
ui.display.bar_radius(42, 102, 156, 36, ui.BG, ui.TITLE_GREY, 4)
ui.display.text_center(
ui.WIDTH // 2, 128, self.lock_label, ui.BOLD, ui.TITLE_GREY, ui.BG
)
ui.display.text_center(
ui.WIDTH // 2 + 10, 220, "Tap to unlock", ui.BOLD, ui.TITLE_GREY, ui.BG
)
ui.display.icon(45, 202, res.load(ui.ICON_CLICK), ui.TITLE_GREY, 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__()

View File

@ -1,30 +1,21 @@
import storage
import storage.device
from trezor import config, res, ui
from trezor import config, ui
from . import HomescreenBase
async def homescreen() -> None:
await Homescreen()
class Homescreen(ui.Layout):
class Homescreen(HomescreenBase):
def __init__(self) -> None:
self.repaint = True
def on_render(self) -> None:
if not self.repaint:
return
image = None
if not storage.is_initialized():
label = "Go to trezor.io/start"
else:
label = storage.device.get_label() or "My Trezor"
image = storage.device.get_homescreen()
if not image:
image = res.load("apps/homescreen/res/bg.toif")
super().__init__()
if config.is_unlocked() and not storage.is_initialized():
self.label = "Go to trezor.io/start"
def render_warning(self) -> None:
if storage.is_initialized() and storage.device.no_backup():
ui.header_error("SEEDLESS")
elif storage.is_initialized() and storage.device.unfinished_backup():
@ -35,7 +26,9 @@ class Homescreen(ui.Layout):
ui.header_warning("PIN NOT SET!")
else:
ui.display.bar(0, 0, ui.WIDTH, ui.HEIGHT, ui.BG)
ui.display.avatar(48, 48 - 10, image, ui.WHITE, ui.BLACK)
ui.display.text_center(ui.WIDTH // 2, 220, label, ui.BOLD, ui.FG, ui.BG)
self.repaint = False
def on_render(self) -> None:
self.render_warning()
self.render_homescreen()
if not config.is_unlocked():
self.render_lock()

View File

@ -0,0 +1,12 @@
from trezor import ui
from . import HomescreenBase
class Lockscreen(HomescreenBase):
def on_render(self) -> None:
self.render_homescreen()
self.render_lock()
def on_touch_end(self, x: int, y: int) -> None:
raise ui.Result(None)

View File

@ -1,18 +1,20 @@
import storage
import storage.device
import storage.sd_salt
from trezor import config, log, loop, res, ui, utils, wire
from trezor import config, log, loop, ui, utils, wire
from trezor.pin import show_pin_timeout
from apps.common.request_pin import can_lock_device, verify_user_pin
from apps.homescreen.lockscreen import Lockscreen
async def bootscreen() -> None:
lockscreen = Lockscreen(lock_label="Disconnected")
ui.display.orientation(storage.device.get_rotation())
while True:
try:
if can_lock_device():
await lockscreen()
await lockscreen
await verify_user_pin()
storage.init_unlocked()
return
@ -28,34 +30,6 @@ async def bootscreen() -> None:
utils.halt(e.__class__.__name__)
async def lockscreen() -> None:
label = storage.device.get_label()
image = storage.device.get_homescreen()
if not label:
label = "My Trezor"
if not image:
image = res.load("apps/homescreen/res/bg.toif")
ui.backlight_fade(ui.BACKLIGHT_DIM)
ui.display.clear()
ui.display.avatar(48, 48, image, ui.TITLE_GREY, ui.BG)
ui.display.text_center(ui.WIDTH // 2, 35, label, ui.BOLD, ui.TITLE_GREY, ui.BG)
ui.display.bar_radius(40, 100, 160, 40, ui.TITLE_GREY, ui.BG, 4)
ui.display.bar_radius(42, 102, 156, 36, ui.BG, ui.TITLE_GREY, 4)
ui.display.text_center(ui.WIDTH // 2, 128, "Locked", ui.BOLD, ui.TITLE_GREY, ui.BG)
ui.display.text_center(
ui.WIDTH // 2 + 10, 220, "Tap to unlock", ui.BOLD, ui.TITLE_GREY, ui.BG
)
ui.display.icon(45, 202, res.load(ui.ICON_CLICK), ui.TITLE_GREY, ui.BG)
ui.backlight_fade(ui.BACKLIGHT_NORMAL)
await ui.click()
ui.display.backlight(ui.BACKLIGHT_NONE)
ui.backlight_fade(ui.BACKLIGHT_NORMAL)
config.init(show_pin_timeout)