From c0bb55258be2bb36b85cf0189f3a1e62b954ab8e Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 6 Sep 2024 14:00:39 +0200 Subject: [PATCH] refactor(core/rust): use SwipeEvent in place of SwipeDetectMsg --- core/embed/rust/src/ui/component/mod.rs | 2 +- .../rust/src/ui/component/swipe_detect.rs | 37 ++++++++----------- core/embed/rust/src/ui/flow/swipe.rs | 9 ++--- .../component/swipe_up_screen.rs | 11 ++---- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/core/embed/rust/src/ui/component/mod.rs b/core/embed/rust/src/ui/component/mod.rs index 0d5e68d9c4..32cea86d3e 100644 --- a/core/embed/rust/src/ui/component/mod.rs +++ b/core/embed/rust/src/ui/component/mod.rs @@ -46,7 +46,7 @@ pub use qr_code::Qr; #[cfg(feature = "touch")] pub use swipe::Swipe; #[cfg(feature = "touch")] -pub use swipe_detect::{SwipeDetect, SwipeDetectMsg}; +pub use swipe_detect::SwipeDetect; pub use text::{ formatted::FormattedText, layout::{LineBreaking, PageBreaking, TextLayout}, diff --git a/core/embed/rust/src/ui/component/swipe_detect.rs b/core/embed/rust/src/ui/component/swipe_detect.rs index 482e832583..5ea202e789 100644 --- a/core/embed/rust/src/ui/component/swipe_detect.rs +++ b/core/embed/rust/src/ui/component/swipe_detect.rs @@ -4,7 +4,7 @@ use crate::{ animation::Animation, component::{Event, EventCtx}, constant::screen, - event::TouchEvent, + event::{SwipeEvent, TouchEvent}, geometry::{Axis, Direction, Offset, Point}, util::animation_disabled, }, @@ -168,18 +168,11 @@ impl core::ops::IndexMut for SwipeConfig { } } -#[derive(Copy, Clone, Eq, PartialEq)] -pub enum SwipeDetectMsg { - Start(Direction), - Move(Direction, u16), - Trigger(Direction), -} - pub struct SwipeDetect { origin: Option, locked: Option, final_animation: Option>, - moved: u16, + moved: i16, } impl SwipeDetect { @@ -237,20 +230,20 @@ impl SwipeDetect { } } - fn progress(&self, val: u16) -> u16 { - ((val as f32 / Self::DISTANCE as f32) * Self::PROGRESS_MAX as f32) as u16 + fn progress(&self, val: u16) -> i16 { + ((val as f32 / Self::DISTANCE as f32) * Self::PROGRESS_MAX as f32) as i16 } - fn eval_anim_frame(&mut self, ctx: &mut EventCtx) -> Option { + fn eval_anim_frame(&mut self, ctx: &mut EventCtx) -> Option { if let Some(locked) = self.locked { let mut finish = false; let res = if let Some(animation) = &self.final_animation { if animation.finished(Instant::now()) { finish = true; if animation.to != 0 { - Some(SwipeDetectMsg::Trigger(locked)) + Some(SwipeEvent::End(locked)) } else { - Some(SwipeDetectMsg::Move(locked, 0)) + Some(SwipeEvent::Move(locked, 0)) } } else { ctx.request_anim_frame(); @@ -258,9 +251,9 @@ impl SwipeDetect { if animation_disabled() { None } else { - Some(SwipeDetectMsg::Move( + Some(SwipeEvent::Move( locked, - animation.value(Instant::now()).max(0) as u16, + animation.value(Instant::now()).max(0), )) } } @@ -310,7 +303,7 @@ impl SwipeDetect { ctx: &mut EventCtx, event: Event, config: SwipeConfig, - ) -> Option { + ) -> Option { match (event, self.origin) { (Event::Touch(TouchEvent::TouchStart(pos)), _) => { if self.final_animation.is_none() { @@ -330,7 +323,7 @@ impl SwipeDetect { Some(locked) => { // advance in locked direction only let moved = config.progress(locked, ofs, self.min_lock(locked)); - Some(SwipeDetectMsg::Move(locked, self.progress(moved))) + Some(SwipeEvent::Move(locked, self.progress(moved))) } None => { let mut res = None; @@ -338,7 +331,7 @@ impl SwipeDetect { let progress = config.progress(dir, ofs, self.min_lock(dir)); if progress > 0 && self.is_lockable(dir) { self.locked = Some(dir); - res = Some(SwipeDetectMsg::Start(dir)); + res = Some(SwipeEvent::Start(dir)); break; } } @@ -346,7 +339,7 @@ impl SwipeDetect { } }; - if let Some(SwipeDetectMsg::Move(_, progress)) = res { + if let Some(SwipeEvent::Move(_, progress)) = res { self.moved = progress; } @@ -400,7 +393,7 @@ impl SwipeDetect { let duration = ((duration.to_millis() as f32 * ratio) as u32).max(0); self.final_animation = Some(Animation::new( - self.moved as i16, + self.moved, final_value, Duration::from_millis(duration), Instant::now(), @@ -410,7 +403,7 @@ impl SwipeDetect { self.final_animation = None; self.moved = 0; self.locked = None; - return Some(SwipeDetectMsg::Trigger(locked)); + return Some(SwipeEvent::End(locked)); } return None; } else { diff --git a/core/embed/rust/src/ui/flow/swipe.rs b/core/embed/rust/src/ui/flow/swipe.rs index 4fe5d84279..7113f9ca33 100644 --- a/core/embed/rust/src/ui/flow/swipe.rs +++ b/core/embed/rust/src/ui/flow/swipe.rs @@ -8,7 +8,7 @@ use crate::{ ui::{ component::{ base::AttachType::{self, Swipe}, - Component, Event, EventCtx, FlowMsg, SwipeDetect, SwipeDetectMsg, + Component, Event, EventCtx, FlowMsg, SwipeDetect, }, display::Color, event::{SwipeEvent, TouchEvent}, @@ -202,7 +202,7 @@ impl SwipeFlow { self.internal_pages = page.get_internal_page_count() as u16; match self.swipe.event(ctx, event, config) { - Some(SwipeDetectMsg::Trigger(dir)) => { + Some(SwipeEvent::End(dir)) => { if let Some(override_decision) = self.decision_override.take() { decision = override_decision; } else { @@ -220,10 +220,7 @@ impl SwipeFlow { } Event::Swipe(SwipeEvent::End(dir)) } - Some(SwipeDetectMsg::Move(dir, progress)) => { - Event::Swipe(SwipeEvent::Move(dir, progress as i16)) - } - Some(SwipeDetectMsg::Start(dir)) => Event::Swipe(SwipeEvent::Start(dir)), + Some(e) => Event::Swipe(e), None => event, } } else { diff --git a/core/embed/rust/src/ui/model_mercury/component/swipe_up_screen.rs b/core/embed/rust/src/ui/model_mercury/component/swipe_up_screen.rs index 7c0100d45d..9c3d81fce4 100644 --- a/core/embed/rust/src/ui/model_mercury/component/swipe_up_screen.rs +++ b/core/embed/rust/src/ui/model_mercury/component/swipe_up_screen.rs @@ -1,6 +1,6 @@ use crate::ui::{ - component::{base::AttachType, Component, Event, EventCtx, SwipeDetect, SwipeDetectMsg}, - event::{SwipeEvent, TouchEvent}, + component::{base::AttachType, Component, Event, EventCtx, SwipeDetect}, + event::SwipeEvent, flow::Swipable, geometry::Rect, shape::Renderer, @@ -49,14 +49,11 @@ impl Component for SwipeUpScreen { .swipe .event(ctx, event, self.content.get_swipe_config()) { - Some(SwipeDetectMsg::Trigger(dir)) => { + Some(SwipeEvent::End(dir)) => { ctx.set_transition_out(AttachType::Swipe(dir)); return Some(SwipeUpScreenMsg::Swiped); } - Some(SwipeDetectMsg::Move(dir, progress)) => { - Event::Swipe(SwipeEvent::Move(dir, progress as i16)) - } - Some(SwipeDetectMsg::Start(dir)) => Event::Swipe(SwipeEvent::Start(dir)), + Some(e) => Event::Swipe(e), _ => event, };