From 198693259dff3e3f826f624e2aa8d993c966fc11 Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Tue, 21 May 2024 23:47:07 +0200 Subject: [PATCH] fix(core/ui): set page_count through rust ButtonRequest --- core/embed/rust/src/ui/component/base.rs | 11 +++- .../rust/src/ui/component/button_request.rs | 2 +- core/embed/rust/src/ui/component/map.rs | 66 +++++++++++++++++++ core/embed/rust/src/ui/component/mod.rs | 2 +- .../ui/model_mercury/flow/confirm_summary.rs | 4 +- .../src/ui/model_mercury/flow/get_address.rs | 4 +- 6 files changed, 84 insertions(+), 5 deletions(-) diff --git a/core/embed/rust/src/ui/component/base.rs b/core/embed/rust/src/ui/component/base.rs index 91a7d53df..375df7fb9 100644 --- a/core/embed/rust/src/ui/component/base.rs +++ b/core/embed/rust/src/ui/component/base.rs @@ -7,7 +7,7 @@ use crate::{ time::Duration, ui::{ button_request::{ButtonRequest, ButtonRequestCode}, - component::{maybe::PaintOverlapping, MsgMap}, + component::{maybe::PaintOverlapping, MsgMap, PageMap}, display::{self, Color}, geometry::{Offset, Rect}, shape::Renderer, @@ -415,6 +415,7 @@ where pub trait ComponentExt: Sized { fn map(self, func: F) -> MsgMap; + fn with_pages(self, func: F) -> PageMap; fn into_child(self) -> Child; fn request_complete_repaint(&mut self, ctx: &mut EventCtx); } @@ -427,6 +428,10 @@ where MsgMap::new(self, func) } + fn with_pages(self, func: F) -> PageMap { + PageMap::new(self, func) + } + fn into_child(self) -> Child { Child::new(self) } @@ -569,6 +574,10 @@ impl EventCtx { self.page_count = Some(count); } + pub fn map_page_count(&mut self, func: impl Fn(usize) -> usize) { + self.page_count = Some(func(self.page_count.unwrap_or(1))); + } + pub fn page_count(&self) -> Option { self.page_count } diff --git a/core/embed/rust/src/ui/component/button_request.rs b/core/embed/rust/src/ui/component/button_request.rs index 006a15879..a5fb1c592 100644 --- a/core/embed/rust/src/ui/component/button_request.rs +++ b/core/embed/rust/src/ui/component/button_request.rs @@ -13,7 +13,7 @@ pub struct OneButtonRequest { } impl OneButtonRequest { - pub fn new(button_request: ButtonRequest, inner: T) -> Self { + pub const fn new(button_request: ButtonRequest, inner: T) -> Self { Self { button_request: Some(button_request), inner, diff --git a/core/embed/rust/src/ui/component/map.rs b/core/embed/rust/src/ui/component/map.rs index 7499b9246..46270abf8 100644 --- a/core/embed/rust/src/ui/component/map.rs +++ b/core/embed/rust/src/ui/component/map.rs @@ -64,3 +64,69 @@ where self.inner.swipe_finished() } } + +pub struct PageMap { + inner: T, + func: F, +} + +impl PageMap { + pub fn new(inner: T, func: F) -> Self { + Self { inner, func } + } +} + +impl Component for PageMap +where + T: Component, + F: Fn(usize) -> usize, +{ + type Msg = T::Msg; + + fn place(&mut self, bounds: Rect) -> Rect { + self.inner.place(bounds) + } + + fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option { + let res = self.inner.event(ctx, event); + ctx.map_page_count(&self.func); + res + } + + fn paint(&mut self) { + self.inner.paint() + } + + fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { + self.inner.render(target); + } + + #[cfg(feature = "ui_bounds")] + fn bounds(&self, sink: &mut dyn FnMut(Rect)) { + self.inner.bounds(sink); + } +} + +#[cfg(feature = "ui_debug")] +impl crate::trace::Trace for PageMap +where + T: Component + crate::trace::Trace, +{ + fn trace(&self, t: &mut dyn crate::trace::Tracer) { + self.inner.trace(t) + } +} + +#[cfg(all(feature = "micropython", feature = "touch", feature = "new_rendering"))] +impl crate::ui::flow::Swipable for PageMap +where + T: Component + crate::ui::flow::Swipable, +{ + fn swipe_start(&mut self, ctx: &mut EventCtx, direction: super::SwipeDirection) -> bool { + self.inner.swipe_start(ctx, direction) + } + + fn swipe_finished(&self) -> bool { + self.inner.swipe_finished() + } +} diff --git a/core/embed/rust/src/ui/component/mod.rs b/core/embed/rust/src/ui/component/mod.rs index 3d891580f..53f3fb908 100644 --- a/core/embed/rust/src/ui/component/mod.rs +++ b/core/embed/rust/src/ui/component/mod.rs @@ -30,7 +30,7 @@ pub use empty::Empty; #[cfg(all(feature = "jpeg", feature = "micropython"))] pub use jpeg::Jpeg; pub use label::Label; -pub use map::MsgMap; +pub use map::{MsgMap, PageMap}; pub use marquee::Marquee; pub use maybe::Maybe; pub use pad::Pad; diff --git a/core/embed/rust/src/ui/model_mercury/flow/confirm_summary.rs b/core/embed/rust/src/ui/model_mercury/flow/confirm_summary.rs index 83f501298..8466b8a37 100644 --- a/core/embed/rust/src/ui/model_mercury/flow/confirm_summary.rs +++ b/core/embed/rust/src/ui/model_mercury/flow/confirm_summary.rs @@ -105,7 +105,9 @@ impl ConfirmSummary { } let content_summary = summary .into_layout()? - .one_button_request(ButtonRequest::from_tstring(br_code, br_type)); + .one_button_request(ButtonRequest::from_tstring(br_code, br_type)) + // Summary(1) + Hold(1) + .with_pages(|summary_pages| summary_pages + 1); // Hold to confirm let content_hold = Frame::left_aligned( diff --git a/core/embed/rust/src/ui/model_mercury/flow/get_address.rs b/core/embed/rust/src/ui/model_mercury/flow/get_address.rs index 60480f8c7..7e3ab6181 100644 --- a/core/embed/rust/src/ui/model_mercury/flow/get_address.rs +++ b/core/embed/rust/src/ui/model_mercury/flow/get_address.rs @@ -171,7 +171,9 @@ impl GetAddress { .with_menu_button() .with_footer(TR::instructions__swipe_up.into(), None) .map(|msg| matches!(msg, FrameMsg::Button(_)).then_some(FlowMsg::Info)) - .one_button_request(ButtonRequest::from_tstring(br_code, br_type)); + .one_button_request(ButtonRequest::from_tstring(br_code, br_type)) + // Count tap-to-confirm screen towards page count + .with_pages(|address_pages| address_pages + 1); // Tap let content_tap = Frame::left_aligned(title, PromptScreen::new_tap_to_confirm())