Tear out drag event listener
This commit is contained in:
parent
c092cfbfb3
commit
22cd62213b
3 changed files with 26 additions and 91 deletions
|
@ -188,7 +188,7 @@ pub struct AppContext {
|
||||||
flushing_effects: bool,
|
flushing_effects: bool,
|
||||||
pending_updates: usize,
|
pending_updates: usize,
|
||||||
pub(crate) actions: Rc<ActionRegistry>,
|
pub(crate) actions: Rc<ActionRegistry>,
|
||||||
pub(crate) active_drag: Option<AnyDragState>,
|
pub(crate) active_drag: Option<AnyDrag>,
|
||||||
pub(crate) active_tooltip: Option<AnyTooltip>,
|
pub(crate) active_tooltip: Option<AnyTooltip>,
|
||||||
pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>,
|
pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>,
|
||||||
pub(crate) frame_consumers: HashMap<DisplayId, Task<()>>,
|
pub(crate) frame_consumers: HashMap<DisplayId, Task<()>>,
|
||||||
|
@ -1264,34 +1264,6 @@ pub struct AnyDrag {
|
||||||
pub cursor_offset: Point<Pixels>,
|
pub cursor_offset: Point<Pixels>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum AnyDragState {
|
|
||||||
EventListener,
|
|
||||||
AnyDrag(AnyDrag),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AnyDragState {
|
|
||||||
pub fn any_drag(&self) -> Option<&AnyDrag> {
|
|
||||||
match self {
|
|
||||||
AnyDragState::EventListener => None,
|
|
||||||
AnyDragState::AnyDrag(any_drag) => Some(any_drag),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn entity_id(&self) -> Option<EntityId> {
|
|
||||||
match self {
|
|
||||||
AnyDragState::EventListener => None,
|
|
||||||
AnyDragState::AnyDrag(any_drag) => Some(any_drag.view.entity_id()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn entity_type(&self) -> Option<TypeId> {
|
|
||||||
match self {
|
|
||||||
AnyDragState::EventListener => None,
|
|
||||||
AnyDragState::AnyDrag(any_drag) => Some(any_drag.view.entity_type()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct AnyTooltip {
|
pub(crate) struct AnyTooltip {
|
||||||
pub view: AnyView,
|
pub view: AnyView,
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
point, px, Action, AnyDrag, AnyDragState, AnyElement, AnyTooltip, AnyView, AppContext,
|
point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
|
||||||
BorrowAppContext, BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId,
|
BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle,
|
||||||
FocusEvent, FocusHandle, IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
|
IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
|
||||||
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point,
|
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, Render, ScrollWheelEvent,
|
||||||
Render, ScrollWheelEvent, SharedString, Size, StackingOrder, Style, StyleRefinement, Styled,
|
SharedString, Size, StackingOrder, Style, StyleRefinement, Styled, Task, View, Visibility,
|
||||||
Task, View, Visibility, WindowContext,
|
WindowContext,
|
||||||
};
|
};
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use refineable::Refineable;
|
use refineable::Refineable;
|
||||||
|
@ -415,19 +415,6 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||||
self
|
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
|
fn on_hover(mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static) -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
|
@ -761,7 +748,6 @@ pub struct Interactivity {
|
||||||
pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>,
|
pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>,
|
||||||
pub drop_listeners: SmallVec<[(TypeId, Box<DropListener>); 2]>,
|
pub drop_listeners: SmallVec<[(TypeId, Box<DropListener>); 2]>,
|
||||||
pub click_listeners: SmallVec<[ClickListener; 2]>,
|
pub click_listeners: SmallVec<[ClickListener; 2]>,
|
||||||
pub drag_event_listeners: SmallVec<[DragEventListener; 1]>,
|
|
||||||
pub drag_listener: Option<DragListener>,
|
pub drag_listener: Option<DragListener>,
|
||||||
pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>,
|
pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>,
|
||||||
pub tooltip_builder: Option<TooltipBuilder>,
|
pub tooltip_builder: Option<TooltipBuilder>,
|
||||||
|
@ -906,11 +892,8 @@ impl Interactivity {
|
||||||
if phase == DispatchPhase::Bubble
|
if phase == DispatchPhase::Bubble
|
||||||
&& interactive_bounds.visibly_contains(&event.position, &cx)
|
&& interactive_bounds.visibly_contains(&event.position, &cx)
|
||||||
{
|
{
|
||||||
if let Some(drag_state_type) = cx
|
if let Some(drag_state_type) =
|
||||||
.active_drag
|
cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
|
||||||
.as_ref()
|
|
||||||
.and_then(|drag| drag.any_drag())
|
|
||||||
.map(|drag| drag.view.entity_type())
|
|
||||||
{
|
{
|
||||||
for (drop_state_type, listener) in &drop_listeners {
|
for (drop_state_type, listener) in &drop_listeners {
|
||||||
if *drop_state_type == drag_state_type {
|
if *drop_state_type == drag_state_type {
|
||||||
|
@ -918,7 +901,7 @@ impl Interactivity {
|
||||||
.active_drag
|
.active_drag
|
||||||
.take()
|
.take()
|
||||||
.expect("checked for type drag state type above");
|
.expect("checked for type drag state type above");
|
||||||
let drag = drag.any_drag().expect("checked for any drag above");
|
|
||||||
listener(drag.view.clone(), cx);
|
listener(drag.view.clone(), cx);
|
||||||
cx.notify();
|
cx.notify();
|
||||||
cx.stop_propagation();
|
cx.stop_propagation();
|
||||||
|
@ -933,16 +916,12 @@ impl Interactivity {
|
||||||
|
|
||||||
let click_listeners = mem::take(&mut self.click_listeners);
|
let click_listeners = mem::take(&mut self.click_listeners);
|
||||||
let drag_listener = mem::take(&mut self.drag_listener);
|
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()
|
if !click_listeners.is_empty() || drag_listener.is_some() {
|
||||||
|| drag_listener.is_some()
|
|
||||||
|| !drag_event_listeners.is_empty()
|
|
||||||
{
|
|
||||||
let pending_mouse_down = element_state.pending_mouse_down.clone();
|
let pending_mouse_down = element_state.pending_mouse_down.clone();
|
||||||
let mouse_down = pending_mouse_down.borrow().clone();
|
let mouse_down = pending_mouse_down.borrow().clone();
|
||||||
if let Some(mouse_down) = mouse_down {
|
if let Some(mouse_down) = mouse_down {
|
||||||
if !drag_event_listeners.is_empty() || drag_listener.is_some() {
|
if let Some(drag_listener) = drag_listener {
|
||||||
let active_state = element_state.clicked_state.clone();
|
let active_state = element_state.clicked_state.clone();
|
||||||
let interactive_bounds = interactive_bounds.clone();
|
let interactive_bounds = interactive_bounds.clone();
|
||||||
|
|
||||||
|
@ -950,29 +929,17 @@ impl Interactivity {
|
||||||
if cx.active_drag.is_some() {
|
if cx.active_drag.is_some() {
|
||||||
if phase == DispatchPhase::Capture {
|
if phase == DispatchPhase::Capture {
|
||||||
cx.notify();
|
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
|
} else if phase == DispatchPhase::Bubble
|
||||||
&& interactive_bounds.visibly_contains(&event.position, cx)
|
&& interactive_bounds.visibly_contains(&event.position, cx)
|
||||||
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
|
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
|
||||||
{
|
{
|
||||||
*active_state.borrow_mut() = ElementClickedState::default();
|
*active_state.borrow_mut() = ElementClickedState::default();
|
||||||
if let Some(drag_listener) = &drag_listener {
|
let cursor_offset = event.position - bounds.origin;
|
||||||
let cursor_offset = event.position - bounds.origin;
|
let drag = drag_listener(cursor_offset, cx);
|
||||||
let drag = drag_listener(cursor_offset, cx);
|
cx.active_drag = Some(drag);
|
||||||
cx.active_drag = Some(AnyDragState::AnyDrag(drag));
|
cx.notify();
|
||||||
cx.notify();
|
cx.stop_propagation();
|
||||||
cx.stop_propagation();
|
|
||||||
}
|
|
||||||
for listener in &drag_event_listeners {
|
|
||||||
listener(event, cx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1235,7 +1202,7 @@ impl Interactivity {
|
||||||
if let Some(drag) = cx.active_drag.take() {
|
if let Some(drag) = cx.active_drag.take() {
|
||||||
for (state_type, group_drag_style) in &self.group_drag_over_styles {
|
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 let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
|
||||||
if Some(*state_type) == drag.entity_type()
|
if *state_type == drag.view.entity_type()
|
||||||
&& group_bounds.contains_point(&mouse_position)
|
&& group_bounds.contains_point(&mouse_position)
|
||||||
{
|
{
|
||||||
style.refine(&group_drag_style.style);
|
style.refine(&group_drag_style.style);
|
||||||
|
@ -1244,7 +1211,7 @@ impl Interactivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (state_type, drag_over_style) in &self.drag_over_styles {
|
for (state_type, drag_over_style) in &self.drag_over_styles {
|
||||||
if Some(*state_type) == drag.entity_type()
|
if *state_type == drag.view.entity_type()
|
||||||
&& bounds
|
&& bounds
|
||||||
.intersect(&cx.content_mask().bounds)
|
.intersect(&cx.content_mask().bounds)
|
||||||
.contains_point(&mouse_position)
|
.contains_point(&mouse_position)
|
||||||
|
@ -1301,7 +1268,6 @@ impl Default for Interactivity {
|
||||||
action_listeners: SmallVec::new(),
|
action_listeners: SmallVec::new(),
|
||||||
drop_listeners: SmallVec::new(),
|
drop_listeners: SmallVec::new(),
|
||||||
click_listeners: SmallVec::new(),
|
click_listeners: SmallVec::new(),
|
||||||
drag_event_listeners: SmallVec::new(),
|
|
||||||
drag_listener: None,
|
drag_listener: None,
|
||||||
hover_listener: None,
|
hover_listener: None,
|
||||||
tooltip_builder: None,
|
tooltip_builder: None,
|
||||||
|
|
|
@ -1188,14 +1188,11 @@ impl<'a> WindowContext<'a> {
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(active_drag) = self.app.active_drag.take() {
|
if let Some(active_drag) = self.app.active_drag.take() {
|
||||||
if let Some(active_drag) = active_drag.any_drag() {
|
self.with_z_index(1, |cx| {
|
||||||
self.with_z_index(1, |cx| {
|
let offset = cx.mouse_position() - active_drag.cursor_offset;
|
||||||
let offset = cx.mouse_position() - active_drag.cursor_offset;
|
let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
||||||
let available_space =
|
active_drag.view.draw(offset, available_space, cx);
|
||||||
size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
});
|
||||||
active_drag.view.draw(offset, available_space, cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
self.active_drag = Some(active_drag);
|
self.active_drag = Some(active_drag);
|
||||||
} else if let Some(active_tooltip) = self.app.active_tooltip.take() {
|
} else if let Some(active_tooltip) = self.app.active_tooltip.take() {
|
||||||
self.with_z_index(1, |cx| {
|
self.with_z_index(1, |cx| {
|
||||||
|
@ -1273,10 +1270,10 @@ impl<'a> WindowContext<'a> {
|
||||||
FileDropEvent::Entered { position, files } => {
|
FileDropEvent::Entered { position, files } => {
|
||||||
self.window.mouse_position = position;
|
self.window.mouse_position = position;
|
||||||
if self.active_drag.is_none() {
|
if self.active_drag.is_none() {
|
||||||
self.active_drag = Some(crate::AnyDragState::AnyDrag(AnyDrag {
|
self.active_drag = Some(AnyDrag {
|
||||||
view: self.build_view(|_| files).into(),
|
view: self.build_view(|_| files).into(),
|
||||||
cursor_offset: position,
|
cursor_offset: position,
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
InputEvent::MouseDown(MouseDownEvent {
|
InputEvent::MouseDown(MouseDownEvent {
|
||||||
position,
|
position,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue