diff --git a/core/embed/rust/src/ui/component/paginated.rs b/core/embed/rust/src/ui/component/paginated.rs index e548c35095..7a4c5d8ae5 100644 --- a/core/embed/rust/src/ui/component/paginated.rs +++ b/core/embed/rust/src/ui/component/paginated.rs @@ -37,6 +37,20 @@ pub trait PaginateFull { fn pager(&self) -> Pager; /// Navigate to the given page. fn change_page(&mut self, active_page: u16); + + fn next_page(&mut self) { + let mut pager = self.pager(); + if pager.goto_next() { + self.change_page(pager.current()); + } + } + + fn prev_page(&mut self) { + let mut pager = self.pager(); + if pager.goto_prev() { + self.change_page(pager.current()); + } + } } impl Paginate for T { diff --git a/core/embed/rust/src/ui/flow/page.rs b/core/embed/rust/src/ui/flow/page.rs index 9776be49e7..e0f1099ade 100644 --- a/core/embed/rust/src/ui/flow/page.rs +++ b/core/embed/rust/src/ui/flow/page.rs @@ -12,17 +12,15 @@ pub struct SwipePage { inner: T, bounds: Rect, axis: Axis, - pager: Pager, limit: Option, } -impl SwipePage { +impl SwipePage { pub fn vertical(inner: T) -> Self { Self { inner, bounds: Rect::zero(), axis: Axis::Vertical, - pager: Pager::single_page(), limit: None, } } @@ -32,7 +30,6 @@ impl SwipePage { inner, bounds: Rect::zero(), axis: Axis::Horizontal, - pager: Pager::single_page(), limit: None, } } @@ -45,6 +42,12 @@ impl SwipePage { self.limit = limit; self } + + fn total_with_limit(&self) -> u16 { + let total = self.inner.pager().total(); + let limit = self.limit.unwrap_or(total); + total.min(limit) + } } impl Component for SwipePage { @@ -52,36 +55,28 @@ impl Component for SwipePage { fn place(&mut self, bounds: Rect) -> Rect { self.bounds = self.inner.place(bounds); - self.pager = self.inner.pager(); - if let Some(limit) = self.limit { - self.pager = self.pager.with_limit(limit); - } self.bounds } fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option { - ctx.set_page_count(self.pager.total() as usize); + ctx.set_page_count(self.total_with_limit() as usize); if let Event::Swipe(SwipeEvent::End(direction)) = event { match (self.axis, direction) { (Axis::Vertical, Direction::Up) => { - self.pager.goto_next(); - self.inner.change_page(self.pager.current()); + self.inner.next_page(); ctx.request_paint(); } (Axis::Vertical, Direction::Down) => { - self.pager.goto_prev(); - self.inner.change_page(self.pager.current()); + self.inner.prev_page(); ctx.request_paint(); } (Axis::Horizontal, Direction::Left) => { - self.pager.goto_next(); - self.inner.change_page(self.pager.current()); + self.inner.next_page(); ctx.request_paint(); } (Axis::Horizontal, Direction::Right) => { - self.pager.goto_prev(); - self.inner.change_page(self.pager.current()); + self.inner.prev_page(); ctx.request_paint(); } _ => {} @@ -98,12 +93,12 @@ impl Component for SwipePage { impl PaginateFull for SwipePage { fn pager(&self) -> Pager { - self.pager + self.inner.pager().with_limit(self.total_with_limit()) } fn change_page(&mut self, to_page: u16) { - self.pager.set_current(to_page); - self.inner.change_page(self.pager.current()); + let to_page = to_page.min(self.total_with_limit()); + self.inner.change_page(to_page); } }