1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-29 19:08:12 +00:00

refactor(core): call super().__init__() in Component and Layout subclasses

This commit is contained in:
Martin Milata 2020-11-20 23:41:33 +01:00
parent 76eb54b6f8
commit fa1566cb71
25 changed files with 39 additions and 15 deletions

View File

@ -6,11 +6,11 @@ class HomescreenBase(ui.Layout):
RENDER_SLEEP = loop.SLEEP_FOREVER RENDER_SLEEP = loop.SLEEP_FOREVER
def __init__(self) -> None: def __init__(self) -> None:
super().__init__()
self.label = storage.device.get_label() or "My Trezor" self.label = storage.device.get_label() or "My Trezor"
self.image = storage.device.get_homescreen() or res.load( self.image = storage.device.get_homescreen() or res.load(
"apps/homescreen/res/bg.toif" "apps/homescreen/res/bg.toif"
) )
self.repaint = True
def on_tap(self) -> None: def on_tap(self) -> None:
"""Called when the user taps the screen.""" """Called when the user taps the screen."""

View File

@ -87,8 +87,8 @@ class InputButton(Button):
class Prompt(ui.Component): class Prompt(ui.Component):
def __init__(self, prompt: str) -> None: def __init__(self, prompt: str) -> None:
super().__init__()
self.prompt = prompt self.prompt = prompt
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:
@ -99,6 +99,7 @@ class Prompt(ui.Component):
class Bip39Keyboard(ui.Layout): class Bip39Keyboard(ui.Layout):
def __init__(self, prompt: str) -> None: def __init__(self, prompt: str) -> None:
super().__init__()
self.prompt = Prompt(prompt) self.prompt = Prompt(prompt)
icon_back = res.load(ui.ICON_BACK) icon_back = res.load(ui.ICON_BACK)

View File

@ -90,8 +90,8 @@ class InputButton(Button):
class Prompt(ui.Component): class Prompt(ui.Component):
def __init__(self, prompt: str) -> None: def __init__(self, prompt: str) -> None:
super().__init__()
self.prompt = prompt self.prompt = prompt
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:
@ -102,6 +102,7 @@ class Prompt(ui.Component):
class Slip39Keyboard(ui.Layout): class Slip39Keyboard(ui.Layout):
def __init__(self, prompt: str) -> None: def __init__(self, prompt: str) -> None:
super().__init__()
self.prompt = Prompt(prompt) self.prompt = Prompt(prompt)
icon_back = res.load(ui.ICON_BACK) icon_back = res.load(ui.ICON_BACK)

View File

@ -206,10 +206,10 @@ async def show_group_threshold_reached(ctx: wire.GenericContext) -> None:
class RecoveryHomescreen(ui.Component): class RecoveryHomescreen(ui.Component):
def __init__(self, text: str, subtext: str = None): def __init__(self, text: str, subtext: str = None):
super().__init__()
self.text = text self.text = text
self.subtext = subtext self.subtext = subtext
self.dry_run = storage.recovery.is_dry_run() self.dry_run = storage.recovery.is_dry_run()
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if not self.repaint: if not self.repaint:

View File

@ -524,10 +524,10 @@ class Slip39NumInput(ui.Component):
SET_GROUP_THRESHOLD = object() SET_GROUP_THRESHOLD = object()
def __init__(self, step, count, min_count, max_count, group_id=None): def __init__(self, step, count, min_count, max_count, group_id=None):
super().__init__()
self.step = step self.step = step
self.input = NumInput(count, min_count=min_count, max_count=max_count) self.input = NumInput(count, min_count=min_count, max_count=max_count)
self.input.on_change = self.on_change self.input.on_change = self.on_change
self.repaint = True
self.group_id = group_id self.group_id = group_id
def dispatch(self, event, x, y): def dispatch(self, event, x, y):
@ -605,6 +605,7 @@ class MnemonicWordSelect(ui.Layout):
NUM_OF_CHOICES = 3 NUM_OF_CHOICES = 3
def __init__(self, words, share_index, word_index, count, group_index=None): def __init__(self, words, share_index, word_index, count, group_index=None):
super().__init__()
self.words = words self.words = words
self.share_index = share_index self.share_index = share_index
self.word_index = word_index self.word_index = word_index

View File

@ -143,6 +143,7 @@ async def _require_confirm_unlock_time(ctx, unlock_time):
class TransactionStep(ui.Component): class TransactionStep(ui.Component):
def __init__(self, state, info): def __init__(self, state, info):
super().__init__()
self.state = state self.state = state
self.info = info self.info = info
@ -159,6 +160,7 @@ class TransactionStep(ui.Component):
class KeyImageSyncStep(ui.Component): class KeyImageSyncStep(ui.Component):
def __init__(self, current, total_num): def __init__(self, current, total_num):
super().__init__()
self.current = current self.current = current
self.total_num = total_num self.total_num = total_num
@ -172,6 +174,7 @@ class KeyImageSyncStep(ui.Component):
class LiveRefreshStep(ui.Component): class LiveRefreshStep(ui.Component):
def __init__(self, current): def __init__(self, current):
super().__init__()
self.current = current self.current = current
def on_render(self): def on_render(self):

View File

@ -16,6 +16,7 @@ if False:
class ConfirmAddCredential(ConfirmInfo): class ConfirmAddCredential(ConfirmInfo):
def __init__(self, cred: Fido2Credential): def __init__(self, cred: Fido2Credential):
super().__init__()
self._cred = cred self._cred = cred
self.load_icon(cred.rp_id_hash) self.load_icon(cred.rp_id_hash)

View File

@ -34,8 +34,8 @@ class ConfirmInfo:
class ConfirmContent(ui.Component): class ConfirmContent(ui.Component):
def __init__(self, info: ConfirmInfo) -> None: def __init__(self, info: ConfirmInfo) -> None:
super().__init__()
self.info = info self.info = info
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:

View File

@ -18,6 +18,7 @@ if False:
class ConfirmRemoveCredential(ConfirmInfo): class ConfirmRemoveCredential(ConfirmInfo):
def __init__(self, cred: Fido2Credential): def __init__(self, cred: Fido2Credential):
super().__init__()
self._cred = cred self._cred = cred
self.load_icon(cred.rp_id_hash) self.load_icon(cred.rp_id_hash)

View File

@ -238,6 +238,9 @@ class Component:
an instance of `Result`. an instance of `Result`.
""" """
def __init__(self) -> None:
self.repaint = True
def dispatch(self, event: int, x: int, y: int) -> None: def dispatch(self, event: int, x: int, y: int) -> None:
if event is RENDER: if event is RENDER:
self.on_render() self.on_render()

View File

@ -133,6 +133,7 @@ class Button(ui.Component):
content: ButtonContent, content: ButtonContent,
style: ButtonStyleType = ButtonDefault, style: ButtonStyleType = ButtonDefault,
) -> None: ) -> None:
super().__init__()
if isinstance(content, str): if isinstance(content, str):
self.text = content self.text = content
self.icon = b"" self.icon = b""
@ -146,7 +147,6 @@ class Button(ui.Component):
self.active_style = style.active self.active_style = style.active
self.disabled_style = style.disabled self.disabled_style = style.disabled
self.state = _INITIAL self.state = _INITIAL
self.repaint = True
def enable(self) -> None: def enable(self) -> None:
if self.state is not _INITIAL: if self.state is not _INITIAL:

View File

@ -15,11 +15,11 @@ _CHECKLIST_OFFSET_X_ICON = const(0)
class Checklist(ui.Component): class Checklist(ui.Component):
def __init__(self, title: str, icon: str) -> None: def __init__(self, title: str, icon: str) -> None:
super().__init__()
self.title = title self.title = title
self.icon = icon self.icon = icon
self.items = [] # type: List[ChecklistItem] self.items = [] # type: List[ChecklistItem]
self.active = 0 self.active = 0
self.repaint = True
def add(self, item: ChecklistItem) -> None: def add(self, item: ChecklistItem) -> None:
self.items.append(item) self.items.append(item)

View File

@ -38,6 +38,7 @@ class Confirm(ui.Layout):
cancel_style: ButtonStyleType = DEFAULT_CANCEL_STYLE, cancel_style: ButtonStyleType = DEFAULT_CANCEL_STYLE,
major_confirm: bool = False, major_confirm: bool = False,
) -> None: ) -> None:
super().__init__()
self.content = content self.content = content
if confirm is not None: if confirm is not None:
@ -192,6 +193,7 @@ class InfoConfirm(ui.Layout):
info: ButtonContent = DEFAULT_INFO, info: ButtonContent = DEFAULT_INFO,
info_style: ButtonStyleType = DEFAULT_INFO_STYLE, info_style: ButtonStyleType = DEFAULT_INFO_STYLE,
) -> None: ) -> None:
super().__init__()
self.content = content self.content = content
self.confirm = Button(ui.grid(14), confirm, confirm_style) self.confirm = Button(ui.grid(14), confirm, confirm_style)
@ -243,6 +245,7 @@ class HoldToConfirm(ui.Layout):
loader_style: LoaderStyleType = DEFAULT_LOADER_STYLE, loader_style: LoaderStyleType = DEFAULT_LOADER_STYLE,
cancel: bool = True, cancel: bool = True,
): ):
super().__init__()
self.content = content self.content = content
self.loader = Loader(loader_style) self.loader = Loader(loader_style)

View File

@ -6,6 +6,7 @@ if False:
class Container(ui.Component): class Container(ui.Component):
def __init__(self, *children: ui.Component): def __init__(self, *children: ui.Component):
super().__init__()
self.children = children self.children = children
def dispatch(self, event: int, x: int, y: int) -> None: def dispatch(self, event: int, x: int, y: int) -> None:

View File

@ -35,6 +35,7 @@ class InfoConfirm(ui.Layout):
confirm: ButtonContent = DEFAULT_CONFIRM, confirm: ButtonContent = DEFAULT_CONFIRM,
style: InfoConfirmStyleType = DEFAULT_STYLE, style: InfoConfirmStyleType = DEFAULT_STYLE,
) -> None: ) -> None:
super().__init__()
self.text = text.split() self.text = text.split()
self.style = style self.style = style
panel_area = ui.grid(0, n_x=1, n_y=1) panel_area = ui.grid(0, n_x=1, n_y=1)
@ -42,7 +43,6 @@ class InfoConfirm(ui.Layout):
confirm_area = ui.grid(4, n_x=1) confirm_area = ui.grid(4, n_x=1)
self.confirm = Button(confirm_area, confirm, style.button) self.confirm = Button(confirm_area, confirm, style.button)
self.confirm.on_click = self.on_confirm # type: ignore self.confirm.on_click = self.on_confirm # type: ignore
self.repaint = True
def dispatch(self, event: int, x: int, y: int) -> None: def dispatch(self, event: int, x: int, y: int) -> None:
if event == ui.RENDER: if event == ui.RENDER:

View File

@ -39,6 +39,7 @@ _TARGET_MS = const(1000)
class Loader(ui.Component): class Loader(ui.Component):
def __init__(self, style: LoaderStyleType = LoaderDefault) -> None: def __init__(self, style: LoaderStyleType = LoaderDefault) -> None:
super().__init__()
self.normal_style = style.normal self.normal_style = style.normal
self.active_style = style.active self.active_style = style.active
self.target_ms = _TARGET_MS self.target_ms = _TARGET_MS
@ -98,6 +99,7 @@ class Loader(ui.Component):
class LoadingAnimation(ui.Layout): class LoadingAnimation(ui.Layout):
def __init__(self, style: LoaderStyleType = LoaderDefault) -> None: def __init__(self, style: LoaderStyleType = LoaderDefault) -> None:
super().__init__()
self.loader = Loader(style) self.loader = Loader(style)
self.loader.on_finish = self.on_finish # type: ignore self.loader.on_finish = self.on_finish # type: ignore
self.loader.start() self.loader.start()

View File

@ -5,6 +5,7 @@ from trezor.ui.text import LABEL_CENTER, Label
class NumInput(ui.Component): class NumInput(ui.Component):
def __init__(self, count: int = 5, max_count: int = 16, min_count: int = 1) -> None: def __init__(self, count: int = 5, max_count: int = 16, min_count: int = 1) -> None:
super().__init__()
self.count = count self.count = count
self.max_count = max_count self.max_count = max_count
self.min_count = min_count self.min_count = min_count

View File

@ -115,8 +115,8 @@ class Input(Button):
class Prompt(ui.Component): class Prompt(ui.Component):
def __init__(self, text: str) -> None: def __init__(self, text: str) -> None:
super().__init__()
self.text = text self.text = text
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:
@ -130,6 +130,7 @@ CANCELLED = object()
class PassphraseKeyboard(ui.Layout): class PassphraseKeyboard(ui.Layout):
def __init__(self, prompt: str, max_length: int, page: int = 1) -> None: def __init__(self, prompt: str, max_length: int, page: int = 1) -> None:
super().__init__()
self.prompt = Prompt(prompt) self.prompt = Prompt(prompt)
self.max_length = max_length self.max_length = max_length
self.page = page self.page = page

View File

@ -32,10 +32,10 @@ def generate_digits() -> Iterable[int]:
class PinInput(ui.Component): class PinInput(ui.Component):
def __init__(self, prompt: str, subprompt: Optional[str], pin: str) -> None: def __init__(self, prompt: str, subprompt: Optional[str], pin: str) -> None:
super().__init__()
self.prompt = prompt self.prompt = prompt
self.subprompt = subprompt self.subprompt = subprompt
self.pin = pin self.pin = pin
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:

View File

@ -6,6 +6,7 @@ if False:
class Popup(ui.Layout): class Popup(ui.Layout):
def __init__(self, content: ui.Component, time_ms: int = 0) -> None: def __init__(self, content: ui.Component, time_ms: int = 0) -> None:
super().__init__()
self.content = content self.content = content
if utils.DISABLE_ANIMATION: if utils.DISABLE_ANIMATION:
self.time_ms = 0 self.time_ms = 0

View File

@ -3,11 +3,11 @@ from trezor import ui
class Qr(ui.Component): class Qr(ui.Component):
def __init__(self, data: str, x: int, y: int, scale: int): def __init__(self, data: str, x: int, y: int, scale: int):
super().__init__()
self.data = data self.data = data
self.x = x self.x = x
self.y = y self.y = y
self.scale = scale self.scale = scale
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:

View File

@ -51,10 +51,10 @@ class Paginated(ui.Layout):
def __init__( def __init__(
self, pages: List[ui.Component], page: int = 0, one_by_one: bool = False self, pages: List[ui.Component], page: int = 0, one_by_one: bool = False
): ):
super().__init__()
self.pages = pages self.pages = pages
self.page = page self.page = page
self.one_by_one = one_by_one self.one_by_one = one_by_one
self.repaint = True
def dispatch(self, event: int, x: int, y: int) -> None: def dispatch(self, event: int, x: int, y: int) -> None:
pages = self.pages pages = self.pages
@ -131,6 +131,7 @@ class PageWithButtons(ui.Component):
index: int, index: int,
count: int, count: int,
) -> None: ) -> None:
super().__init__()
self.content = content self.content = content
self.paginated = paginated self.paginated = paginated
self.index = index self.index = index
@ -188,6 +189,7 @@ class PaginatedWithButtons(ui.Layout):
def __init__( def __init__(
self, pages: List[ui.Component], page: int = 0, one_by_one: bool = False self, pages: List[ui.Component], page: int = 0, one_by_one: bool = False
) -> None: ) -> None:
super().__init__()
self.pages = [ self.pages = [
PageWithButtons(p, self, i, len(pages)) for i, p in enumerate(pages) PageWithButtons(p, self, i, len(pages)) for i, p in enumerate(pages)
] ]

View File

@ -19,6 +19,7 @@ _SWIPE_TRESHOLD = const(30)
class Swipe(ui.Component): class Swipe(ui.Component):
def __init__(self, directions: int = SWIPE_ALL, area: ui.Area = None) -> None: def __init__(self, directions: int = SWIPE_ALL, area: ui.Area = None) -> None:
super().__init__()
if area is None: if area is None:
area = (0, 0, ui.WIDTH, ui.HEIGHT) area = (0, 0, ui.WIDTH, ui.HEIGHT)
self.area = area self.area = area

View File

@ -129,13 +129,13 @@ class Text(ui.Component):
max_lines: int = TEXT_MAX_LINES, max_lines: int = TEXT_MAX_LINES,
new_lines: bool = True, new_lines: bool = True,
): ):
super().__init__()
self.header_text = header_text self.header_text = header_text
self.header_icon = header_icon self.header_icon = header_icon
self.icon_color = icon_color self.icon_color = icon_color
self.max_lines = max_lines self.max_lines = max_lines
self.new_lines = new_lines self.new_lines = new_lines
self.content = [] # type: List[TextContent] self.content = [] # type: List[TextContent]
self.repaint = True
def normal(self, *content: TextContent) -> None: def normal(self, *content: TextContent) -> None:
self.content.append(ui.NORMAL) self.content.append(ui.NORMAL)
@ -187,11 +187,11 @@ class Label(ui.Component):
align: int = LABEL_LEFT, align: int = LABEL_LEFT,
style: int = ui.NORMAL, style: int = ui.NORMAL,
) -> None: ) -> None:
super().__init__()
self.area = area self.area = area
self.content = content self.content = content
self.align = align self.align = align
self.style = style self.style = style
self.repaint = True
def on_render(self) -> None: def on_render(self) -> None:
if self.repaint: if self.repaint:

View File

@ -10,6 +10,7 @@ if False:
class WordSelector(ui.Layout): class WordSelector(ui.Layout):
def __init__(self, content: ui.Component) -> None: def __init__(self, content: ui.Component) -> None:
super().__init__()
self.content = content self.content = content
self.w12 = Button(ui.grid(6, n_y=4), "12") self.w12 = Button(ui.grid(6, n_y=4), "12")
self.w12.on_click = self.on_w12 # type: ignore self.w12.on_click = self.on_w12 # type: ignore