From fe60f264c457f737bff7b3ef460b9f05603dc050 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 9 Oct 2023 21:46:49 -0600 Subject: [PATCH] Checkpoint --- crates/gpui3/src/events.rs | 10 ++-- crates/gpui3/src/interactive.rs | 73 ++++++++++++++++++++++--- crates/gpui3/src/platform/mac/events.rs | 10 ++-- crates/gpui3/src/platform/mac/window.rs | 6 +- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/crates/gpui3/src/events.rs b/crates/gpui3/src/events.rs index e9ea56198c174eb3ec943d79729c786a5e1b64d3..52719ae066e509292ea047a908229fedad9feaaa 100644 --- a/crates/gpui3/src/events.rs +++ b/crates/gpui3/src/events.rs @@ -89,7 +89,7 @@ impl Default for NavigationDirection { } #[derive(Clone, Debug, Default)] -pub struct MouseMovedEvent { +pub struct MouseMoveEvent { pub position: Point, pub pressed_button: Option, pub modifiers: Modifiers, @@ -140,13 +140,13 @@ impl ScrollDelta { } #[derive(Clone, Debug, Default)] -pub struct MouseExitedEvent { +pub struct MouseExitEvent { pub position: Point, pub pressed_button: Option, pub modifiers: Modifiers, } -impl Deref for MouseExitedEvent { +impl Deref for MouseExitEvent { type Target = Modifiers; fn deref(&self) -> &Self::Target { @@ -161,8 +161,8 @@ pub enum Event { ModifiersChanged(ModifiersChangedEvent), MouseDown(MouseDownEvent), MouseUp(MouseUpEvent), - MouseMoved(MouseMovedEvent), - MouseExited(MouseExitedEvent), + MouseMoved(MouseMoveEvent), + MouseExited(MouseExitEvent), ScrollWheel(ScrollWheelEvent), } diff --git a/crates/gpui3/src/interactive.rs b/crates/gpui3/src/interactive.rs index f5013609e449b491c3368617de60e59d29f75fd0..c7413a802776c7a1eab27336b64a1d05aa6ac48b 100644 --- a/crates/gpui3/src/interactive.rs +++ b/crates/gpui3/src/interactive.rs @@ -1,5 +1,6 @@ use crate::{ - Bounds, DispatchPhase, MouseButton, MouseDownEvent, MouseUpEvent, Pixels, ViewContext, + Bounds, DispatchPhase, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, + ScrollWheelEvent, ViewContext, }; use parking_lot::Mutex; use smallvec::SmallVec; @@ -122,6 +123,40 @@ pub trait Interactive { } }) } + + fn on_mouse_move( + mut self, + handler: impl Fn(&mut S, &MouseMoveEvent, &mut ViewContext) + Send + Sync + 'static, + ) -> Self + where + Self: Sized, + { + self.listeners() + .mouse_move + .push(Arc::new(move |view, event, bounds, phase, cx| { + if phase == DispatchPhase::Bubble && bounds.contains_point(event.position) { + handler(view, event, cx); + } + })); + self + } + + fn on_scroll_wheel( + mut self, + handler: impl Fn(&mut S, &ScrollWheelEvent, &mut ViewContext) + Send + Sync + 'static, + ) -> Self + where + Self: Sized, + { + self.listeners() + .scroll_wheel + .push(Arc::new(move |view, event, bounds, phase, cx| { + if phase == DispatchPhase::Bubble && bounds.contains_point(event.position) { + handler(view, event, cx); + } + })); + self + } } type MouseDownHandler = Arc< @@ -137,25 +172,47 @@ type MouseUpHandler = Arc< + 'static, >; +type MouseMoveHandler = Arc< + dyn Fn(&mut V, &MouseMoveEvent, &Bounds, DispatchPhase, &mut ViewContext) + + Send + + Sync + + 'static, +>; +type ScrollWheelHandler = Arc< + dyn Fn(&mut V, &ScrollWheelEvent, &Bounds, DispatchPhase, &mut ViewContext) + + Send + + Sync + + 'static, +>; + pub struct MouseEventListeners { mouse_down: SmallVec<[MouseDownHandler; 2]>, mouse_up: SmallVec<[MouseUpHandler; 2]>, + mouse_move: SmallVec<[MouseMoveHandler; 2]>, + scroll_wheel: SmallVec<[ScrollWheelHandler; 2]>, } impl MouseEventListeners { pub fn paint(&self, bounds: Bounds, cx: &mut ViewContext) { for handler in self.mouse_down.iter().cloned() { cx.on_mouse_event(move |view, event: &MouseDownEvent, phase, cx| { - if bounds.contains_point(event.position) { - handler(view, event, &bounds, phase, cx); - } + handler(view, event, &bounds, phase, cx); }) } for handler in self.mouse_up.iter().cloned() { cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| { - if bounds.contains_point(event.position) { - handler(view, event, &bounds, phase, cx); - } + handler(view, event, &bounds, phase, cx); + }) + } + for handler in self.mouse_move.iter().cloned() { + cx.on_mouse_event(move |view, event: &MouseMoveEvent, phase, cx| { + handler(view, event, &bounds, phase, cx); + }) + } + + for handler in self.scroll_wheel.iter().cloned() { + cx.on_mouse_event(move |view, event: &ScrollWheelEvent, phase, cx| { + handler(view, event, &bounds, phase, cx); }) } } @@ -166,6 +223,8 @@ impl Default for MouseEventListeners { Self { mouse_down: Default::default(), mouse_up: Default::default(), + mouse_move: Default::default(), + scroll_wheel: Default::default(), } } } diff --git a/crates/gpui3/src/platform/mac/events.rs b/crates/gpui3/src/platform/mac/events.rs index a2c96608ec14b9e99177752ed19f22d452ed995a..d776d7ab02c3cac07589b29506cf04b7bc43df64 100644 --- a/crates/gpui3/src/platform/mac/events.rs +++ b/crates/gpui3/src/platform/mac/events.rs @@ -1,7 +1,7 @@ use crate::{ point, px, Event, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, ModifiersChangedEvent, - MouseButton, MouseDownEvent, MouseExitedEvent, MouseMovedEvent, MouseUpEvent, - NavigationDirection, Pixels, ScrollDelta, ScrollWheelEvent, TouchPhase, + MouseButton, MouseDownEvent, MouseExitEvent, MouseMoveEvent, MouseUpEvent, NavigationDirection, + Pixels, ScrollDelta, ScrollWheelEvent, TouchPhase, }; use cocoa::{ appkit::{NSEvent, NSEventModifierFlags, NSEventPhase, NSEventType}, @@ -202,7 +202,7 @@ impl Event { }; window_height.map(|window_height| { - Self::MouseMoved(MouseMovedEvent { + Self::MouseMoved(MouseMoveEvent { pressed_button: Some(pressed_button), position: point( px(native_event.locationInWindow().x as f32), @@ -213,7 +213,7 @@ impl Event { }) } NSEventType::NSMouseMoved => window_height.map(|window_height| { - Self::MouseMoved(MouseMovedEvent { + Self::MouseMoved(MouseMoveEvent { position: point( px(native_event.locationInWindow().x as f32), window_height - px(native_event.locationInWindow().y as f32), @@ -223,7 +223,7 @@ impl Event { }) }), NSEventType::NSMouseExited => window_height.map(|window_height| { - Self::MouseExited(MouseExitedEvent { + Self::MouseExited(MouseExitEvent { position: point( px(native_event.locationInWindow().x as f32), window_height - px(native_event.locationInWindow().y as f32), diff --git a/crates/gpui3/src/platform/mac/window.rs b/crates/gpui3/src/platform/mac/window.rs index d994942002b723a20862a72b5623008684f76375..03ce03b34b96ecf90cbd91b8640487a1ca4e6eb3 100644 --- a/crates/gpui3/src/platform/mac/window.rs +++ b/crates/gpui3/src/platform/mac/window.rs @@ -2,7 +2,7 @@ use super::{display_bounds_from_native, ns_string, MacDisplay, MetalRenderer, NS use crate::{ display_bounds_to_native, point, px, size, AnyWindowHandle, Bounds, Event, Executor, GlobalPixels, KeyDownEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton, - MouseDownEvent, MouseMovedEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PlatformWindow, Point, Scene, Size, Timer, WindowAppearance, WindowBounds, WindowKind, WindowOptions, WindowPromptLevel, }; @@ -1141,7 +1141,7 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) { match &event { Event::MouseMoved( - event @ MouseMovedEvent { + event @ MouseMoveEvent { pressed_button: Some(_), .. }, @@ -1596,7 +1596,7 @@ extern "C" fn accepts_first_mouse(this: &Object, _: Sel, _: id) -> BOOL { async fn synthetic_drag( window_state: Weak>, drag_id: usize, - event: MouseMovedEvent, + event: MouseMoveEvent, ) { loop { Timer::after(Duration::from_millis(16)).await;