From 1c698656c36dcfbe8e85975e7c6d204e8330e338 Mon Sep 17 00:00:00 2001 From: grdddj Date: Thu, 4 May 2023 16:15:19 +0200 Subject: [PATCH] feat(core/rust): create common function for ellipsised text --- .../model_tt/component/keyboard/passphrase.rs | 37 ++++--------------- core/embed/rust/src/ui/util.rs | 29 +++++++++++++++ 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs index 0b56a2f155..bd4f3bc958 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs @@ -11,6 +11,7 @@ use crate::ui::{ swipe::{Swipe, SwipeDirection}, theme, ScrollBar, }, + util::long_line_content_with_ellipsis, }; pub enum PassphraseKeyboardMsg { @@ -309,7 +310,7 @@ impl Component for Input { fn paint(&mut self) { let style = theme::label_keyboard(); - let mut text_baseline = self.area.top_left() + Offset::y(style.text_font.text_height()) + let text_baseline = self.area.top_left() + Offset::y(style.text_font.text_height()) - Offset::y(style.text_font.text_baseline()); let text = self.textbox.content(); @@ -324,36 +325,12 @@ impl Component for Input { // Accounting for the pending marker, which draws itself one pixel longer than // the last character let available_area_width = self.area.width() - 1; - let text_to_display = if style.text_font.text_width(text) <= available_area_width { - text // whole text can fit - } else { - // Text is longer, showing its right end with ellipsis at the beginning. - let ellipsis = "..."; - let ellipsis_width = style.text_font.text_width(ellipsis); + let text_to_display = + long_line_content_with_ellipsis(text, "...", style.text_font, available_area_width); - // Drawing the ellipsis and moving the baseline for the rest of the text. - display::text( - text_baseline, - ellipsis, - style.text_font, - style.text_color, - style.background_color, - ); - text_baseline = text_baseline + Offset::x(ellipsis_width); - - // Finding out how many additional text characters will fit in, - // starting from the right end. - let remaining_available_width = available_area_width - ellipsis_width; - let chars_from_right = style - .text_font - .longest_suffix(remaining_available_width, text); - - &text[text.len() - chars_from_right..] - }; - - display::text( + display::text_left( text_baseline, - text_to_display, + &text_to_display, style.text_font, style.text_color, style.background_color, @@ -363,7 +340,7 @@ impl Component for Input { if self.multi_tap.pending_key().is_some() { paint_pending_marker( text_baseline, - text_to_display, + &text_to_display, style.text_font, style.text_color, ); diff --git a/core/embed/rust/src/ui/util.rs b/core/embed/rust/src/ui/util.rs index c332c5cbdc..6c00cdadec 100644 --- a/core/embed/rust/src/ui/util.rs +++ b/core/embed/rust/src/ui/util.rs @@ -6,6 +6,9 @@ use crate::ui::{ }; use cstr_core::CStr; +use heapless::String; + +use super::display::Font; pub trait ResultExt { fn assert_if_debugging_ui(self, message: &str); @@ -85,6 +88,7 @@ pub fn set_animation_disabled(disabled: bool) { pub fn animation_disabled() -> bool { false } + #[cfg(not(feature = "ui_debug"))] pub fn set_animation_disabled(_disabled: bool) {} @@ -118,6 +122,31 @@ pub fn icon_text_center( ); } +/// Returns text to be fit on one line of a given length. +/// When the text is too long to fit, it is truncated with ellipsis +/// on the left side. +// Hardcoding 50 as the length of the returned String - there should +// not be any lines as long as this. +pub fn long_line_content_with_ellipsis( + text: &str, + ellipsis: &str, + text_font: Font, + available_width: i16, +) -> String<50> { + if text_font.text_width(text) <= available_width { + String::from(text) // whole text can fit + } else { + // Text is longer, showing its right end with ellipsis at the beginning. + // Finding out how many additional text characters will fit in, + // starting from the right end. + let ellipsis_width = text_font.text_width(ellipsis); + let remaining_available_width = available_width - ellipsis_width; + let chars_from_right = text_font.longest_suffix(remaining_available_width, text); + + build_string!(50, ellipsis, &text[text.len() - chars_from_right..]) + } +} + #[cfg(test)] mod tests { use crate::strutil;