From 04f6d779e2381f56bfa5852a5998f744c76616c5 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Thu, 21 Nov 2024 11:16:51 +0100 Subject: [PATCH] refactor(core): move show_info_with_cancel - not implemented for model_r --- core/embed/rust/src/ui/api/firmware_upy.rs | 23 ++++++++ .../embed/rust/src/ui/model_mercury/layout.rs | 43 --------------- .../src/ui/model_mercury/ui_features_fw.rs | 30 +++++++++++ .../rust/src/ui/model_tr/ui_features_fw.rs | 11 ++++ core/embed/rust/src/ui/model_tt/layout.rs | 53 ------------------- .../rust/src/ui/model_tt/ui_features_fw.rs | 43 ++++++++++++++- core/embed/rust/src/ui/ui_features_fw.rs | 7 +++ core/mocks/generated/trezorui2.pyi | 22 -------- core/mocks/generated/trezorui_api.pyi | 11 ++++ .../src/trezor/ui/layouts/mercury/__init__.py | 6 +-- core/src/trezor/ui/layouts/tt/__init__.py | 10 ++-- 11 files changed, 132 insertions(+), 127 deletions(-) diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index cb9a0b78bc..20abf14858 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -430,6 +430,19 @@ extern "C" fn new_show_info(n_args: usize, args: *const Obj, kwargs: *mut Map) - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +extern "C" fn new_show_info_with_cancel(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { + let block = move |_args: &[Obj], kwargs: &Map| { + let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; + let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; + let horizontal: bool = kwargs.get_or(Qstr::MP_QSTR_horizontal, false)?; + let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?; + + let layout = ModelUI::show_info_with_cancel(title, items, horizontal, chunkify)?; + Ok(LayoutObj::new_root(layout)?.into()) + }; + unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } +} + extern "C" fn new_show_lockscreen(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let label: TString<'static> = kwargs @@ -960,6 +973,16 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// """Info screen.""" Qstr::MP_QSTR_show_info => obj_fn_kw!(0, new_show_info).as_obj(), + /// def show_info_with_cancel( + /// *, + /// title: str, + /// items: Iterable[Tuple[str, str]], + /// horizontal: bool = False, + /// chunkify: bool = False, + /// ) -> LayoutObj[UiResult]: + /// """Show metadata for outgoing transaction.""" + Qstr::MP_QSTR_show_info_with_cancel => obj_fn_kw!(0, new_show_info_with_cancel).as_obj(), + /// def show_lockscreen( /// *, /// label: str | None, diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index 7d04b11e14..025e8647ea 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -562,39 +562,6 @@ extern "C" fn new_confirm_summary(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_show_info_with_cancel(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; - let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; - let _horizontal: bool = kwargs.get_or(Qstr::MP_QSTR_horizontal, false)?; // FIXME - let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?; - - let mut paragraphs = ParagraphVecShort::new(); - - for para in IterBuf::new().try_iterate(items)? { - let [key, value]: [Obj; 2] = util::iter_into_array(para)?; - let key: TString = key.try_into()?; - let value: TString = value.try_into()?; - paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, key).no_break()); - if chunkify { - paragraphs.add(Paragraph::new( - theme::get_chunkified_text_style(value.len()), - value, - )); - } else { - paragraphs.add(Paragraph::new(&theme::TEXT_MONO, value)); - } - } - - let obj = LayoutObj::new(SwipeUpScreen::new( - Frame::left_aligned(title, SwipeContent::new(paragraphs.into_paragraphs())) - .with_cancel_button(), - ))?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; @@ -813,16 +780,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Confirm new PIN setup with an option to cancel action.""" Qstr::MP_QSTR_flow_confirm_set_new_pin => obj_fn_kw!(0, new_confirm_set_new_pin).as_obj(), - /// def show_info_with_cancel( - /// *, - /// title: str, - /// items: Iterable[Tuple[str, str]], - /// horizontal: bool = False, - /// chunkify: bool = False, - /// ) -> LayoutObj[UiResult]: - /// """Show metadata for outgoing transaction.""" - Qstr::MP_QSTR_show_info_with_cancel => obj_fn_kw!(0, new_show_info_with_cancel).as_obj(), - /// def confirm_value( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs index 1f99091f83..f36bfb5118 100644 --- a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs @@ -505,6 +505,36 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { Ok(obj) } + fn show_info_with_cancel( + title: TString<'static>, + items: Obj, + _horizontal: bool, + chunkify: bool, + ) -> Result { + let mut paragraphs = ParagraphVecShort::new(); + + for para in IterBuf::new().try_iterate(items)? { + let [key, value]: [Obj; 2] = util::iter_into_array(para)?; + let key: TString = key.try_into()?; + let value: TString = value.try_into()?; + paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, key).no_break()); + if chunkify { + paragraphs.add(Paragraph::new( + theme::get_chunkified_text_style(value.len()), + value, + )); + } else { + paragraphs.add(Paragraph::new(&theme::TEXT_MONO, value)); + } + } + + let layout = RootComponent::new(SwipeUpScreen::new( + Frame::left_aligned(title, SwipeContent::new(paragraphs.into_paragraphs())) + .with_cancel_button(), + )); + Ok(layout) + } + fn show_lockscreen( label: TString<'static>, bootscreen: bool, diff --git a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs index de0ca17443..6e0a6b13cb 100644 --- a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs @@ -570,6 +570,17 @@ impl UIFeaturesFirmware for ModelTRFeatures { Ok(obj) } + fn show_info_with_cancel( + _title: TString<'static>, + _items: Obj, + _horizontal: bool, + _chunkify: bool, + ) -> Result { + Err::, Error>(Error::ValueError( + c"show_info_with_cancel not supported", + )) + } + fn show_lockscreen( label: TString<'static>, bootscreen: bool, diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 43f3bf89cc..9eff10fa6a 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -597,49 +597,6 @@ extern "C" fn new_show_address_details(n_args: usize, args: *const Obj, kwargs: unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_show_info_with_cancel(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; - let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; - let horizontal: bool = kwargs.get_or(Qstr::MP_QSTR_horizontal, false)?; - let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?; - - let mut paragraphs = ParagraphVecShort::new(); - - for para in IterBuf::new().try_iterate(items)? { - let [key, value]: [Obj; 2] = util::iter_into_array(para)?; - let key: TString = key.try_into()?; - let value: TString = value.try_into()?; - paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, key).no_break()); - if chunkify { - paragraphs.add(Paragraph::new( - theme::get_chunkified_text_style(value.len()), - value, - )); - } else { - paragraphs.add(Paragraph::new(&theme::TEXT_MONO, value)); - } - } - - let axis = match horizontal { - true => geometry::Axis::Horizontal, - _ => geometry::Axis::Vertical, - }; - - let obj = LayoutObj::new( - Frame::left_aligned( - theme::label_title(), - title, - SimplePage::new(paragraphs.into_paragraphs(), axis, theme::BG) - .with_swipe_right_to_go_back(), - ) - .with_cancel_button(), - )?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; @@ -808,16 +765,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Show address details - QR code, account, path, cosigner xpubs.""" Qstr::MP_QSTR_show_address_details => obj_fn_kw!(0, new_show_address_details).as_obj(), - /// def show_info_with_cancel( - /// *, - /// title: str, - /// items: Iterable[Tuple[str, str]], - /// horizontal: bool = False, - /// chunkify: bool = False, - /// ) -> LayoutObj[UiResult]: - /// """Show metadata for outgoing transaction.""" - Qstr::MP_QSTR_show_info_with_cancel => obj_fn_kw!(0, new_show_info_with_cancel).as_obj(), - /// def confirm_value( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs index defa475a79..2c22472427 100644 --- a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs @@ -19,6 +19,7 @@ use crate::{ }, Border, ComponentExt, Empty, Jpeg, Label, Never, Timeout, }, + geometry, layout::{ obj::{LayoutMaybeTrace, LayoutObj, RootComponent}, util::RecoveryType, @@ -32,7 +33,7 @@ use super::{ check_homescreen_format, Bip39Input, Button, ButtonMsg, ButtonPage, ButtonStyleSheet, CancelConfirmMsg, CoinJoinProgress, Dialog, FidoConfirm, Frame, Homescreen, IconDialog, Lockscreen, MnemonicKeyboard, NumberInputDialog, PassphraseKeyboard, PinKeyboard, Progress, - SelectWordCount, SetBrightnessDialog, ShareWords, Slip39Input, + SelectWordCount, SetBrightnessDialog, ShareWords, SimplePage, Slip39Input, }, theme, ModelTTFeatures, }; @@ -588,6 +589,46 @@ impl UIFeaturesFirmware for ModelTTFeatures { ) } + fn show_info_with_cancel( + title: TString<'static>, + items: Obj, + horizontal: bool, + chunkify: bool, + ) -> Result { + let mut paragraphs = ParagraphVecShort::new(); + + for para in IterBuf::new().try_iterate(items)? { + let [key, value]: [Obj; 2] = util::iter_into_array(para)?; + let key: TString = key.try_into()?; + let value: TString = value.try_into()?; + paragraphs.add(Paragraph::new(&theme::TEXT_NORMAL, key).no_break()); + if chunkify { + paragraphs.add(Paragraph::new( + theme::get_chunkified_text_style(value.len()), + value, + )); + } else { + paragraphs.add(Paragraph::new(&theme::TEXT_MONO, value)); + } + } + + let axis = match horizontal { + true => geometry::Axis::Horizontal, + _ => geometry::Axis::Vertical, + }; + + let layout = RootComponent::new( + Frame::left_aligned( + theme::label_title(), + title, + SimplePage::new(paragraphs.into_paragraphs(), axis, theme::BG) + .with_swipe_right_to_go_back(), + ) + .with_cancel_button(), + ); + Ok(layout) + } + fn show_lockscreen( label: TString<'static>, bootscreen: bool, diff --git a/core/embed/rust/src/ui/ui_features_fw.rs b/core/embed/rust/src/ui/ui_features_fw.rs index cf554f6f7c..f5c41ed57b 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -159,6 +159,13 @@ pub trait UIFeaturesFirmware { time_ms: u32, ) -> Result, Error>; // TODO: return LayoutMaybeTrace + fn show_info_with_cancel( + title: TString<'static>, + items: Obj, // TODO: replace Obj + horizontal: bool, + chunkify: bool, + ) -> Result; + fn show_lockscreen( label: TString<'static>, bootscreen: bool, diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index f102160a95..a30bd4599d 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -69,17 +69,6 @@ def flow_confirm_set_new_pin( """Confirm new PIN setup with an option to cancel action.""" -# rust/src/ui/model_mercury/layout.rs -def show_info_with_cancel( - *, - title: str, - items: Iterable[Tuple[str, str]], - horizontal: bool = False, - chunkify: bool = False, -) -> LayoutObj[UiResult]: - """Show metadata for outgoing transaction.""" - - # rust/src/ui/model_mercury/layout.rs def confirm_value( *, @@ -422,17 +411,6 @@ def show_address_details( """Show address details - QR code, account, path, cosigner xpubs.""" -# rust/src/ui/model_tt/layout.rs -def show_info_with_cancel( - *, - title: str, - items: Iterable[Tuple[str, str]], - horizontal: bool = False, - chunkify: bool = False, -) -> LayoutObj[UiResult]: - """Show metadata for outgoing transaction.""" - - # rust/src/ui/model_tt/layout.rs def confirm_value( *, diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index 2d80b98348..1c3c64ff69 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -327,6 +327,17 @@ def show_info( """Info screen.""" +# rust/src/ui/api/firmware_upy.rs +def show_info_with_cancel( + *, + title: str, + items: Iterable[Tuple[str, str]], + horizontal: bool = False, + chunkify: bool = False, +) -> LayoutObj[UiResult]: + """Show metadata for outgoing transaction.""" + + # rust/src/ui/api/firmware_upy.rs def show_lockscreen( *, diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index 5937cef6c3..acf40acccc 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -601,7 +601,7 @@ def confirm_value( raise ValueError("Either verb or hold=True must be set") info_items = info_items or [] - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=info_title if info_title else TR.words__title_information, items=info_items, chunkify=chunkify_info, @@ -958,7 +958,7 @@ def confirm_modify_fee( items: list[tuple[str, str]] = [] if fee_rate_amount: items.append((TR.bitcoin__new_fee_rate, fee_rate_amount)) - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=TR.confirm_total__title_fee, items=items, ) @@ -1025,7 +1025,7 @@ async def confirm_signverify( ) ) - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=TR.words__title_information, items=items, horizontal=True, diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index aebf7cf00c..80de346c4d 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -659,7 +659,7 @@ def confirm_value( raise ValueError("Either verb or hold=True must be set") info_items = info_items or [] - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=info_title if info_title else TR.words__title_information, items=info_items, chunkify=chunkify_info, @@ -754,7 +754,7 @@ def _confirm_summary( info_button=bool(info_items), ) info_items = info_items or [] - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=info_title if info_title else TR.words__title_information, items=info_items, ) @@ -791,7 +791,7 @@ if not utils.BITCOIN_ONLY: info_button=True, cancel_arrow=True, ) - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=TR.confirm_total__title_fee, items=[(f"{k}:", v) for (k, v) in fee_info_items], ) @@ -1003,7 +1003,7 @@ def confirm_modify_fee( items: list[tuple[str, str]] = [] if fee_rate_amount: items.append((TR.bitcoin__new_fee_rate, fee_rate_amount)) - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=TR.confirm_total__title_fee, items=items, ) @@ -1070,7 +1070,7 @@ async def confirm_signverify( ) ) - info_layout = trezorui2.show_info_with_cancel( + info_layout = trezorui_api.show_info_with_cancel( title=TR.words__title_information, items=items, horizontal=True,