mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-08 22:40:59 +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},
|
swipe::{Swipe, SwipeDirection},
|
||||||
theme, ScrollBar,
|
theme, ScrollBar,
|
||||||
},
|
},
|
||||||
|
util::long_line_content_with_ellipsis,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub enum PassphraseKeyboardMsg {
|
pub enum PassphraseKeyboardMsg {
|
||||||
@ -309,7 +310,7 @@ impl Component for Input {
|
|||||||
fn paint(&mut self) {
|
fn paint(&mut self) {
|
||||||
let style = theme::label_keyboard();
|
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());
|
- Offset::y(style.text_font.text_baseline());
|
||||||
|
|
||||||
let text = self.textbox.content();
|
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
|
// Accounting for the pending marker, which draws itself one pixel longer than
|
||||||
// the last character
|
// the last character
|
||||||
let available_area_width = self.area.width() - 1;
|
let available_area_width = self.area.width() - 1;
|
||||||
let text_to_display = if style.text_font.text_width(text) <= available_area_width {
|
let text_to_display =
|
||||||
text // whole text can fit
|
long_line_content_with_ellipsis(text, "...", style.text_font, available_area_width);
|
||||||
} else {
|
|
||||||
// Text is longer, showing its right end with ellipsis at the beginning.
|
|
||||||
let ellipsis = "...";
|
|
||||||
let ellipsis_width = style.text_font.text_width(ellipsis);
|
|
||||||
|
|
||||||
// Drawing the ellipsis and moving the baseline for the rest of the text.
|
display::text_left(
|
||||||
display::text(
|
|
||||||
text_baseline,
|
text_baseline,
|
||||||
ellipsis,
|
&text_to_display,
|
||||||
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(
|
|
||||||
text_baseline,
|
|
||||||
text_to_display,
|
|
||||||
style.text_font,
|
style.text_font,
|
||||||
style.text_color,
|
style.text_color,
|
||||||
style.background_color,
|
style.background_color,
|
||||||
@ -363,7 +340,7 @@ impl Component for Input {
|
|||||||
if self.multi_tap.pending_key().is_some() {
|
if self.multi_tap.pending_key().is_some() {
|
||||||
paint_pending_marker(
|
paint_pending_marker(
|
||||||
text_baseline,
|
text_baseline,
|
||||||
text_to_display,
|
&text_to_display,
|
||||||
style.text_font,
|
style.text_font,
|
||||||
style.text_color,
|
style.text_color,
|
||||||
);
|
);
|
||||||
|
@ -6,6 +6,9 @@ use crate::ui::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use cstr_core::CStr;
|
use cstr_core::CStr;
|
||||||
|
use heapless::String;
|
||||||
|
|
||||||
|
use super::display::Font;
|
||||||
|
|
||||||
pub trait ResultExt {
|
pub trait ResultExt {
|
||||||
fn assert_if_debugging_ui(self, message: &str);
|
fn assert_if_debugging_ui(self, message: &str);
|
||||||
@ -85,6 +88,7 @@ pub fn set_animation_disabled(disabled: bool) {
|
|||||||
pub fn animation_disabled() -> bool {
|
pub fn animation_disabled() -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "ui_debug"))]
|
#[cfg(not(feature = "ui_debug"))]
|
||||||
pub fn set_animation_disabled(_disabled: bool) {}
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::strutil;
|
use crate::strutil;
|
||||||
|
Loading…
Reference in New Issue
Block a user