Work with window handles instead of ids in drag code

This commit is contained in:
Nathan Sobo 2023-08-08 11:12:32 -06:00
parent d896d89842
commit da7dc9c880
5 changed files with 34 additions and 34 deletions

View File

@ -1,8 +1,8 @@
use collections::CommandPaletteFilter; use collections::CommandPaletteFilter;
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{ use gpui::{
actions, elements::*, keymap_matcher::Keystroke, Action, AppContext, Element, MouseState, actions, elements::*, keymap_matcher::Keystroke, Action, AnyWindowHandle, AppContext, Element,
ViewContext, MouseState, ViewContext,
}; };
use picker::{Picker, PickerDelegate, PickerEvent}; use picker::{Picker, PickerDelegate, PickerEvent};
use std::cmp; use std::cmp;
@ -28,7 +28,7 @@ pub struct CommandPaletteDelegate {
pub enum Event { pub enum Event {
Dismissed, Dismissed,
Confirmed { Confirmed {
window_id: usize, window: AnyWindowHandle,
focused_view_id: usize, focused_view_id: usize,
action: Box<dyn Action>, action: Box<dyn Action>,
}, },

View File

@ -6,7 +6,7 @@ use gpui::{
geometry::{rect::RectF, vector::Vector2F}, geometry::{rect::RectF, vector::Vector2F},
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
scene::{MouseDown, MouseDrag}, scene::{MouseDown, MouseDrag},
AnyElement, Element, View, ViewContext, WeakViewHandle, WindowContext, AnyElement, AnyWindowHandle, Element, View, ViewContext, WeakViewHandle, WindowContext,
}; };
const DEAD_ZONE: f32 = 4.; const DEAD_ZONE: f32 = 4.;
@ -21,7 +21,7 @@ enum State<V: View> {
region: RectF, region: RectF,
}, },
Dragging { Dragging {
window_id: usize, window: AnyWindowHandle,
position: Vector2F, position: Vector2F,
region_offset: Vector2F, region_offset: Vector2F,
region: RectF, region: RectF,
@ -49,14 +49,14 @@ impl<V: View> Clone for State<V> {
region, region,
}, },
State::Dragging { State::Dragging {
window_id, window,
position, position,
region_offset, region_offset,
region, region,
payload, payload,
render, render,
} => Self::Dragging { } => Self::Dragging {
window_id: window_id.clone(), window: window.clone(),
position: position.clone(), position: position.clone(),
region_offset: region_offset.clone(), region_offset: region_offset.clone(),
region: region.clone(), region: region.clone(),
@ -87,16 +87,16 @@ impl<V: View> DragAndDrop<V> {
self.containers.insert(handle); self.containers.insert(handle);
} }
pub fn currently_dragged<T: Any>(&self, window_id: usize) -> Option<(Vector2F, Rc<T>)> { pub fn currently_dragged<T: Any>(&self, window: AnyWindowHandle) -> Option<(Vector2F, Rc<T>)> {
self.currently_dragged.as_ref().and_then(|state| { self.currently_dragged.as_ref().and_then(|state| {
if let State::Dragging { if let State::Dragging {
position, position,
payload, payload,
window_id: window_dragged_from, window: window_dragged_from,
.. ..
} = state } = state
{ {
if &window_id != window_dragged_from { if &window != window_dragged_from {
return None; return None;
} }
@ -126,9 +126,9 @@ impl<V: View> DragAndDrop<V> {
cx: &mut WindowContext, cx: &mut WindowContext,
render: Rc<impl 'static + Fn(&T, &mut ViewContext<V>) -> AnyElement<V>>, render: Rc<impl 'static + Fn(&T, &mut ViewContext<V>) -> AnyElement<V>>,
) { ) {
let window_id = cx.window_id(); let window = cx.window();
cx.update_global(|this: &mut Self, cx| { cx.update_global(|this: &mut Self, cx| {
this.notify_containers_for_window(window_id, cx); this.notify_containers_for_window(window, cx);
match this.currently_dragged.as_ref() { match this.currently_dragged.as_ref() {
Some(&State::Down { Some(&State::Down {
@ -141,7 +141,7 @@ impl<V: View> DragAndDrop<V> {
}) => { }) => {
if (event.position - (region.origin() + region_offset)).length() > DEAD_ZONE { if (event.position - (region.origin() + region_offset)).length() > DEAD_ZONE {
this.currently_dragged = Some(State::Dragging { this.currently_dragged = Some(State::Dragging {
window_id, window,
region_offset, region_offset,
region, region,
position: event.position, position: event.position,
@ -163,7 +163,7 @@ impl<V: View> DragAndDrop<V> {
.. ..
}) => { }) => {
this.currently_dragged = Some(State::Dragging { this.currently_dragged = Some(State::Dragging {
window_id, window,
region_offset, region_offset,
region, region,
position: event.position, position: event.position,
@ -188,14 +188,14 @@ impl<V: View> DragAndDrop<V> {
State::Down { .. } => None, State::Down { .. } => None,
State::DeadZone { .. } => None, State::DeadZone { .. } => None,
State::Dragging { State::Dragging {
window_id, window,
region_offset, region_offset,
position, position,
region, region,
payload, payload,
render, render,
} => { } => {
if cx.window_id() != window_id { if cx.window() != window {
return None; return None;
} }
@ -260,27 +260,27 @@ impl<V: View> DragAndDrop<V> {
pub fn cancel_dragging<P: Any>(&mut self, cx: &mut WindowContext) { pub fn cancel_dragging<P: Any>(&mut self, cx: &mut WindowContext) {
if let Some(State::Dragging { if let Some(State::Dragging {
payload, window_id, .. payload, window, ..
}) = &self.currently_dragged }) = &self.currently_dragged
{ {
if payload.is::<P>() { if payload.is::<P>() {
let window_id = *window_id; let window = *window;
self.currently_dragged = Some(State::Canceled); self.currently_dragged = Some(State::Canceled);
self.notify_containers_for_window(window_id, cx); self.notify_containers_for_window(window, cx);
} }
} }
} }
fn finish_dragging(&mut self, cx: &mut WindowContext) { fn finish_dragging(&mut self, cx: &mut WindowContext) {
if let Some(State::Dragging { window_id, .. }) = self.currently_dragged.take() { if let Some(State::Dragging { window, .. }) = self.currently_dragged.take() {
self.notify_containers_for_window(window_id, cx); self.notify_containers_for_window(window, cx);
} }
} }
fn notify_containers_for_window(&mut self, window_id: usize, cx: &mut WindowContext) { fn notify_containers_for_window(&mut self, window: AnyWindowHandle, cx: &mut WindowContext) {
self.containers.retain(|container| { self.containers.retain(|container| {
if let Some(container) = container.upgrade(cx) { if let Some(container) = container.upgrade(cx) {
if container.window_id() == window_id { if container.window() == window {
container.update(cx, |_, cx| cx.notify()); container.update(cx, |_, cx| cx.notify());
} }
true true

View File

@ -1415,7 +1415,7 @@ impl ProjectPanel {
if cx if cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window())
.is_some() .is_some()
&& dragged_entry_destination && dragged_entry_destination
.as_ref() .as_ref()
@ -1459,7 +1459,7 @@ impl ProjectPanel {
.on_up(MouseButton::Left, move |_, this, cx| { .on_up(MouseButton::Left, move |_, this, cx| {
if let Some((_, dragged_entry)) = cx if let Some((_, dragged_entry)) = cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window())
{ {
this.move_entry( this.move_entry(
*dragged_entry, *dragged_entry,
@ -1472,7 +1472,7 @@ impl ProjectPanel {
.on_move(move |_, this, cx| { .on_move(move |_, this, cx| {
if cx if cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window())
.is_some() .is_some()
{ {
this.dragged_entry_destination = if matches!(kind, EntryKind::File(_)) { this.dragged_entry_destination = if matches!(kind, EntryKind::File(_)) {

View File

@ -48,7 +48,7 @@ impl TerminalPanel {
fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self { fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
let weak_self = cx.weak_handle(); let weak_self = cx.weak_handle();
let pane = cx.add_view(|cx| { let pane = cx.add_view(|cx| {
let window_id = cx.window_id(); let window = cx.window();
let mut pane = Pane::new( let mut pane = Pane::new(
workspace.weak_handle(), workspace.weak_handle(),
workspace.project().clone(), workspace.project().clone(),
@ -60,7 +60,7 @@ impl TerminalPanel {
pane.set_can_navigate(false, cx); pane.set_can_navigate(false, cx);
pane.on_can_drop(move |drag_and_drop, cx| { pane.on_can_drop(move |drag_and_drop, cx| {
drag_and_drop drag_and_drop
.currently_dragged::<DraggedItem>(window_id) .currently_dragged::<DraggedItem>(window)
.map_or(false, |(_, item)| { .map_or(false, |(_, item)| {
item.handle.act_as::<TerminalView>(cx).is_some() item.handle.act_as::<TerminalView>(cx).is_some()
}) })

View File

@ -28,11 +28,11 @@ where
let drag_and_drop = cx.global::<DragAndDrop<Workspace>>(); let drag_and_drop = cx.global::<DragAndDrop<Workspace>>();
let drag_position = if (pane.can_drop)(drag_and_drop, cx) { let drag_position = if (pane.can_drop)(drag_and_drop, cx) {
drag_and_drop drag_and_drop
.currently_dragged::<DraggedItem>(cx.window_id()) .currently_dragged::<DraggedItem>(cx.window())
.map(|(drag_position, _)| drag_position) .map(|(drag_position, _)| drag_position)
.or_else(|| { .or_else(|| {
drag_and_drop drag_and_drop
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window())
.map(|(drag_position, _)| drag_position) .map(|(drag_position, _)| drag_position)
}) })
} else { } else {
@ -91,10 +91,10 @@ where
let drag_and_drop = cx.global::<DragAndDrop<Workspace>>(); let drag_and_drop = cx.global::<DragAndDrop<Workspace>>();
if drag_and_drop if drag_and_drop
.currently_dragged::<DraggedItem>(cx.window_id()) .currently_dragged::<DraggedItem>(cx.window())
.is_some() .is_some()
|| drag_and_drop || drag_and_drop
.currently_dragged::<ProjectEntryId>(cx.window_id()) .currently_dragged::<ProjectEntryId>(cx.window())
.is_some() .is_some()
{ {
cx.notify(); cx.notify();
@ -122,11 +122,11 @@ pub fn handle_dropped_item<V: View>(
} }
let drag_and_drop = cx.global::<DragAndDrop<Workspace>>(); let drag_and_drop = cx.global::<DragAndDrop<Workspace>>();
let action = if let Some((_, dragged_item)) = let action = if let Some((_, dragged_item)) =
drag_and_drop.currently_dragged::<DraggedItem>(cx.window_id()) drag_and_drop.currently_dragged::<DraggedItem>(cx.window())
{ {
Action::Move(dragged_item.pane.clone(), dragged_item.handle.id()) Action::Move(dragged_item.pane.clone(), dragged_item.handle.id())
} else if let Some((_, project_entry)) = } else if let Some((_, project_entry)) =
drag_and_drop.currently_dragged::<ProjectEntryId>(cx.window_id()) drag_and_drop.currently_dragged::<ProjectEntryId>(cx.window())
{ {
Action::Open(*project_entry) Action::Open(*project_entry)
} else { } else {