fixup! refactor(core/rust) use TString in Label and Button

pull/3649/head
tychovrahe 2 months ago
parent 578dc94201
commit 7486a5c915

@ -64,16 +64,18 @@ pub fn init() {
} }
} }
// SAFETY: Invalidates all references coming from the flash-based blob. /// # Safety
// In other words, none should exist when this function is called. /// Invalidates all references coming from the flash-based blob.
/// In other words, none should exist when this function is called.
pub unsafe fn deinit() { pub unsafe fn deinit() {
// SAFETY: Given the above, we can safely clear the cached object. // SAFETY: Given the above, we can safely clear the cached object.
unsafe { TRANSLATIONS_ON_FLASH = None }; unsafe { TRANSLATIONS_ON_FLASH = None };
} }
// SAFETY: Gives out a reference to a TranslationsBlob which can be invalidated /// # Safety
// by calling `erase()`. The caller must not store this reference, nor any that /// Gives out a reference to a TranslationsBlob which can be invalidated
// come from it, beyond the lifetime of the current function. /// by calling `erase()`. The caller must not store this reference, nor any that
/// come from it, beyond the lifetime of the current function.
pub unsafe fn get<'a>() -> Option<&'a Translations<'a>> { pub unsafe fn get<'a>() -> Option<&'a Translations<'a>> {
// SAFETY: We are in a single-threaded environment. // SAFETY: We are in a single-threaded environment.
unsafe { TRANSLATIONS_ON_FLASH.as_ref() } unsafe { TRANSLATIONS_ON_FLASH.as_ref() }

@ -226,6 +226,6 @@ impl Component for Marquee {
impl crate::trace::Trace for Marquee { impl crate::trace::Trace for Marquee {
fn trace(&self, t: &mut dyn crate::trace::Tracer) { fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.component("Marquee"); t.component("Marquee");
t.string("text", self.text.into()); t.string("text", self.text);
} }
} }

@ -169,7 +169,7 @@ impl Page {
} }
pub fn title(&self) -> Option<TString<'static>> { pub fn title(&self) -> Option<TString<'static>> {
self.title.clone() self.title
} }
pub fn has_prev_page(&self) -> bool { pub fn has_prev_page(&self) -> bool {

@ -76,8 +76,8 @@ impl Homescreen {
// Buttons will not be visible, we only need both left and right to be existing // Buttons will not be visible, we only need both left and right to be existing
// so we can get the events from them. // so we can get the events from them.
let invisible_btn_layout = ButtonLayout::text_none_text("".into(), "".into()); let invisible_btn_layout = ButtonLayout::text_none_text("".into(), "".into());
let loader = loader_description let loader =
.map(|desc| Child::new(ProgressLoader::new(desc.into(), HOLD_TO_LOCK_MS))); loader_description.map(|desc| Child::new(ProgressLoader::new(desc, HOLD_TO_LOCK_MS)));
Self { Self {
label: Label::centered(label, theme::TEXT_BIG), label: Label::centered(label, theme::TEXT_BIG),
notification, notification,

@ -152,7 +152,7 @@ impl<'a> PinEntry<'a> {
let (showing_real_prompt, header_line_content, pin_line_content) = if show_subprompt { let (showing_real_prompt, header_line_content, pin_line_content) = if show_subprompt {
( (
false, false,
TR::pin__title_wrong_pin.map_translated(|t| String::from(t)), TR::pin__title_wrong_pin.map_translated(String::from),
String::from(subprompt.map(|t| t)), String::from(subprompt.map(|t| t)),
) )
} else { } else {
@ -331,7 +331,7 @@ impl Component for PinEntry<'_> {
impl crate::trace::Trace for PinEntry<'_> { impl crate::trace::Trace for PinEntry<'_> {
fn trace(&self, t: &mut dyn crate::trace::Tracer) { fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.component("PinKeyboard"); t.component("PinKeyboard");
t.string("subprompt", self.subprompt.into()); t.string("subprompt", self.subprompt);
t.string("pin", self.textbox.content().into()); t.string("pin", self.textbox.content().into());
t.child("choice_page", &self.choice_page); t.child("choice_page", &self.choice_page);
} }

@ -21,7 +21,7 @@ pub struct Title {
impl Title { impl Title {
pub fn new(title: TString<'static>) -> Self { pub fn new(title: TString<'static>) -> Self {
Self { Self {
title: title.clone(), title,
marquee: Marquee::new(title, theme::FONT_HEADER, theme::FG, theme::BG), marquee: Marquee::new(title, theme::FONT_HEADER, theme::FG, theme::BG),
needs_marquee: false, needs_marquee: false,
area: Rect::zero(), area: Rect::zero(),
@ -35,13 +35,13 @@ impl Title {
} }
pub fn get_text(&self) -> &str { pub fn get_text(&self) -> &str {
self.title.map(|s| s.as_ref()) self.title.map(|s| s)
} }
pub fn set_text(&mut self, ctx: &mut EventCtx, new_text: TString<'static>) { pub fn set_text(&mut self, ctx: &mut EventCtx, new_text: TString<'static>) {
self.title = new_text.clone(); self.title = new_text;
self.marquee.set_text(new_text.clone()); self.marquee.set_text(new_text);
let text_width = theme::FONT_HEADER.text_width(new_text.map(|s| s.as_ref())); let text_width = theme::FONT_HEADER.text_width(new_text.map(|s| s));
self.needs_marquee = text_width > self.area.width(); self.needs_marquee = text_width > self.area.width();
// Resetting the marquee to the beginning and starting it when necessary. // Resetting the marquee to the beginning and starting it when necessary.
self.marquee.reset(); self.marquee.reset();
@ -56,7 +56,7 @@ impl Title {
let title_baseline = area.top_left() + Offset::y(text_height - 1); let title_baseline = area.top_left() + Offset::y(text_height - 1);
display::text_left( display::text_left(
title_baseline, title_baseline,
title.map(|s| s.as_ref()), title.map(|s| s),
theme::FONT_HEADER, theme::FONT_HEADER,
theme::FG, theme::FG,
theme::BG, theme::BG,
@ -69,7 +69,7 @@ impl Title {
let title_baseline = area.top_center() + Offset::y(text_height - 1); let title_baseline = area.top_center() + Offset::y(text_height - 1);
display::text_center( display::text_center(
title_baseline, title_baseline,
title.map(|s| s.as_ref()), title.map(|s| s),
theme::FONT_HEADER, theme::FONT_HEADER,
theme::FG, theme::FG,
theme::BG, theme::BG,
@ -83,7 +83,7 @@ impl Component for Title {
fn place(&mut self, bounds: Rect) -> Rect { fn place(&mut self, bounds: Rect) -> Rect {
self.area = bounds; self.area = bounds;
self.marquee.place(bounds); self.marquee.place(bounds);
let width = theme::FONT_HEADER.text_width(self.title.map(|s| s.as_ref())); let width = theme::FONT_HEADER.text_width(self.title.map(|s| s));
self.needs_marquee = width > self.area.width(); self.needs_marquee = width > self.area.width();
bounds bounds
} }

@ -730,7 +730,7 @@ extern "C" fn new_altcoin_tx_summary(n_args: usize, args: *const Obj, kwargs: *m
let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?; let items: Obj = kwargs.get(Qstr::MP_QSTR_items)?;
// cached allocated translated strings that get_page can reuse // cached allocated translated strings that get_page can reuse
let tr_title_fee = TR::confirm_total__title_fee.try_into()?; let tr_title_fee = TR::confirm_total__title_fee.into();
let get_page = move |page_index| { let get_page = move |page_index| {
match page_index { match page_index {
@ -1637,7 +1637,7 @@ extern "C" fn new_confirm_firmware_update(
) )
.with_info_screen( .with_info_screen(
TR::firmware_update__title_fingerprint.as_tstring(), TR::firmware_update__title_fingerprint.as_tstring(),
fingerprint.into(), fingerprint,
), ),
)?; )?;
Ok(obj.into()) Ok(obj.into())

@ -191,7 +191,6 @@ impl Button {
match &self.content { match &self.content {
ButtonContent::Empty => {} ButtonContent::Empty => {}
ButtonContent::Text(text) => { ButtonContent::Text(text) => {
let text = text;
let width = style.font.text_width(text.map(|c| c)); let width = style.font.text_width(text.map(|c| c));
let height = style.font.text_height(); let height = style.font.text_height();
let start_of_baseline = self.area.center() let start_of_baseline = self.area.center()

@ -40,11 +40,8 @@ impl<U> CoinJoinProgress<U> {
indeterminate: bool, indeterminate: bool,
) -> Result<CoinJoinProgress<impl Component<Msg = Never> + MaybeTrace>, Error> { ) -> Result<CoinJoinProgress<impl Component<Msg = Never> + MaybeTrace>, Error> {
let style = theme::label_coinjoin_progress(); let style = theme::label_coinjoin_progress();
let label = Label::centered( let label = Label::centered(TR::coinjoin__title_do_not_disconnect.into(), style)
TryInto::<TString>::try_into(TR::coinjoin__title_do_not_disconnect)?, .vertically_centered();
style,
)
.vertically_centered();
let bg = painter::rect_painter(style.background_color, theme::BG); let bg = painter::rect_painter(style.background_color, theme::BG);
let inner = (bg, label); let inner = (bg, label);
CoinJoinProgress::with_background(text, inner, indeterminate) CoinJoinProgress::with_background(text, inner, indeterminate)
@ -65,7 +62,7 @@ where
indeterminate, indeterminate,
content: Frame::centered( content: Frame::centered(
theme::label_title(), theme::label_title(),
TR::coinjoin__title_progress.try_into()?, TR::coinjoin__title_progress.into(),
Split::bottom(RECTANGLE_HEIGHT, 0, Empty, inner), Split::bottom(RECTANGLE_HEIGHT, 0, Empty, inner),
) )
.into_child(), .into_child(),

@ -47,8 +47,8 @@ where
input: NumberInput::new(min, max, init_value).into_child(), input: NumberInput::new(min, max, init_value).into_child(),
paragraphs: Paragraphs::new(Paragraph::new(&theme::TEXT_NORMAL, text)).into_child(), paragraphs: Paragraphs::new(Paragraph::new(&theme::TEXT_NORMAL, text)).into_child(),
paragraphs_pad: Pad::with_background(theme::BG), paragraphs_pad: Pad::with_background(theme::BG),
info_button: Button::with_text(TR::buttons__info.try_into()?).into_child(), info_button: Button::with_text(TR::buttons__info.into()).into_child(),
confirm_button: Button::with_text(TR::buttons__continue.try_into()?) confirm_button: Button::with_text(TR::buttons__continue.into())
.styled(theme::button_confirm()) .styled(theme::button_confirm())
.into_child(), .into_child(),
}) })

@ -49,8 +49,8 @@ where
T: Component, T: Component,
{ {
pub fn with_hold(mut self) -> Result<Self, Error> { pub fn with_hold(mut self) -> Result<Self, Error> {
self.button_confirm = Button::with_text(TR::buttons__hold_to_confirm.try_into()?) self.button_confirm =
.styled(theme::button_confirm()); Button::with_text(TR::buttons__hold_to_confirm.into()).styled(theme::button_confirm());
self.loader = Some(Loader::new()); self.loader = Some(Loader::new());
Ok(self) Ok(self)
} }

@ -623,7 +623,7 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m
ButtonPage::new(paragraphs.into_paragraphs(), theme::BG).with_hold()? ButtonPage::new(paragraphs.into_paragraphs(), theme::BG).with_hold()?
} else { } else {
ButtonPage::new(paragraphs.into_paragraphs(), theme::BG) ButtonPage::new(paragraphs.into_paragraphs(), theme::BG)
.with_cancel_confirm(None, Some(TR::buttons__confirm.try_into()?)) .with_cancel_confirm(None, Some(TR::buttons__confirm.into()))
}; };
let obj = LayoutObj::new(Frame::left_aligned( let obj = LayoutObj::new(Frame::left_aligned(
theme::label_title(), theme::label_title(),
@ -861,7 +861,7 @@ extern "C" fn new_confirm_modify_output(n_args: usize, args: *const Obj, kwargs:
theme::label_title(), theme::label_title(),
tr_title.into(), tr_title.into(),
ButtonPage::<_>::new(paragraphs, theme::BG) ButtonPage::<_>::new(paragraphs, theme::BG)
.with_cancel_confirm(Some("^".into()), Some(TR::buttons__continue.try_into()?)), .with_cancel_confirm(Some("^".into()), Some(TR::buttons__continue.into())),
))?; ))?;
Ok(obj.into()) Ok(obj.into())
}; };
@ -1018,7 +1018,7 @@ extern "C" fn new_confirm_fido(n_args: usize, args: *const Obj, kwargs: *mut Map
let controls = Button::cancel_confirm( let controls = Button::cancel_confirm(
Button::with_icon(theme::ICON_CANCEL), Button::with_icon(theme::ICON_CANCEL),
Button::with_text(TR::buttons__confirm.try_into()?).styled(theme::button_confirm()), Button::with_text(TR::buttons__confirm.into()).styled(theme::button_confirm()),
true, true,
); );
@ -1253,7 +1253,7 @@ extern "C" fn new_request_pin(n_args: usize, args: *const Obj, kwargs: *mut Map)
let allow_cancel: bool = kwargs.get_or(Qstr::MP_QSTR_allow_cancel, true)?; let allow_cancel: bool = kwargs.get_or(Qstr::MP_QSTR_allow_cancel, true)?;
let warning: bool = kwargs.get_or(Qstr::MP_QSTR_wrong_pin, false)?; let warning: bool = kwargs.get_or(Qstr::MP_QSTR_wrong_pin, false)?;
let warning = if warning { let warning = if warning {
Some(TR::pin__wrong_pin.try_into()?) Some(TR::pin__wrong_pin.into())
} else { } else {
None None
}; };
@ -1447,8 +1447,8 @@ extern "C" fn new_confirm_recovery(n_args: usize, args: *const Obj, kwargs: *mut
Dialog::new( Dialog::new(
paragraphs, paragraphs,
Button::cancel_info_confirm( Button::cancel_info_confirm(
TR::buttons__continue.try_into()?, TR::buttons__continue.into(),
TR::buttons__more_info.try_into()?, TR::buttons__more_info.into(),
), ),
), ),
))? ))?
@ -1500,13 +1500,12 @@ extern "C" fn new_show_group_share_success(
let lines_iterable: Obj = kwargs.get(Qstr::MP_QSTR_lines)?; let lines_iterable: Obj = kwargs.get(Qstr::MP_QSTR_lines)?;
let lines: [StrBuffer; 4] = util::iter_into_array(lines_iterable)?; let lines: [StrBuffer; 4] = util::iter_into_array(lines_iterable)?;
let obj = let obj = LayoutObj::new(IconDialog::new_shares(
LayoutObj::new(IconDialog::new_shares( lines,
lines, theme::button_bar(Button::with_text(TR::buttons__continue.into()).map(|msg| {
theme::button_bar(Button::with_text(TR::buttons__continue.try_into()?).map( (matches!(msg, ButtonMsg::Clicked)).then(|| CancelConfirmMsg::Confirmed)
|msg| (matches!(msg, ButtonMsg::Clicked)).then(|| CancelConfirmMsg::Confirmed), })),
)), ))?;
))?;
Ok(obj.into()) Ok(obj.into())
}; };
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
@ -1529,7 +1528,7 @@ extern "C" fn new_show_remaining_shares(n_args: usize, args: *const Obj, kwargs:
theme::label_title(), theme::label_title(),
tr_title.into(), tr_title.into(),
ButtonPage::<_>::new(paragraphs.into_paragraphs(), theme::BG) ButtonPage::<_>::new(paragraphs.into_paragraphs(), theme::BG)
.with_cancel_confirm(None, Some(TR::buttons__continue.try_into()?)) .with_cancel_confirm(None, Some(TR::buttons__continue.into()))
.with_confirm_style(theme::button_default()) .with_confirm_style(theme::button_default())
.without_cancel(), .without_cancel(),
))?; ))?;
@ -1638,18 +1637,16 @@ extern "C" fn new_confirm_firmware_update(
let description: StrBuffer = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; let description: StrBuffer = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let fingerprint: StrBuffer = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?; let fingerprint: StrBuffer = kwargs.get(Qstr::MP_QSTR_fingerprint)?.try_into()?;
let title_str = TR::firmware_update__title.try_into()?; let title_str = TR::firmware_update__title.into();
let title = Label::left_aligned(title_str, theme::TEXT_BOLD).vertically_centered(); let title = Label::left_aligned(title_str, theme::TEXT_BOLD).vertically_centered();
let msg = Label::left_aligned(description.into(), theme::TEXT_NORMAL); let msg = Label::left_aligned(description.into(), theme::TEXT_NORMAL);
let left = let left = Button::with_text(TR::buttons__cancel.into()).styled(theme::button_default());
Button::with_text(TR::buttons__cancel.try_into()?).styled(theme::button_default()); let right = Button::with_text(TR::buttons__install.into()).styled(theme::button_confirm());
let right =
Button::with_text(TR::buttons__install.try_into()?).styled(theme::button_confirm());
let obj = LayoutObj::new( let obj = LayoutObj::new(
Confirm::new(theme::BG, left, right, ConfirmTitle::Text(title), msg).with_info( Confirm::new(theme::BG, left, right, ConfirmTitle::Text(title), msg).with_info(
TR::firmware_update__title_fingerprint.try_into()?, TR::firmware_update__title_fingerprint.into(),
fingerprint.into(), fingerprint.into(),
theme::button_moreinfo(), theme::button_moreinfo(),
), ),
@ -2186,8 +2183,11 @@ mod tests {
#[test] #[test]
fn trace_example_layout() { fn trace_example_layout() {
let buttons = let buttons = Button::cancel_confirm(
Button::cancel_confirm(Button::with_text("Left"), Button::with_text("Right"), false); Button::with_text("Left".into()),
Button::with_text("Right".into()),
false,
);
let ops = OpTextLayout::new(theme::TEXT_NORMAL) let ops = OpTextLayout::new(theme::TEXT_NORMAL)
.text_normal("Testing text layout, with some text, and some more text. And ") .text_normal("Testing text layout, with some text, and some more text. And ")

Loading…
Cancel
Save