Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Fix clear drag overlay when external drag ends outside window (#45759)
Xiaobo Liu693962917b5a
diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rsindex 4a61069a0a9..ebbce8f84c9 100644--- a/crates/gpui/src/elements/div.rs+++ b/crates/gpui/src/elements/div.rs@@ -21,10 +21,10 @@ use crate::{Display, Element, ElementId, Entity, EntityId, FocusHandle, Global, GlobalElementId, Hitbox,HitboxBehavior, HitboxId, InspectorElementId, IntoElement, IsZero, KeyContext, KeyDownEvent,KeyUpEvent, KeyboardButton, KeyboardClickEvent, LayoutId, ModifiersChangedEvent, MouseButton,- MouseClickEvent, MouseDownEvent, MouseMoveEvent, MousePressureEvent, MouseUpEvent, Overflow,- ParentElement, Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style,- StyleRefinement, Styled, Task, TooltipId, Visibility, Window, WindowControlArea, point, px,- size,+ MouseClickEvent, MouseDownEvent, MouseExitEvent, MouseMoveEvent, MousePressureEvent,+ MouseUpEvent, Overflow, ParentElement, Pixels, Point, Render, ScrollWheelEvent, SharedString,+ Size, Style, StyleRefinement, Styled, Task, TooltipId, Visibility, Window, WindowControlArea,+ point, px, size,};use collections::HashMap;use gpui_util::ResultExt;@@ -305,6 +305,22 @@ impl Interactivity {}));}+ /// Bind the given callback to the mouse exit event, during the bubble phase.+ /// The imperative API equivalent to [`InteractiveElement::on_mouse_exit`].+ ///+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.+ pub fn on_mouse_exit(+ &mut self,+ listener: impl Fn(&MouseExitEvent, &mut Window, &mut App) + 'static,+ ) {+ self.mouse_exit_listeners+ .push(Box::new(move |event, phase, hitbox, window, cx| {+ if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {+ (listener)(event, window, cx);+ }+ }));+ }+/// Bind the given callback to the mouse drag event of the given type. Note that this/// will be called for all move events, inside or outside of this element, as long as the/// drag was started with this element under the mouse. Useful for implementing draggable@@ -919,6 +935,18 @@ pub trait InteractiveElement: Sized {self}+ /// Bind the given callback to the mouse exit event, during the bubble phase.+ /// The fluent API equivalent to [`Interactivity::on_mouse_exit`].+ ///+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.+ fn on_mouse_exit(+ mut self,+ listener: impl Fn(&MouseExitEvent, &mut Window, &mut App) + 'static,+ ) -> Self {+ self.interactivity().on_mouse_exit(listener);+ self+ }+/// Bind the given callback to the mouse drag event of the given type. Note that this/// will be called for all move events, inside or outside of this element, as long as the/// drag was started with this element under the mouse. Useful for implementing draggable@@ -1525,6 +1553,8 @@ pub(crate) type MousePressureListener =Box<dyn Fn(&MousePressureEvent, DispatchPhase, &Hitbox, &mut Window, &mut App) + 'static>;pub(crate) type MouseMoveListener =Box<dyn Fn(&MouseMoveEvent, DispatchPhase, &Hitbox, &mut Window, &mut App) + 'static>;+pub(crate) type MouseExitListener =+ Box<dyn Fn(&MouseExitEvent, DispatchPhase, &Hitbox, &mut Window, &mut App) + 'static>;pub(crate) type ScrollWheelListener =Box<dyn Fn(&ScrollWheelEvent, DispatchPhase, &Hitbox, &mut Window, &mut App) + 'static>;@@ -1907,6 +1937,7 @@ pub struct Interactivity {pub(crate) mouse_up_listeners: Vec<MouseUpListener>,pub(crate) mouse_pressure_listeners: Vec<MousePressureListener>,pub(crate) mouse_move_listeners: Vec<MouseMoveListener>,+ pub(crate) mouse_exit_listeners: Vec<MouseExitListener>,pub(crate) scroll_wheel_listeners: Vec<ScrollWheelListener>,pub(crate) pinch_listeners: Vec<PinchListener>,pub(crate) key_down_listeners: Vec<KeyDownListener>,@@ -2152,6 +2183,7 @@ impl Interactivity {|| !self.mouse_pressure_listeners.is_empty()|| !self.mouse_down_listeners.is_empty()|| !self.mouse_move_listeners.is_empty()+ || !self.mouse_exit_listeners.is_empty()|| !self.click_listeners.is_empty()|| !self.aux_click_listeners.is_empty()|| !self.scroll_wheel_listeners.is_empty()@@ -2531,6 +2563,13 @@ impl Interactivity {})}+ for listener in self.mouse_exit_listeners.drain(..) {+ let hitbox = hitbox.clone();+ window.on_mouse_event(move |event: &MouseExitEvent, phase, window, cx| {+ listener(event, phase, &hitbox, window, cx);+ })+ }+for listener in self.scroll_wheel_listeners.drain(..) {let hitbox = hitbox.clone();window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rsindex bf3b33efc72..4dfa07b6b2c 100644--- a/crates/project_panel/src/project_panel.rs+++ b/crates/project_panel/src/project_panel.rs@@ -24,10 +24,10 @@ use gpui::{ClipboardItem, Context, CursorStyle, DismissEvent, Div, DragMoveEvent, Entity, EventEmitter,ExternalPaths, FocusHandle, Focusable, FontWeight, Hsla, InteractiveElement, KeyContext,ListHorizontalSizingBehavior, ListSizingBehavior, Modifiers, ModifiersChangedEvent,- MouseButton, MouseDownEvent, ParentElement, PathPromptOptions, Pixels, Point, PromptLevel,- Render, ScrollStrategy, Stateful, Styled, Subscription, Task, UniformListScrollHandle,- WeakEntity, Window, actions, anchored, deferred, div, hsla, linear_color_stop, linear_gradient,- point, px, size, transparent_white, uniform_list,+ MouseButton, MouseDownEvent, MouseExitEvent, ParentElement, PathPromptOptions, Pixels, Point,+ PromptLevel, Render, ScrollStrategy, Stateful, Styled, Subscription, Task,+ UniformListScrollHandle, WeakEntity, Window, actions, anchored, deferred, div, hsla,+ linear_color_stop, linear_gradient, point, px, size, transparent_white, uniform_list,};use language::DiagnosticSeverity;use markdown_preview::markdown_preview_view::MarkdownPreviewView;@@ -2128,8 +2128,7 @@ impl ProjectPanel {fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {if cx.stop_active_drag(window) {- self.drag_target_entry.take();- self.hover_expand_task.take();+ self.clear_drag_state(cx);return;}self.marked_entries.clear();@@ -4713,6 +4712,17 @@ impl ProjectPanel {}}+ fn clear_drag_state(&mut self, cx: &mut Context<Self>) {+ let had_drag_state = self.drag_target_entry.take().is_some()+ | self.folded_directory_drag_target.take().is_some()+ | self.hover_scroll_task.take().is_some()+ | self.hover_expand_task.take().is_some()+ | self.previous_drag_position.take().is_some();+ if had_drag_state {+ cx.notify();+ }+ }+fn is_copy_modifier_set(modifiers: &Modifiers) -> bool {cfg!(target_os = "macos") && modifiers.alt|| cfg!(not(target_os = "macos")) && modifiers.control@@ -5756,8 +5766,7 @@ impl ProjectPanel {)).on_drop(cx.listener(move |this, external_paths: &ExternalPaths, window, cx| {- this.drag_target_entry = None;- this.hover_scroll_task.take();+ this.clear_drag_state(cx);this.drop_external_files(external_paths.paths(), entry_id, window, cx);cx.stop_propagation();},@@ -5886,9 +5895,7 @@ impl ProjectPanel {}).on_drop(cx.listener(move |this, selections: &DraggedSelection, window, cx| {- this.drag_target_entry = None;- this.hover_scroll_task.take();- this.hover_expand_task.take();+ this.clear_drag_state(cx);if folded_directory_drag_target.is_some() {return;}@@ -6281,9 +6288,7 @@ impl ProjectPanel {)).on_drop(cx.listener(move |this, selections: &DraggedSelection, window, cx| {- this.hover_scroll_task.take();- this.drag_target_entry = None;- this.folded_directory_drag_target = None;+ this.clear_drag_state(cx);if let Some(target_entry_id) = target_entry_id {this.drag_onto(selections,@@ -6380,9 +6385,7 @@ impl ProjectPanel {div.when(drag_and_drop_enabled, |div| {div.on_drop(cx.listener(move |this, selections: &DraggedSelection, window, cx| {- this.hover_scroll_task.take();- this.drag_target_entry = None;- this.folded_directory_drag_target = None;+ this.clear_drag_state(cx);if let Some(target_entry_id) = target_entry_id {this.drag_onto(selections, target_entry_id, is_file, window, cx);}@@ -6855,7 +6858,7 @@ impl Render for ProjectPanel {this.previous_drag_position = Some(e.event.position);if !e.bounds.contains(&e.event.position) {- this.drag_target_entry = None;+ this.clear_drag_state(cx);return;}this.hover_scroll_task.take();@@ -6912,6 +6915,10 @@ impl Render for ProjectPanel {.when(panel_settings.drag_and_drop, |this| {this.on_drag_move(cx.listener(handle_drag_move::<ExternalPaths>)).on_drag_move(cx.listener(handle_drag_move::<DraggedSelection>))+ .on_mouse_exit(cx.listener(|this, _: &MouseExitEvent, window, cx| {+ cx.stop_active_drag(window);+ this.clear_drag_state(cx);+ }))}).size_full().relative()@@ -7298,8 +7305,7 @@ impl Render for ProjectPanel {)).on_drop(cx.listener(move |this, external_paths: &ExternalPaths, window, cx| {- this.drag_target_entry = None;- this.hover_scroll_task.take();+ this.clear_drag_state(cx);if let Some(entry_id) = this.state.last_worktree_root_id {this.drop_external_files(external_paths.paths(),@@ -7313,8 +7319,7 @@ impl Render for ProjectPanel {)).on_drop(cx.listener(move |this, selections: &DraggedSelection, window, cx| {- this.drag_target_entry = None;- this.hover_scroll_task.take();+ this.clear_drag_state(cx);if let Some(entry_id) = this.state.last_worktree_root_id {this.drag_onto(selections, entry_id, false, window, cx);}@@ -7439,8 +7444,7 @@ impl Render for ProjectPanel {}).on_drop(cx.listener(move |this, external_paths: &ExternalPaths, window, cx| {- this.drag_target_entry = None;- this.hover_scroll_task.take();+ this.clear_drag_state(cx);if let Some(task) = this.workspace.update(cx, |workspace, cx| {