Skip to content

Commit

Permalink
refactor(core/mercury): separate upy args parsing
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
obrusvit committed Sep 30, 2024
1 parent 5e31e44 commit a8f0f9f
Show file tree
Hide file tree
Showing 16 changed files with 1,226 additions and 1,069 deletions.
48 changes: 15 additions & 33 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
error::{self, Error},
maybe_trace::MaybeTrace,
micropython::{map::Map, obj::Obj, qstr::Qstr, util},
strutil::TString,
translations::TR,
ui::{
Expand All @@ -15,7 +14,6 @@ use crate::{
FlowController, FlowMsg, SwipeFlow, SwipePage,
},
geometry::Direction,
layout::obj::LayoutObj,
},
};

Expand Down Expand Up @@ -96,33 +94,17 @@ impl FlowController for ConfirmActionSimple {
}
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn new_confirm_action(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, new_confirm_action_obj) }
}

fn new_confirm_action_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Error> {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let action: Option<TString> = kwargs.get(Qstr::MP_QSTR_action)?.try_into_option()?;
let description: Option<TString> = kwargs.get(Qstr::MP_QSTR_description)?.try_into_option()?;
let subtitle: Option<TString> = kwargs
.get(Qstr::MP_QSTR_subtitle)
.unwrap_or(Obj::const_none())
.try_into_option()?;
// let verb: Option<TString> = kwargs
// .get(Qstr::MP_QSTR_verb)
// .unwrap_or_else(|_| Obj::const_none())
// .try_into_option()?;
let verb_cancel: Option<TString> = kwargs
.get(Qstr::MP_QSTR_verb_cancel)
.unwrap_or_else(|_| Obj::const_none())
.try_into_option()?;
let reverse: bool = kwargs.get_or(Qstr::MP_QSTR_reverse, false)?;
let hold: bool = kwargs.get_or(Qstr::MP_QSTR_hold, false)?;
// let hold_danger: bool = kwargs.get_or(Qstr::MP_QSTR_hold_danger, false)?;
let prompt_screen: bool = kwargs.get_or(Qstr::MP_QSTR_prompt_screen, false)?;
let prompt_title: TString = kwargs.get_or(Qstr::MP_QSTR_prompt_title, title)?;

pub fn new_confirm_action(
title: TString<'static>,
action: Option<TString<'static>>,
description: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
reverse: bool,
hold: bool,
prompt_screen: bool,
prompt_title: TString<'static>,
) -> Result<SwipeFlow, error::Error> {
let paragraphs = {
let action = action.unwrap_or("".into());
let description = description.unwrap_or("".into());
Expand Down Expand Up @@ -159,7 +141,7 @@ pub fn new_confirm_action_uni<T: Component + MaybeTrace + 'static>(
prompt_screen: Option<TString<'static>>,
hold: bool,
info: bool,
) -> Result<Obj, error::Error> {
) -> Result<SwipeFlow, error::Error> {
let (prompt_screen, prompt_pages, flow, page) = create_flow(title, prompt_screen, hold);

let mut content_intro = Frame::left_aligned(title, content)
Expand Down Expand Up @@ -220,12 +202,12 @@ fn create_menu_and_confirm(
info: bool,
prompt_screen: Option<TString<'static>>,
flow: SwipeFlow,
) -> Result<Obj, Error> {
) -> Result<SwipeFlow, Error> {
let flow = create_menu(flow, verb_cancel, info, prompt_screen)?;

let flow = create_confirm(flow, subtitle, hold, prompt_screen)?;

Ok(LayoutObj::new(flow)?.into())
Ok(flow)
}

fn create_menu(
Expand Down Expand Up @@ -310,7 +292,7 @@ pub fn new_confirm_action_simple<T: Component + Paginate + MaybeTrace + 'static>
prompt_screen: Option<TString<'static>>,
hold: bool,
info: bool,
) -> Result<Obj, error::Error> {
) -> Result<SwipeFlow, error::Error> {
new_confirm_action_uni(
SwipeContent::new(SwipePage::vertical(content)),
title,
Expand Down
129 changes: 57 additions & 72 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_firmware_update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
error,
micropython::{map::Map, obj::Obj, qstr::Qstr, util},
strutil::TString,
translations::TR,
ui::{
Expand All @@ -14,7 +13,6 @@ use crate::{
FlowController, FlowMsg, SwipeFlow,
},
geometry::Direction,
layout::obj::LayoutObj,
},
};

Expand Down Expand Up @@ -65,78 +63,65 @@ impl FlowController for ConfirmFirmwareUpdate {
}
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn new_confirm_firmware_update(
n_args: usize,
args: *const Obj,
kwargs: *mut Map,
) -> Obj {
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, ConfirmFirmwareUpdate::new_obj) }
}

impl ConfirmFirmwareUpdate {
fn new_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Error> {
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let fingerprint: TString = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?;
pub fn new_confirm_firmware_update(
description: TString<'static>,
fingerprint: TString<'static>,
) -> Result<SwipeFlow, error::Error> {
let paragraphs = Paragraphs::new(Paragraph::new(&theme::TEXT_MAIN_GREY_LIGHT, description));
let content_intro = Frame::left_aligned(
TR::firmware_update__title.into(),
SwipeContent::new(paragraphs),
)
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_swipe(Direction::Up, SwipeSettings::default())
.with_swipe(Direction::Left, SwipeSettings::default())
.map(|msg| matches!(msg, FrameMsg::Button(FlowMsg::Info)).then_some(FlowMsg::Info));

let paragraphs = Paragraphs::new(Paragraph::new(&theme::TEXT_MAIN_GREY_LIGHT, description));
let content_intro = Frame::left_aligned(
TR::firmware_update__title.into(),
SwipeContent::new(paragraphs),
)
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_swipe(Direction::Up, SwipeSettings::default())
.with_swipe(Direction::Left, SwipeSettings::default())
.map(|msg| matches!(msg, FrameMsg::Button(FlowMsg::Info)).then_some(FlowMsg::Info));
let content_menu = Frame::left_aligned(
TString::empty(),
VerticalMenu::empty()
.item(
theme::ICON_CHEVRON_RIGHT,
TR::firmware_update__title_fingerprint.into(),
)
.danger(theme::ICON_CANCEL, TR::buttons__cancel.into()),
)
.with_cancel_button()
.with_swipe(Direction::Right, SwipeSettings::immediate())
.map(|msg| match msg {
FrameMsg::Content(VerticalMenuChoiceMsg::Selected(i)) => Some(FlowMsg::Choice(i)),
FrameMsg::Button(_) => Some(FlowMsg::Cancelled),
});

let content_menu = Frame::left_aligned(
TString::empty(),
VerticalMenu::empty()
.item(
theme::ICON_CHEVRON_RIGHT,
TR::firmware_update__title_fingerprint.into(),
)
.danger(theme::ICON_CANCEL, TR::buttons__cancel.into()),
)
.with_cancel_button()
.with_swipe(Direction::Right, SwipeSettings::immediate())
.map(|msg| match msg {
FrameMsg::Content(VerticalMenuChoiceMsg::Selected(i)) => Some(FlowMsg::Choice(i)),
FrameMsg::Button(_) => Some(FlowMsg::Cancelled),
});
let paragraphs_fingerprint =
Paragraphs::new(Paragraph::new(&theme::TEXT_MONO_GREY_LIGHT, fingerprint));
let content_fingerprint = Frame::left_aligned(
TR::firmware_update__title_fingerprint.into(),
SwipeContent::new(paragraphs_fingerprint),
)
.with_cancel_button()
.with_swipe(Direction::Right, SwipeSettings::default())
.map(|msg| matches!(msg, FrameMsg::Button(FlowMsg::Cancelled)).then_some(FlowMsg::Cancelled));

let paragraphs_fingerprint =
Paragraphs::new(Paragraph::new(&theme::TEXT_MONO_GREY_LIGHT, fingerprint));
let content_fingerprint = Frame::left_aligned(
TR::firmware_update__title_fingerprint.into(),
SwipeContent::new(paragraphs_fingerprint),
)
.with_cancel_button()
.with_swipe(Direction::Right, SwipeSettings::default())
.map(|msg| {
matches!(msg, FrameMsg::Button(FlowMsg::Cancelled)).then_some(FlowMsg::Cancelled)
});
let content_confirm = Frame::left_aligned(
TR::firmware_update__title.into(),
SwipeContent::new(PromptScreen::new_hold_to_confirm()),
)
.with_menu_button()
.with_footer(TR::instructions__hold_to_confirm.into(), None)
.with_swipe(Direction::Down, SwipeSettings::default())
.with_swipe(Direction::Left, SwipeSettings::default())
.map(|msg| match msg {
FrameMsg::Content(PromptMsg::Confirmed) => Some(FlowMsg::Confirmed),
FrameMsg::Button(_) => Some(FlowMsg::Info),
_ => None,
});

let content_confirm = Frame::left_aligned(
TR::firmware_update__title.into(),
SwipeContent::new(PromptScreen::new_hold_to_confirm()),
)
.with_menu_button()
.with_footer(TR::instructions__hold_to_confirm.into(), None)
.with_swipe(Direction::Down, SwipeSettings::default())
.with_swipe(Direction::Left, SwipeSettings::default())
.map(|msg| match msg {
FrameMsg::Content(PromptMsg::Confirmed) => Some(FlowMsg::Confirmed),
FrameMsg::Button(_) => Some(FlowMsg::Info),
_ => None,
});

let res = SwipeFlow::new(&ConfirmFirmwareUpdate::Intro)?
.with_page(&ConfirmFirmwareUpdate::Intro, content_intro)?
.with_page(&ConfirmFirmwareUpdate::Menu, content_menu)?
.with_page(&ConfirmFirmwareUpdate::Fingerprint, content_fingerprint)?
.with_page(&ConfirmFirmwareUpdate::Confirm, content_confirm)?;
Ok(LayoutObj::new(res)?.into())
}
let res = SwipeFlow::new(&ConfirmFirmwareUpdate::Intro)?
.with_page(&ConfirmFirmwareUpdate::Intro, content_intro)?
.with_page(&ConfirmFirmwareUpdate::Menu, content_menu)?
.with_page(&ConfirmFirmwareUpdate::Fingerprint, content_fingerprint)?
.with_page(&ConfirmFirmwareUpdate::Confirm, content_confirm)?;
Ok(res)
}
65 changes: 22 additions & 43 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use heapless::Vec;

use crate::{
error,
micropython::{iter::IterBuf, map::Map, obj::Obj, qstr::Qstr, util},
micropython::{iter::IterBuf, obj::Obj, util},
strutil::TString,
translations::TR,
ui::{
Expand All @@ -13,7 +13,6 @@ use crate::{
FlowController, FlowMsg, SwipeFlow,
},
geometry::Direction,
layout::obj::LayoutObj,
},
};

Expand Down Expand Up @@ -212,46 +211,26 @@ fn get_cancel_page(
})
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn new_confirm_output(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, new_confirm_output_obj) }
}

fn new_confirm_output_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Error> {
let title: Option<TString> = kwargs.get(Qstr::MP_QSTR_title)?.try_into_option()?;
let subtitle: Option<TString> = kwargs.get(Qstr::MP_QSTR_subtitle)?.try_into_option()?;

let account: Option<TString> = kwargs.get(Qstr::MP_QSTR_account)?.try_into_option()?;
let account_path: Option<TString> =
kwargs.get(Qstr::MP_QSTR_account_path)?.try_into_option()?;

let br_name: TString = kwargs.get(Qstr::MP_QSTR_br_name)?.try_into()?;
let br_code: u16 = kwargs.get(Qstr::MP_QSTR_br_code)?.try_into()?;

let message: Obj = kwargs.get(Qstr::MP_QSTR_message)?;
let amount: Option<Obj> = kwargs.get(Qstr::MP_QSTR_amount)?.try_into_option()?;

let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?;
let text_mono: bool = kwargs.get_or(Qstr::MP_QSTR_text_mono, true)?;

let address: Option<Obj> = kwargs.get(Qstr::MP_QSTR_address)?.try_into_option()?;
let address_title: Option<TString> =
kwargs.get(Qstr::MP_QSTR_address_title)?.try_into_option()?;

let summary_items: Obj = kwargs.get(Qstr::MP_QSTR_summary_items)?;
let fee_items: Obj = kwargs.get(Qstr::MP_QSTR_fee_items)?;

let summary_title: Option<TString> =
kwargs.get(Qstr::MP_QSTR_summary_title)?.try_into_option()?;
let summary_br_name: Option<TString> = kwargs
.get(Qstr::MP_QSTR_summary_br_name)?
.try_into_option()?;
let summary_br_code: Option<u16> = kwargs
.get(Qstr::MP_QSTR_summary_br_code)?
.try_into_option()?;

let cancel_text: Option<TString> = kwargs.get(Qstr::MP_QSTR_cancel_text)?.try_into_option()?;

pub fn new_confirm_output(
title: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
account: Option<TString<'static>>,
account_path: Option<TString<'static>>,
br_name: TString<'static>,
br_code: u16,
message: Obj,
amount: Option<Obj>,
chunkify: bool,
text_mono: bool,
address: Option<Obj>,
address_title: Option<TString<'static>>,
summary_items: Obj,
fee_items: Obj,
summary_title: Option<TString<'static>>,
summary_br_name: Option<TString<'static>>,
summary_br_code: Option<u16>,
cancel_text: Option<TString<'static>>,
) -> Result<SwipeFlow, error::Error> {
// Main
let main_content = ConfirmBlobParams::new(title.unwrap_or(TString::empty()), message, None)
.with_subtitle(subtitle)
Expand Down Expand Up @@ -450,5 +429,5 @@ fn new_confirm_output_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Err
.with_page(&ConfirmOutput::CancelTap, get_cancel_page())?
};

Ok(LayoutObj::new(res)?.into())
Ok(res)
}
13 changes: 2 additions & 11 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_reset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
error,
micropython::{map::Map, obj::Obj, qstr::Qstr, util},
strutil::TString,
translations::TR,
ui::{
Expand All @@ -15,7 +14,6 @@ use crate::{
FlowController, FlowMsg, SwipeFlow,
},
geometry::Direction,
layout::obj::LayoutObj,
},
};

Expand Down Expand Up @@ -93,14 +91,7 @@ impl FlowController for ConfirmResetRecover {
}
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn new_confirm_reset(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, new_confirm_reset_obj) }
}

fn new_confirm_reset_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Error> {
let recovery: bool = kwargs.get_or(Qstr::MP_QSTR_recovery, false)?;

pub fn new_confirm_reset(recovery: bool) -> Result<SwipeFlow, error::Error> {
let (title, br, cancel_btn_text) = if recovery {
(
TR::recovery__title_recover.into(),
Expand Down Expand Up @@ -168,5 +159,5 @@ fn new_confirm_reset_obj(_args: &[Obj], kwargs: &Map) -> Result<Obj, error::Erro
.with_page(&ConfirmResetCreate::Menu, content_menu)?
.with_page(&ConfirmResetCreate::Confirm, content_confirm)?
};
Ok(LayoutObj::new(res)?.into())
Ok(res)
}
Loading

0 comments on commit a8f0f9f

Please sign in to comment.