Skip to content

Commit

Permalink
towards a reusable "cta_back()" for common "< Back" menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Do committed Jul 17, 2024
1 parent 03e2807 commit 63095ea
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 34 deletions.
9 changes: 7 additions & 2 deletions src/krux/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def load_method(self):
t("Load from SD card"),
None if not self.has_sd_card() else lambda: None,
),
("< " + t("Back"), lambda: None),
cta_back(None),
],
)
index, _ = load_menu.run_loop()
Expand Down Expand Up @@ -904,9 +904,14 @@ def choose_len_mnemonic(ctx):
[
(t("12 words"), lambda: 12),
(t("24 words"), lambda: 24),
("< " + t("Back"), lambda: None),
cta_back(None),
],
)
_, num_words = submenu.run_loop()
ctx.display.clear()
return num_words


def cta_back(status=MENU_EXIT, tlabel="Back"):

This comment has been minimized.

Copy link
@tadeubas

tadeubas Jul 17, 2024

Contributor

Hi @jdlcdl , don't need to do that way, the usage will be only for the Back button (as the return of the function always place a back arrow too) for now so you can simplify removing the tlabel and passing "Back" direct to the t() function, that way i18n will work

"""Reusable 'call-to-action: go back'. Currently a menu item tuple"""
return ("< " + t(tlabel), lambda: status)
4 changes: 2 additions & 2 deletions src/krux/pages/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import board
import gc
from . import Page, Menu, MENU_EXIT, MENU_CONTINUE
from . import Page, Menu, MENU_EXIT, MENU_CONTINUE, cta_back
from ..sd_card import SDHandler
from ..krux_settings import t
from ..format import generate_thousands_separator, render_decimal_separator
Expand Down Expand Up @@ -115,7 +115,7 @@ def select_file(

# We need to add this option because /sd can be empty!
items.append("Back")
menu_items.append(("< " + t("Back"), lambda: MENU_EXIT))
menu_items.append(cta_back())

submenu = Menu(self.ctx, menu_items)
index, _ = submenu.run_loop()
Expand Down
7 changes: 4 additions & 3 deletions src/krux/pages/home_pages/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Menu,
MENU_CONTINUE,
MENU_EXIT,
cta_back,
)

SCAN_ADDRESS_LIMIT = 50
Expand All @@ -50,7 +51,7 @@ def addresses_menu(self):
(t("Scan Address"), self.pre_scan_address),
(t("Receive Addresses"), self.list_address_type),
(t("Change Addresses"), lambda: self.list_address_type(1)),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand Down Expand Up @@ -102,7 +103,7 @@ def list_address_type(self, addr_type=0):
lambda: MENU_EXIT,
)
)
items.append(("< " + t("Back"), lambda: MENU_EXIT))
items.append(cta_back())

submenu = Menu(self.ctx, items)
stay_on_this_addr_menu = True
Expand Down Expand Up @@ -137,7 +138,7 @@ def pre_scan_address(self):
[
(t("Receive"), self.scan_address),
(t("Change"), lambda: self.scan_address(1)),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand Down
8 changes: 4 additions & 4 deletions src/krux/pages/home_pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
Page,
Menu,
MENU_CONTINUE,
MENU_EXIT,
ESC_KEY,
LOAD_FROM_CAMERA,
LOAD_FROM_SD,
cta_back,
)

MAX_POLICY_COSIGNERS_DISPLAYED = 5
Expand Down Expand Up @@ -169,7 +169,7 @@ def wallet(self):
(t("Passphrase"), self.passphrase),
(t("Customize"), self.customize),
("BIP85", self.bip85),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -189,7 +189,7 @@ def sign(self):
[
("PSBT", self.sign_psbt),
(t("Message"), self.sign_message),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down Expand Up @@ -228,7 +228,7 @@ def _sign_menu(self):
t("Sign to SD card"),
None if not self.has_sd_card() else lambda: None,
),
("< " + t("Back"), lambda: None),
cta_back(None),
],
)
index, _ = sign_menu.run_loop()
Expand Down
10 changes: 5 additions & 5 deletions src/krux/pages/home_pages/mnemonic_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
Page,
Menu,
MENU_CONTINUE,
MENU_EXIT,
cta_back,
)


Expand All @@ -42,7 +42,7 @@ def mnemonic(self):
(t("QR Code"), self.qr_code_backup),
(t("Encrypted"), self.encrypt_mnemonic_menu),
(t("Other Formats"), self.other_backup_formats),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -57,7 +57,7 @@ def qr_code_backup(self):
("Compact SeedQR", lambda: self.display_seed_qr(True)),
("SeedQR", self.display_seed_qr),
(t("Encrypted QR Code"), self.encrypt_qr_code),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -77,7 +77,7 @@ def other_backup_formats(self):
(t("Numbers"), self.display_mnemonic_numbers),
("Stackbit 1248", self.stackbit),
("Tiny Seed", self.tiny_seed),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand Down Expand Up @@ -149,7 +149,7 @@ def display_mnemonic_numbers(self):
Utils.BASE_OCT_SUFFIX,
),
),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
submenu.run_loop()
Expand Down
5 changes: 3 additions & 2 deletions src/krux/pages/home_pages/pub_key_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Menu,
MENU_CONTINUE,
MENU_EXIT,
cta_back,
)
from ...sd_card import PUBKEY_FILE_EXTENSION
from ...key import P2SH_P2WPKH, P2SH_P2WSH, P2WPKH, P2WSH
Expand Down Expand Up @@ -68,7 +69,7 @@ def _pub_key_text(version):
else lambda: _save_xpub_to_sd(version)
),
),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
]
full_pub_key = self.ctx.wallet.key.account_pubkey_str(version)
menu_offset = 5 + len(self.ctx.display.to_lines(full_pub_key))
Expand Down Expand Up @@ -116,7 +117,7 @@ def _pub_key_qr(version):
pub_key_menu_items.append(
(title + " - " + t("QR Code"), lambda ver=version: _pub_key_qr(ver))
)
pub_key_menu_items.append(("< " + t("Back"), lambda: MENU_EXIT))
pub_key_menu_items.append(cta_back())
pub_key_menu = Menu(self.ctx, pub_key_menu_items)
while True:
_, status = pub_key_menu.run_loop()
Expand Down
4 changes: 2 additions & 2 deletions src/krux/pages/home_pages/sign_message_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from embit import bip32, compact
import hashlib
import binascii
from .. import MENU_CONTINUE, LOAD_FROM_CAMERA, LOAD_FROM_SD, Menu
from .. import MENU_CONTINUE, LOAD_FROM_CAMERA, LOAD_FROM_SD, Menu, cta_back
from ...themes import theme
from ...display import (
DEFAULT_PADDING,
Expand Down Expand Up @@ -199,7 +199,7 @@ def sign_message(self):
t("Sign to SD card"),
None if not self.has_sd_card() else lambda: None,
),
("< " + t("Back"), lambda: None),
cta_back(None),
],
)
index, _ = sign_menu.run_loop()
Expand Down
13 changes: 7 additions & 6 deletions src/krux/pages/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
ESC_KEY,
LETTERS,
choose_len_mnemonic,
cta_back,
)

DIGITS = "0123456789"
Expand Down Expand Up @@ -74,7 +75,7 @@ def load_key(self):
(t("Via Camera"), self.load_key_from_camera),
(t("Via Manual Input"), self.load_key_from_manual_input),
(t("From Storage"), self.load_mnemonic_from_storage),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -92,7 +93,7 @@ def load_key_from_camera(self):
"Tiny Seed",
self.load_key_from_tiny_seed_image,
),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -109,7 +110,7 @@ def load_key_from_manual_input(self):
(t("Word Numbers"), self.pre_load_key_from_digits),
("Tiny Seed (Bits)", self.load_key_from_tiny_seed),
("Stackbit 1248", self.load_key_from_1248),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -136,7 +137,7 @@ def new_key(self):
(t("Via Words"), lambda: self.load_key_from_text(new=True)),
(t("Via D6"), self.new_key_from_dice),
(t("Via D20"), lambda: self.new_key_from_dice(True)),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down Expand Up @@ -241,7 +242,7 @@ def _load_key_from_words(self, words, charset=LETTERS):
(t("Load Wallet"), lambda: None),
(t("Passphrase"), lambda: None),
(t("Customize"), lambda: None),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
offset=info_len * FONT_HEIGHT + DEFAULT_PADDING,
)
Expand Down Expand Up @@ -548,7 +549,7 @@ def pre_load_key_from_digits(self):
(t("Decimal"), self.load_key_from_digits),
(t("Hexadecimal"), self.load_key_from_hexadecimal),
(t("Octal"), self.load_key_from_octal),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down
9 changes: 6 additions & 3 deletions src/krux/pages/qr_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import qrcode
from embit.wordlists.bip39 import WORDLIST
from . import Page, Menu, MENU_CONTINUE, MENU_EXIT, ESC_KEY
from . import Page, Menu, MENU_CONTINUE, MENU_EXIT, ESC_KEY, cta_back
from ..themes import theme, WHITE, BLACK
from ..krux_settings import t
from ..qr import get_size
Expand Down Expand Up @@ -414,7 +414,7 @@ def save_qr_image_menu(self):
),
)
)
qr_menu.append(("< " + t("Back"), lambda: MENU_EXIT))
qr_menu.append(cta_back())
submenu = Menu(self.ctx, qr_menu, offset=2 * FONT_HEIGHT)
submenu.run_loop()
return MENU_CONTINUE
Expand Down Expand Up @@ -487,8 +487,11 @@ def toggle_brightness():
),
),
(t("Print to QR"), printer_func),
(t("Back to Menu"), lambda: MENU_EXIT),
cta_back(tlabel="Back to Menu"),
]
_ = t(
"Back to Menu"
) # TODO: a better way to do this (food for i18n tooling)
submenu = Menu(self.ctx, qr_menu)
_, status = submenu.run_loop()
if status == MENU_EXIT:
Expand Down
5 changes: 4 additions & 1 deletion src/krux/pages/settings_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
MENU_EXIT,
ESC_KEY,
DEFAULT_PADDING,
cta_back,
)
import os

Expand Down Expand Up @@ -233,9 +234,11 @@ def handler():
# Case for "Back" on the main Settings
if settings_namespace.namespace == Settings.namespace:
items.append((t("Factory Settings"), self.restore_settings))
# TODO: solve below so tests don't fail "assert_has_calls" checks
# items.append(cta_back(self._settings_exit_check))
items.append(("< " + t("Back"), self._settings_exit_check))
else:
items.append(("< " + t("Back"), lambda: MENU_EXIT))
items.append(cta_back())

submenu = Menu(self.ctx, items)
index, status = submenu.run_loop()
Expand Down
4 changes: 2 additions & 2 deletions src/krux/pages/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
Page,
Menu,
MENU_CONTINUE,
MENU_EXIT,
ESC_KEY,
LETTERS,
UPPERCASE_LETTERS,
NUM_SPECIAL_1,
NUM_SPECIAL_2,
cta_back,
)
from .file_manager import SD_ROOT_PATH
from ..format import generate_thousands_separator
Expand All @@ -55,7 +55,7 @@ def __init__(self, ctx):
(t("Descriptor Addresses"), self.descriptor_addresses),
(t("Remove Mnemonic"), self.rm_stored_mnemonic),
(t("Wipe Device"), self.wipe_device),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
),
)
Expand Down
5 changes: 3 additions & 2 deletions src/krux/pages/wallet_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
UPPERCASE_LETTERS,
NUM_SPECIAL_1,
NUM_SPECIAL_2,
cta_back,
)
from .settings_page import DIGITS
from ..key import SINGLESIG_SCRIPT_PURPOSE, MULTISIG_SCRIPT_PURPOSE
Expand Down Expand Up @@ -62,7 +63,7 @@ def load_passphrase_menu(self):
[
(t("Type BIP39 Passphrase"), self._load_passphrase),
(t("Scan BIP39 Passphrase"), self._load_qr_passphrase),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
disable_statusbar=True,
)
Expand Down Expand Up @@ -134,7 +135,7 @@ def customize_wallet(self, key):
("Single/Multisig", lambda: None),
(t("Script Type"), (lambda: None) if not multisig else None),
(t("Account"), lambda: None),
("< " + t("Back"), lambda: MENU_EXIT),
cta_back(),
],
offset=info_len * FONT_HEIGHT + DEFAULT_PADDING,
)
Expand Down

0 comments on commit 63095ea

Please sign in to comment.