From 12388ed24adec6a1c44988751a24fca15ad90bcc Mon Sep 17 00:00:00 2001 From: obrusvit Date: Wed, 25 Sep 2024 16:07:59 +0200 Subject: [PATCH] refactor(core/mercury): remove duplicated struct - removes 2nd definition of ConfirmBlobParams, the choice of fields and what supplied in ctor and what in `with_` methods should be thought through again. [no changelog] --- .../rust/src/ui/model_mercury/flow/mod.rs | 4 +- .../rust/src/ui/model_mercury/flow/util.rs | 115 ++++++++++++++-- .../embed/rust/src/ui/model_mercury/layout.rs | 130 ++---------------- 3 files changed, 113 insertions(+), 136 deletions(-) diff --git a/core/embed/rust/src/ui/model_mercury/flow/mod.rs b/core/embed/rust/src/ui/model_mercury/flow/mod.rs index 91c1472367..9a646aec3d 100644 --- a/core/embed/rust/src/ui/model_mercury/flow/mod.rs +++ b/core/embed/rust/src/ui/model_mercury/flow/mod.rs @@ -15,10 +15,9 @@ pub mod request_passphrase; pub mod set_brightness; pub mod show_share_words; pub mod show_tutorial; +pub mod util; pub mod warning_hi_prio; -mod util; - pub use confirm_action::{ new_confirm_action, new_confirm_action_simple, ConfirmActionMenu, ConfirmActionStrings, }; @@ -38,4 +37,5 @@ pub use request_passphrase::RequestPassphrase; pub use set_brightness::SetBrightness; pub use show_share_words::ShowShareWords; pub use show_tutorial::ShowTutorial; +pub use util::{ConfirmBlobParams, ShowInfoParams}; pub use warning_hi_prio::WarningHiPrio; diff --git a/core/embed/rust/src/ui/model_mercury/flow/util.rs b/core/embed/rust/src/ui/model_mercury/flow/util.rs index 21cd83fe30..3f3b952dbe 100644 --- a/core/embed/rust/src/ui/model_mercury/flow/util.rs +++ b/core/embed/rust/src/ui/model_mercury/flow/util.rs @@ -1,6 +1,9 @@ -use super::super::{ - component::{Frame, FrameMsg}, - theme, +use super::{ + super::{ + component::{Frame, FrameMsg}, + theme, + }, + ConfirmActionMenu, ConfirmActionStrings, }; use crate::{ error::Error, @@ -11,13 +14,16 @@ use crate::{ component::{ base::ComponentExt, swipe_detect::SwipeSettings, - text::paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, VecExt}, + text::{ + paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, VecExt}, + TextStyle, + }, Component, }, - flow::{FlowMsg, Swipable, SwipePage}, + flow::{FlowMsg, Swipable, SwipeFlow, SwipePage}, geometry::Direction, layout::util::{ConfirmBlob, StrOrBytes}, - model_mercury::component::SwipeContent, + model_mercury::{component::SwipeContent, flow}, }, }; use heapless::Vec; @@ -29,11 +35,19 @@ pub struct ConfirmBlobParams { footer_description: Option>, data: Obj, description: Option>, + description_font: &'static TextStyle, extra: Option>, - menu_button: bool, + verb: Option>, + verb_cancel: Option>, + verb_info: Option>, + info_button: bool, cancel_button: bool, + menu_button: bool, + prompt: bool, + hold: bool, chunkify: bool, text_mono: bool, + page_limit: Option, swipe_up: bool, swipe_down: bool, swipe_right: bool, @@ -52,11 +66,19 @@ impl ConfirmBlobParams { footer_description: None, data, description, + description_font: &theme::TEXT_NORMAL, extra: None, - menu_button: false, + verb: None, + verb_cancel: None, + verb_info: None, + info_button: false, cancel_button: false, + menu_button: false, + prompt: false, + hold: false, chunkify: false, text_mono: true, + page_limit: None, swipe_up: false, swipe_down: false, swipe_right: false, @@ -83,6 +105,36 @@ impl ConfirmBlobParams { self } + pub const fn with_info_button(mut self, info_button: bool) -> Self { + self.info_button = info_button; + self + } + + pub const fn with_verb(mut self, verb: Option>) -> Self { + self.verb = verb; + self + } + + pub const fn with_verb_cancel(mut self, verb_cancel: Option>) -> Self { + self.verb_cancel = verb_cancel; + self + } + + pub const fn with_verb_info(mut self, verb_info: Option>) -> Self { + self.verb_info = verb_info; + self + } + + pub const fn with_prompt(mut self, prompt: bool) -> Self { + self.prompt = prompt; + self + } + + pub const fn with_hold(mut self, hold: bool) -> Self { + self.hold = hold; + self + } + pub const fn with_swipe_up(mut self) -> Self { self.swipe_up = true; self @@ -118,6 +170,16 @@ impl ConfirmBlobParams { self } + pub const fn with_page_limit(mut self, page_limit: Option) -> Self { + self.page_limit = page_limit; + self + } + + pub const fn with_description_font(mut self, description_font: &'static TextStyle) -> Self { + self.description_font = description_font; + self + } + pub fn into_layout( self, ) -> Result + Swipable + MaybeTrace, Error> { @@ -174,6 +236,38 @@ impl ConfirmBlobParams { Ok(frame.map(|msg| matches!(msg, FrameMsg::Button(_)).then_some(FlowMsg::Info))) } + + pub fn into_flow(self) -> Result { + let paragraphs = ConfirmBlob { + description: self.description.unwrap_or("".into()), + extra: self.extra.unwrap_or("".into()), + data: self.data.try_into()?, + description_font: self.description_font, + extra_font: &theme::TEXT_DEMIBOLD, + data_font: if self.chunkify { + let data: TString = self.data.try_into()?; + theme::get_chunkified_text_style(data.len()) + } else if self.text_mono { + &theme::TEXT_MONO + } else { + &theme::TEXT_NORMAL + }, + } + .into_paragraphs(); + + flow::new_confirm_action_simple( + paragraphs, + ConfirmActionMenu::new(self.verb_cancel, self.info_button, self.verb_info), + ConfirmActionStrings::new( + self.title, + self.subtitle, + self.verb, + self.prompt.then_some(self.title), + ), + self.hold, + self.page_limit, + ) + } } pub struct ShowInfoParams { @@ -242,11 +336,6 @@ impl ShowInfoParams { self } - pub const fn with_chunkify(mut self, chunkify: bool) -> Self { - self.chunkify = chunkify; - self - } - pub const fn with_swipe_up(mut self) -> Self { self.swipe_up = true; self diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index dc9ea273ed..174399d929 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -51,6 +51,7 @@ use crate::{ }, model_mercury::{ component::{check_homescreen_format, SwipeContent}, + flow::util::ConfirmBlobParams, flow::{new_confirm_action_simple, ConfirmActionMenu, ConfirmActionStrings}, theme::ICON_BULLET_CHECKMARK, }, @@ -250,126 +251,6 @@ extern "C" fn new_confirm_emphasized(n_args: usize, args: *const Obj, kwargs: *m unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -struct ConfirmBlobParams { - title: TString<'static>, - subtitle: Option>, - data: Obj, - description: Option>, - description_font: &'static TextStyle, - extra: Option>, - verb: Option>, - verb_cancel: Option>, - verb_info: Option>, - info_button: bool, - prompt: bool, - hold: bool, - chunkify: bool, - text_mono: bool, - page_limit: Option, -} - -impl ConfirmBlobParams { - fn new( - title: TString<'static>, - data: Obj, - description: Option>, - verb: Option>, - verb_info: Option>, - prompt: bool, - hold: bool, - ) -> Self { - Self { - title, - subtitle: None, - data, - description, - description_font: &theme::TEXT_NORMAL, - extra: None, - verb, - verb_cancel: None, - verb_info, - info_button: false, - prompt, - hold, - chunkify: false, - text_mono: true, - page_limit: None, - } - } - - fn with_extra(mut self, extra: Option>) -> Self { - self.extra = extra; - self - } - - fn with_subtitle(mut self, subtitle: Option>) -> Self { - self.subtitle = subtitle; - self - } - - fn with_verb_cancel(mut self, verb_cancel: Option>) -> Self { - self.verb_cancel = verb_cancel; - self - } - - fn with_info_button(mut self, info_button: bool) -> Self { - self.info_button = info_button; - self - } - - fn with_chunkify(mut self, chunkify: bool) -> Self { - self.chunkify = chunkify; - self - } - - fn with_text_mono(mut self, text_mono: bool) -> Self { - self.text_mono = text_mono; - self - } - - fn with_page_limit(mut self, page_limit: Option) -> Self { - self.page_limit = page_limit; - self - } - - fn with_description_font(mut self, description_font: &'static TextStyle) -> Self { - self.description_font = description_font; - self - } - - fn into_flow(self) -> Result { - let paragraphs = ConfirmBlob { - description: self.description.unwrap_or("".into()), - extra: self.extra.unwrap_or("".into()), - data: self.data.try_into()?, - description_font: self.description_font, - extra_font: &theme::TEXT_DEMIBOLD, - data_font: if self.chunkify { - let data: TString = self.data.try_into()?; - theme::get_chunkified_text_style(data.len()) - } else if self.text_mono { - &theme::TEXT_MONO - } else { - &theme::TEXT_NORMAL - }, - } - .into_paragraphs(); - - new_confirm_action_simple( - paragraphs, - ConfirmActionMenu::new(self.verb_cancel, self.info_button, self.verb_info), - ConfirmActionStrings::new( - self.title, - self.subtitle, - self.verb, - self.prompt.then_some(self.title), - ), - self.hold, - self.page_limit, - ) - } -} - extern "C" fn new_confirm_blob(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()?; @@ -435,6 +316,8 @@ extern "C" fn new_confirm_blob(n_args: usize, args: *const Obj, kwargs: *mut Map .with_chunkify(chunkify) .with_page_limit(page_limit) .into_flow() + .and_then(LayoutObj::new) + .map(Into::into) }; unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } @@ -592,7 +475,7 @@ extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Ma let value: Obj = kwargs.get(Qstr::MP_QSTR_value)?; let info_button: bool = kwargs.get_or(Qstr::MP_QSTR_info_button, false)?; - let verb: Option = kwargs + let _verb: Option = kwargs .get(Qstr::MP_QSTR_verb) .unwrap_or_else(|_| Obj::const_none()) .try_into_option()?; @@ -610,7 +493,12 @@ extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Ma .with_info_button(info_button) .with_chunkify(chunkify) .with_text_mono(text_mono) + .with_verb_cancel(verb_cancel) + .with_prompt(hold) + .with_hold(hold) .into_flow() + .and_then(LayoutObj::new) + .map(Into::into) }; unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } }