1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-24 14:20:57 +00:00

feat(core/rust/ui): support longer passphrases

[no changelog]
This commit is contained in:
grdddj 2022-10-05 13:54:01 +02:00 committed by Martin Milata
parent da83d98539
commit 23a0a37a45
3 changed files with 113 additions and 26 deletions

View File

@ -1057,6 +1057,22 @@ impl Font {
} }
} }
} }
/// Get the length of the longest suffix from a given `text`
/// that will fit into the area `width` pixels wide.
pub fn longest_suffix(self, width: i16, text: &str) -> usize {
let mut text_width = 0;
for (chars_from_right, c) in text.chars().rev().enumerate() {
let c_width = self.char_width(c);
if text_width + c_width > width {
// Another character cannot be fitted, we're done.
return chars_from_right;
}
text_width += c_width;
}
text.len() // it fits in its entirety
}
} }
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]

View File

@ -155,6 +155,10 @@ impl<const L: usize> TextBox<L> {
self.text.is_empty() self.text.is_empty()
} }
pub fn is_full(&self) -> bool {
self.text.len() == self.text.capacity()
}
/// Delete the last character of content, if any. /// Delete the last character of content, if any.
pub fn delete_last(&mut self, ctx: &mut EventCtx) { pub fn delete_last(&mut self, ctx: &mut EventCtx) {
let changed = self.text.pop().is_some(); let changed = self.text.pop().is_some();
@ -207,6 +211,7 @@ impl<const L: usize> TextBox<L> {
} }
} }
/// Create a visible "underscoring" of the last letter of a text.
pub fn paint_pending_marker(text_baseline: Point, text: &str, font: Font, color: Color) { pub fn paint_pending_marker(text_baseline: Point, text: &str, font: Font, color: Color) {
// Measure the width of the last character of input. // Measure the width of the last character of input.
if let Some(last) = text.chars().last() { if let Some(last) = text.chars().last() {

View File

@ -85,15 +85,25 @@ impl PassphraseKeyboard {
// Clear the pending state. // Clear the pending state.
self.input self.input
.mutate(ctx, |ctx, i| i.multi_tap.clear_pending_state(ctx)); .mutate(ctx, |ctx, i| i.multi_tap.clear_pending_state(ctx));
// Make sure to completely repaint the buttons. // Make sure to completely repaint the new buttons.
for btn in &mut self.keys[key_page] { for btn in &mut self.keys[key_page] {
btn.request_complete_repaint(ctx); btn.request_complete_repaint(ctx);
} }
// Reset backlight to normal level on next paint. // Reset backlight to normal level on next paint.
self.fade = true; self.fade = true;
// So that swipe does not visually enable the input buttons when max length
// reached
self.update_input_btns_state(ctx);
} }
/// Possibly changing the buttons' state after change of the input.
fn after_edit(&mut self, ctx: &mut EventCtx) { fn after_edit(&mut self, ctx: &mut EventCtx) {
self.update_back_btn_state(ctx);
self.update_input_btns_state(ctx);
}
/// When the input is empty, disable the back button.
fn update_back_btn_state(&mut self, ctx: &mut EventCtx) {
if self.input.inner().textbox.is_empty() { if self.input.inner().textbox.is_empty() {
self.back.mutate(ctx, |ctx, b| b.disable(ctx)); self.back.mutate(ctx, |ctx, b| b.disable(ctx));
} else { } else {
@ -101,6 +111,19 @@ impl PassphraseKeyboard {
} }
} }
/// When the input has reached max length, disable all the input buttons.
fn update_input_btns_state(&mut self, ctx: &mut EventCtx) {
for btn in self.keys[self.scrollbar.active_page].iter_mut() {
btn.mutate(ctx, |ctx, b| {
if self.input.inner().textbox.is_full() {
b.disable(ctx);
} else {
b.enable(ctx);
}
});
}
}
pub fn passphrase(&self) -> &str { pub fn passphrase(&self) -> &str {
self.input.inner().textbox.content() self.input.inner().textbox.content()
} }
@ -131,9 +154,12 @@ impl Component for PassphraseKeyboard {
self.scrollbar.place(scroll_area); self.scrollbar.place(scroll_area);
self.scrollbar self.scrollbar
.set_count_and_active_page(PAGE_COUNT, STARTING_PAGE); .set_count_and_active_page(PAGE_COUNT, STARTING_PAGE);
for (key, btn) in self.keys[self.scrollbar.active_page].iter_mut().enumerate() {
// Assign the keys in each page to buttons on a 5x3 grid, starting from the // Place all the possible character buttons on all swipe-separated screens.
// second row. for swipe_screen in &mut self.keys {
for (key, btn) in swipe_screen.iter_mut().enumerate() {
// Assign the keys in each page to buttons on a 5x3 grid, starting
// from the second row.
let area = key_grid.cell(if key < 9 { let area = key_grid.cell(if key < 9 {
// The grid has 3 columns, and we skip the first row. // The grid has 3 columns, and we skip the first row.
key + 3 key + 3
@ -143,6 +169,8 @@ impl Component for PassphraseKeyboard {
}); });
btn.place(area); btn.place(area);
} }
}
bounds bounds
} }
@ -175,6 +203,11 @@ impl Component for PassphraseKeyboard {
None None
}; };
} }
// Process key button events in case we did not reach maximum passphrase length.
// (All input buttons should be disallowed in that case, this is just a safety
// measure.)
if !self.input.inner().textbox.is_full() {
for (key, btn) in self.keys[self.scrollbar.active_page].iter_mut().enumerate() { for (key, btn) in self.keys[self.scrollbar.active_page].iter_mut().enumerate() {
if let Some(Clicked) = btn.event(ctx, event) { if let Some(Clicked) = btn.event(ctx, event) {
// Key button was clicked. If this button is pending, let's cycle the pending // Key button was clicked. If this button is pending, let's cycle the pending
@ -188,6 +221,7 @@ impl Component for PassphraseKeyboard {
return None; return None;
} }
} }
}
None None
} }
@ -249,23 +283,55 @@ impl Component for Input {
const TEXT_OFFSET: Offset = Offset::y(8); const TEXT_OFFSET: Offset = Offset::y(8);
let style = theme::label_default(); let style = theme::label_default();
let text_baseline = self.area.bottom_left() - TEXT_OFFSET; let mut text_baseline = self.area.bottom_left() - TEXT_OFFSET;
let text = self.textbox.content(); let text = self.textbox.content();
// Preparing the new text to be displayed.
// Possible optimization is to redraw the background only when pending character // Possible optimization is to redraw the background only when pending character
// is replaced, or only draw rectangle over the pending character and // is replaced, or only draw rectangle over the pending character and
// marker. // marker.
display::rect_fill(self.area, theme::BG); display::rect_fill(self.area, theme::BG);
// Find out how much text can fit into the textbox.
// 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.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.font.text_width(ellipsis);
// Drawing the ellipsis and moving the baseline for the rest of the text.
display::text( display::text(
text_baseline, text_baseline,
text, ellipsis,
style.font, style.font,
style.text_color, style.text_color,
style.background_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.font.longest_suffix(remaining_available_width, text);
&text[text.len() - chars_from_right..]
};
display::text(
text_baseline,
text_to_display,
style.font,
style.text_color,
style.background_color,
);
// Paint the pending marker. // Paint the pending marker.
if self.multi_tap.pending_key().is_some() { if self.multi_tap.pending_key().is_some() {
paint_pending_marker(text_baseline, text, style.font, style.text_color); paint_pending_marker(text_baseline, text_to_display, style.font, style.text_color);
} }
} }