1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

fix(core/ui): set page_count through rust ButtonRequest

This commit is contained in:
Martin Milata 2024-05-21 23:47:07 +02:00
parent 53609a85fd
commit 198693259d
6 changed files with 84 additions and 5 deletions

View File

@ -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<F>(self, func: F) -> MsgMap<Self, F>;
fn with_pages<F>(self, func: F) -> PageMap<Self, F>;
fn into_child(self) -> Child<Self>;
fn request_complete_repaint(&mut self, ctx: &mut EventCtx);
}
@ -427,6 +428,10 @@ where
MsgMap::new(self, func)
}
fn with_pages<F>(self, func: F) -> PageMap<Self, F> {
PageMap::new(self, func)
}
fn into_child(self) -> Child<Self> {
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<usize> {
self.page_count
}

View File

@ -13,7 +13,7 @@ pub struct OneButtonRequest<T> {
}
impl<T> OneButtonRequest<T> {
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,

View File

@ -64,3 +64,69 @@ where
self.inner.swipe_finished()
}
}
pub struct PageMap<T, F> {
inner: T,
func: F,
}
impl<T, F> PageMap<T, F> {
pub fn new(inner: T, func: F) -> Self {
Self { inner, func }
}
}
impl<T, F> Component for PageMap<T, F>
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<Self::Msg> {
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<T, F> crate::trace::Trace for PageMap<T, F>
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<T, F> crate::ui::flow::Swipable for PageMap<T, F>
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()
}
}

View File

@ -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;

View File

@ -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(

View File

@ -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())