Skip to content

Commit

Permalink
Using defaults instead of creating common used lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeubas committed Aug 29, 2024
1 parent 33a1bb0 commit 96c5a26
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 53 deletions.
69 changes: 42 additions & 27 deletions src/krux/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 = [
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/krux/pages/home_pages/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
Menu,
MenuItem,
MENU_CONTINUE,
MENU_EXIT,
)

SCAN_ADDRESS_LIMIT = 50
Expand Down Expand Up @@ -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,
)
)

Expand All @@ -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,
)
)

Expand Down
6 changes: 3 additions & 3 deletions src/krux/pages/home_pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/krux/pages/home_pages/sign_message_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions src/krux/pages/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
5 changes: 2 additions & 3 deletions src/krux/pages/new_mnemonic/dice_rolls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
Page,
Menu,
MenuItem,
MENU_EXIT,
ESC_KEY,
choose_len_mnemonic,
)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/qr_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 8 additions & 8 deletions src/krux/pages/wallet_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 96c5a26

Please sign in to comment.