diff --git a/src/apps/management/layout_reset_device.py b/src/apps/management/layout_reset_device.py index 19e55f25..2941b6a4 100644 --- a/src/apps/management/layout_reset_device.py +++ b/src/apps/management/layout_reset_device.py @@ -1,7 +1,6 @@ -from trezor import wire, loop, res, ui -from trezor.ui.swipe import Swipe, SWIPE_UP, SWIPE_DOWN +from trezor import wire, ui from trezor.ui.button import Button, CONFIRM_BUTTON, CONFIRM_BUTTON_ACTIVE -from trezor.ui.scroll import Scroll +from trezor.ui.scroll import paginate, render_scrollbar, animate_swipe from trezor.crypto import hashlib, random, bip39 from trezor.utils import unimport_gen, chunks @@ -35,51 +34,6 @@ def request_new_pin(): raise Exception() # TODO: wrong PIN should be handled in unified way -def change_page(page, page_count): - while True: - swipe = yield from Swipe().wait() - if swipe == SWIPE_UP and page < page_count - 1: # Scroll down - return page + 1 - elif swipe == SWIPE_DOWN and page > 0: # Scroll up - return page - 1 - -def paginate(render_page, page_count, page=0): - while True: - changer = change_page(page, page_count) - renderer = render_page(page, page_count) - waiter = loop.Wait([changer, renderer]) - result = yield waiter - if changer in waiter.finished: - page = result - else: - return result - - -def render_scrollbar(page, page_count): - screen_height = const(220) - size = const(8) - - padding = 15 - if page_count * padding > screen_height: - padding = screen_height // page_count - - x = 225 - y = (screen_height // 2) - (page_count // 2) * padding - - for i in range(0, page_count): - if i != page: - ui.display.bar(x, y + i * padding, size, size, ui.GREY, ui.BLACK, 4) - ui.display.bar(x, y + page * padding, size, size, ui.WHITE, ui.BLACK, 4) - - -def animate_swipe(): - def render(fg): - ui.display.bar(102, 214, 36, 4, fg, ui.BLACK, 2) - ui.display.bar(106, 222, 28, 4, fg, ui.BLACK, 2) - ui.display.bar(110, 230, 20, 4, fg, ui.BLACK, 2) - yield from ui.animate_pulse(render, ui.WHITE, ui.GREY, speed=300000, delay=200000) - - def show_mnemonic(mnemonic): words_per_page = const(4) mnemonic_words = list(enumerate(mnemonic.split())) @@ -100,15 +54,14 @@ def show_mnemonic(mnemonic): ui.display.text_right(40, top, '%d.' % pos, ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) ui.display.text(45, top, '%s' % word, ui.BOLD, ui.WHITE, ui.BLACK) - # Finish button if page + 1 == page_count: + # Finish button finish = Button((0, 240 - 48, 240, 48), 'Finish', normal_style=CONFIRM_BUTTON, active_style=CONFIRM_BUTTON_ACTIVE) yield from finish.wait() - - # Swipe icon else: + # Swipe icon yield from animate_swipe() yield from paginate(render, len(mnemonic_pages)) diff --git a/src/trezor/ui/scroll.py b/src/trezor/ui/scroll.py index f0327c8b..f1be9292 100644 --- a/src/trezor/ui/scroll.py +++ b/src/trezor/ui/scroll.py @@ -1,32 +1,49 @@ -from . import display -from trezor import ui, loop, res +from .swipe import Swipe, SWIPE_UP, SWIPE_DOWN +from trezor import loop, ui -class Scroll(): +def change_page(page, page_count): + while True: + s = yield from Swipe() + if s == SWIPE_UP and page < page_count - 1: + return page + 1 # Scroll down + elif s == SWIPE_DOWN and page > 0: + return page - 1 # Scroll up - def __init__(self, page=0, totale_lines=0, lines_per_page=4): - self.page = page - self.totale_lines = totale_lines - self.lines_per_page = lines_per_page - def render(self): - count = self.totale_lines // self.lines_per_page - padding = 20 - screen_height = const(220) - cursor = 8 - - if count * padding > screen_height: - padding = screen_height // count +def paginate(render_page, page_count, page=0): + while True: + changer = change_page(page, page_count) + renderer = render_page(page, page_count) + waiter = loop.Wait([changer, renderer]) + result = yield waiter + if changer in waiter.finished: + page = result + else: + return result - x = 230 - y = ((screen_height // 2)) - ((count // 2) * padding) - for i in range(0, count): - if (i != self.page): - ui.display.bar(x, y + i * padding, cursor, cursor, ui.GREY, ui.BLACK, 4) - ui.display.bar(x, y + self.page * padding, cursor, cursor, ui.WHITE, ui.BLACK, 4) +def render_scrollbar(page, page_count): + screen_height = const(220) + size = const(8) - def wait(self): - while True: - self.render() + padding = 15 + if page_count * padding > screen_height: + padding = screen_height // page_count + x = 225 + y = (screen_height // 2) - (page_count // 2) * padding + + for i in range(0, page_count): + if i != page: + ui.display.bar(x, y + i * padding, size, + size, ui.GREY, ui.BLACK, 4) + ui.display.bar(x, y + page * padding, size, size, ui.WHITE, ui.BLACK, 4) + + +def animate_swipe(): + def render(fg): + ui.display.bar(102, 214, 36, 4, fg, ui.BLACK, 2) + ui.display.bar(106, 222, 28, 4, fg, ui.BLACK, 2) + ui.display.bar(110, 230, 20, 4, fg, ui.BLACK, 2) + yield from ui.animate_pulse(render, ui.WHITE, ui.GREY, speed=300000, delay=200000)