From 96c5a260410c9b3a3c98e0f2070c336be333fd2f Mon Sep 17 00:00:00 2001 From: tadeubas Date: Thu, 29 Aug 2024 11:26:35 -0300 Subject: [PATCH] Using defaults instead of creating common used lambdas --- src/krux/pages/__init__.py | 69 ++++++++++++-------- src/krux/pages/file_manager.py | 2 +- src/krux/pages/home_pages/addresses.py | 5 +- src/krux/pages/home_pages/home.py | 6 +- src/krux/pages/home_pages/sign_message_ui.py | 6 +- src/krux/pages/login.py | 6 +- src/krux/pages/new_mnemonic/dice_rolls.py | 5 +- src/krux/pages/qr_view.py | 2 +- src/krux/pages/settings_page.py | 2 +- src/krux/pages/wallet_settings.py | 16 ++--- 10 files changed, 66 insertions(+), 53 deletions(-) diff --git a/src/krux/pages/__init__.py b/src/krux/pages/__init__.py index 1848d6f9..1f6b5abc 100644 --- a/src/krux/pages/__init__.py +++ b/src/krux/pages/__init__.py @@ -101,10 +101,10 @@ def load_method(self): load_menu = Menu( self.ctx, [ - MenuItem(t("Load from camera"), lambda: None), - MenuItemSD(t("Load from SD card"), lambda: None), + MenuItem(t("Load from camera")), + MenuItemSD(t("Load from SD card")), ], - back_status=lambda: None, + back_status=MenuItem.action_none, ) index, _ = load_menu.run_loop() return index @@ -483,6 +483,43 @@ def index(self, i): return self.offset + i +class MenuItem: + """Handle items for the Menu""" + + @staticmethod + def action_none(): + """Reusable lambda: None function""" + return None + + @staticmethod + def enabled_true(): + """Reusable lambda: True function""" + return True + + @staticmethod + def action_menuexit(): + """Reusable lambda: MENU_EXIT function""" + return MENU_EXIT + + def __init__(self, text, action=action_none, enabled=enabled_true): + self.label = text + self.action = action + self.enabled = enabled + + @staticmethod + def back(text="Back", action=action_none): + """Create a standard back MenuItem""" + text = t("Back") if text == "Back" else text + return MenuItem("< " + text, action) + + +class MenuItemSD(MenuItem): + """Reusable MenuItem for the Menu that automatic disables when SD card not detected""" + + def __init__(self, text, action=MenuItem.action_none): + super().__init__(text, action, SDHandler.sd_card_available) + + class Menu: """Represents a menu that can render itself to the screen, handle item selection, and invoke menu item callbacks that return a status @@ -495,7 +532,7 @@ def __init__( offset=None, disable_statusbar=False, back_label="Back", - back_status=lambda: MENU_EXIT, + back_status=MenuItem.action_menuexit, ): self.ctx = ctx if back_label: @@ -822,28 +859,6 @@ def _draw_menu(self, selected_item_index): offset_y += delta_y -class MenuItem: - """Handle items for the Menu""" - - def __init__(self, text, action, enabled=lambda: True): - self.label = text - self.action = action - self.enabled = enabled - - @staticmethod - def back(text="Back", action=lambda: MENU_EXIT): - """Create a standard back MenuItem""" - text = t("Back") if text == "Back" else text - return MenuItem("< " + text, action) - - -class MenuItemSD(MenuItem): - """Reusable MenuItem for the Menu that automatic disables when SD card not detected""" - - def __init__(self, text, action): - super().__init__(text, action, SDHandler.sd_card_available) - - def choose_len_mnemonic(ctx, double_mnemonic=False): """Reusable '12 or 24 words?" menu choice""" items = [ @@ -855,7 +870,7 @@ def choose_len_mnemonic(ctx, double_mnemonic=False): submenu = Menu( ctx, items, - back_status=lambda: None, + back_status=MenuItem.action_none, ) _, num_words = submenu.run_loop() ctx.display.clear() diff --git a/src/krux/pages/file_manager.py b/src/krux/pages/file_manager.py index 9611f044..71effb1e 100644 --- a/src/krux/pages/file_manager.py +++ b/src/krux/pages/file_manager.py @@ -61,7 +61,7 @@ def select_file( if path != SD_ROOT_PATH: items.append("..") - menu_items.append(MenuItem("../", lambda: MENU_EXIT)) + menu_items.append(MenuItem("../", MenuItem.action_menuexit)) # sorts by name ignorecase dir_files = sorted(os.listdir(path), key=str.lower) diff --git a/src/krux/pages/home_pages/addresses.py b/src/krux/pages/home_pages/addresses.py index 68fef82d..1b296619 100644 --- a/src/krux/pages/home_pages/addresses.py +++ b/src/krux/pages/home_pages/addresses.py @@ -30,7 +30,6 @@ Menu, MenuItem, MENU_CONTINUE, - MENU_EXIT, ) SCAN_ADDRESS_LIMIT = 50 @@ -75,7 +74,7 @@ def list_address_type(self, addr_type=0): items.append( MenuItem( "%d..%d" % (address_index - max_addresses, address_index - 1), - lambda: MENU_EXIT, + MenuItem.action_menuexit, ) ) @@ -100,7 +99,7 @@ def list_address_type(self, addr_type=0): items.append( MenuItem( "%d..%d" % (address_index, address_index + max_addresses - 1), - lambda: MENU_EXIT, + MenuItem.action_menuexit, ) ) diff --git a/src/krux/pages/home_pages/home.py b/src/krux/pages/home_pages/home.py index 13ac116c..e68da747 100644 --- a/src/krux/pages/home_pages/home.py +++ b/src/krux/pages/home_pages/home.py @@ -226,10 +226,10 @@ def _sign_menu(self): sign_menu = Menu( self.ctx, [ - MenuItem(t("Sign to QR code"), lambda: None), - MenuItemSD(t("Sign to SD card"), lambda: None), + MenuItem(t("Sign to QR code")), + MenuItemSD(t("Sign to SD card")), ], - back_status=lambda: None, + back_status=MenuItem.action_none, ) index, _ = sign_menu.run_loop() return index diff --git a/src/krux/pages/home_pages/sign_message_ui.py b/src/krux/pages/home_pages/sign_message_ui.py index 8e7f81e0..1556186c 100644 --- a/src/krux/pages/home_pages/sign_message_ui.py +++ b/src/krux/pages/home_pages/sign_message_ui.py @@ -197,10 +197,10 @@ def sign_message(self): sign_menu = Menu( self.ctx, [ - MenuItem(t("Sign to QR code"), lambda: None), - MenuItemSD(t("Sign to SD card"), lambda: None), + MenuItem(t("Sign to QR code")), + MenuItemSD(t("Sign to SD card")), ], - back_status=lambda: None, + back_status=MenuItem.action_none, ) index, _ = sign_menu.run_loop() diff --git a/src/krux/pages/login.py b/src/krux/pages/login.py index d1cff0b2..bb4b0979 100644 --- a/src/krux/pages/login.py +++ b/src/krux/pages/login.py @@ -288,9 +288,9 @@ def _load_key_from_words(self, words, charset=LETTERS): submenu = Menu( self.ctx, [ - MenuItem(t("Load Wallet"), lambda: None), - MenuItem(t("Passphrase"), lambda: None), - MenuItem(t("Customize"), lambda: None), + MenuItem(t("Load Wallet")), + MenuItem(t("Passphrase")), + MenuItem(t("Customize")), ], offset=info_len * FONT_HEIGHT + DEFAULT_PADDING, ) diff --git a/src/krux/pages/new_mnemonic/dice_rolls.py b/src/krux/pages/new_mnemonic/dice_rolls.py index bdeb6ae2..de811876 100644 --- a/src/krux/pages/new_mnemonic/dice_rolls.py +++ b/src/krux/pages/new_mnemonic/dice_rolls.py @@ -24,7 +24,6 @@ Page, Menu, MenuItem, - MENU_EXIT, ESC_KEY, choose_len_mnemonic, ) @@ -306,8 +305,8 @@ def delete_roll(buffer): submenu = Menu( self.ctx, [ - MenuItem(t("Stats for Nerds"), lambda: MENU_EXIT), - MenuItem(t("Generate Mnemonic"), lambda: MENU_EXIT), + MenuItem(t("Stats for Nerds"), MenuItem.action_menuexit), + MenuItem(t("Generate Mnemonic"), MenuItem.action_menuexit), ], offset=menu_offset, back_label=None, diff --git a/src/krux/pages/qr_view.py b/src/krux/pages/qr_view.py index a2ae7761..20876ca3 100644 --- a/src/krux/pages/qr_view.py +++ b/src/krux/pages/qr_view.py @@ -479,7 +479,7 @@ def toggle_brightness(): printer_func = self.print_qr if self.has_printer() else None qr_menu = [ - MenuItem(t("Return to QR Viewer"), lambda: None), + MenuItem(t("Return to QR Viewer")), MenuItem(t("Toggle Brightness"), toggle_brightness), MenuItem(t("Print to QR"), printer_func), ] diff --git a/src/krux/pages/settings_page.py b/src/krux/pages/settings_page.py index f97fc5f9..b37f9fb4 100644 --- a/src/krux/pages/settings_page.py +++ b/src/krux/pages/settings_page.py @@ -193,7 +193,7 @@ def handler(): if len(items) == 1: return items[0].action() - back_status = lambda: MENU_EXIT # pylint: disable=C3001 + back_status = MenuItem.action_menuexit # Case for "Back" on the main Settings if settings_namespace.namespace == Settings.namespace: items.append(MenuItem(t("Factory Settings"), self.restore_settings)) diff --git a/src/krux/pages/wallet_settings.py b/src/krux/pages/wallet_settings.py index 3a50766b..c7a0ba3b 100644 --- a/src/krux/pages/wallet_settings.py +++ b/src/krux/pages/wallet_settings.py @@ -134,10 +134,10 @@ def customize_wallet(self, key): submenu = Menu( self.ctx, [ - MenuItem(t("Network"), lambda: None), - MenuItem("Single/Multisig", lambda: None), - MenuItem(t("Script Type"), lambda: None, lambda: not multisig), - MenuItem(t("Account"), lambda: None), + MenuItem(t("Network")), + MenuItem("Single/Multisig"), + MenuItem(t("Script Type"), enabled=lambda: not multisig), + MenuItem(t("Account")), ], offset=info_len * FONT_HEIGHT + DEFAULT_PADDING, ) @@ -166,8 +166,8 @@ def _coin_type(self): submenu = Menu( self.ctx, [ - MenuItem("Mainnet", lambda: None), - MenuItem("Testnet", lambda: None), + MenuItem("Mainnet"), + MenuItem("Testnet"), ], disable_statusbar=True, back_label=None, @@ -180,8 +180,8 @@ def _multisig(self): submenu = Menu( self.ctx, [ - MenuItem(t("Single-sig"), lambda: MENU_EXIT), - MenuItem(t("Multisig"), lambda: MENU_EXIT), + MenuItem(t("Single-sig"), MenuItem.action_menuexit), + MenuItem(t("Multisig"), MenuItem.action_menuexit), ], disable_statusbar=True, back_label=None,