1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-01 11:58:28 +00:00

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
This commit is contained in:
obrusvit 2024-11-25 08:11:16 +01:00
parent 0e36849c36
commit d6af39b5a6
11 changed files with 121 additions and 118 deletions

View File

@ -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<TString> = 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()?;
@ -962,6 +977,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,

View File

@ -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()?;
@ -558,16 +523,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,

View File

@ -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::{
@ -205,6 +206,38 @@ impl UIFeaturesFirmware for ModelMercuryFeatures {
Ok(flow)
}
fn confirm_emphasized(
title: TString<'static>,
items: Obj,
verb: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
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>,

View File

@ -190,6 +190,16 @@ impl UIFeaturesFirmware for ModelTRFeatures {
)
}
fn confirm_emphasized(
_title: TString<'static>,
_items: Obj,
_verb: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelTRFeatures>, Error>(Error::ValueError(
c"confirm_emphasized not implemented",
))
}
fn confirm_fido(
title: TString<'static>,
app_name: TString<'static>,

View File

@ -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<TString> = 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,

View File

@ -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<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
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>,

View File

@ -63,6 +63,12 @@ pub trait UIFeaturesFirmware {
max_feerate: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn confirm_emphasized(
title: TString<'static>,
items: Obj, // TODO: replace Obj
verb: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn confirm_fido(
title: TString<'static>,
app_name: TString<'static>,

View File

@ -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(
*,
@ -201,17 +191,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(
*,

View File

@ -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(
*,

View File

@ -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,

View File

@ -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,
@ -1229,7 +1229,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"),