Skip to content

Instantly share code, notes, and snippets.

@CODLCO
Created March 9, 2025 18:56
Show Gist options
  • Save CODLCO/d571b1e17648bb768df991eb4cdfb9f0 to your computer and use it in GitHub Desktop.
Save CODLCO/d571b1e17648bb768df991eb4cdfb9f0 to your computer and use it in GitHub Desktop.
import lvgl as lv
from .theme import styles
from embit import bip39
class MnemonicTable(lv.table):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.words = [""]
self.display_numbers = False
self.wordlist = bip39.WORDLIST
# Styles
cell_style = lv.style_t()
lv.style_copy(cell_style, styles["theme"].style.label.prim)
cell_style.body.opa = 0
cell_style.text.font = lv.font_roboto_22
num_style = lv.style_t()
lv.style_copy(num_style, cell_style)
num_style.text.opa = lv.OPA._40
self.set_col_cnt(4)
self.set_row_cnt(12)
self.set_col_width(0, 40)
self.set_col_width(2, 40)
self.set_col_width(1, 180)
self.set_col_width(3, 180)
self.set_style(lv.page.STYLE.BG, cell_style)
self.set_style(lv.table.STYLE.CELL1, cell_style)
self.set_style(lv.table.STYLE.CELL2, num_style)
for i in range(12):
self.set_cell_value(i, 0, "%d" % (i + 1))
self.set_cell_value(i, 2, "%d" % (i + 13))
self.set_cell_type(i, 0, lv.table.STYLE.CELL2)
self.set_cell_type(i, 2, lv.table.STYLE.CELL2)
# Add toggle button
self.toggle_btn = lv.btn(self)
self.toggle_btn.set_size(100, 40)
self.toggle_btn.align(self, lv.ALIGN.TOP_RIGHT, -10, 10)
self.toggle_label = lv.label(self.toggle_btn)
self.toggle_label.set_text("Show Numbers")
self.toggle_btn.set_event_cb(self.toggle_event_handler)
def toggle_event_handler(self, obj, event):
if event == lv.EVENT.CLICKED:
self.display_numbers = not self.display_numbers
self.toggle_label.set_text("Show Numbers" if not self.display_numbers else "Show Words")
self.update()
def set_mnemonic(self, mnemonic: str):
self.words = mnemonic.split()
self.update()
def update(self):
for i in range(24):
row = i % 12
col = 1 + 2 * (i // 12)
if i < len(self.words):
if self.display_numbers:
if self.words[i] in self.wordlist:
num = self.wordlist.index(self.words[i])
self.set_cell_value(row, col, "{:04d}".format(num))
else:
self.set_cell_value(row, col, "????")
else:
self.set_cell_value(row, col, self.words[i])
else:
self.set_cell_value(row, col, "")
def get_mnemonic(self) -> str:
return " ".join(self.words)
def get_last_word(self) -> str:
if len(self.words) == 0:
return ""
else:
return self.words[-1]
def del_char(self):
if len(self.words) == 0:
return
if len(self.words[-1]) == 0:
self.words = self.words[:-1]
else:
self.words[-1] = self.words[-1][:-1]
self.update()
def autocomplete_word(self, word):
if len(self.words) > 24:
return
if len(self.words) == 0:
self.words.append(word)
else:
self.words[-1] = word
if len(self.words) < 24:
self.words.append("")
self.update()
def add_char(self, c):
if len(self.words) > 24:
return
if len(self.words) == 0:
self.words.append(c)
else:
self.words[-1] += c
self.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment