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>,
back: 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,
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)
}
}

Loading…
Cancel
Save