From f7ce99e7d794c065799220e7900935aa1ec9fd80 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 10 Sep 2024 15:59:32 +0200 Subject: [PATCH] refactor(core/rust): EventPropagation enum to indicate in a better way that some event should be propagated further there are probably more places where this would be appropriate --- core/embed/rust/src/ui/component/base.rs | 13 ++++++++ .../model_mercury/component/swipe_content.rs | 31 +++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/core/embed/rust/src/ui/component/base.rs b/core/embed/rust/src/ui/component/base.rs index 0a37b257ea..a6f87dcef0 100644 --- a/core/embed/rust/src/ui/component/base.rs +++ b/core/embed/rust/src/ui/component/base.rs @@ -381,6 +381,19 @@ pub enum Event { Swipe(SwipeEvent), } +/// Result of an event processor. +/// +/// Indicates whether to continue processing the event, propagate it further, +/// or stop processing it. +#[derive(Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "debug", derive(ufmt::derive::uDebug))] +pub enum EventPropagation { + /// Event was not consumed by the component, propagate it further. + Continue, + /// Event was consumed by the component, do not propagate it further. + Stop, +} + #[derive(Copy, Clone, PartialEq, Eq)] #[cfg_attr(feature = "debug", derive(ufmt::derive::uDebug))] pub struct TimerToken(u32); diff --git a/core/embed/rust/src/ui/model_mercury/component/swipe_content.rs b/core/embed/rust/src/ui/model_mercury/component/swipe_content.rs index 5d8e3bee99..9ac2928845 100644 --- a/core/embed/rust/src/ui/model_mercury/component/swipe_content.rs +++ b/core/embed/rust/src/ui/model_mercury/component/swipe_content.rs @@ -1,7 +1,10 @@ use crate::{ time::{Duration, Stopwatch}, ui::{ - component::{base::AttachType, Component, Event, EventCtx}, + component::{ + base::{AttachType, EventPropagation}, + Component, Event, EventCtx, + }, constant::screen, display::Color, event::SwipeEvent, @@ -98,7 +101,12 @@ impl SwipeAttachAnimation { self.timer = Stopwatch::new_stopped(); } - pub fn lazy_start(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> bool { + pub fn lazy_start( + &mut self, + ctx: &mut EventCtx, + event: Event, + animate: bool, + ) -> EventPropagation { if let Event::Attach(attach_type) = event { if self.show_attach_anim && animate { self.attach_type = Some(attach_type); @@ -120,8 +128,8 @@ impl SwipeAttachAnimation { } } match event { - Event::Touch(_) => !self.is_active(), - _ => true, + Event::Touch(_) if self.is_active() => EventPropagation::Stop, + _ => EventPropagation::Continue, } } } @@ -184,8 +192,13 @@ impl SwipeContext { (offset, clip, mask) } - fn process_event(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> bool { - let inner_event = self.attach_animation.lazy_start(ctx, event, animate); + fn process_event( + &mut self, + ctx: &mut EventCtx, + event: Event, + animate: bool, + ) -> EventPropagation { + let propagate = self.attach_animation.lazy_start(ctx, event, animate); if let Event::Attach(_) = event { self.progress = 0; @@ -204,7 +217,7 @@ impl SwipeContext { ctx.request_paint(); } - inner_event + propagate } } @@ -233,9 +246,9 @@ impl SwipeContent { } fn process_event(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> Option { - let inner_event = self.swipe_context.process_event(ctx, event, animate); + let propagate = self.swipe_context.process_event(ctx, event, animate); - if inner_event { + if matches!(propagate, EventPropagation::Continue) { self.inner.event(ctx, event) } else { None