Skip to content

Commit

Permalink
refactor(core): consistent use of confirm_address
Browse files Browse the repository at this point in the history
- use `confirm_address` trait fn in both `confirm_output` and
`confirm_address` layout functions

[no changelog]
  • Loading branch information
obrusvit committed Dec 18, 2024
1 parent 0d476fb commit 9ee2db3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 73 deletions.
12 changes: 5 additions & 7 deletions core/embed/rust/src/ui/api/firmware_micropython.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ extern "C" fn new_confirm_address(n_args: usize, args: *const Obj, kwargs: *mut
.get(Qstr::MP_QSTR_description)
.unwrap_or_else(|_| Obj::const_none())
.try_into_option()?;
let extra: Option<TString> = kwargs
.get(Qstr::MP_QSTR_extra)
.unwrap_or_else(|_| 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 info_button: bool = kwargs.get_or(Qstr::MP_QSTR_info_button, false)?;
let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?;

let layout = ModelUI::confirm_address(title, data, description, extra, verb, chunkify)?;
Ok(LayoutObj::new_root(layout)?.into())
let layout_obj =
ModelUI::confirm_address(title, data, description, verb, info_button, chunkify)?;
Ok(layout_obj.into())
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
Expand Down Expand Up @@ -1165,8 +1163,8 @@ pub static mp_module_trezorui_api: Module = obj_module! {
/// title: str,
/// data: str | bytes,
/// description: str | None,
/// extra: str | None,
/// verb: str | None = None,
/// info_button: bool = False,
/// chunkify: bool = False,
/// ) -> LayoutObj[UiResult]:
/// """Confirm address."""
Expand Down
8 changes: 3 additions & 5 deletions core/embed/rust/src/ui/model_mercury/ui_firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ impl FirmwareUI for UIMercury {
_title: TString<'static>,
_data: Obj,
_description: Option<TString<'static>>,
_extra: Option<TString<'static>>,
_verb: Option<TString<'static>>,
_info_button: bool,
_chunkify: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
) -> Result<Gc<LayoutObj>, Error> {
// confirm_value is used instead
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"confirm_address not implemented",
))
Err::<Gc<LayoutObj>, Error>(Error::ValueError(c"confirm_address not implemented"))
}

fn confirm_blob(
Expand Down
42 changes: 29 additions & 13 deletions core/embed/rust/src/ui/model_tr/ui_firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,49 @@ impl FirmwareUI for UIModelTR {
fn confirm_address(
title: TString<'static>,
data: Obj,
_description: Option<TString<'static>>,
_extra: Option<TString<'static>>,
description: Option<TString<'static>>,
verb: Option<TString<'static>>,
info_button: bool,
chunkify: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
) -> Result<Gc<LayoutObj>, Error> {
let verb = verb.unwrap_or(TR::buttons__confirm.into());
let address: TString = data.try_into()?;

let get_page = move |page_index| {
assert!(page_index == 0);

let btn_layout = ButtonLayout::cancel_armed_info(verb);
let btn_actions = ButtonActions::cancel_confirm_info();
let style = if chunkify {
// Chunkifying the address into smaller pieces when requested
theme::TEXT_MONO_ADDRESS_CHUNKS
let (btn_layout, btn_actions) = if info_button {
(
ButtonLayout::cancel_armed_info(verb),
ButtonActions::cancel_confirm_info(),
)
} else {
theme::TEXT_MONO_DATA
(
ButtonLayout::cancel_none_text(verb),
ButtonActions::cancel_none_confirm(),
)
};
let ops = OpTextLayout::new(style).text_mono(address);
let mut ops = OpTextLayout::new(theme::TEXT_MONO_DATA);
if let Some(description) = description {
// NOTE: need to explicitly turn off the chunkification before rendering the
// address label (for some reason it does not help to turn it off after
// rendering the chunks)
if chunkify {
ops = ops.chunkify_text(None);
}
ops = ops.text_normal(description).newline();
}
if chunkify {
// Chunkifying the address into smaller pieces when requested
ops = ops.chunkify_text(Some((theme::MONO_CHUNKS, 2)));
}
ops = ops.text_mono(address);
let formatted = FormattedText::new(ops).vertically_centered();
Page::new(btn_layout, btn_actions, formatted).with_title(title)
};
let pages = FlowPages::new(get_page, 1);

let layout = RootComponent::new(Flow::new(pages));
Ok(layout)
let obj = LayoutObj::new(Flow::new(pages))?;
Ok(obj)
}

fn confirm_blob(
Expand Down
36 changes: 6 additions & 30 deletions core/embed/rust/src/ui/model_tt/ui_firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,15 @@ impl FirmwareUI for UIModelTT {
title: TString<'static>,
data: Obj,
description: Option<TString<'static>>,
extra: Option<TString<'static>>,
verb: Option<TString<'static>>,
info_button: bool,
chunkify: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
) -> Result<Gc<LayoutObj>, Error> {
let verb = verb.unwrap_or(TR::buttons__confirm.into());
let data_style = if chunkify {
let address: TString = data.try_into()?;
theme::get_chunkified_text_style(address.len())
} else {
&theme::TEXT_MONO
};

let paragraphs = ConfirmBlob {
description: description.unwrap_or("".into()),
extra: extra.unwrap_or("".into()),
data: data.try_into()?,
description_font: &theme::TEXT_NORMAL,
extra_font: &theme::TEXT_DEMIBOLD,
data_font: data_style,
}
.into_paragraphs();

let layout = RootComponent::new(
Frame::left_aligned(
theme::label_title(),
title,
ButtonPage::new(paragraphs, theme::BG)
.with_swipe_left()
.with_cancel_confirm(None, Some(verb)),
)
.with_info_button(),
);
Ok(layout)
ConfirmBlobParams::new(title, data, description, Some(verb), None, false)
.with_info_button(info_button)
.with_chunkify(chunkify)
.into_layout()
}

fn confirm_blob(
Expand Down
4 changes: 2 additions & 2 deletions core/embed/rust/src/ui/ui_firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub trait FirmwareUI {
title: TString<'static>,
data: Obj, // TODO: replace Obj
description: Option<TString<'static>>,
extra: Option<TString<'static>>,
verb: Option<TString<'static>>,
info_button: bool,
chunkify: bool,
) -> Result<impl LayoutMaybeTrace, Error>;
) -> Result<Gc<LayoutObj>, Error>; // TODO: return LayoutMaybeTrace

#[allow(clippy::too_many_arguments)]
fn confirm_blob(
Expand Down
2 changes: 1 addition & 1 deletion core/mocks/generated/trezorui_api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def confirm_address(
title: str,
data: str | bytes,
description: str | None,
extra: str | None,
verb: str | None = None,
info_button: bool = False,
chunkify: bool = False,
) -> LayoutObj[UiResult]:
"""Confirm address."""
Expand Down
15 changes: 7 additions & 8 deletions core/src/trezor/ui/layouts/tr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ async def show_address(
trezorui_api.confirm_address(
title=title,
data=address,
description="", # unused on TR
extra=None, # unused on TR
description=None,
info_button=True,
chunkify=chunkify,
),
br_name if send_button_request else None,
Expand Down Expand Up @@ -485,13 +485,12 @@ async def confirm_output(

while True:
await interact(
trezorui_api.confirm_blob(
trezorui_api.confirm_address(
title=address_title,
data=address,
description=address_label or "",
subtitle=None,
description=address_label or None,
verb=TR.buttons__continue,
verb_cancel="",
info_button=False,
chunkify=chunkify,
),
"confirm_output",
Expand All @@ -500,9 +499,9 @@ async def confirm_output(

try:
await interact(
trezorui_api.confirm_blob(
trezorui_api.confirm_value(
title=amount_title,
data=amount,
value=amount,
description=None,
subtitle=None,
verb_cancel="^",
Expand Down
12 changes: 5 additions & 7 deletions core/src/trezor/ui/layouts/tt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ async def show_address(
title=title,
data=address,
description=network or "",
extra=None,
info_button=True,
chunkify=chunkify,
),
br_name if send_button_request else None,
Expand Down Expand Up @@ -427,13 +427,11 @@ async def confirm_output(
while True:
# if the user cancels here, raise ActionCancelled (by default)
await interact(
trezorui_api.confirm_value(
trezorui_api.confirm_address(
title=recipient_title,
subtitle=address_label,
description=None,
value=address,
description=address_label,
data=address,
verb=TR.buttons__continue,
hold=False,
info_button=False,
chunkify=chunkify,
),
Expand Down Expand Up @@ -1108,7 +1106,7 @@ async def confirm_signverify(
data=address,
description="",
verb=TR.buttons__continue,
extra=None,
info_button=True,
chunkify=chunkify,
)

Expand Down

0 comments on commit 9ee2db3

Please sign in to comment.