Skip to content

Commit

Permalink
refactor(core/mercury): remove duplicated struct
Browse files Browse the repository at this point in the history
- 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]
  • Loading branch information
obrusvit committed Sep 26, 2024
1 parent 6cbba09 commit d082f42
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 106 deletions.
4 changes: 2 additions & 2 deletions core/embed/rust/src/ui/model_mercury/flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,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};
#[cfg(feature = "universal_fw")]
pub use confirm_fido::new_confirm_fido;
Expand All @@ -34,4 +33,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;
60 changes: 59 additions & 1 deletion core/embed/rust/src/ui/model_mercury/flow/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use crate::{
text::paragraphs::{Paragraph, ParagraphSource, ParagraphVecShort, VecExt},
Component,
},
flow::{FlowMsg, Swipable, SwipePage},
flow::{FlowMsg, Swipable, SwipePage, SwipeFlow},
geometry::Direction,
layout::util::{ConfirmBlob, StrOrBytes},
model_mercury::component::SwipeContent,
model_mercury::flow,
},
};
use heapless::Vec;
Expand All @@ -30,8 +31,12 @@ pub struct ConfirmBlobParams {
data: Obj,
description: Option<TString<'static>>,
extra: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
menu_button: bool,
cancel_button: bool,
info_button: bool,
prompt: bool,
hold: bool,
chunkify: bool,
text_mono: bool,
swipe_up: bool,
Expand All @@ -52,9 +57,13 @@ impl ConfirmBlobParams {
footer_description: None,
data,
description,
verb_cancel: None,
extra: None,
menu_button: false,
cancel_button: false,
info_button: false,
prompt: false,
hold: false,
chunkify: false,
text_mono: true,
swipe_up: false,
Expand Down Expand Up @@ -83,6 +92,26 @@ 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_cancel(mut self, verb_cancel: Option<TString<'static>>) -> Self {
self.verb_cancel = verb_cancel;
self
}

pub const fn with_hold(mut self, hold: bool) -> Self {
self.hold = hold;
self
}

pub const fn with_prompt(mut self, prompt: bool) -> Self {
self.prompt = prompt;
self
}

pub const fn with_swipe_up(mut self) -> Self {
self.swipe_up = true;
self
Expand Down Expand Up @@ -174,6 +203,35 @@ impl ConfirmBlobParams {

Ok(frame.map(|msg| matches!(msg, FrameMsg::Button(_)).then_some(FlowMsg::Info)))
}

pub fn into_flow(self) -> Result<SwipeFlow, Error> {
let paragraphs = ConfirmBlob {
description: self.description.unwrap_or("".into()),
extra: self.extra.unwrap_or("".into()),
data: self.data.try_into()?,
description_font: &theme::TEXT_NORMAL,
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,
self.title,
self.subtitle,
self.verb_cancel,
self.prompt.then_some(self.title),
self.hold,
self.info_button,
)
}
}

pub struct ShowInfoParams {
Expand Down
117 changes: 14 additions & 103 deletions core/embed/rust/src/ui/model_mercury/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::{
model_mercury::{
component::{check_homescreen_format, SwipeContent},
flow::new_confirm_action_simple,
flow::util::ConfirmBlobParams,
theme::ICON_BULLET_CHECKMARK,
},
},
Expand Down Expand Up @@ -250,110 +251,14 @@ 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<TString<'static>>,
data: Obj,
description: Option<TString<'static>>,
extra: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
info_button: bool,
prompt: bool,
hold: bool,
chunkify: bool,
text_mono: bool,
}

impl ConfirmBlobParams {
fn new(
title: TString<'static>,
data: Obj,
description: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
prompt: bool,
hold: bool,
) -> Self {
Self {
title,
subtitle: None,
data,
description,
extra: None,
verb,
verb_cancel,
info_button: false,
prompt,
hold,
chunkify: false,
text_mono: true,
}
}

fn with_extra(mut self, extra: Option<TString<'static>>) -> Self {
self.extra = extra;
self
}

fn with_subtitle(mut self, subtitle: Option<TString<'static>>) -> Self {
self.subtitle = subtitle;
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 into_flow(self) -> Result<Obj, Error> {
let paragraphs = ConfirmBlob {
description: self.description.unwrap_or("".into()),
extra: self.extra.unwrap_or("".into()),
data: self.data.try_into()?,
description_font: &theme::TEXT_NORMAL,
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,
self.title,
self.subtitle,
self.verb_cancel,
self.prompt.then_some(self.title),
self.hold,
self.info_button,
)
}
}

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()?;
let data: Obj = kwargs.get(Qstr::MP_QSTR_data)?;
let description: Option<TString> =
kwargs.get(Qstr::MP_QSTR_description)?.try_into_option()?;
let extra: Option<TString> = kwargs.get(Qstr::MP_QSTR_extra)?.try_into_option()?;
let verb: Option<TString> = kwargs
let _verb: Option<TString> = kwargs
.get(Qstr::MP_QSTR_verb)
.unwrap_or_else(|_| Obj::const_none())
.try_into_option()?;
Expand All @@ -369,14 +274,15 @@ extern "C" fn new_confirm_blob(n_args: usize, args: *const Obj, kwargs: *mut Map
title,
data,
description,
verb,
verb_cancel,
prompt_screen,
hold,
)
.with_extra(extra)
.with_chunkify(chunkify)
.with_verb_cancel(verb_cancel)
.with_hold(hold)
.with_prompt(prompt_screen)
.into_flow()
.and_then(LayoutObj::new)
.map(Into::into)
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
Expand Down Expand Up @@ -527,7 +433,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<TString> = kwargs
let _verb: Option<TString> = kwargs
.get(Qstr::MP_QSTR_verb)
.unwrap_or_else(|_| Obj::const_none())
.try_into_option()?;
Expand All @@ -539,12 +445,17 @@ extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Ma
let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?;
let text_mono: bool = kwargs.get_or(Qstr::MP_QSTR_text_mono, true)?;

ConfirmBlobParams::new(title, value, description, verb, verb_cancel, hold, hold)
ConfirmBlobParams::new(title, value, description)
.with_subtitle(subtitle)
.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) }
}
Expand Down

0 comments on commit d082f42

Please sign in to comment.