mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-14 03:30:02 +00:00
ui/loader: shrink before deactivating
This commit is contained in:
parent
d791253e07
commit
6a0f18bef1
@ -1,52 +1,65 @@
|
||||
import utime
|
||||
from micropython import const
|
||||
from trezor import loop
|
||||
from trezor import ui
|
||||
from trezor import res
|
||||
from trezor import loop, res, ui
|
||||
|
||||
_TARGET_MS = const(1000)
|
||||
_SHRINK_BY = const(2)
|
||||
|
||||
|
||||
class Loader(ui.Widget):
|
||||
|
||||
def __init__(self, target_ms=1000, style=ui.LDR_DEFAULT):
|
||||
self.target_ms = target_ms
|
||||
self.start_ticks_ms = None
|
||||
def __init__(self, style=ui.LDR_DEFAULT):
|
||||
self.target_ms = _TARGET_MS
|
||||
self.normal_style = style['normal'] or ui.LDR_DEFAULT['normal']
|
||||
self.active_style = style['active'] or ui.LDR_DEFAULT['active']
|
||||
self.start_ms = None
|
||||
self.stop_ms = None
|
||||
|
||||
def start(self):
|
||||
self.start_ticks_ms = utime.ticks_ms()
|
||||
ui.display.bar(0, 32, 240, 240 - 80, ui.BG)
|
||||
self.start_ms = utime.ticks_ms()
|
||||
self.stop_ms = None
|
||||
|
||||
def stop(self):
|
||||
ui.display.bar(0, 32, 240, 240 - 80, ui.BG)
|
||||
if self.start_ticks_ms is not None:
|
||||
ticks_diff = utime.ticks_ms() - self.start_ticks_ms
|
||||
if self.start_ms is not None and self.stop_ms is None:
|
||||
diff_ms = utime.ticks_ms() - self.start_ms
|
||||
else:
|
||||
ticks_diff = 0
|
||||
self.start_ticks_ms = None
|
||||
return ticks_diff >= self.target_ms
|
||||
diff_ms = 0
|
||||
self.stop_ms = utime.ticks_ms()
|
||||
return diff_ms >= self.target_ms
|
||||
|
||||
def is_active(self):
|
||||
return self.start_ticks_ms is not None
|
||||
return self.start_ms is not None
|
||||
|
||||
def render(self):
|
||||
progress = min(utime.ticks_ms() - self.start_ticks_ms, self.target_ms)
|
||||
if progress == self.target_ms:
|
||||
target = self.target_ms
|
||||
start = self.start_ms
|
||||
stop = self.stop_ms
|
||||
now = utime.ticks_ms()
|
||||
if stop is None:
|
||||
r = min(now - start, target)
|
||||
else:
|
||||
r = max(stop - start + (stop - now) * _SHRINK_BY, 0)
|
||||
if r == 0:
|
||||
self.start_ms = None
|
||||
self.stop_ms = None
|
||||
if r == target:
|
||||
s = self.active_style
|
||||
else:
|
||||
s = self.normal_style
|
||||
if s['icon'] is None:
|
||||
ui.display.loader(
|
||||
progress, -24, s['fg-color'], s['bg-color'])
|
||||
r, -24, s['fg-color'], s['bg-color'])
|
||||
elif s['icon-fg-color'] is None:
|
||||
ui.display.loader(
|
||||
progress, -24, s['fg-color'], s['bg-color'], res.load(s['icon']))
|
||||
r, -24, s['fg-color'], s['bg-color'], res.load(s['icon']))
|
||||
else:
|
||||
ui.display.loader(
|
||||
progress, -24, s['fg-color'], s['bg-color'], res.load(s['icon']), s['icon-fg-color'])
|
||||
r, -24, s['fg-color'], s['bg-color'], res.load(s['icon']), s['icon-fg-color'])
|
||||
|
||||
def __iter__(self):
|
||||
sleep = loop.sleep(1000000 // 60) # 60 fps
|
||||
sleep = loop.sleep(1000000 // 30) # 30 fps
|
||||
ui.display.bar(0, 32, 240, 240 - 80, ui.BG) # clear
|
||||
while self.is_active():
|
||||
self.render()
|
||||
yield sleep
|
||||
ui.display.bar(0, 32, 240, 240 - 80, ui.BG) # clear
|
||||
|
Loading…
Reference in New Issue
Block a user