Add event based drag API to GPUI, continue binding mouse handlers to terminal
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use crate::{
|
||||
point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
|
||||
BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle,
|
||||
IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
|
||||
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, Render, ScrollWheelEvent,
|
||||
SharedString, Size, StackingOrder, Style, StyleRefinement, Styled, Task, View, Visibility,
|
||||
WindowContext,
|
||||
point, px, Action, AnyDrag, AnyDragState, AnyElement, AnyTooltip, AnyView, AppContext,
|
||||
BorrowAppContext, BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId,
|
||||
FocusEvent, FocusHandle, IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
|
||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point,
|
||||
Render, ScrollWheelEvent, SharedString, Size, StackingOrder, Style, StyleRefinement, Styled,
|
||||
Task, View, Visibility, WindowContext,
|
||||
};
|
||||
use collections::HashMap;
|
||||
use refineable::Refineable;
|
||||
@@ -415,6 +415,19 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
self
|
||||
}
|
||||
|
||||
fn on_drag_event(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
|
||||
) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.interactivity()
|
||||
.drag_event_listeners
|
||||
.push(Box::new(listener));
|
||||
self
|
||||
}
|
||||
|
||||
fn on_hover(mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
@@ -559,6 +572,8 @@ pub type KeyDownListener = Box<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowC
|
||||
|
||||
pub type KeyUpListener = Box<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + 'static>;
|
||||
|
||||
pub type DragEventListener = Box<dyn Fn(&MouseMoveEvent, &mut WindowContext) + 'static>;
|
||||
|
||||
pub type ActionListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + 'static>;
|
||||
|
||||
pub fn div() -> Div {
|
||||
@@ -746,6 +761,7 @@ pub struct Interactivity {
|
||||
pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>,
|
||||
pub drop_listeners: SmallVec<[(TypeId, Box<DropListener>); 2]>,
|
||||
pub click_listeners: SmallVec<[ClickListener; 2]>,
|
||||
pub drag_event_listeners: SmallVec<[DragEventListener; 1]>,
|
||||
pub drag_listener: Option<DragListener>,
|
||||
pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>,
|
||||
pub tooltip_builder: Option<TooltipBuilder>,
|
||||
@@ -890,8 +906,11 @@ impl Interactivity {
|
||||
if phase == DispatchPhase::Bubble
|
||||
&& interactive_bounds.visibly_contains(&event.position, &cx)
|
||||
{
|
||||
if let Some(drag_state_type) =
|
||||
cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
|
||||
if let Some(drag_state_type) = cx
|
||||
.active_drag
|
||||
.as_ref()
|
||||
.and_then(|drag| drag.any_drag())
|
||||
.map(|drag| drag.view.entity_type())
|
||||
{
|
||||
for (drop_state_type, listener) in &drop_listeners {
|
||||
if *drop_state_type == drag_state_type {
|
||||
@@ -899,11 +918,14 @@ impl Interactivity {
|
||||
.active_drag
|
||||
.take()
|
||||
.expect("checked for type drag state type above");
|
||||
let drag = drag.any_drag().expect("checked for any drag above");
|
||||
listener(drag.view.clone(), cx);
|
||||
cx.notify();
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cx.active_drag = None;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -911,12 +933,16 @@ impl Interactivity {
|
||||
|
||||
let click_listeners = mem::take(&mut self.click_listeners);
|
||||
let drag_listener = mem::take(&mut self.drag_listener);
|
||||
let drag_event_listeners = mem::take(&mut self.drag_event_listeners);
|
||||
|
||||
if !click_listeners.is_empty() || drag_listener.is_some() {
|
||||
if !click_listeners.is_empty()
|
||||
|| drag_listener.is_some()
|
||||
|| !drag_event_listeners.is_empty()
|
||||
{
|
||||
let pending_mouse_down = element_state.pending_mouse_down.clone();
|
||||
let mouse_down = pending_mouse_down.borrow().clone();
|
||||
if let Some(mouse_down) = mouse_down {
|
||||
if let Some(drag_listener) = drag_listener {
|
||||
if !drag_event_listeners.is_empty() || drag_listener.is_some() {
|
||||
let active_state = element_state.clicked_state.clone();
|
||||
let interactive_bounds = interactive_bounds.clone();
|
||||
|
||||
@@ -924,17 +950,29 @@ impl Interactivity {
|
||||
if cx.active_drag.is_some() {
|
||||
if phase == DispatchPhase::Capture {
|
||||
cx.notify();
|
||||
} else if interactive_bounds.visibly_contains(&event.position, cx)
|
||||
&& (event.position - mouse_down.position).magnitude()
|
||||
> DRAG_THRESHOLD
|
||||
{
|
||||
for listener in &drag_event_listeners {
|
||||
listener(event, cx);
|
||||
}
|
||||
}
|
||||
} else if phase == DispatchPhase::Bubble
|
||||
&& interactive_bounds.visibly_contains(&event.position, cx)
|
||||
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
|
||||
{
|
||||
*active_state.borrow_mut() = ElementClickedState::default();
|
||||
let cursor_offset = event.position - bounds.origin;
|
||||
let drag = drag_listener(cursor_offset, cx);
|
||||
cx.active_drag = Some(drag);
|
||||
cx.notify();
|
||||
cx.stop_propagation();
|
||||
if let Some(drag_listener) = &drag_listener {
|
||||
let cursor_offset = event.position - bounds.origin;
|
||||
let drag = drag_listener(cursor_offset, cx);
|
||||
cx.active_drag = Some(AnyDragState::AnyDrag(drag));
|
||||
cx.notify();
|
||||
cx.stop_propagation();
|
||||
}
|
||||
for listener in &drag_event_listeners {
|
||||
listener(event, cx);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1197,7 +1235,7 @@ impl Interactivity {
|
||||
if let Some(drag) = cx.active_drag.take() {
|
||||
for (state_type, group_drag_style) in &self.group_drag_over_styles {
|
||||
if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
|
||||
if *state_type == drag.view.entity_type()
|
||||
if Some(*state_type) == drag.entity_type()
|
||||
&& group_bounds.contains_point(&mouse_position)
|
||||
{
|
||||
style.refine(&group_drag_style.style);
|
||||
@@ -1206,7 +1244,7 @@ impl Interactivity {
|
||||
}
|
||||
|
||||
for (state_type, drag_over_style) in &self.drag_over_styles {
|
||||
if *state_type == drag.view.entity_type()
|
||||
if Some(*state_type) == drag.entity_type()
|
||||
&& bounds
|
||||
.intersect(&cx.content_mask().bounds)
|
||||
.contains_point(&mouse_position)
|
||||
@@ -1263,6 +1301,7 @@ impl Default for Interactivity {
|
||||
action_listeners: SmallVec::new(),
|
||||
drop_listeners: SmallVec::new(),
|
||||
click_listeners: SmallVec::new(),
|
||||
drag_event_listeners: SmallVec::new(),
|
||||
drag_listener: None,
|
||||
hover_listener: None,
|
||||
tooltip_builder: None,
|
||||
|
||||
Reference in New Issue
Block a user