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

refactor(core): optimize copyabilitity in swipe_detect

This commit is contained in:
matejcik 2024-06-04 16:06:08 +02:00 committed by matejcik
parent a67bc19bac
commit fbfb000d62
2 changed files with 38 additions and 33 deletions

View File

@ -9,7 +9,7 @@ use crate::{
}, },
}; };
#[derive(Clone)] #[derive(Copy, Clone)]
pub struct SwipeSettings { pub struct SwipeSettings {
pub duration: Duration, pub duration: Duration,
} }
@ -32,7 +32,7 @@ impl SwipeSettings {
} }
} }
#[derive(Clone, Default)] #[derive(Copy, Clone, Default)]
pub struct SwipeConfig { pub struct SwipeConfig {
pub horizontal_pages: bool, pub horizontal_pages: bool,
pub vertical_pages: bool, pub vertical_pages: bool,
@ -54,33 +54,17 @@ impl SwipeConfig {
} }
} }
pub fn with_swipe(&self, dir: SwipeDirection, settings: SwipeSettings) -> Self { pub fn with_swipe(mut self, dir: SwipeDirection, settings: SwipeSettings) -> Self {
let mut new = self.clone(); self[dir] = Some(settings);
match dir { self
SwipeDirection::Up => new.up = Some(settings),
SwipeDirection::Down => new.down = Some(settings),
SwipeDirection::Left => new.left = Some(settings),
SwipeDirection::Right => new.right = Some(settings),
}
new
} }
pub fn is_allowed(&self, dir: SwipeDirection) -> bool { pub fn is_allowed(&self, dir: SwipeDirection) -> bool {
match dir { self[dir].is_some()
SwipeDirection::Up => self.up.is_some(),
SwipeDirection::Down => self.down.is_some(),
SwipeDirection::Left => self.left.is_some(),
SwipeDirection::Right => self.right.is_some(),
}
} }
pub fn duration(&self, dir: SwipeDirection) -> Option<Duration> { pub fn duration(&self, dir: SwipeDirection) -> Option<Duration> {
match dir { self[dir].as_ref().map(|s| s.duration)
SwipeDirection::Up => self.up.as_ref().map(|s| s.duration),
SwipeDirection::Down => self.down.as_ref().map(|s| s.duration),
SwipeDirection::Left => self.left.as_ref().map(|s| s.duration),
SwipeDirection::Right => self.right.as_ref().map(|s| s.duration),
}
} }
pub fn has_horizontal_pages(&self) -> bool { pub fn has_horizontal_pages(&self) -> bool {
self.horizontal_pages self.horizontal_pages
@ -90,16 +74,38 @@ impl SwipeConfig {
self.vertical_pages self.vertical_pages
} }
pub fn with_horizontal_pages(&self) -> Self { pub fn with_horizontal_pages(mut self) -> Self {
let mut new = self.clone(); self.horizontal_pages = true;
new.horizontal_pages = true; self
new
} }
pub fn with_vertical_pages(&self) -> Self { pub fn with_vertical_pages(mut self) -> Self {
let mut new = self.clone(); self.vertical_pages = true;
new.vertical_pages = true; self
new }
}
impl core::ops::Index<SwipeDirection> for SwipeConfig {
type Output = Option<SwipeSettings>;
fn index(&self, index: SwipeDirection) -> &Self::Output {
match index {
SwipeDirection::Up => &self.up,
SwipeDirection::Down => &self.down,
SwipeDirection::Left => &self.left,
SwipeDirection::Right => &self.right,
}
}
}
impl core::ops::IndexMut<SwipeDirection> for SwipeConfig {
fn index_mut(&mut self, index: SwipeDirection) -> &mut Self::Output {
match index {
SwipeDirection::Up => &mut self.up,
SwipeDirection::Down => &mut self.down,
SwipeDirection::Left => &mut self.left,
SwipeDirection::Right => &mut self.right,
}
} }
} }
@ -109,7 +115,6 @@ pub enum SwipeDetectMsg {
Trigger(SwipeDirection), Trigger(SwipeDirection),
} }
#[derive(Clone)]
pub struct SwipeDetect { pub struct SwipeDetect {
origin: Option<Point>, origin: Option<Point>,
locked: Option<SwipeDirection>, locked: Option<SwipeDirection>,

View File

@ -131,7 +131,7 @@ impl<Q: FlowState, S: FlowStore> Component for SwipeFlow<Q, S> {
config = config.with_swipe(SwipeDirection::Left, SwipeSettings::default()) config = config.with_swipe(SwipeDirection::Left, SwipeSettings::default())
} }
match self.swipe.event(ctx, event, config.clone()) { match self.swipe.event(ctx, event, config) {
Some(SwipeDetectMsg::Trigger(dir)) => { Some(SwipeDetectMsg::Trigger(dir)) => {
if let Some(override_decision) = self.decision_override.take() { if let Some(override_decision) = self.decision_override.take() {
decision = override_decision; decision = override_decision;