mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 14:28:07 +00:00
feat(core/rust): create common function for ellipsised text
This commit is contained in:
parent
e00af4f7f3
commit
1c698656c3
@ -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,
|
||||
);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user