From 0ee61e214de67b53216ffd2b9a4022883220188a Mon Sep 17 00:00:00 2001 From: Mikayla Date: Fri, 8 Dec 2023 17:02:25 -0800 Subject: [PATCH] Fix drag events --- crates/workspace2/src/pane_group.rs | 40 +++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/workspace2/src/pane_group.rs b/crates/workspace2/src/pane_group.rs index 4865a40a57..9dc4e84c00 100644 --- a/crates/workspace2/src/pane_group.rs +++ b/crates/workspace2/src/pane_group.rs @@ -696,11 +696,11 @@ impl SplitDirection { mod element { - use std::{iter, sync::Arc}; + use std::{cell::RefCell, iter, rc::Rc, sync::Arc}; use gpui::{ px, relative, Along, AnyElement, Axis, Bounds, CursorStyle, Element, IntoElement, - MouseMoveEvent, ParentElement, Pixels, Style, WindowContext, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Style, WindowContext, }; use parking_lot::Mutex; use smallvec::SmallVec; @@ -789,13 +789,11 @@ mod element { } }); - while dbg!(proposed_current_pixel_change).abs() > px(0.) { + while proposed_current_pixel_change.abs() > px(0.) { let Some(current_ix) = successors.next() else { break; }; - dbg!(current_ix); - let next_target_size = Pixels::max( size(current_ix + 1, flexes.as_slice()) - proposed_current_pixel_change, min_size, @@ -826,6 +824,7 @@ mod element { fn push_handle( flexes: Arc>>, + dragged_handle: Rc>>, axis: Axis, ix: usize, pane_bounds: Bounds, @@ -859,8 +858,17 @@ mod element { gpui::red(), ); + cx.on_mouse_event({ + let dragged_handle = dragged_handle.clone(); + move |e: &MouseDownEvent, phase, cx| { + if phase.bubble() && handle_bounds.contains(&e.position) { + dragged_handle.replace(Some(ix)); + } + } + }); cx.on_mouse_event(move |e: &MouseMoveEvent, phase, cx| { - if phase.bubble() && e.dragging() && handle_bounds.contains(&e.position) { + let dragged_handle = dragged_handle.borrow(); + if *dragged_handle == Some(ix) { Self::compute_resize(&flexes, e, ix, axis, axis_bounds, cx) } }); @@ -872,7 +880,7 @@ mod element { type Element = Self; fn element_id(&self) -> Option { - Some("pane axis".into()) + Some(self.basis.into()) } fn into_element(self) -> Self::Element { @@ -881,7 +889,7 @@ mod element { } impl Element for PaneAxisElement { - type State = (); + type State = Rc>>; fn layout( &mut self, @@ -892,8 +900,8 @@ mod element { style.size.width = relative(1.).into(); style.size.height = relative(1.).into(); let layout_id = cx.request_layout(&style, None); - - (layout_id, ()) + let dragged_pane = state.unwrap_or_else(|| Rc::new(RefCell::new(None))); + (layout_id, dragged_pane) } fn paint( @@ -934,6 +942,7 @@ mod element { if ix < len - 1 { Self::push_handle( self.flexes.clone(), + state.clone(), self.axis, ix, child_bounds, @@ -945,6 +954,17 @@ mod element { origin = origin.apply_along(self.axis, |val| val + child_size.along(self.axis)); } + + cx.with_z_index(1, |cx| { + cx.on_mouse_event({ + let state = state.clone(); + move |e: &MouseUpEvent, phase, cx| { + if phase.bubble() { + state.replace(None); + } + } + }); + }) } }