1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-09 15:00:58 +00:00

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
This commit is contained in:
matejcik 2024-09-10 15:59:32 +02:00 committed by matejcik
parent c0bb55258b
commit f7ce99e7d7
2 changed files with 35 additions and 9 deletions

View File

@ -381,6 +381,19 @@ pub enum Event {
Swipe(SwipeEvent), 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)] #[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "debug", derive(ufmt::derive::uDebug))] #[cfg_attr(feature = "debug", derive(ufmt::derive::uDebug))]
pub struct TimerToken(u32); pub struct TimerToken(u32);

View File

@ -1,7 +1,10 @@
use crate::{ use crate::{
time::{Duration, Stopwatch}, time::{Duration, Stopwatch},
ui::{ ui::{
component::{base::AttachType, Component, Event, EventCtx}, component::{
base::{AttachType, EventPropagation},
Component, Event, EventCtx,
},
constant::screen, constant::screen,
display::Color, display::Color,
event::SwipeEvent, event::SwipeEvent,
@ -98,7 +101,12 @@ impl SwipeAttachAnimation {
self.timer = Stopwatch::new_stopped(); 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 let Event::Attach(attach_type) = event {
if self.show_attach_anim && animate { if self.show_attach_anim && animate {
self.attach_type = Some(attach_type); self.attach_type = Some(attach_type);
@ -120,8 +128,8 @@ impl SwipeAttachAnimation {
} }
} }
match event { match event {
Event::Touch(_) => !self.is_active(), Event::Touch(_) if self.is_active() => EventPropagation::Stop,
_ => true, _ => EventPropagation::Continue,
} }
} }
} }
@ -184,8 +192,13 @@ impl SwipeContext {
(offset, clip, mask) (offset, clip, mask)
} }
fn process_event(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> bool { fn process_event(
let inner_event = self.attach_animation.lazy_start(ctx, event, animate); &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 { if let Event::Attach(_) = event {
self.progress = 0; self.progress = 0;
@ -204,7 +217,7 @@ impl SwipeContext {
ctx.request_paint(); ctx.request_paint();
} }
inner_event propagate
} }
} }
@ -233,9 +246,9 @@ impl<T: Component> SwipeContent<T> {
} }
fn process_event(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> Option<T::Msg> { fn process_event(&mut self, ctx: &mut EventCtx, event: Event, animate: bool) -> Option<T::Msg> {
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) self.inner.event(ctx, event)
} else { } else {
None None