feat(core): T3T1 ShareWords component

obrusvit 1 month ago
parent c6748d6b75
commit c6a8510269

@ -23,6 +23,7 @@ mod page;
mod progress;
mod result;
mod scroll;
mod share_words;
mod simple_page;
mod swipe;
mod welcome_screen;
@ -58,6 +59,7 @@ pub use page::ButtonPage;
pub use progress::Progress;
pub use result::{ResultFooter, ResultScreen, ResultStyle};
pub use scroll::ScrollBar;
pub use share_words::ShareWords;
pub use simple_page::SimplePage;
pub use swipe::{Swipe, SwipeDirection};
pub use vertical_menu::{VerticalMenu, VerticalMenuChoiceMsg};

@ -0,0 +1,141 @@
use super::theme;
use crate::{
strutil::TString,
ui::{
component::{Component, Event, EventCtx, PageMsg, Paginate},
geometry::{Alignment, Offset, Rect},
model_mercury::component::{Instructions, Swipe, SwipeDirection},
shape,
shape::Renderer,
},
};
use heapless::{String, Vec};
const MAX_WORDS: usize = 33; // super-shamir has 33 words, all other have less
/// Component showing mnemonic/share words during backup procedure. Model T3T1
/// contains one word per screen. A user is instructed to swipe up/down to see
/// next/previous word.
pub struct ShareWords<'a> {
area: Rect,
share_words: Vec<TString<'a>, MAX_WORDS>,
page_index: usize,
/// Area reserved for a shown word from mnemonic/share
area_word: Rect,
/// TODO: review when swipe concept done for T3T1
swipe: Swipe,
/// Footer component for instructions and word counting
instructions: Instructions<'static>,
}
impl<'a> ShareWords<'a> {
pub fn new(share_words: Vec<TString<'a>, MAX_WORDS>) -> Self {
Self {
area: Rect::zero(),
share_words,
page_index: 0,
area_word: Rect::zero(),
swipe: Swipe::new().up().down(),
instructions: Instructions::new("Swipe up"),
}
}
fn is_final_page(&self) -> bool {
self.page_index == self.share_words.len() - 1
}
}
impl<'a> Component for ShareWords<'a> {
type Msg = PageMsg<()>;
fn place(&mut self, bounds: Rect) -> Rect {
self.area = bounds;
let (_, bot) = bounds.split_top(33);
let area_word = bot.split_top(91).0;
self.area_word = area_word;
let area_instruction = bot.split_bottom(Instructions::HEIGHT_SIMPLE).1;
self.instructions.place(area_instruction);
self.swipe.place(bounds); // Swipe possible on the whole screen area
self.area
}
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
let swipe = self.swipe.event(ctx, event);
match swipe {
Some(SwipeDirection::Up) => {
if self.is_final_page() {
return Some(PageMsg::Confirmed);
}
self.change_page(self.page_index + 1);
ctx.request_paint();
}
Some(SwipeDirection::Down) => {
self.change_page(self.page_index.saturating_sub(1));
ctx.request_paint();
}
_ => (),
}
None
}
fn paint(&mut self) {
// TODO: remove when ui-t3t1 done
}
fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
// corner highlights
let (_, top_right_shape, bot_left_shape, bot_right_shape) =
shape::CornerHighlight::from_rect(self.area_word, theme::GREY_DARK, theme::BG);
top_right_shape.render(target);
bot_left_shape.render(target);
bot_right_shape.render(target);
// the ordinal number of the current word
let ordinal_val = self.page_index as u8 + 1;
let ordinal_pos = self.area_word.top_left()
+ Offset::y(theme::TEXT_SUB.text_font.visible_text_height("1"));
let ordinal = build_string!(3, inttostr!(ordinal_val), ".");
shape::Text::new(ordinal_pos, &ordinal)
.with_font(theme::TEXT_SUB.text_font)
.with_fg(theme::GREY)
.render(target);
// the share word
let word = self.share_words[self.page_index];
let word_baseline = self.area_word.center()
+ Offset::y(theme::TEXT_SUPER.text_font.visible_text_height("A") / 2);
word.map(|w| {
shape::Text::new(word_baseline, w)
.with_font(theme::TEXT_SUPER.text_font)
.with_align(Alignment::Center)
.render(target);
});
// footer with instructions
self.instructions.render(target);
}
#[cfg(feature = "ui_bounds")]
fn bounds(&self, _sink: &mut dyn FnMut(Rect)) {}
}
impl<'a> Paginate for ShareWords<'a> {
fn page_count(&mut self) -> usize {
self.share_words.len()
}
fn change_page(&mut self, active_page: usize) {
self.page_index = active_page;
}
}
#[cfg(feature = "ui_debug")]
impl<'a> crate::trace::Trace for ShareWords<'a> {
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.component("ShareWords");
let word = &self.share_words[self.page_index];
let content =
word.map(|w| build_string!(50, inttostr!(self.page_index as u8 + 1), ". ", w, "\n"));
t.string("screen_content", content.as_str().into());
}
}

@ -1,4 +1,5 @@
use core::{cmp::Ordering, convert::TryInto};
use heapless::Vec;
use crate::{
error::Error,
@ -52,8 +53,8 @@ use super::{
FidoMsg, Frame, FrameMsg, Homescreen, HomescreenMsg, IconDialog, Lockscreen, MnemonicInput,
MnemonicKeyboard, MnemonicKeyboardMsg, NumberInputDialog, NumberInputDialogMsg,
PassphraseKeyboard, PassphraseKeyboardMsg, PinKeyboard, PinKeyboardMsg, Progress,
SelectWordCount, SelectWordCountMsg, SelectWordMsg, SimplePage, Slip39Input, VerticalMenu,
VerticalMenuChoiceMsg,
SelectWordCount, SelectWordCountMsg, SelectWordMsg, ShareWords, SimplePage, Slip39Input,
VerticalMenu, VerticalMenuChoiceMsg,
},
theme,
};
@ -206,6 +207,15 @@ where
}
}
impl ComponentMsgObj for ShareWords<'_> {
fn msg_try_into_obj(&self, msg: Self::Msg) -> Result<Obj, Error> {
match msg {
PageMsg::Confirmed => Ok(CONFIRMED.as_obj()),
_ => Err(Error::TypeError),
}
}
}
impl<T> ComponentMsgObj for VerticalMenu<T>
where
T: AsRef<str>,
@ -1336,10 +1346,7 @@ extern "C" fn new_show_tx_context_menu(n_args: usize, args: *const Obj, kwargs:
let options: [(StrBuffer, Icon); 3] = [
(StrBuffer::from("Address QR code"), theme::ICON_QR_CODE),
(
StrBuffer::from("Fee info"),
theme::ICON_CHEVRON_RIGHT,
),
(StrBuffer::from("Fee info"), theme::ICON_CHEVRON_RIGHT),
(StrBuffer::from("Cancel transaction"), theme::ICON_CANCEL),
];
let content = VerticalMenu::context_menu(options);
@ -1353,20 +1360,12 @@ extern "C" fn new_show_tx_context_menu(n_args: usize, args: *const Obj, kwargs:
extern "C" fn new_show_share_words(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
let title: StrBuffer = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let pages: Obj = kwargs.get(Qstr::MP_QSTR_pages)?;
let share_words_obj: Obj = kwargs.get(Qstr::MP_QSTR_share_words)?;
let share_words_vec: Vec<TString, 33> = util::iter_into_vec(share_words_obj)?;
let mut paragraphs = ParagraphVecLong::new();
for page in IterBuf::new().try_iterate(pages)? {
let text: StrBuffer = page.try_into()?;
paragraphs.add(Paragraph::new(&theme::TEXT_MONO, text).break_after());
}
let obj = LayoutObj::new(Frame::left_aligned(
title,
ButtonPage::<_, StrBuffer>::new(paragraphs.into_paragraphs(), theme::BG)
.with_hold()?
.without_cancel(),
))?;
let share_words = ShareWords::new(share_words_vec);
let frame_with_share_words = Frame::left_aligned(title, share_words).with_info_button();
let obj = LayoutObj::new(frame_with_share_words)?;
Ok(obj.into())
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
@ -2082,9 +2081,9 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// def show_share_words(
/// *,
/// title: str,
/// pages: Iterable[str],
/// share_words: Iterable[str],
/// ) -> LayoutObj[UiResult]:
/// """Show mnemonic for backup. Expects the words pre-divided into individual pages."""
/// """Show mnemonic for backup."""
Qstr::MP_QSTR_show_share_words => obj_fn_kw!(0, new_show_share_words).as_obj(),
/// def request_number(

@ -96,7 +96,7 @@ impl Shape<'_> for CornerHighlight {
)
}
fn draw(&mut self, canvas: &mut dyn Canvas, cache: &DrawingCache<'_>) {
fn draw(&mut self, canvas: &mut dyn Canvas, _cache: &DrawingCache<'_>) {
let align: Alignment2D = self.corner.into();
// base circle
@ -150,7 +150,7 @@ impl Shape<'_> for CornerHighlight {
});
}
fn cleanup(&mut self, cache: &super::DrawingCache<'_>) {}
fn cleanup(&mut self, _cache: &super::DrawingCache<'_>) {}
}
impl<'s> ShapeClone<'s> for CornerHighlight {

@ -399,9 +399,9 @@ def show_tx_context_menu() -> LayoutObj[int]:
def show_share_words(
*,
title: str,
pages: Iterable[str],
share_words: Iterable[str],
) -> LayoutObj[UiResult]:
"""Show mnemonic for backup. Expects the words pre-divided into individual pages."""
"""Show mnemonic for backup."""
# rust/src/ui/model_mercury/layout.rs

@ -18,30 +18,6 @@ if TYPE_CHECKING:
CONFIRMED = trezorui2.CONFIRMED # global_import_cache
def _split_share_into_pages(share_words: Sequence[str], per_page: int = 4) -> list[str]:
pages: list[str] = []
current = ""
fill = 2
for i, word in enumerate(share_words):
if i % per_page == 0:
if i != 0:
pages.append(current)
current = ""
# Align numbers to the right.
lastnum = i + per_page + 1
fill = 1 if lastnum < 10 else 2
else:
current += "\n"
current += f"{i + 1:>{fill}}. {word}"
if current:
pages.append(current)
return pages
async def show_share_words(
share_words: Sequence[str],
share_index: int | None = None,
@ -56,13 +32,11 @@ async def show_share_words(
group_index + 1, share_index + 1
)
pages = _split_share_into_pages(share_words)
result = await interact(
RustLayout(
trezorui2.show_share_words(
title=title,
pages=pages,
share_words=share_words,
),
),
"backup_words",

Loading…
Cancel
Save