From 10cdb1f70084ae30c327a0f5a9ce093387eee47c Mon Sep 17 00:00:00 2001 From: Bryan Woods Date: Sat, 16 Nov 2024 18:13:43 -0800 Subject: [PATCH] Adding `on_tap` --- composable-views/src/gesture/mod.rs | 2 ++ composable-views/src/gesture/tap.rs | 14 ++++++------- composable-views/src/lib.rs | 22 ++++++++++++++++++++- composable-views/src/modifiers/mod.rs | 1 - composable-views/src/modifiers/offset.rs | 25 ------------------------ 5 files changed, 30 insertions(+), 34 deletions(-) delete mode 100644 composable-views/src/modifiers/offset.rs diff --git a/composable-views/src/gesture/mod.rs b/composable-views/src/gesture/mod.rs index b5c2cf9f..82ce36bf 100644 --- a/composable-views/src/gesture/mod.rs +++ b/composable-views/src/gesture/mod.rs @@ -7,6 +7,8 @@ pub use recognizer::*; mod tap; +pub use tap::TapGesture; + #[derive(Copy, Clone, Eq, PartialEq)] pub struct Id(pub(crate) std::num::NonZeroU128); diff --git a/composable-views/src/gesture/tap.rs b/composable-views/src/gesture/tap.rs index 95a5dea7..64576c56 100644 --- a/composable-views/src/gesture/tap.rs +++ b/composable-views/src/gesture/tap.rs @@ -2,18 +2,18 @@ use crate::gesture::{self, Id}; use crate::{Bounds, Event, Output, Point, Size, View}; use composable::Effects; -pub struct TapGesture { - id: Id, - view: V, - action: A, - send: E, +pub struct TapGesture { + pub(crate) id: Id, + pub(crate) view: V, + pub(crate) action: A, + pub(crate) send: E, } -impl View for TapGesture +impl View for TapGesture where V: View, - E: Effects, A: Clone, + E: Effects, { #[inline(always)] fn size(&self) -> Size { diff --git a/composable-views/src/lib.rs b/composable-views/src/lib.rs index c96dafe0..1be10e71 100644 --- a/composable-views/src/lib.rs +++ b/composable-views/src/lib.rs @@ -2,6 +2,7 @@ use std::ops::Deref; pub use lyon::math::{Box2D as Bounds, Point, Size, Transform}; +pub use gesture::{Id, TapGesture}; pub use layout::{Layout, Spacer}; pub use modifiers::fixed::{Fixed, FixedHeight, FixedWidth}; pub use modifiers::padding::Padding; @@ -11,7 +12,7 @@ pub use shapes::{Path, Shape}; #[doc(inline)] pub use text::Text; -use composable::{From, TryInto}; +use composable::{Effects, From, TryInto}; /// Alias for `euclid::default::SideOffsets2D` pub type Offsets = lyon::geom::euclid::default::SideOffsets2D; @@ -137,6 +138,25 @@ pub trait View: Sized { fn across(self) -> impl View { self } + + + fn on_tap( + self, + id: Id, + action: A, + send: E, + ) -> TapGesture + where + A: Clone, + E: Effects, + { + TapGesture { + id, + view: self, + action, + send, + } + } } impl View for Box { diff --git a/composable-views/src/modifiers/mod.rs b/composable-views/src/modifiers/mod.rs index a3d381d8..bb7e3673 100644 --- a/composable-views/src/modifiers/mod.rs +++ b/composable-views/src/modifiers/mod.rs @@ -1,3 +1,2 @@ pub mod fixed; -pub mod offset; pub mod padding; diff --git a/composable-views/src/modifiers/offset.rs b/composable-views/src/modifiers/offset.rs deleted file mode 100644 index 7c09fad5..00000000 --- a/composable-views/src/modifiers/offset.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::{Bounds, Event, Output, Point, Size, View}; - -pub struct Offset { - pub(crate) view: V, - pub(crate) offsets: Point, -} - -impl View for Offset { - #[inline(always)] - fn size(&self) -> Size { - self.view.size() - } - - #[inline] - fn event(&self, event: Event, offset: Point, mut bounds: Bounds) { - bounds.min += self.offsets.to_vector(); - self.view.event(event, offset, bounds) - } - - #[inline] - fn draw(&self, mut bounds: Bounds, onto: &mut impl Output) { - bounds.min += self.offsets.to_vector(); - self.view.draw(bounds, onto) - } -}