From 099d00de8452a1072220f1aa9c6a3c8687c48e22 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Fri, 3 Feb 2023 17:52:26 +0100 Subject: [PATCH] perf(core/rust): use less Button instances in PassphraseKeyboard [no changelog] --- .../model_tt/component/keyboard/passphrase.rs | 70 ++++++++++--------- 1 file changed, 37 insertions(+), 33 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 8ff3ad41c..23b6750b0 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 @@ -24,7 +24,7 @@ pub struct PassphraseKeyboard { input: Child, back: Child>, confirm: Child>, - keys: [[Child>; KEY_COUNT]; PAGE_COUNT], + keys: [Child>; KEY_COUNT], scrollbar: ScrollBar, fade: bool, } @@ -59,16 +59,8 @@ impl PassphraseKeyboard { .initially_enabled(false) .with_long_press(theme::ERASE_HOLD_DURATION) .into_child(), - keys: KEYBOARD.map(|page| { - page.map(|text| { - if text == " " { - let icon = Icon::new(theme::ICON_SPACE); - Child::new(Button::with_icon(icon)) - } else { - Child::new(Button::with_text(text)) - } - }) - }), + keys: KEYBOARD[STARTING_PAGE] + .map(|text| Child::new(Button::new(Self::key_content(text)))), scrollbar: ScrollBar::horizontal(), fade: false, } @@ -83,6 +75,13 @@ impl PassphraseKeyboard { } } + fn key_content(text: &'static str) -> ButtonContent<&'static str> { + match text { + " " => ButtonContent::Icon(Icon::new(theme::ICON_SPACE)), + t => ButtonContent::Text(t), + } + } + fn on_page_swipe(&mut self, ctx: &mut EventCtx, swipe: SwipeDirection) { // Change the page number. let key_page = self.scrollbar.active_page; @@ -95,10 +94,8 @@ impl PassphraseKeyboard { // Clear the pending state. self.input .mutate(ctx, |ctx, i| i.multi_tap.clear_pending_state(ctx)); - // Make sure to completely repaint the new buttons. - for btn in &mut self.keys[key_page] { - btn.request_complete_repaint(ctx); - } + // Update buttons. + self.replace_button_content(ctx, key_page); // Reset backlight to normal level on next paint. self.fade = true; // So that swipe does not visually enable the input buttons when max length @@ -106,6 +103,15 @@ impl PassphraseKeyboard { self.update_input_btns_state(ctx); } + fn replace_button_content(&mut self, ctx: &mut EventCtx, page: usize) { + for (i, btn) in self.keys.iter_mut().enumerate() { + let text = KEYBOARD[page][i]; + let content = Self::key_content(text); + btn.mutate(ctx, |ctx, b| b.set_content(ctx, content)); + btn.request_complete_repaint(ctx); + } + } + /// Possibly changing the buttons' state after change of the input. fn after_edit(&mut self, ctx: &mut EventCtx) { self.update_back_btn_state(ctx); @@ -123,7 +129,7 @@ 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() { + for btn in self.keys.iter_mut() { btn.mutate(ctx, |ctx, b| { if self.input.inner().textbox.is_full() { b.disable(ctx); @@ -164,20 +170,18 @@ impl Component for PassphraseKeyboard { self.scrollbar .set_count_and_active_page(PAGE_COUNT, STARTING_PAGE); - // Place all the possible character buttons on all swipe-separated screens. - 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 { - // The grid has 3 columns, and we skip the first row. - key - } else { - // For the last key (the "0" position) we skip one cell. - key + 1 - }); - btn.place(area); - } + // Place all the character buttons. + for (key, btn) in &mut self.keys.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 { + // The grid has 3 columns, and we skip the first row. + key + } else { + // For the last key (the "0" position) we skip one cell. + key + 1 + }); + btn.place(area); } bounds @@ -229,7 +233,7 @@ impl Component for PassphraseKeyboard { // (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.iter_mut().enumerate() { if let Some(ButtonMsg::Clicked) = btn.event(ctx, event) { // Key button was clicked. If this button is pending, let's cycle the pending // character in textbox. If not, let's just append the first character. @@ -251,7 +255,7 @@ impl Component for PassphraseKeyboard { self.scrollbar.paint(); self.confirm.paint(); self.back.paint(); - for btn in &mut self.keys[self.scrollbar.active_page] { + for btn in &mut self.keys { btn.paint(); } if self.fade { @@ -266,7 +270,7 @@ impl Component for PassphraseKeyboard { self.scrollbar.bounds(sink); self.confirm.bounds(sink); self.back.bounds(sink); - for btn in &self.keys[self.scrollbar.active_page] { + for btn in &self.keys { btn.bounds(sink) } }