diff --git a/crates/gpui3/src/elements/div2.rs b/crates/gpui3/src/elements/div2.rs index 6a624b80e2883645a99e76d129b0f6ac92240157..f198711c16f0713d00af9aa370a9c0832d7e8a64 100644 --- a/crates/gpui3/src/elements/div2.rs +++ b/crates/gpui3/src/elements/div2.rs @@ -12,6 +12,7 @@ use std::sync::Arc; #[derive(Default)] pub struct DivState { active_state: Arc>, + pending_click: Arc>>, } #[derive(Copy, Clone, Default, Eq, PartialEq)] @@ -147,30 +148,61 @@ where } }); } + } - // for listener in self.listeners.mouse_down.iter().cloned() { - // cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| { - // listener(state, event, &bounds, phase, cx); - // }) - // } - - // for listener in self.listeners.mouse_up.iter().cloned() { - // cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { - // listener(state, event, &bounds, phase, cx); - // }) - // } - - // for listener in self.listeners.mouse_move.iter().cloned() { - // cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| { - // listener(state, event, &bounds, phase, cx); - // }) - // } - - // for listener in self.listeners.scroll_wheel.iter().cloned() { - // cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| { - // listener(state, event, &bounds, phase, cx); - // }) - // } + fn paint_event_listeners( + &self, + bounds: Bounds, + pending_click: Arc>>, + cx: &mut ViewContext, + ) { + let click_listeners = self.listeners.mouse_click.clone(); + let mouse_down = pending_click.lock().clone(); + if let Some(mouse_down) = mouse_down { + cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { + if phase == DispatchPhase::Bubble && bounds.contains_point(event.position) { + let mouse_click = MouseClickEvent { + down: mouse_down.clone(), + up: event.clone(), + }; + for listener in &click_listeners { + listener(state, &mouse_click, &bounds, cx); + } + } + + *pending_click.lock() = None; + }); + } else { + cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| { + if phase == DispatchPhase::Bubble && bounds.contains_point(event.position) { + *pending_click.lock() = Some(event.clone()); + } + }); + } + + for listener in self.listeners.mouse_down.iter().cloned() { + cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| { + listener(state, event, &bounds, phase, cx); + }) + } + + for listener in self.listeners.mouse_up.iter().cloned() { + cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { + listener(state, event, &bounds, phase, cx); + }) + } + + for listener in self.listeners.mouse_move.iter().cloned() { + cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| { + listener(state, event, &bounds, phase, cx); + }) + } + + for listener in self.listeners.scroll_wheel.iter().cloned() { + cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| { + listener(state, event, &bounds, phase, cx); + }) + } } } @@ -264,6 +296,7 @@ where element_state.active_state.clone(), cx, ); + this.paint_event_listeners(bounds, element_state.pending_click.clone(), cx); }); });