From 3ec720aa5a131ad0920ffb90e2a3c49a668d9744 Mon Sep 17 00:00:00 2001 From: ritschwumm Date: Sat, 9 Sep 2023 22:29:16 +0200 Subject: [PATCH 01/16] fix typo? just guessing, but `x` is not mentioned elsewhere, and i didn't see how the hslider would know about `value` --- docs/binding.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/binding.md b/docs/binding.md index 57dd8198..3d8c37db 100644 --- a/docs/binding.md +++ b/docs/binding.md @@ -15,7 +15,7 @@ To create a binding for a member of a struct, use `make_lens!` and `bind` Suppos struct MyState { value: f32, } -make_lens!(MyLens, MyState, f32, x); +make_lens!(MyLens, MyState, f32, value); ``` then we can use `bind` to create a control for `value`: From 8bc74be098a46535c3e96a61250c658eeda86a17 Mon Sep 17 00:00:00 2001 From: Aleks Rutins Date: Fri, 6 Oct 2023 09:24:36 -0400 Subject: [PATCH 02/16] Fix colon & add more keys --- src/winit_event_loop.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/winit_event_loop.rs b/src/winit_event_loop.rs index 84615a14..2198f8e6 100644 --- a/src/winit_event_loop.rs +++ b/src/winit_event_loop.rs @@ -458,11 +458,23 @@ pub fn rui(view: impl View) { Some(Key::Character(if cx.key_mods.shift { 'Z' } else { 'z' })) } VirtualKeyCode::Semicolon => Some(Key::Character(';')), - VirtualKeyCode::Colon => Some(Key::Character(';')), + VirtualKeyCode::Colon => Some(Key::Character(':')), VirtualKeyCode::Caret => Some(Key::Character('^')), VirtualKeyCode::Asterisk => Some(Key::Character('*')), VirtualKeyCode::Period => Some(Key::Character('.')), VirtualKeyCode::Comma => Some(Key::Character(',')), + VirtualKeyCode::Equals | VirtualKeyCode::NumpadEquals => { + Some(Key::Character('=')) + } + VirtualKeyCode::Plus | VirtualKeyCode::NumpadAdd => { + Some(Key::Character('+')) + } + VirtualKeyCode::Minus | VirtualKeyCode::NumpadSubtract => { + Some(Key::Character(if cx.key_mods.shift { '_' } else { '-' })) + } + VirtualKeyCode::Slash | VirtualKeyCode::NumpadDivide => { + Some(Key::Character(if cx.key_mods.shift { '?' } else { '/' })) + } VirtualKeyCode::Return => Some(Key::Enter), VirtualKeyCode::Tab => Some(Key::Tab), VirtualKeyCode::Space => Some(Key::Space), From 77f487906f207b64bbc468cbd09f7625edd2a57a Mon Sep 17 00:00:00 2001 From: Aleks Rutins Date: Fri, 6 Oct 2023 09:27:20 -0400 Subject: [PATCH 03/16] Actually fix colon --- src/winit_event_loop.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/winit_event_loop.rs b/src/winit_event_loop.rs index 2198f8e6..60e93612 100644 --- a/src/winit_event_loop.rs +++ b/src/winit_event_loop.rs @@ -457,7 +457,9 @@ pub fn rui(view: impl View) { VirtualKeyCode::Z => { Some(Key::Character(if cx.key_mods.shift { 'Z' } else { 'z' })) } - VirtualKeyCode::Semicolon => Some(Key::Character(';')), + VirtualKeyCode::Semicolon => { + Some(Key::Character(if cx.key_mods.shift { ':' } else { ';' })) + } VirtualKeyCode::Colon => Some(Key::Character(':')), VirtualKeyCode::Caret => Some(Key::Character('^')), VirtualKeyCode::Asterisk => Some(Key::Character('*')), From 3a4969744531df098b4968f52c09730a164b4b99 Mon Sep 17 00:00:00 2001 From: Aleks Rutins Date: Fri, 6 Oct 2023 09:43:19 -0400 Subject: [PATCH 04/16] Even more symbols! --- src/winit_event_loop.rs | 51 +++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/winit_event_loop.rs b/src/winit_event_loop.rs index 60e93612..7fb358c6 100644 --- a/src/winit_event_loop.rs +++ b/src/winit_event_loop.rs @@ -369,16 +369,36 @@ pub fn rui(view: impl View) { if let Some(code) = input.virtual_keycode { let key = match code { // VirtualKeyCode::Character(c) => Some(Key::Character(c)), - VirtualKeyCode::Key1 => Some(Key::Character('1')), - VirtualKeyCode::Key2 => Some(Key::Character('2')), - VirtualKeyCode::Key3 => Some(Key::Character('3')), - VirtualKeyCode::Key4 => Some(Key::Character('4')), - VirtualKeyCode::Key5 => Some(Key::Character('5')), - VirtualKeyCode::Key6 => Some(Key::Character('6')), - VirtualKeyCode::Key7 => Some(Key::Character('7')), - VirtualKeyCode::Key8 => Some(Key::Character('8')), - VirtualKeyCode::Key9 => Some(Key::Character('9')), - VirtualKeyCode::Key0 => Some(Key::Character('0')), + VirtualKeyCode::Key1 => { + Some(Key::Character(if cx.key_mods.shift { '!' } else { '1' })) + } + VirtualKeyCode::Key2 => { + Some(Key::Character(if cx.key_mods.shift { '@' } else { '2' })) + } + VirtualKeyCode::Key3 => { + Some(Key::Character(if cx.key_mods.shift { '#' } else { '3' })) + } + VirtualKeyCode::Key4 => { + Some(Key::Character(if cx.key_mods.shift { '$' } else { '4' })) + } + VirtualKeyCode::Key5 => { + Some(Key::Character(if cx.key_mods.shift { '%' } else { '5' })) + } + VirtualKeyCode::Key6 => { + Some(Key::Character(if cx.key_mods.shift { '^' } else { '6' })) + } + VirtualKeyCode::Key7 => { + Some(Key::Character(if cx.key_mods.shift { '&' } else { '7' })) + } + VirtualKeyCode::Key8 => { + Some(Key::Character(if cx.key_mods.shift { '*' } else { '8' })) + } + VirtualKeyCode::Key9 => { + Some(Key::Character(if cx.key_mods.shift { '(' } else { '9' })) + } + VirtualKeyCode::Key0 => { + Some(Key::Character(if cx.key_mods.shift { ')' } else { '0' })) + } VirtualKeyCode::A => { Some(Key::Character(if cx.key_mods.shift { 'A' } else { 'a' })) } @@ -463,8 +483,12 @@ pub fn rui(view: impl View) { VirtualKeyCode::Colon => Some(Key::Character(':')), VirtualKeyCode::Caret => Some(Key::Character('^')), VirtualKeyCode::Asterisk => Some(Key::Character('*')), - VirtualKeyCode::Period => Some(Key::Character('.')), - VirtualKeyCode::Comma => Some(Key::Character(',')), + VirtualKeyCode::Period => { + Some(Key::Character(if cx.key_mods.shift { '>' } else { '.' })) + } + VirtualKeyCode::Comma => { + Some(Key::Character(if cx.key_mods.shift { '<' } else { ',' })) + } VirtualKeyCode::Equals | VirtualKeyCode::NumpadEquals => { Some(Key::Character('=')) } @@ -477,6 +501,9 @@ pub fn rui(view: impl View) { VirtualKeyCode::Slash | VirtualKeyCode::NumpadDivide => { Some(Key::Character(if cx.key_mods.shift { '?' } else { '/' })) } + VirtualKeyCode::Grave => { + Some(Key::Character(if cx.key_mods.shift { '~' } else { '`' })) + } VirtualKeyCode::Return => Some(Key::Enter), VirtualKeyCode::Tab => Some(Key::Tab), VirtualKeyCode::Space => Some(Key::Space), From 347164c2290b071ee470b71e3f8c198ec0c1d40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20R=C5=AF=C5=BEi=C4=8Dka?= Date: Sat, 9 Dec 2023 15:47:48 +0100 Subject: [PATCH 05/16] added versions of hover, drag, tap which pass the mouse position and button --- src/modifiers.rs | 21 +++++++ src/views/drag_p.rs | 135 +++++++++++++++++++++++++++++++++++++++++++ src/views/hover_p.rs | 94 ++++++++++++++++++++++++++++++ src/views/mod.rs | 6 ++ src/views/tap_p.rs | 98 +++++++++++++++++++++++++++++++ 5 files changed, 354 insertions(+) create mode 100644 src/views/drag_p.rs create mode 100644 src/views/hover_p.rs create mode 100644 src/views/tap_p.rs diff --git a/src/modifiers.rs b/src/modifiers.rs index 98bc8b71..78dea82b 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -36,6 +36,14 @@ pub trait Modifiers: View + Sized { Drag::new(self, f) } + /// Calls a function in response to a drag. Version which passes the position. + fn drag_p) + 'static>( + self, + f: F, + ) -> DragP { + DragP::new(self, f) + } + /// Calls a function in response to a drag. Version which passes in a binding. fn drag_s< T: 'static, @@ -54,6 +62,11 @@ pub trait Modifiers: View + Sized { Hover::new(self, f) } + /// Calls a function in response to a mouse hovering. Version which passes the position + fn hover_p(self, f: F) -> HoverP { + HoverP::new(self, f) + } + /// Add an environment value. fn env(self, value: E) -> SetenvView { SetenvView::new(self, value) @@ -111,6 +124,14 @@ pub trait Modifiers: View + Sized { TapA::new(self, action) } + /// Version of `tap` which passes the tap position and mouse button. + fn tap_p) -> A + 'static>( + self, + f: F, + ) -> TapP { + TapP::new(self, f) + } + /// Specify the title of the window. fn window_title(self, title: &str) -> TitleView { TitleView::new(self, title) diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs new file mode 100644 index 00000000..2598c304 --- /dev/null +++ b/src/views/drag_p.rs @@ -0,0 +1,135 @@ +pub use super::drag::GestureState; +use crate::*; +use std::any::Any; + +/// Struct for the `drag_p` gesture. +pub struct DragP { + child: V, + func: F, + grab: bool, +} + +impl DragP +where + V: View, + F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, +{ + pub fn new(v: V, f: F) -> Self { + Self { + child: v, + func: f, + grab: false, + } + } + + pub fn grab_cursor(v: V, f: F) -> Self { + Self { + child: v, + func: f, + grab: true, + } + } +} + +impl View for DragP +where + V: View, + F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, + A: 'static, +{ + fn process( + &self, + event: &Event, + path: &mut IdPath, + cx: &mut Context, + actions: &mut Vec>, + ) { + let vid = cx.view_id(path); + match &event { + Event::TouchBegin { id, position } => { + if cx.touches[*id].is_default() && self.hittest(path, *position, cx).is_some() { + cx.touches[*id] = vid; + cx.starts[*id] = *position; + cx.previous_position[*id] = *position; + cx.grab_cursor = self.grab; + + actions.push(Box::new((self.func)( + cx, + [0.0, 0.0].into(), + cx.mouse_button, + ))); + } + } + Event::TouchMove { + id, + position, + delta: _, + } => { + if cx.touches[*id] == vid { + actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); + cx.previous_position[*id] = *position; + } + } + Event::TouchEnd { id, position } => { + if cx.touches[*id] == vid { + cx.touches[*id] = ViewId::default(); + cx.grab_cursor = false; + actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); + } + } + _ => (), + } + } + + fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { + path.push(0); + self.child.draw(path, args); + path.pop(); + } + + fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { + path.push(0); + let sz = self.child.layout(path, args); + path.pop(); + sz + } + + fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { + path.push(0); + self.child.dirty(path, xform, cx); + path.pop(); + } + + fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { + path.push(0); + let id = self.child.hittest(path, pt, cx); + path.pop(); + id + } + + fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { + path.push(0); + self.child.commands(path, cx, cmds); + path.pop(); + } + + fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { + path.push(0); + self.child.gc(path, cx, map); + path.pop(); + } + + fn access( + &self, + path: &mut IdPath, + cx: &mut Context, + nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, + ) -> Option { + path.push(0); + let node_id = self.child.access(path, cx, nodes); + path.pop(); + node_id + } +} + +impl private::Sealed for DragP {} diff --git a/src/views/hover_p.rs b/src/views/hover_p.rs new file mode 100644 index 00000000..17a8dbc5 --- /dev/null +++ b/src/views/hover_p.rs @@ -0,0 +1,94 @@ +use crate::*; +use std::any::Any; + +/// Struct for the `hover_p` gesture. +pub struct HoverP { + child: V, + func: F, +} + +impl HoverP +where + V: View, + F: Fn(&mut Context, LocalPoint) -> A + 'static, +{ + pub fn new(v: V, f: F) -> Self { + Self { child: v, func: f } + } +} + +impl View for HoverP +where + V: View, + F: Fn(&mut Context, LocalPoint) -> A + 'static, + A: 'static, +{ + fn process( + &self, + event: &Event, + path: &mut IdPath, + cx: &mut Context, + actions: &mut Vec>, + ) { + if let Event::TouchMove { position, .. } = &event { + if cx.mouse_button.is_none() && self.hittest(path, *position, cx).is_some() { + actions.push(Box::new((self.func)(cx, *position))); + } + } + path.push(0); + self.child.process(event, path, cx, actions); + path.pop(); + } + + fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { + path.push(0); + self.child.draw(path, args); + path.pop(); + } + + fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { + path.push(0); + let sz = self.child.layout(path, args); + path.pop(); + sz + } + + fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { + path.push(0); + self.child.dirty(path, xform, cx); + path.pop(); + } + + fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { + path.push(0); + let id = self.child.hittest(path, pt, cx); + path.pop(); + id + } + + fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { + path.push(0); + self.child.commands(path, cx, cmds); + path.pop(); + } + + fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { + path.push(0); + self.child.gc(path, cx, map); + path.pop(); + } + + fn access( + &self, + path: &mut IdPath, + cx: &mut Context, + nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, + ) -> Option { + path.push(0); + let node_id = self.child.access(path, cx, nodes); + path.pop(); + node_id + } +} + +impl private::Sealed for HoverP {} diff --git a/src/views/mod.rs b/src/views/mod.rs index 6efcac77..26676933 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -16,6 +16,8 @@ mod cond; pub use cond::*; mod drag; pub use drag::*; +mod drag_p; +pub use drag_p::*; mod emptyview; pub use emptyview::*; mod env; @@ -30,6 +32,8 @@ mod handle; pub use handle::*; mod hover; pub use hover::*; +mod hover_p; +pub use hover_p::*; mod key; pub use key::*; mod knob; @@ -63,6 +67,8 @@ mod state; pub use state::*; mod tap; pub use tap::*; +mod tap_p; +pub use tap_p::*; mod text_editor; pub use text_editor::*; mod text; diff --git a/src/views/tap_p.rs b/src/views/tap_p.rs new file mode 100644 index 00000000..13c3c8d4 --- /dev/null +++ b/src/views/tap_p.rs @@ -0,0 +1,98 @@ +use crate::*; +use std::any::Any; + +/// Struct for the `tap` gesture. +pub struct TapP { + /// Child view tree. + child: V, + + /// Called when a tap occurs. + func: F, +} + +impl TapP +where + V: View, + F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, +{ + pub fn new(v: V, f: F) -> Self { + Self { child: v, func: f } + } +} + +impl View for TapP +where + V: View, + F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, + A: 'static, +{ + fn process( + &self, + event: &Event, + path: &mut IdPath, + cx: &mut Context, + actions: &mut Vec>, + ) { + let vid = cx.view_id(path); + match &event { + Event::TouchBegin { id, position } => { + if self.hittest(path, *position, cx).is_some() { + cx.touches[*id] = vid; + } + } + Event::TouchEnd { id, position } => { + if cx.touches[*id] == vid { + cx.touches[*id] = ViewId::default(); + actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))) + } + } + _ => (), + } + } + + fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { + path.push(0); + self.child.draw(path, args); + path.pop(); + } + + fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { + path.push(0); + let sz = self.child.layout(path, args); + path.pop(); + sz + } + + fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { + path.push(0); + let id = self.child.hittest(path, pt, cx); + path.pop(); + id + } + + fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { + path.push(0); + self.child.commands(path, cx, cmds); + path.pop(); + } + + fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { + path.push(0); + self.child.gc(path, cx, map); + path.pop(); + } + + fn access( + &self, + path: &mut IdPath, + cx: &mut Context, + nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, + ) -> Option { + path.push(0); + let node_id = self.child.access(path, cx, nodes); + path.pop(); + node_id + } +} + +impl private::Sealed for TapP where V: View {} From dd3876ebe48be3a4af250a8595e2e1ffdc444758 Mon Sep 17 00:00:00 2001 From: ruza-net Date: Sat, 9 Dec 2023 16:32:30 +0100 Subject: [PATCH 06/16] fix: drag begin sends (0,0) --- src/views/drag_p.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs index 2598c304..acd1cce8 100644 --- a/src/views/drag_p.rs +++ b/src/views/drag_p.rs @@ -53,11 +53,7 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)( - cx, - [0.0, 0.0].into(), - cx.mouse_button, - ))); + actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); } } Event::TouchMove { From f4e2528694163acd77aa028a9ad693adc065e2c6 Mon Sep 17 00:00:00 2001 From: ruza-net Date: Sat, 9 Dec 2023 16:47:07 +0100 Subject: [PATCH 07/16] pass GestureState to drag_p --- src/modifiers.rs | 2 +- src/views/drag_p.rs | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index 78dea82b..46b2cba1 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -37,7 +37,7 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a drag. Version which passes the position. - fn drag_p) + 'static>( + fn drag_p) + 'static>( self, f: F, ) -> DragP { diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs index acd1cce8..bfda657b 100644 --- a/src/views/drag_p.rs +++ b/src/views/drag_p.rs @@ -12,7 +12,7 @@ pub struct DragP { impl DragP where V: View, - F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, + F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, { pub fn new(v: V, f: F) -> Self { Self { @@ -34,7 +34,7 @@ where impl View for DragP where V: View, - F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, + F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, A: 'static, { fn process( @@ -53,7 +53,12 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); + actions.push(Box::new((self.func)( + cx, + *position, + GestureState::Began, + cx.mouse_button, + ))); } } Event::TouchMove { @@ -62,7 +67,12 @@ where delta: _, } => { if cx.touches[*id] == vid { - actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); + actions.push(Box::new((self.func)( + cx, + *position, + GestureState::Changed, + cx.mouse_button, + ))); cx.previous_position[*id] = *position; } } @@ -70,7 +80,12 @@ where if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))); + actions.push(Box::new((self.func)( + cx, + *position, + GestureState::Ended, + cx.mouse_button, + ))); } } _ => (), From 49d60af65146845f16fd74e19a9b370cd3db4080 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 00:43:26 -0800 Subject: [PATCH 08/16] Reduce duplicate code for tap gesture (#58) * Try use TapP for both tap and tap_p * Fix compilation * Remove unused code * Move code * Rename * Eliminate TapA * Fix warning --- src/modifiers.rs | 12 ++--- src/views/mod.rs | 2 - src/views/tap.rs | 130 +++++++++++++-------------------------------- src/views/tap_p.rs | 98 ---------------------------------- 4 files changed, 42 insertions(+), 200 deletions(-) delete mode 100644 src/views/tap_p.rs diff --git a/src/modifiers.rs b/src/modifiers.rs index 46b2cba1..b175e7fc 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -114,22 +114,22 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a tap. - fn tap A + 'static>(self, f: F) -> Tap { - Tap::new(self, f) + fn tap A + 'static>(self, f: F) -> Tap, A> { + Tap::new(self, TapAdapter::{f}) } /// Version of `tap` which takes an action type instead /// of a function. - fn tap_a(self, action: A) -> TapA { - TapA::new(self, action) + fn tap_a(self, action: A) -> Tap, A> { + Tap::new(self, TapActionAdapter::{action}) } /// Version of `tap` which passes the tap position and mouse button. fn tap_p) -> A + 'static>( self, f: F, - ) -> TapP { - TapP::new(self, f) + ) -> Tap, A> { + Tap::new(self, TapFunc::{f}) } /// Specify the title of the window. diff --git a/src/views/mod.rs b/src/views/mod.rs index 26676933..04968fd8 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -67,8 +67,6 @@ mod state; pub use state::*; mod tap; pub use tap::*; -mod tap_p; -pub use tap_p::*; mod text_editor; pub use text_editor::*; mod text; diff --git a/src/views/tap.rs b/src/views/tap.rs index e55f893e..f4cce7e6 100644 --- a/src/views/tap.rs +++ b/src/views/tap.rs @@ -1,124 +1,66 @@ use crate::*; use std::any::Any; -/// Struct for the `tap` gesture. -pub struct Tap { - /// Child view tree. - child: V, - - /// Called when a tap occurs. - func: F, +pub trait TapFn { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A; } -impl Tap -where - V: View, - F: Fn(&mut Context) -> A + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f } - } +pub struct TapFunc { + pub f: F } -impl View for Tap -where - V: View, - F: Fn(&mut Context) -> A + 'static, - A: 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - let vid = cx.view_id(path); - match &event { - Event::TouchBegin { id, position } => { - if self.hittest(path, *position, cx).is_some() { - cx.touches[*id] = vid; - } - } - Event::TouchEnd { id, position: _ } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - actions.push(Box::new((self.func)(cx))) - } - } - _ => (), - } - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz +impl) -> A> TapFn for TapFunc { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A { + (self.f)(cx, pt, button) } +} - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } +pub struct TapAdapter { + pub f: F +} - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); +impl A> TapFn for TapAdapter { + fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { + (self.f)(cx) } +} - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } +pub struct TapActionAdapter { + pub action: A +} - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id +impl TapFn for TapActionAdapter { + fn call(&self, _cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { + self.action.clone() } } -impl private::Sealed for Tap where V: View {} - -/// Struct for the `tap_a` gesture. -pub struct TapA { +/// Struct for the `tap` gesture. +pub struct Tap { /// Child view tree. child: V, /// Called when a tap occurs. - action: A, + func: F, + + phantom_a: std::marker::PhantomData } -impl TapA +impl Tap where V: View, + F: TapFn + 'static, { - pub fn new(child: V, action: A) -> Self { - Self { child, action } + pub fn new(v: V, f: F) -> Self { + Self { child: v, func: f, phantom_a: std::marker::PhantomData::default() } } } -impl View for TapA +impl View for Tap where V: View, - A: Clone + 'static, + F: TapFn + 'static, + A: 'static, { fn process( &self, @@ -134,10 +76,10 @@ where cx.touches[*id] = vid; } } - Event::TouchEnd { id, position: _ } => { + Event::TouchEnd { id, position } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); - actions.push(Box::new(self.action.clone())) + actions.push(Box::new(self.func.call(cx, *position, cx.mouse_button))) } } _ => (), @@ -189,4 +131,4 @@ where } } -impl private::Sealed for TapA where V: View {} +impl private::Sealed for Tap where V: View {} diff --git a/src/views/tap_p.rs b/src/views/tap_p.rs deleted file mode 100644 index 13c3c8d4..00000000 --- a/src/views/tap_p.rs +++ /dev/null @@ -1,98 +0,0 @@ -use crate::*; -use std::any::Any; - -/// Struct for the `tap` gesture. -pub struct TapP { - /// Child view tree. - child: V, - - /// Called when a tap occurs. - func: F, -} - -impl TapP -where - V: View, - F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f } - } -} - -impl View for TapP -where - V: View, - F: Fn(&mut Context, LocalPoint, Option) -> A + 'static, - A: 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - let vid = cx.view_id(path); - match &event { - Event::TouchBegin { id, position } => { - if self.hittest(path, *position, cx).is_some() { - cx.touches[*id] = vid; - } - } - Event::TouchEnd { id, position } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - actions.push(Box::new((self.func)(cx, *position, cx.mouse_button))) - } - } - _ => (), - } - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz - } - - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } - - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); - } - - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } - - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id - } -} - -impl private::Sealed for TapP where V: View {} From 3e319690ed77212c2acba38fdae2f7af068036f9 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 08:44:52 -0800 Subject: [PATCH 09/16] Implement hover using HoverP --- src/modifiers.rs | 8 ++++---- src/views/hover_p.rs | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index b175e7fc..c843bb20 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -58,13 +58,13 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a mouse hovering. - fn hover(self, f: F) -> Hover { - Hover::new(self, f) + fn hover A + 'static>(self, f: F) -> HoverP> { + HoverP::new(self, HoverFunc{f}) } /// Calls a function in response to a mouse hovering. Version which passes the position - fn hover_p(self, f: F) -> HoverP { - HoverP::new(self, f) + fn hover_p A + 'static>(self, f: F) -> HoverP> { + HoverP::new(self, HoverFuncP{f}) } /// Add an environment value. diff --git a/src/views/hover_p.rs b/src/views/hover_p.rs index 17a8dbc5..2d7f5634 100644 --- a/src/views/hover_p.rs +++ b/src/views/hover_p.rs @@ -1,27 +1,52 @@ use crate::*; use std::any::Any; +pub trait HoverFn { + fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>); +} + +pub struct HoverFuncP { + pub f: F +} + +impl A> HoverFn for HoverFuncP { + fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>) { + if inside { + actions.push(Box::new((self.f)(cx, pt))) + } + } +} + +pub struct HoverFunc { + pub f: F +} + +impl A> HoverFn for HoverFunc { + fn call(&self, cx: &mut Context, _pt: LocalPoint, inside: bool, actions: &mut Vec>) { + actions.push(Box::new((self.f)(cx, inside))) + } +} + /// Struct for the `hover_p` gesture. pub struct HoverP { child: V, func: F, } -impl HoverP +impl HoverP where V: View, - F: Fn(&mut Context, LocalPoint) -> A + 'static, + F: HoverFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { child: v, func: f } } } -impl View for HoverP +impl View for HoverP where V: View, - F: Fn(&mut Context, LocalPoint) -> A + 'static, - A: 'static, + F: HoverFn + 'static, { fn process( &self, @@ -31,8 +56,9 @@ where actions: &mut Vec>, ) { if let Event::TouchMove { position, .. } = &event { - if cx.mouse_button.is_none() && self.hittest(path, *position, cx).is_some() { - actions.push(Box::new((self.func)(cx, *position))); + if cx.mouse_button.is_none() { + let inside = self.hittest(path, *position, cx).is_some(); + self.func.call(cx, *position, inside, actions); } } path.push(0); From 03476fad9e74422878f31f195f4633c13dae551c Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 08:49:18 -0800 Subject: [PATCH 10/16] Remove HoverP --- src/modifiers.rs | 8 +-- src/views/hover.rs | 39 +++++++++++--- src/views/hover_p.rs | 120 ------------------------------------------- src/views/mod.rs | 2 - 4 files changed, 36 insertions(+), 133 deletions(-) delete mode 100644 src/views/hover_p.rs diff --git a/src/modifiers.rs b/src/modifiers.rs index c843bb20..b673907d 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -58,13 +58,13 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a mouse hovering. - fn hover A + 'static>(self, f: F) -> HoverP> { - HoverP::new(self, HoverFunc{f}) + fn hover A + 'static>(self, f: F) -> Hover> { + Hover::new(self, HoverFunc{f}) } /// Calls a function in response to a mouse hovering. Version which passes the position - fn hover_p A + 'static>(self, f: F) -> HoverP> { - HoverP::new(self, HoverFuncP{f}) + fn hover_p A + 'static>(self, f: F) -> Hover> { + Hover::new(self, HoverFuncP{f}) } /// Add an environment value. diff --git a/src/views/hover.rs b/src/views/hover.rs index af6da417..c6a3d64f 100644 --- a/src/views/hover.rs +++ b/src/views/hover.rs @@ -1,27 +1,52 @@ use crate::*; use std::any::Any; -/// Struct for the `Hover` gesture. +pub trait HoverFn { + fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>); +} + +pub struct HoverFuncP { + pub f: F +} + +impl A> HoverFn for HoverFuncP { + fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>) { + if inside { + actions.push(Box::new((self.f)(cx, pt))) + } + } +} + +pub struct HoverFunc { + pub f: F +} + +impl A> HoverFn for HoverFunc { + fn call(&self, cx: &mut Context, _pt: LocalPoint, inside: bool, actions: &mut Vec>) { + actions.push(Box::new((self.f)(cx, inside))) + } +} + +/// Struct for the `hover` and 'hover_p` gestures. pub struct Hover { child: V, func: F, } -impl Hover +impl Hover where V: View, - F: Fn(&mut Context, bool) -> A + 'static, + F: HoverFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { child: v, func: f } } } -impl View for Hover +impl View for Hover where V: View, - F: Fn(&mut Context, bool) -> A + 'static, - A: 'static, + F: HoverFn + 'static, { fn process( &self, @@ -33,7 +58,7 @@ where if let Event::TouchMove { position, .. } = &event { if cx.mouse_button.is_none() { let inside = self.hittest(path, *position, cx).is_some(); - actions.push(Box::new((self.func)(cx, inside))); + self.func.call(cx, *position, inside, actions); } } path.push(0); diff --git a/src/views/hover_p.rs b/src/views/hover_p.rs deleted file mode 100644 index 2d7f5634..00000000 --- a/src/views/hover_p.rs +++ /dev/null @@ -1,120 +0,0 @@ -use crate::*; -use std::any::Any; - -pub trait HoverFn { - fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>); -} - -pub struct HoverFuncP { - pub f: F -} - -impl A> HoverFn for HoverFuncP { - fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>) { - if inside { - actions.push(Box::new((self.f)(cx, pt))) - } - } -} - -pub struct HoverFunc { - pub f: F -} - -impl A> HoverFn for HoverFunc { - fn call(&self, cx: &mut Context, _pt: LocalPoint, inside: bool, actions: &mut Vec>) { - actions.push(Box::new((self.f)(cx, inside))) - } -} - -/// Struct for the `hover_p` gesture. -pub struct HoverP { - child: V, - func: F, -} - -impl HoverP -where - V: View, - F: HoverFn + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f } - } -} - -impl View for HoverP -where - V: View, - F: HoverFn + 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - if let Event::TouchMove { position, .. } = &event { - if cx.mouse_button.is_none() { - let inside = self.hittest(path, *position, cx).is_some(); - self.func.call(cx, *position, inside, actions); - } - } - path.push(0); - self.child.process(event, path, cx, actions); - path.pop(); - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz - } - - fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { - path.push(0); - self.child.dirty(path, xform, cx); - path.pop(); - } - - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } - - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); - } - - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } - - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id - } -} - -impl private::Sealed for HoverP {} diff --git a/src/views/mod.rs b/src/views/mod.rs index 04968fd8..50674313 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -32,8 +32,6 @@ mod handle; pub use handle::*; mod hover; pub use hover::*; -mod hover_p; -pub use hover_p::*; mod key; pub use key::*; mod knob; From caeb27e9be27e5a25cef6291612426ecd1cdfaf8 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 08:55:38 -0800 Subject: [PATCH 11/16] Action type erasure --- src/modifiers.rs | 12 ++++++------ src/views/tap.rs | 41 +++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index b673907d..bd052c17 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -114,22 +114,22 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a tap. - fn tap A + 'static>(self, f: F) -> Tap, A> { - Tap::new(self, TapAdapter::{f}) + fn tap A + 'static>(self, f: F) -> Tap> { + Tap::new(self, TapAdapter{f}) } /// Version of `tap` which takes an action type instead /// of a function. - fn tap_a(self, action: A) -> Tap, A> { - Tap::new(self, TapActionAdapter::{action}) + fn tap_a(self, action: A) -> Tap> { + Tap::new(self, TapActionAdapter{action}) } /// Version of `tap` which passes the tap position and mouse button. fn tap_p) -> A + 'static>( self, f: F, - ) -> Tap, A> { - Tap::new(self, TapFunc::{f}) + ) -> Tap> { + Tap::new(self, TapFunc{f}) } /// Specify the title of the window. diff --git a/src/views/tap.rs b/src/views/tap.rs index f4cce7e6..6859ac0f 100644 --- a/src/views/tap.rs +++ b/src/views/tap.rs @@ -1,17 +1,17 @@ use crate::*; use std::any::Any; -pub trait TapFn { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A; +pub trait TapFn { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option, actions: &mut Vec>); } pub struct TapFunc { pub f: F } -impl) -> A> TapFn for TapFunc { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A { - (self.f)(cx, pt, button) +impl) -> A> TapFn for TapFunc { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option, actions: &mut Vec>) { + actions.push(Box::new((self.f)(cx, pt, button))) } } @@ -19,9 +19,9 @@ pub struct TapAdapter { pub f: F } -impl A> TapFn for TapAdapter { - fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { - (self.f)(cx) +impl A> TapFn for TapAdapter { + fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option, actions: &mut Vec>) { + actions.push(Box::new((self.f)(cx))) } } @@ -29,38 +29,35 @@ pub struct TapActionAdapter { pub action: A } -impl TapFn for TapActionAdapter { - fn call(&self, _cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { - self.action.clone() +impl TapFn for TapActionAdapter { + fn call(&self, _cx: &mut Context, _pt: LocalPoint, _button: Option, actions: &mut Vec>) { + actions.push(Box::new(self.action.clone())) } } /// Struct for the `tap` gesture. -pub struct Tap { +pub struct Tap { /// Child view tree. child: V, /// Called when a tap occurs. func: F, - - phantom_a: std::marker::PhantomData } -impl Tap +impl Tap where V: View, - F: TapFn + 'static, + F: TapFn + 'static, { pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f, phantom_a: std::marker::PhantomData::default() } + Self { child: v, func: f } } } -impl View for Tap +impl View for Tap where V: View, - F: TapFn + 'static, - A: 'static, + F: TapFn + 'static, { fn process( &self, @@ -79,7 +76,7 @@ where Event::TouchEnd { id, position } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); - actions.push(Box::new(self.func.call(cx, *position, cx.mouse_button))) + self.func.call(cx, *position, cx.mouse_button, actions) } } _ => (), @@ -131,4 +128,4 @@ where } } -impl private::Sealed for Tap where V: View {} +impl private::Sealed for Tap where V: View {} From bed6fb2f2f21180077addb8a29af114d05a4c314 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 08:56:57 -0800 Subject: [PATCH 12/16] cargo fmt --- src/modifiers.rs | 20 +++++++++++++------- src/views/hover.rs | 20 ++++++++++++++++---- src/views/tap.rs | 38 +++++++++++++++++++++++++++++++------- src/viewtuple.rs | 2 +- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index bd052c17..a6bd7eaa 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -58,13 +58,19 @@ pub trait Modifiers: View + Sized { } /// Calls a function in response to a mouse hovering. - fn hover A + 'static>(self, f: F) -> Hover> { - Hover::new(self, HoverFunc{f}) + fn hover A + 'static>( + self, + f: F, + ) -> Hover> { + Hover::new(self, HoverFunc { f }) } /// Calls a function in response to a mouse hovering. Version which passes the position - fn hover_p A + 'static>(self, f: F) -> Hover> { - Hover::new(self, HoverFuncP{f}) + fn hover_p A + 'static>( + self, + f: F, + ) -> Hover> { + Hover::new(self, HoverFuncP { f }) } /// Add an environment value. @@ -115,13 +121,13 @@ pub trait Modifiers: View + Sized { /// Calls a function in response to a tap. fn tap A + 'static>(self, f: F) -> Tap> { - Tap::new(self, TapAdapter{f}) + Tap::new(self, TapAdapter { f }) } /// Version of `tap` which takes an action type instead /// of a function. fn tap_a(self, action: A) -> Tap> { - Tap::new(self, TapActionAdapter{action}) + Tap::new(self, TapActionAdapter { action }) } /// Version of `tap` which passes the tap position and mouse button. @@ -129,7 +135,7 @@ pub trait Modifiers: View + Sized { self, f: F, ) -> Tap> { - Tap::new(self, TapFunc{f}) + Tap::new(self, TapFunc { f }) } /// Specify the title of the window. diff --git a/src/views/hover.rs b/src/views/hover.rs index c6a3d64f..3bd349c6 100644 --- a/src/views/hover.rs +++ b/src/views/hover.rs @@ -6,11 +6,17 @@ pub trait HoverFn { } pub struct HoverFuncP { - pub f: F + pub f: F, } impl A> HoverFn for HoverFuncP { - fn call(&self, cx: &mut Context, pt: LocalPoint, inside: bool, actions: &mut Vec>) { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + inside: bool, + actions: &mut Vec>, + ) { if inside { actions.push(Box::new((self.f)(cx, pt))) } @@ -18,11 +24,17 @@ impl A> HoverFn for HoverFuncP } pub struct HoverFunc { - pub f: F + pub f: F, } impl A> HoverFn for HoverFunc { - fn call(&self, cx: &mut Context, _pt: LocalPoint, inside: bool, actions: &mut Vec>) { + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + inside: bool, + actions: &mut Vec>, + ) { actions.push(Box::new((self.f)(cx, inside))) } } diff --git a/src/views/tap.rs b/src/views/tap.rs index 6859ac0f..9cf8f305 100644 --- a/src/views/tap.rs +++ b/src/views/tap.rs @@ -2,35 +2,59 @@ use crate::*; use std::any::Any; pub trait TapFn { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option, actions: &mut Vec>); + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + button: Option, + actions: &mut Vec>, + ); } pub struct TapFunc { - pub f: F + pub f: F, } impl) -> A> TapFn for TapFunc { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option, actions: &mut Vec>) { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + button: Option, + actions: &mut Vec>, + ) { actions.push(Box::new((self.f)(cx, pt, button))) } } pub struct TapAdapter { - pub f: F + pub f: F, } impl A> TapFn for TapAdapter { - fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option, actions: &mut Vec>) { + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + _button: Option, + actions: &mut Vec>, + ) { actions.push(Box::new((self.f)(cx))) } } pub struct TapActionAdapter { - pub action: A + pub action: A, } impl TapFn for TapActionAdapter { - fn call(&self, _cx: &mut Context, _pt: LocalPoint, _button: Option, actions: &mut Vec>) { + fn call( + &self, + _cx: &mut Context, + _pt: LocalPoint, + _button: Option, + actions: &mut Vec>, + ) { actions.push(Box::new(self.action.clone())) } } diff --git a/src/viewtuple.rs b/src/viewtuple.rs index b896917e..e8193823 100644 --- a/src/viewtuple.rs +++ b/src/viewtuple.rs @@ -154,4 +154,4 @@ impl_view_tuple!(124; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13 impl_view_tuple!(125; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124; 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); impl_view_tuple!(126; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124, V125; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125; 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); impl_view_tuple!(127; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126; 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); -impl_view_tuple!(128; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126, V127; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127; 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); \ No newline at end of file +impl_view_tuple!(128; V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40, V41, V42, V43, V44, V45, V46, V47, V48, V49, V50, V51, V52, V53, V54, V55, V56, V57, V58, V59, V60, V61, V62, V63, V64, V65, V66, V67, V68, V69, V70, V71, V72, V73, V74, V75, V76, V77, V78, V79, V80, V81, V82, V83, V84, V85, V86, V87, V88, V89, V90, V91, V92, V93, V94, V95, V96, V97, V98, V99, V100, V101, V102, V103, V104, V105, V106, V107, V108, V109, V110, V111, V112, V113, V114, V115, V116, V117, V118, V119, V120, V121, V122, V123, V124, V125, V126, V127; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127; 127, 126, 125, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); From 53c92e5ad84864e1ffd17b14bc04e6df6a9c1ae8 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:22:27 -0800 Subject: [PATCH 13/16] Add DragFn --- src/modifiers.rs | 4 +-- src/views/drag_p.rs | 62 ++++++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index a6bd7eaa..fc490f04 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -40,8 +40,8 @@ pub trait Modifiers: View + Sized { fn drag_p) + 'static>( self, f: F, - ) -> DragP { - DragP::new(self, f) + ) -> DragP> { + DragP::new(self, DragFuncP{f}) } /// Calls a function in response to a drag. Version which passes in a binding. diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs index bfda657b..317b5dfb 100644 --- a/src/views/drag_p.rs +++ b/src/views/drag_p.rs @@ -2,6 +2,36 @@ pub use super::drag::GestureState; use crate::*; use std::any::Any; +pub trait DragFn { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + state: GestureState, + button: Option, + actions: &mut Vec>, + ); +} + +pub struct DragFuncP { + pub f: F, +} + +impl) -> A> DragFn + for DragFuncP +{ + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, pt, state, button))) + } +} + /// Struct for the `drag_p` gesture. pub struct DragP { child: V, @@ -9,10 +39,10 @@ pub struct DragP { grab: bool, } -impl DragP +impl DragP where V: View, - F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, + F: DragFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { @@ -31,11 +61,10 @@ where } } -impl View for DragP +impl View for DragP where V: View, - F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, - A: 'static, + F: DragFn + 'static, { fn process( &self, @@ -53,12 +82,7 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Began, - cx.mouse_button, - ))); + self.func.call(cx, *position, GestureState::Began, cx.mouse_button, actions); } } Event::TouchMove { @@ -67,12 +91,8 @@ where delta: _, } => { if cx.touches[*id] == vid { - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Changed, - cx.mouse_button, - ))); + + self.func.call(cx, *position, GestureState::Changed, cx.mouse_button, actions); cx.previous_position[*id] = *position; } } @@ -80,12 +100,8 @@ where if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Ended, - cx.mouse_button, - ))); + + self.func.call(cx, *position, GestureState::Ended, cx.mouse_button, actions); } } _ => (), From 6f374b6719a7b9bc1541a1dfd09c4149912f4b87 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:25:48 -0800 Subject: [PATCH 14/16] Pass delta --- src/modifiers.rs | 2 +- src/views/drag_p.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index fc490f04..32b0ff50 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -41,7 +41,7 @@ pub trait Modifiers: View + Sized { self, f: F, ) -> DragP> { - DragP::new(self, DragFuncP{f}) + DragP::new(self, DragFuncP { f }) } /// Calls a function in response to a drag. Version which passes in a binding. diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs index 317b5dfb..060af01a 100644 --- a/src/views/drag_p.rs +++ b/src/views/drag_p.rs @@ -7,6 +7,7 @@ pub trait DragFn { &self, cx: &mut Context, pt: LocalPoint, + delta: LocalOffset, state: GestureState, button: Option, actions: &mut Vec>, @@ -24,6 +25,7 @@ impl, actions: &mut Vec>, @@ -82,17 +84,30 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - self.func.call(cx, *position, GestureState::Began, cx.mouse_button, actions); + self.func.call( + cx, + *position, + LocalOffset::zero(), + GestureState::Began, + cx.mouse_button, + actions, + ); } } Event::TouchMove { id, position, - delta: _, + delta, } => { if cx.touches[*id] == vid { - - self.func.call(cx, *position, GestureState::Changed, cx.mouse_button, actions); + self.func.call( + cx, + *position, + *delta, + GestureState::Changed, + cx.mouse_button, + actions, + ); cx.previous_position[*id] = *position; } } @@ -101,7 +116,14 @@ where cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - self.func.call(cx, *position, GestureState::Ended, cx.mouse_button, actions); + self.func.call( + cx, + *position, + LocalOffset::zero(), + GestureState::Ended, + cx.mouse_button, + actions, + ); } } _ => (), From d87c4f97b0025ddc3126222905ed22c0c7849d6e Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:36:46 -0800 Subject: [PATCH 15/16] Remove DragP --- src/modifiers.rs | 8 +- src/views/drag.rs | 92 ++++++++++++++++++---- src/views/drag_p.rs | 184 -------------------------------------------- src/views/mod.rs | 2 - 4 files changed, 79 insertions(+), 207 deletions(-) delete mode 100644 src/views/drag_p.rs diff --git a/src/modifiers.rs b/src/modifiers.rs index 32b0ff50..19bad92f 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -32,16 +32,16 @@ pub trait Modifiers: View + Sized { fn drag) + 'static>( self, f: F, - ) -> Drag { - Drag::new(self, f) + ) -> Drag> { + Drag::new(self, DragFunc { f }) } /// Calls a function in response to a drag. Version which passes the position. fn drag_p) + 'static>( self, f: F, - ) -> DragP> { - DragP::new(self, DragFuncP { f }) + ) -> Drag> { + Drag::new(self, DragFuncP { f }) } /// Calls a function in response to a drag. Version which passes in a binding. diff --git a/src/views/drag.rs b/src/views/drag.rs index 93500020..c78d9544 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -8,17 +8,69 @@ pub enum GestureState { Ended, } -/// Struct for the `drag` gesture. +pub trait DragFn { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ); +} + +pub struct DragFunc { + pub f: F, +} + +impl) -> A> DragFn + for DragFunc +{ + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, delta, state, button))) + } +} + +pub struct DragFuncP { + pub f: F, +} + +impl) -> A> DragFn + for DragFuncP +{ + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + _delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, pt, state, button))) + } +} + +/// Struct for the `drag` and `drag_p` gestures. pub struct Drag { child: V, func: F, grab: bool, } -impl Drag +impl Drag where V: View, - F: Fn(&mut Context, LocalOffset, GestureState, Option) -> A + 'static, + F: DragFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { @@ -28,20 +80,19 @@ where } } - pub fn grab_cursor(self) -> Self { + pub fn grab_cursor(v: V, f: F) -> Self { Self { - child: self.child, - func: self.func, + child: v, + func: f, grab: true, } } } -impl View for Drag +impl View for Drag where V: View, - F: Fn(&mut Context, LocalOffset, GestureState, Option) -> A + 'static, - A: 'static, + F: DragFn + 'static, { fn process( &self, @@ -59,12 +110,14 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)( + self.func.call( cx, - [0.0, 0.0].into(), + *position, + LocalOffset::zero(), GestureState::Began, cx.mouse_button, - ))); + actions, + ); } } Event::TouchMove { @@ -73,25 +126,30 @@ where delta, } => { if cx.touches[*id] == vid { - actions.push(Box::new((self.func)( + self.func.call( cx, + *position, *delta, GestureState::Changed, cx.mouse_button, - ))); + actions, + ); cx.previous_position[*id] = *position; } } - Event::TouchEnd { id, .. } => { + Event::TouchEnd { id, position } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - actions.push(Box::new((self.func)( + + self.func.call( cx, + *position, LocalOffset::zero(), GestureState::Ended, cx.mouse_button, - ))); + actions, + ); } } _ => (), diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs deleted file mode 100644 index 060af01a..00000000 --- a/src/views/drag_p.rs +++ /dev/null @@ -1,184 +0,0 @@ -pub use super::drag::GestureState; -use crate::*; -use std::any::Any; - -pub trait DragFn { - fn call( - &self, - cx: &mut Context, - pt: LocalPoint, - delta: LocalOffset, - state: GestureState, - button: Option, - actions: &mut Vec>, - ); -} - -pub struct DragFuncP { - pub f: F, -} - -impl) -> A> DragFn - for DragFuncP -{ - fn call( - &self, - cx: &mut Context, - pt: LocalPoint, - _delta: LocalOffset, - state: GestureState, - button: Option, - actions: &mut Vec>, - ) { - actions.push(Box::new((self.f)(cx, pt, state, button))) - } -} - -/// Struct for the `drag_p` gesture. -pub struct DragP { - child: V, - func: F, - grab: bool, -} - -impl DragP -where - V: View, - F: DragFn + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { - child: v, - func: f, - grab: false, - } - } - - pub fn grab_cursor(v: V, f: F) -> Self { - Self { - child: v, - func: f, - grab: true, - } - } -} - -impl View for DragP -where - V: View, - F: DragFn + 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - let vid = cx.view_id(path); - match &event { - Event::TouchBegin { id, position } => { - if cx.touches[*id].is_default() && self.hittest(path, *position, cx).is_some() { - cx.touches[*id] = vid; - cx.starts[*id] = *position; - cx.previous_position[*id] = *position; - cx.grab_cursor = self.grab; - - self.func.call( - cx, - *position, - LocalOffset::zero(), - GestureState::Began, - cx.mouse_button, - actions, - ); - } - } - Event::TouchMove { - id, - position, - delta, - } => { - if cx.touches[*id] == vid { - self.func.call( - cx, - *position, - *delta, - GestureState::Changed, - cx.mouse_button, - actions, - ); - cx.previous_position[*id] = *position; - } - } - Event::TouchEnd { id, position } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - cx.grab_cursor = false; - - self.func.call( - cx, - *position, - LocalOffset::zero(), - GestureState::Ended, - cx.mouse_button, - actions, - ); - } - } - _ => (), - } - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz - } - - fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { - path.push(0); - self.child.dirty(path, xform, cx); - path.pop(); - } - - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } - - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); - } - - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } - - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id - } -} - -impl private::Sealed for DragP {} diff --git a/src/views/mod.rs b/src/views/mod.rs index 50674313..6efcac77 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -16,8 +16,6 @@ mod cond; pub use cond::*; mod drag; pub use drag::*; -mod drag_p; -pub use drag_p::*; mod emptyview; pub use emptyview::*; mod env; From dd6062f29207c59cd7ee84299c5bd92bf4881d33 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:57:46 -0800 Subject: [PATCH 16/16] Remove DragS --- src/modifiers.rs | 8 +- src/views/drag.rs | 191 +++++++++------------------------------------- 2 files changed, 40 insertions(+), 159 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index 19bad92f..d00c948b 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -1,5 +1,7 @@ + use crate::*; use accesskit::Role; +use std::marker::PhantomData; /// Modifiers common to all views. pub trait Modifiers: View + Sized { @@ -51,10 +53,10 @@ pub trait Modifiers: View + Sized { F: Fn(&mut T, LocalOffset, GestureState, Option) + 'static, >( self, - s: B, + b: B, f: F, - ) -> DragS { - DragS::new(self, s, f) + ) -> Drag> { + Drag::new(self, DragFuncS { f, b, phantom: PhantomData::default() }) } /// Calls a function in response to a mouse hovering. diff --git a/src/views/drag.rs b/src/views/drag.rs index c78d9544..72c4aac5 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -1,5 +1,6 @@ use crate::*; use std::any::Any; +use std::marker::PhantomData; #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum GestureState { @@ -60,6 +61,37 @@ impl { + pub f: F, + pub b: B, + pub phantom: PhantomData, +} + +impl< + F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, + B: Binding, + A: 'static, + T: 'static, + > DragFn for DragFuncS +{ + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)( + self.b.get_mut(cx), + delta, + state, + button, + ))) + } +} + /// Struct for the `drag` and `drag_p` gestures. pub struct Drag { child: V, @@ -80,10 +112,10 @@ where } } - pub fn grab_cursor(v: V, f: F) -> Self { + pub fn grab_cursor(self) -> Self { Self { - child: v, - func: f, + child: self.child, + func: self.func, grab: true, } } @@ -209,159 +241,6 @@ where impl private::Sealed for Drag {} -/// Struct for the `drag` gesture. -pub struct DragS { - child: V, - func: F, - binding: B, - phantom: std::marker::PhantomData, - grab: bool, -} - -impl DragS -where - V: View, - F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, - B: Binding, - A: 'static, - T: 'static, -{ - pub fn new(v: V, b: B, f: F) -> Self { - Self { - child: v, - func: f, - binding: b, - phantom: std::marker::PhantomData::default(), - grab: false, - } - } - - pub fn grab_cursor(self) -> Self { - Self { - child: self.child, - func: self.func, - binding: self.binding, - phantom: std::marker::PhantomData::default(), - grab: true, - } - } -} - -impl View for DragS -where - V: View, - F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, - B: Binding, - A: 'static, - T: 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - let vid = cx.view_id(path); - match &event { - Event::TouchBegin { id, position } => { - if self.hittest(path, *position, cx).is_some() { - cx.touches[*id] = vid; - cx.starts[*id] = *position; - cx.previous_position[*id] = *position; - cx.grab_cursor = self.grab; - } - } - Event::TouchMove { - id, - position, - delta, - } => { - if cx.touches[*id] == vid { - let button = cx.mouse_button; - actions.push(Box::new((self.func)( - self.binding.get_mut(cx), - *delta, - GestureState::Changed, - button, - ))); - cx.previous_position[*id] = *position; - } - } - Event::TouchEnd { id, .. } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - cx.grab_cursor = false; - let button = cx.mouse_button; - actions.push(Box::new((self.func)( - self.binding.get_mut(cx), - LocalOffset::zero(), - GestureState::Ended, - button, - ))); - } - } - _ => { - path.push(0); - self.child.process(event, path, cx, actions); - path.pop(); - } - } - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz - } - - fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { - path.push(0); - self.child.dirty(path, xform, cx); - path.pop(); - } - - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } - - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); - } - - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } - - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id - } -} - -impl private::Sealed for DragS {} - #[cfg(test)] mod tests {