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

View File

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

View File

@ -1415,7 +1415,7 @@ impl ProjectPanel {
if cx
.global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id())
.currently_dragged::<ProjectEntryId>(cx.window())
.is_some()
&& dragged_entry_destination
.as_ref()
@ -1459,7 +1459,7 @@ impl ProjectPanel {
.on_up(MouseButton::Left, move |_, this, cx| {
if let Some((_, dragged_entry)) = cx
.global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id())
.currently_dragged::<ProjectEntryId>(cx.window())
{
this.move_entry(
*dragged_entry,
@ -1472,7 +1472,7 @@ impl ProjectPanel {
.on_move(move |_, this, cx| {
if cx
.global::<DragAndDrop<Workspace>>()
.currently_dragged::<ProjectEntryId>(cx.window_id())
.currently_dragged::<ProjectEntryId>(cx.window())
.is_some()
{
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 {
let weak_self = cx.weak_handle();
let pane = cx.add_view(|cx| {
let window_id = cx.window_id();
let window = cx.window();
let mut pane = Pane::new(
workspace.weak_handle(),
workspace.project().clone(),
@ -60,7 +60,7 @@ impl TerminalPanel {
pane.set_can_navigate(false, cx);
pane.on_can_drop(move |drag_and_drop, cx| {
drag_and_drop
.currently_dragged::<DraggedItem>(window_id)
.currently_dragged::<DraggedItem>(window)
.map_or(false, |(_, item)| {
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_position = if (pane.can_drop)(drag_and_drop, cx) {
drag_and_drop
.currently_dragged::<DraggedItem>(cx.window_id())
.currently_dragged::<DraggedItem>(cx.window())
.map(|(drag_position, _)| drag_position)
.or_else(|| {
drag_and_drop
.currently_dragged::<ProjectEntryId>(cx.window_id())
.currently_dragged::<ProjectEntryId>(cx.window())
.map(|(drag_position, _)| drag_position)
})
} else {
@ -91,10 +91,10 @@ where
let drag_and_drop = cx.global::<DragAndDrop<Workspace>>();
if drag_and_drop
.currently_dragged::<DraggedItem>(cx.window_id())
.currently_dragged::<DraggedItem>(cx.window())
.is_some()
|| drag_and_drop
.currently_dragged::<ProjectEntryId>(cx.window_id())
.currently_dragged::<ProjectEntryId>(cx.window())
.is_some()
{
cx.notify();
@ -122,11 +122,11 @@ pub fn handle_dropped_item<V: View>(
}
let drag_and_drop = cx.global::<DragAndDrop<Workspace>>();
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())
} 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)
} else {