refactor(core): optimize copyabilitity in swipe_detect

pull/3911/head
matejcik 1 month ago committed by matejcik
parent a67bc19bac
commit fbfb000d62

@ -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(mut self) -> Self {
self.vertical_pages = true;
self
}
}
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,
}
} }
}
pub fn with_vertical_pages(&self) -> Self { impl core::ops::IndexMut<SwipeDirection> for SwipeConfig {
let mut new = self.clone(); fn index_mut(&mut self, index: SwipeDirection) -> &mut Self::Output {
new.vertical_pages = true; match index {
new 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>,

@ -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;

Loading…
Cancel
Save