perf(core/rust): use less Button instances in PassphraseKeyboard

[no changelog]
pull/2742/head
Martin Milata 1 year ago
parent 0df81b18e3
commit 099d00de84

@ -24,7 +24,7 @@ pub struct PassphraseKeyboard {
input: Child<Input>, input: Child<Input>,
back: Child<Button<&'static str>>, back: Child<Button<&'static str>>,
confirm: Child<Button<&'static str>>, confirm: Child<Button<&'static str>>,
keys: [[Child<Button<&'static str>>; KEY_COUNT]; PAGE_COUNT], keys: [Child<Button<&'static str>>; KEY_COUNT],
scrollbar: ScrollBar, scrollbar: ScrollBar,
fade: bool, fade: bool,
} }
@ -59,16 +59,8 @@ impl PassphraseKeyboard {
.initially_enabled(false) .initially_enabled(false)
.with_long_press(theme::ERASE_HOLD_DURATION) .with_long_press(theme::ERASE_HOLD_DURATION)
.into_child(), .into_child(),
keys: KEYBOARD.map(|page| { keys: KEYBOARD[STARTING_PAGE]
page.map(|text| { .map(|text| Child::new(Button::new(Self::key_content(text)))),
if text == " " {
let icon = Icon::new(theme::ICON_SPACE);
Child::new(Button::with_icon(icon))
} else {
Child::new(Button::with_text(text))
}
})
}),
scrollbar: ScrollBar::horizontal(), scrollbar: ScrollBar::horizontal(),
fade: false, 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) { fn on_page_swipe(&mut self, ctx: &mut EventCtx, swipe: SwipeDirection) {
// Change the page number. // Change the page number.
let key_page = self.scrollbar.active_page; let key_page = self.scrollbar.active_page;
@ -95,10 +94,8 @@ 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 new buttons. // Update buttons.
for btn in &mut self.keys[key_page] { self.replace_button_content(ctx, key_page);
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 // 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); 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. /// 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_back_btn_state(ctx);
@ -123,7 +129,7 @@ impl PassphraseKeyboard {
/// When the input has reached max length, disable all the input buttons. /// When the input has reached max length, disable all the input buttons.
fn update_input_btns_state(&mut self, ctx: &mut EventCtx) { 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| { btn.mutate(ctx, |ctx, b| {
if self.input.inner().textbox.is_full() { if self.input.inner().textbox.is_full() {
b.disable(ctx); b.disable(ctx);
@ -164,20 +170,18 @@ impl Component for PassphraseKeyboard {
self.scrollbar self.scrollbar
.set_count_and_active_page(PAGE_COUNT, STARTING_PAGE); .set_count_and_active_page(PAGE_COUNT, STARTING_PAGE);
// Place all the possible character buttons on all swipe-separated screens. // Place all the character buttons.
for swipe_screen in &mut self.keys { for (key, btn) in &mut self.keys.iter_mut().enumerate() {
for (key, btn) in swipe_screen.iter_mut().enumerate() { // Assign the keys in each page to buttons on a 5x3 grid, starting
// Assign the keys in each page to buttons on a 5x3 grid, starting // from the second row.
// 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
key } else {
} else { // For the last key (the "0" position) we skip one cell.
// For the last key (the "0" position) we skip one cell. key + 1
key + 1 });
}); btn.place(area);
btn.place(area);
}
} }
bounds bounds
@ -229,7 +233,7 @@ impl Component for PassphraseKeyboard {
// (All input buttons should be disallowed in that case, this is just a safety // (All input buttons should be disallowed in that case, this is just a safety
// measure.) // measure.)
if !self.input.inner().textbox.is_full() { 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) { if let Some(ButtonMsg::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
// character in textbox. If not, let's just append the first character. // character in textbox. If not, let's just append the first character.
@ -251,7 +255,7 @@ impl Component for PassphraseKeyboard {
self.scrollbar.paint(); self.scrollbar.paint();
self.confirm.paint(); self.confirm.paint();
self.back.paint(); self.back.paint();
for btn in &mut self.keys[self.scrollbar.active_page] { for btn in &mut self.keys {
btn.paint(); btn.paint();
} }
if self.fade { if self.fade {
@ -266,7 +270,7 @@ impl Component for PassphraseKeyboard {
self.scrollbar.bounds(sink); self.scrollbar.bounds(sink);
self.confirm.bounds(sink); self.confirm.bounds(sink);
self.back.bounds(sink); self.back.bounds(sink);
for btn in &self.keys[self.scrollbar.active_page] { for btn in &self.keys {
btn.bounds(sink) btn.bounds(sink)
} }
} }

Loading…
Cancel
Save