From 429a2fc623bb1b56f9d67b2d48f20fad629b04b2 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Sun, 23 Jul 2023 13:28:30 -0700 Subject: [PATCH] Add drag end events Fix left dragging cascade WIP: Implement right dragging, WIP: use drag end events to set and reset state around initial flex orientation --- crates/gpui/src/app/window.rs | 20 ++++++++++++++++ crates/gpui/src/scene/mouse_event.rs | 1 + crates/workspace/src/pane_group.rs | 34 +++++++++++++++------------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 1dc88d2e71..e4beb58873 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -518,6 +518,18 @@ impl<'a> WindowContext<'a> { // NOTE: The order of event pushes is important! MouseUp events MUST be fired // before click events, and so the MouseUp events need to be pushed before // MouseClick events. + + // Synthesize one last drag event to end the drag + mouse_events.push(MouseEvent::Drag(MouseDrag { + region: Default::default(), + prev_mouse_position: self.window.mouse_position, + platform_event: MouseMovedEvent { + position: e.position, + pressed_button: Some(e.button), + modifiers: e.modifiers, + }, + end: true, + })); mouse_events.push(MouseEvent::Up(MouseUp { region: Default::default(), platform_event: e.clone(), @@ -565,8 +577,16 @@ impl<'a> WindowContext<'a> { region: Default::default(), prev_mouse_position: self.window.mouse_position, platform_event: e.clone(), + end: false, })); } else if let Some((_, clicked_button)) = self.window.clicked_region { + mouse_events.push(MouseEvent::Drag(MouseDrag { + region: Default::default(), + prev_mouse_position: self.window.mouse_position, + platform_event: e.clone(), + end: true, + })); + // Mouse up event happened outside the current window. Simulate mouse up button event let button_event = e.to_button_event(clicked_button); mouse_events.push(MouseEvent::Up(MouseUp { diff --git a/crates/gpui/src/scene/mouse_event.rs b/crates/gpui/src/scene/mouse_event.rs index a492da771b..21d3716aa6 100644 --- a/crates/gpui/src/scene/mouse_event.rs +++ b/crates/gpui/src/scene/mouse_event.rs @@ -32,6 +32,7 @@ pub struct MouseDrag { pub region: RectF, pub prev_mouse_position: Vector2F, pub platform_event: MouseMovedEvent, + pub end: bool } impl Deref for MouseDrag { diff --git a/crates/workspace/src/pane_group.rs b/crates/workspace/src/pane_group.rs index bed99d6cc4..05684229cc 100644 --- a/crates/workspace/src/pane_group.rs +++ b/crates/workspace/src/pane_group.rs @@ -587,7 +587,6 @@ mod element { use std::{cell::RefCell, ops::Range, rc::Rc}; use gpui::{ - elements::MouseEventHandler, geometry::{ rect::RectF, vector::{vec2f, Vector2F}, @@ -697,6 +696,10 @@ mod element { }; move |drag, workspace: &mut Workspace, cx| { + if drag.end { + dbg!("FINISHED"); + return; + } let min_size = match axis { Axis::Horizontal => HORIZONTAL_MIN_SIZE, Axis::Vertical => VERTICAL_MIN_SIZE, @@ -713,10 +716,10 @@ mod element { let mut proposed_current_pixel_change = current_target_size - size(ix, flexes.as_slice()); - let flex_changes = |pixel_dx, target_ix, flexes: &[f32]| { + let flex_changes = |pixel_dx, target_ix, next: isize, flexes: &[f32]| { let flex_change = pixel_dx / drag_bounds.length_along(axis); let current_target_flex = flexes[target_ix] + flex_change; - let next_target_flex = flexes[target_ix + 1] - flex_change; + let next_target_flex = flexes[(target_ix as isize + next) as usize] - flex_change; (current_target_flex, next_target_flex) }; @@ -725,7 +728,7 @@ mod element { let current_pixel_change = current_target_size - size(ix, flexes.as_slice()); let (current_target_flex, next_target_flex) = - flex_changes(current_pixel_change, ix, flexes.as_slice()); + flex_changes(current_pixel_change, ix, 1, flexes.as_slice()); flexes[ix] = current_target_flex; flexes[ix + 1] = next_target_flex; @@ -733,35 +736,31 @@ mod element { let mut ix_offset = 0; while proposed_current_pixel_change > 0.01 && ix + 1 + ix_offset < flexes.len() { + let current_ix = ix_offset + ix; let next_target_size = f32::max( - size(ix + 1, flexes.as_slice()) - proposed_current_pixel_change, + size(current_ix + 1, flexes.as_slice()) - proposed_current_pixel_change, min_size, ); current_target_size = f32::min( current_target_size, - size(ix, flexes.as_slice()) + size(ix + 1, flexes.as_slice()) + size(current_ix, flexes.as_slice()) + + size(current_ix + 1, flexes.as_slice()) - next_target_size, ); let current_pixel_change = - current_target_size - size(ix, flexes.as_slice()); + current_target_size - size(current_ix, flexes.as_slice()); let (current_target_flex, next_target_flex) = - flex_changes(current_pixel_change, ix, flexes.as_slice()); + flex_changes(current_pixel_change, current_ix, 1, flexes.as_slice()); - flexes[ix_offset + ix] = current_target_flex; - flexes[ix_offset + ix + 1] = next_target_flex; + flexes[current_ix] = current_target_flex; + flexes[current_ix + 1] = next_target_flex; - dbg!( - current_pixel_change, - proposed_current_pixel_change, - proposed_current_pixel_change - current_pixel_change - ); proposed_current_pixel_change -= current_pixel_change; ix_offset += 1; } - dbg!("done"); } workspace.schedule_serialize(cx); @@ -924,6 +923,9 @@ mod element { visible_bounds.clone(), ), ) + .on_down(MouseButton::Left, |_, _: &mut Workspace, _| { + dbg!("INITIATE"); + }) .on_click(MouseButton::Left, { let flexes = self.flexes.clone(); move |e, v: &mut Workspace, cx| {