From 14ff8f7fd6884d9289cb9c465d15160890a76b0c Mon Sep 17 00:00:00 2001 From: obrusvit Date: Mon, 25 Nov 2024 08:11:16 +0100 Subject: [PATCH] refactor(core): move confirm_emphasized - this fn is basically the same as confirm_action_simple for mercury because the demibold font is the same as normal - not implemented for model_r --- core/embed/rust/src/ui/api/firmware_upy.rs | 25 +++++++++++ .../embed/rust/src/ui/model_mercury/layout.rs | 45 ------------------- .../src/ui/model_mercury/ui_features_fw.rs | 35 ++++++++++++++- .../rust/src/ui/model_tr/ui_features_fw.rs | 10 +++++ core/embed/rust/src/ui/model_tt/layout.rs | 45 ------------------- .../rust/src/ui/model_tt/ui_features_fw.rs | 32 ++++++++++++- core/embed/rust/src/ui/ui_features_fw.rs | 6 +++ core/mocks/generated/trezorui2.pyi | 21 --------- core/mocks/generated/trezorui_api.pyi | 11 +++++ .../src/trezor/ui/layouts/mercury/__init__.py | 5 +-- core/src/trezor/ui/layouts/tt/__init__.py | 4 +- 11 files changed, 121 insertions(+), 118 deletions(-) diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index d8b07ef45c..6cfda6ae5c 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -178,6 +178,21 @@ extern "C" fn new_confirm_coinjoin(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +extern "C" fn new_confirm_emphasized(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 verb: Option = kwargs + .get(Qstr::MP_QSTR_verb) + .unwrap_or_else(|_| Obj::const_none()) + .try_into_option()?; + + let layout = ModelUI::confirm_emphasized(title, items, verb)?; + Ok(LayoutObj::new_root(layout)?.into()) + }; + unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } +} + extern "C" fn new_confirm_fido(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()?; @@ -983,6 +998,16 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// """Confirm coinjoin authorization.""" Qstr::MP_QSTR_confirm_coinjoin => obj_fn_kw!(0, new_confirm_coinjoin).as_obj(), + /// def confirm_emphasized( + /// *, + /// title: str, + /// items: Iterable[str | tuple[bool, str]], + /// verb: str | None = None, + /// ) -> LayoutObj[UiResult]: + /// """Confirm formatted text that has been pre-split in python. For tuples + /// the first component is a bool indicating whether this part is emphasized.""" + Qstr::MP_QSTR_confirm_emphasized => obj_fn_kw!(0, new_confirm_emphasized).as_obj(), + /// def confirm_fido( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index f62f2a64eb..b4c47546b6 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -226,41 +226,6 @@ where } } -extern "C" fn new_confirm_emphasized(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 mut ops = OpTextLayout::new(theme::TEXT_NORMAL); - for item in IterBuf::new().try_iterate(items)? { - if item.is_str() { - ops = ops.text_normal(TString::try_from(item)?) - } else { - let [emphasis, text]: [Obj; 2] = util::iter_into_array(item)?; - let text: TString = text.try_into()?; - if emphasis.try_into()? { - ops = ops.text_demibold(text); - } else { - ops = ops.text_normal(text); - } - } - } - - new_confirm_action_simple( - FormattedText::new(ops).vertically_centered(), - ConfirmActionExtra::Menu(ConfirmActionMenuStrings::new()), - ConfirmActionStrings::new(title, None, None, Some(title)), - false, - None, - 0, - false, - ) - .and_then(LayoutObj::new_root) - .map(Into::into) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_confirm_blob_intro(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()?; @@ -541,16 +506,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_obj(), - /// def confirm_emphasized( - /// *, - /// title: str, - /// items: Iterable[str | tuple[bool, str]], - /// verb: str | None = None, - /// ) -> LayoutObj[UiResult]: - /// """Confirm formatted text that has been pre-split in python. For tuples - /// the first component is a bool indicating whether this part is emphasized.""" - Qstr::MP_QSTR_confirm_emphasized => obj_fn_kw!(0, new_confirm_emphasized).as_obj(), - /// def confirm_blob_intro( /// *, /// 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 e76beb92c8..2ee5225f08 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 @@ -11,13 +11,14 @@ use crate::{ connect::Connect, swipe_detect::SwipeSettings, text::{ + op::OpTextLayout, paragraphs::{ Checklist, Paragraph, ParagraphSource, ParagraphVecLong, ParagraphVecShort, Paragraphs, VecExt, }, TextStyle, }, - Border, CachedJpeg, ComponentExt, Empty, Never, Timeout, + Border, CachedJpeg, ComponentExt, Empty, FormattedText, Never, Timeout, }, geometry::{self, Direction}, layout::{ @@ -188,6 +189,38 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { Ok(flow) } + fn confirm_emphasized( + title: TString<'static>, + items: Obj, + verb: Option>, + ) -> Result { + let mut ops = OpTextLayout::new(theme::TEXT_NORMAL); + for item in IterBuf::new().try_iterate(items)? { + if item.is_str() { + ops = ops.text_normal(TString::try_from(item)?) + } else { + let [emphasis, text]: [Obj; 2] = util::iter_into_array(item)?; + let text: TString = text.try_into()?; + if emphasis.try_into()? { + ops = ops.text_demibold(text); + } else { + ops = ops.text_normal(text); + } + } + } + + let flow = flow::new_confirm_action_simple( + FormattedText::new(ops).vertically_centered(), + ConfirmActionExtra::Menu(ConfirmActionMenuStrings::new()), + ConfirmActionStrings::new(title, None, None, Some(title)), + false, + None, + 0, + false, + )?; + Ok(flow) + } + fn confirm_fido( title: TString<'static>, app_name: TString<'static>, 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 8f3e0671b2..8f30278a9b 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 @@ -190,6 +190,16 @@ impl UIFeaturesFirmware for ModelTRFeatures { ) } + fn confirm_emphasized( + _title: TString<'static>, + _items: Obj, + _verb: Option>, + ) -> Result { + Err::, Error>(Error::ValueError( + c"confirm_emphasized not implemented", + )) + } + fn confirm_fido( title: TString<'static>, app_name: TString<'static>, diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 9a7a92b0a4..a3bb33888f 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -317,41 +317,6 @@ impl ComponentMsgObj for super::component::bl_confirm::Confirm<'_> { } } -extern "C" fn new_confirm_emphasized(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 verb: Option = kwargs - .get(Qstr::MP_QSTR_verb) - .unwrap_or_else(|_| Obj::const_none()) - .try_into_option()?; - - let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; - let mut ops = OpTextLayout::new(theme::TEXT_NORMAL); - for item in IterBuf::new().try_iterate(items)? { - if item.is_str() { - ops = ops.text_normal(TString::try_from(item)?) - } else { - let [emphasis, text]: [Obj; 2] = util::iter_into_array(item)?; - let text: TString = text.try_into()?; - if emphasis.try_into()? { - ops = ops.text_demibold(text); - } else { - ops = ops.text_normal(text); - } - } - } - - let obj = LayoutObj::new(Frame::left_aligned( - theme::label_title(), - title, - ButtonPage::new(FormattedText::new(ops).vertically_centered(), theme::BG) - .with_cancel_confirm(None, verb), - ))?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_address_details(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let qr_title: TString<'static> = kwargs.get(Qstr::MP_QSTR_qr_title)?.try_into()?; @@ -421,16 +386,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// from trezorui_api import * /// - /// def confirm_emphasized( - /// *, - /// title: str, - /// items: Iterable[str | tuple[bool, str]], - /// verb: str | None = None, - /// ) -> LayoutObj[UiResult]: - /// """Confirm formatted text that has been pre-split in python. For tuples - /// the first component is a bool indicating whether this part is emphasized.""" - Qstr::MP_QSTR_confirm_emphasized => obj_fn_kw!(0, new_confirm_emphasized).as_obj(), - /// def show_address_details( /// *, /// qr_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 ab90c17db8..68710bf01e 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 @@ -11,13 +11,14 @@ use crate::{ connect::Connect, image::BlendedImage, text::{ + op::OpTextLayout, paragraphs::{ Checklist, Paragraph, ParagraphSource, ParagraphVecLong, ParagraphVecShort, Paragraphs, VecExt, }, TextStyle, }, - Border, ComponentExt, Empty, Jpeg, Label, Never, Timeout, + Border, ComponentExt, Empty, FormattedText, Jpeg, Label, Never, Timeout, }, geometry, layout::{ @@ -186,6 +187,35 @@ impl UIFeaturesFirmware for ModelTTFeatures { Ok(layout) } + fn confirm_emphasized( + title: TString<'static>, + items: Obj, + verb: Option>, + ) -> Result { + let mut ops = OpTextLayout::new(theme::TEXT_NORMAL); + for item in IterBuf::new().try_iterate(items)? { + if item.is_str() { + ops = ops.text_normal(TString::try_from(item)?) + } else { + let [emphasis, text]: [Obj; 2] = util::iter_into_array(item)?; + let text: TString = text.try_into()?; + if emphasis.try_into()? { + ops = ops.text_demibold(text); + } else { + ops = ops.text_normal(text); + } + } + } + + let layout = RootComponent::new(Frame::left_aligned( + theme::label_title(), + title, + ButtonPage::new(FormattedText::new(ops).vertically_centered(), theme::BG) + .with_cancel_confirm(None, verb), + )); + Ok(layout) + } + fn confirm_fido( title: TString<'static>, app_name: TString<'static>, diff --git a/core/embed/rust/src/ui/ui_features_fw.rs b/core/embed/rust/src/ui/ui_features_fw.rs index af7e259214..5bf0c21e1c 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -63,6 +63,12 @@ pub trait UIFeaturesFirmware { max_feerate: TString<'static>, ) -> Result; + fn confirm_emphasized( + title: TString<'static>, + items: Obj, // TODO: replace Obj + verb: Option>, + ) -> Result; + fn confirm_fido( title: TString<'static>, app_name: TString<'static>, diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 4e326e6e72..c49c50f0d2 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -3,16 +3,6 @@ from trezor import utils from trezorui_api import * -# rust/src/ui/model_mercury/layout.rs -def confirm_emphasized( - *, - title: str, - items: Iterable[str | tuple[bool, str]], - verb: str | None = None, -) -> LayoutObj[UiResult]: - """Confirm formatted text that has been pre-split in python. For tuples - the first component is a bool indicating whether this part is emphasized.""" - # rust/src/ui/model_mercury/layout.rs def confirm_blob_intro( *, @@ -189,17 +179,6 @@ from trezor import utils from trezorui_api import * -# rust/src/ui/model_tt/layout.rs -def confirm_emphasized( - *, - title: str, - items: Iterable[str | tuple[bool, str]], - verb: str | None = None, -) -> LayoutObj[UiResult]: - """Confirm formatted text that has been pre-split in python. For tuples - the first component is a bool indicating whether this part is emphasized.""" - - # rust/src/ui/model_tt/layout.rs def show_address_details( *, diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index 33c5439886..29de793a84 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -140,6 +140,17 @@ def confirm_coinjoin( """Confirm coinjoin authorization.""" +# rust/src/ui/api/firmware_upy.rs +def confirm_emphasized( + *, + title: str, + items: Iterable[str | tuple[bool, str]], + verb: str | None = None, +) -> LayoutObj[UiResult]: + """Confirm formatted text that has been pre-split in python. For tuples + the first component is a bool indicating whether this part is emphasized.""" + + # rust/src/ui/api/firmware_upy.rs def confirm_fido( *, diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index fa93601388..3d2c74387a 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -74,12 +74,11 @@ def confirm_single( # Placeholders are coming from translations in form of {0} template_str = "{0}" - if template_str not in description: - template_str = "{}" + assert template_str in description begin, _separator, end = description.partition(template_str) return raise_if_not_confirmed( - trezorui2.confirm_emphasized( + trezorui_api.confirm_emphasized( title=title, items=(begin, (True, description_param), end), verb=verb, diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 2a28e37da3..d2f6d69b82 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -74,7 +74,7 @@ def confirm_single( begin, _separator, end = description.partition(template_str) return raise_if_not_confirmed( - trezorui2.confirm_emphasized( + trezorui_api.confirm_emphasized( title=title, items=(begin, (True, description_param), end), verb=verb, @@ -1251,7 +1251,7 @@ def confirm_set_new_pin( br_code: ButtonRequestType = BR_CODE_OTHER, ) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.confirm_emphasized( + trezorui_api.confirm_emphasized( title=title, items=( (True, description + "\n\n"),