From bdedeab7af6b57c8be6cbcd5d2b98c703765d48e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 29 Feb 2024 17:34:36 -0700 Subject: [PATCH] Get GPUI compiling with some todos --- crates/gpui/src/window.rs | 19 +++++++------------ crates/gpui/src/window/element_cx.rs | 28 ++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 309e2622cb..b014f8eaed 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -1143,12 +1143,12 @@ impl<'a> WindowContext<'a> { return; }; + let mut mouse_listeners = mem::take(&mut self.window.rendered_frame.mouse_listeners); + // Capture phase, events bubble from back to front. Handlers for this phase are used for // special purposes, such as detecting events outside of a given Bounds. - for listener in &mut self.window.rendered_frame.mouse_listeners { - self.with_element_context(|cx| { - handler(event, DispatchPhase::Capture, cx); - }); + for listener in &mut mouse_listeners { + listener.dispatch(event, DispatchPhase::Capture, &mouse_occlusion, self); if !self.app.propagate_event { break; } @@ -1156,20 +1156,15 @@ impl<'a> WindowContext<'a> { // Bubble phase, where most normal handlers do their work. if self.app.propagate_event { - for (_, _, handler) in handlers.iter_mut().rev() { - self.with_element_context(|cx| { - handler(event, DispatchPhase::Bubble, cx); - }); + for listener in mouse_listeners.iter_mut().rev() { + listener.dispatch(event, DispatchPhase::Bubble, &mouse_occlusion, self); if !self.app.propagate_event { break; } } } - self.window - .rendered_frame - .mouse_listeners - .insert(event.type_id(), handlers); + self.window.rendered_frame.mouse_listeners = mouse_listeners; if self.app.propagate_event && self.has_active_drag() { if event.is::() { diff --git a/crates/gpui/src/window/element_cx.rs b/crates/gpui/src/window/element_cx.rs index 650af25317..123e06cee5 100644 --- a/crates/gpui/src/window/element_cx.rs +++ b/crates/gpui/src/window/element_cx.rs @@ -38,10 +38,26 @@ use crate::{ SUBPIXEL_VARIANTS, }; -struct MouseListener { - occlusion_id: OcclusionId, - invert_occlusion: bool, - callback: Box, +pub(crate) struct MouseListener { + pub(crate) occlusion_id: OcclusionId, + pub(crate) invert_occlusion: bool, + pub(crate) callback: Box, +} + +impl MouseListener { + pub(crate) fn dispatch( + &mut self, + event: &dyn Any, + phase: DispatchPhase, + mouse_occlusion: &Occlusion, + cx: &mut WindowContext, + ) { + if (self.occlusion_id == mouse_occlusion.id) != self.invert_occlusion { + cx.with_element_context(|cx| { + (self.callback)(event, phase, cx); + }); + } + } } pub(crate) struct RequestedInputHandler { @@ -54,7 +70,7 @@ pub(crate) struct TooltipRequest { pub(crate) tooltip: AnyTooltip, } -struct CursorStyleRequest { +pub(crate) struct CursorStyleRequest { pub(crate) occlusion_id: OcclusionId, pub(crate) style: CursorStyle, } @@ -64,7 +80,7 @@ pub(crate) struct OcclusionId(usize); /// Identifies an occlusion, see [ElementContext::insert_occlusion] for more details. #[derive(Clone, Deref)] -pub(crate) struct Occlusion { +pub struct Occlusion { pub(crate) id: OcclusionId, #[deref] pub(crate) bounds: Bounds,