mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-03 17:44:30 +00:00
Add deadzones to drag and drop
This commit is contained in:
parent
1920de81d9
commit
36bc90b2b8
1 changed files with 41 additions and 3 deletions
|
@ -9,11 +9,17 @@ use gpui::{
|
||||||
View, WeakViewHandle,
|
View, WeakViewHandle,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const DEAD_ZONE: f32 = 4.;
|
||||||
|
|
||||||
enum State<V: View> {
|
enum State<V: View> {
|
||||||
Down {
|
Down {
|
||||||
region_offset: Vector2F,
|
region_offset: Vector2F,
|
||||||
region: RectF,
|
region: RectF,
|
||||||
},
|
},
|
||||||
|
DeadZone {
|
||||||
|
region_offset: Vector2F,
|
||||||
|
region: RectF,
|
||||||
|
},
|
||||||
Dragging {
|
Dragging {
|
||||||
window_id: usize,
|
window_id: usize,
|
||||||
position: Vector2F,
|
position: Vector2F,
|
||||||
|
@ -35,6 +41,13 @@ impl<V: View> Clone for State<V> {
|
||||||
region_offset,
|
region_offset,
|
||||||
region,
|
region,
|
||||||
},
|
},
|
||||||
|
&State::DeadZone {
|
||||||
|
region_offset,
|
||||||
|
region,
|
||||||
|
} => State::DeadZone {
|
||||||
|
region_offset,
|
||||||
|
region,
|
||||||
|
},
|
||||||
State::Dragging {
|
State::Dragging {
|
||||||
window_id,
|
window_id,
|
||||||
position,
|
position,
|
||||||
|
@ -101,7 +114,7 @@ impl<V: View> DragAndDrop<V> {
|
||||||
pub fn drag_started(event: MouseDown, cx: &mut EventContext) {
|
pub fn drag_started(event: MouseDown, cx: &mut EventContext) {
|
||||||
cx.update_global(|this: &mut Self, _| {
|
cx.update_global(|this: &mut Self, _| {
|
||||||
this.currently_dragged = Some(State::Down {
|
this.currently_dragged = Some(State::Down {
|
||||||
region_offset: event.region.origin() - event.position,
|
region_offset: event.position - event.region.origin(),
|
||||||
region: event.region,
|
region: event.region,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -122,7 +135,31 @@ impl<V: View> DragAndDrop<V> {
|
||||||
region_offset,
|
region_offset,
|
||||||
region,
|
region,
|
||||||
})
|
})
|
||||||
| Some(&State::Dragging {
|
| Some(&State::DeadZone {
|
||||||
|
region_offset,
|
||||||
|
region,
|
||||||
|
}) => {
|
||||||
|
if (dbg!(event.position) - (dbg!(region.origin() + region_offset))).length()
|
||||||
|
> DEAD_ZONE
|
||||||
|
{
|
||||||
|
this.currently_dragged = Some(State::Dragging {
|
||||||
|
window_id,
|
||||||
|
region_offset,
|
||||||
|
region,
|
||||||
|
position: event.position,
|
||||||
|
payload,
|
||||||
|
render: Rc::new(move |payload, cx| {
|
||||||
|
render(payload.downcast_ref::<T>().unwrap(), cx)
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.currently_dragged = Some(State::DeadZone {
|
||||||
|
region_offset,
|
||||||
|
region,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(&State::Dragging {
|
||||||
region_offset,
|
region_offset,
|
||||||
region,
|
region,
|
||||||
..
|
..
|
||||||
|
@ -151,6 +188,7 @@ impl<V: View> DragAndDrop<V> {
|
||||||
.and_then(|state| {
|
.and_then(|state| {
|
||||||
match state {
|
match state {
|
||||||
State::Down { .. } => None,
|
State::Down { .. } => None,
|
||||||
|
State::DeadZone { .. } => None,
|
||||||
State::Dragging {
|
State::Dragging {
|
||||||
window_id,
|
window_id,
|
||||||
region_offset,
|
region_offset,
|
||||||
|
@ -163,7 +201,7 @@ impl<V: View> DragAndDrop<V> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let position = position + region_offset;
|
let position = position - region_offset;
|
||||||
Some(
|
Some(
|
||||||
Overlay::new(
|
Overlay::new(
|
||||||
MouseEventHandler::<DraggedElementHandler>::new(0, cx, |_, cx| {
|
MouseEventHandler::<DraggedElementHandler>::new(0, cx, |_, cx| {
|
||||||
|
|
Loading…
Reference in a new issue