Skip to content

Commit

Permalink
Merge pull request #425 from tadeubas/develop
Browse files Browse the repository at this point in the history
Refactor of the back btn code
  • Loading branch information
odudex authored Jul 23, 2024
2 parents 5fe3270 + 9354cb9 commit 63cdf79
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 57 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[![created at](https://img.shields.io/github/created-at/selfcustody/krux)](https://github.com/selfcustody/krux/commit/bb8e2d63e031417111ff7cb2b8877c10e19410be)
[![downloads](https://img.shields.io/github/downloads/selfcustody/krux/total)](https://github.com/selfcustody/krux/releases)
[![downloads (latest release)](https://img.shields.io/github/downloads/selfcustody/krux/latest/total)](https://github.com/selfcustody/krux/releases)
[![commits (since latest release)](https://img.shields.io/github/commits-since/selfcustody/krux/latest/develop)](https://github.com/selfcustody/krux/compare/main...develop)
[![contributors](https://img.shields.io/github/contributors-anon/selfcustody/krux)](https://github.com/selfcustody/krux/graphs/contributors)
[![commit activity](https://img.shields.io/github/commit-activity/y/selfcustody/krux)](https://github.com/selfcustody/krux/commits)
[![codecov](https://codecov.io/gh/selfcustody/krux/branch/main/graph/badge.svg?token=XU80PT6Q9V)](https://codecov.io/gh/selfcustody/krux)
[![calver](https://img.shields.io/badge/calver-YY.0M.MICRO-22bfda.svg)](https://calver.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/selfcustody/krux/blob/main/LICENSE.md)
Expand Down
21 changes: 12 additions & 9 deletions src/krux/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def load_method(self):
t("Load from SD card"),
None if not self.has_sd_card() else lambda: None,
),
cta_back(lambda: None),
],
back_status=lambda: None,
)
index, _ = load_menu.run_loop()
return index
Expand Down Expand Up @@ -569,9 +569,17 @@ class Menu:
and invoke menu item callbacks that return a status
"""

def __init__(self, ctx, menu, offset=None, disable_statusbar=False):
def __init__(
self,
ctx,
menu,
offset=None,
disable_statusbar=False,
back_label=t("Back"),
back_status=lambda: MENU_EXIT,
):
self.ctx = ctx
self.menu = menu
self.menu = menu + [("< " + back_label, back_status)] if back_label else menu
self.disable_statusbar = disable_statusbar
if offset is None:
# Default offset for status bar
Expand Down Expand Up @@ -907,14 +915,9 @@ def choose_len_mnemonic(ctx):
[
(t("12 words"), lambda: 12),
(t("24 words"), lambda: 24),
cta_back(lambda: None),
],
back_status=lambda: None,
)
_, num_words = submenu.run_loop()
ctx.display.clear()
return num_words


def cta_back(status=lambda: MENU_EXIT, label=t("Back")):
"""Reusable 'call-to-action: go back'. Currently a menu item tuple"""
return ("< " + label, status)
4 changes: 1 addition & 3 deletions src/krux/pages/encryption_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
Page,
Menu,
MENU_CONTINUE,
MENU_EXIT,
ESC_KEY,
LETTERS,
UPPERCASE_LETTERS,
Expand All @@ -53,6 +52,7 @@ def encryption_key(self):
(t("Type Key"), self.load_key),
(t("Scan Key QR Code"), self.load_qr_encryption_key),
],
back_label=None,
)
_, key = submenu.run_loop()
if key in (ESC_KEY, MENU_CONTINUE):
Expand Down Expand Up @@ -108,7 +108,6 @@ def encrypt_menu(self):
),
),
(t("Encrypted QR Code"), self.encrypted_qr_code),
(t("Back"), lambda: MENU_EXIT),
]
submenu = Menu(self.ctx, encrypt_outputs_menu)
_, _ = submenu.run_loop()
Expand Down Expand Up @@ -254,7 +253,6 @@ def load_from_storage(self, remove_opt=False):
),
)
)
mnemonic_ids_menu.append((t("Back"), lambda: MENU_EXIT))
submenu = Menu(self.ctx, mnemonic_ids_menu)
index, status = submenu.run_loop()
if index == len(submenu.menu) - 1:
Expand Down
7 changes: 3 additions & 4 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, cta_back
from . import Page, Menu, MENU_EXIT, MENU_CONTINUE
from ..sd_card import SDHandler
from ..krux_settings import t
from ..format import generate_thousands_separator, render_decimal_separator
Expand Down Expand Up @@ -56,8 +56,8 @@ def select_file(
while True:
# if is a dir then list all files in it
if SDHandler.dir_exists(path):
items = []
menu_items = []
items = [] # simple reference for the files shown on the menu_items
menu_items = [] # the user menu to interact

if path != SD_ROOT_PATH:
items.append("..")
Expand Down Expand Up @@ -115,7 +115,6 @@ def select_file(

# We need to add this option because /sd can be empty!
items.append("Back")
menu_items.append(cta_back())

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

SCAN_ADDRESS_LIMIT = 50
Expand All @@ -51,7 +50,6 @@ 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)),
cta_back(),
],
)
submenu.run_loop()
Expand Down Expand Up @@ -103,7 +101,6 @@ def list_address_type(self, addr_type=0):
lambda: MENU_EXIT,
)
)
items.append(cta_back())

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

MAX_POLICY_COSIGNERS_DISPLAYED = 5
Expand Down Expand Up @@ -61,6 +60,7 @@ def __init__(self, ctx):
(t("Sign"), self.sign),
(t("Shutdown"), self.shutdown),
],
back_label=None,
),
)

Expand Down Expand Up @@ -169,7 +169,6 @@ def wallet(self):
(t("Passphrase"), self.passphrase),
(t("Customize"), self.customize),
("BIP85", self.bip85),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -189,7 +188,6 @@ def sign(self):
[
("PSBT", self.sign_psbt),
(t("Message"), self.sign_message),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down Expand Up @@ -228,8 +226,8 @@ def _sign_menu(self):
t("Sign to SD card"),
None if not self.has_sd_card() else lambda: None,
),
cta_back(lambda: None),
],
back_status=lambda: None,
)
index, _ = sign_menu.run_loop()
return index
Expand Down
5 changes: 0 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,6 @@
Page,
Menu,
MENU_CONTINUE,
cta_back,
)


Expand All @@ -42,7 +41,6 @@ def mnemonic(self):
(t("QR Code"), self.qr_code_backup),
(t("Encrypted"), self.encrypt_mnemonic_menu),
(t("Other Formats"), self.other_backup_formats),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -57,7 +55,6 @@ 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),
cta_back(),
],
)
submenu.run_loop()
Expand All @@ -77,7 +74,6 @@ def other_backup_formats(self):
(t("Numbers"), self.display_mnemonic_numbers),
("Stackbit 1248", self.stackbit),
("Tiny Seed", self.tiny_seed),
cta_back(),
],
)
submenu.run_loop()
Expand Down Expand Up @@ -149,7 +145,6 @@ def display_mnemonic_numbers(self):
Utils.BASE_OCT_SUFFIX,
),
),
cta_back(),
],
)
submenu.run_loop()
Expand Down
3 changes: 0 additions & 3 deletions src/krux/pages/home_pages/pub_key_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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 @@ -69,7 +68,6 @@ def _pub_key_text(version):
else lambda: _save_xpub_to_sd(version)
),
),
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 @@ -117,7 +115,6 @@ 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(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, cta_back
from .. import MENU_CONTINUE, LOAD_FROM_CAMERA, LOAD_FROM_SD, Menu
from ...themes import theme
from ...display import (
DEFAULT_PADDING,
Expand Down Expand Up @@ -199,8 +199,8 @@ def sign_message(self):
t("Sign to SD card"),
None if not self.has_sd_card() else lambda: None,
),
cta_back(lambda: None),
],
back_status=lambda: None,
)
index, _ = sign_menu.run_loop()

Expand Down
8 changes: 1 addition & 7 deletions src/krux/pages/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
ESC_KEY,
LETTERS,
choose_len_mnemonic,
cta_back,
)

DIGITS = "0123456789"
Expand All @@ -64,6 +63,7 @@ def __init__(self, ctx):
(t("About"), self.about),
(t("Shutdown"), self.shutdown),
],
back_label=None,
),
)

Expand All @@ -75,7 +75,6 @@ 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),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -93,7 +92,6 @@ def load_key_from_camera(self):
"Tiny Seed",
self.load_key_from_tiny_seed_image,
),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -110,7 +108,6 @@ 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),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand All @@ -137,7 +134,6 @@ 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)),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down Expand Up @@ -245,7 +241,6 @@ def _load_key_from_words(self, words, charset=LETTERS):
(t("Load Wallet"), lambda: None),
(t("Passphrase"), lambda: None),
(t("Customize"), lambda: None),
cta_back(),
],
offset=info_len * FONT_HEIGHT + DEFAULT_PADDING,
)
Expand Down Expand Up @@ -552,7 +547,6 @@ 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),
cta_back(),
],
)
index, status = submenu.run_loop()
Expand Down
1 change: 1 addition & 0 deletions src/krux/pages/new_mnemonic/dice_rolls.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def delete_roll(buffer):
(t("Generate Mnemonic"), lambda: MENU_EXIT),
],
offset=menu_offset,
back_label=None,
)
index, _ = submenu.run_loop()
if index == 0:
Expand Down
8 changes: 3 additions & 5 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, cta_back
from . import Page, Menu, MENU_CONTINUE, MENU_EXIT, ESC_KEY
from ..themes import theme, WHITE, BLACK
from ..krux_settings import t
from ..qr import get_size
Expand Down Expand Up @@ -414,8 +414,7 @@ def save_qr_image_menu(self):
),
)
)
qr_menu.append(cta_back())
submenu = Menu(self.ctx, qr_menu, offset=2 * FONT_HEIGHT)
submenu = Menu(self.ctx, qr_menu, offset=2 * FONT_HEIGHT, back_label=None)
submenu.run_loop()
return MENU_CONTINUE
# return MENU_EXIT # Use this to exit QR Viewer after saving
Expand Down Expand Up @@ -487,9 +486,8 @@ def toggle_brightness():
),
),
(t("Print to QR"), printer_func),
cta_back(label=t("Back to Menu")),
]
submenu = Menu(self.ctx, qr_menu)
submenu = Menu(self.ctx, qr_menu, back_label=t("Back to Menu"))
_, status = submenu.run_loop()
if status == MENU_EXIT:
return MENU_CONTINUE
Loading

0 comments on commit 63cdf79

Please sign in to comment.