Add event based drag API to GPUI, continue binding mouse handlers to terminal
This commit is contained in:
parent
8d57d6ca6f
commit
ab140ee4c2
9 changed files with 295 additions and 224 deletions
|
@ -185,7 +185,7 @@ pub struct AppContext {
|
|||
flushing_effects: bool,
|
||||
pending_updates: usize,
|
||||
pub(crate) actions: Rc<ActionRegistry>,
|
||||
pub(crate) active_drag: Option<AnyDrag>,
|
||||
pub(crate) active_drag: Option<AnyDragState>,
|
||||
pub(crate) active_tooltip: Option<AnyTooltip>,
|
||||
pub(crate) next_frame_callbacks: HashMap<DisplayId, Vec<FrameCallback>>,
|
||||
pub(crate) frame_consumers: HashMap<DisplayId, Task<()>>,
|
||||
|
@ -1172,6 +1172,34 @@ pub struct AnyDrag {
|
|||
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)]
|
||||
pub(crate) struct AnyTooltip {
|
||||
pub view: AnyView,
|
||||
|
|
|
@ -370,39 +370,6 @@ impl<E: Element> DrawableElement<E> {
|
|||
}
|
||||
}
|
||||
|
||||
// impl<V: 'static, E: Element> Element for DrawableElement<V, E> {
|
||||
// type State = <E::Element as Element>::State;
|
||||
|
||||
// fn layout(
|
||||
// &mut self,
|
||||
// element_state: Option<Self::State>,
|
||||
// cx: &mut WindowContext,
|
||||
// ) -> (LayoutId, Self::State) {
|
||||
|
||||
// }
|
||||
|
||||
// fn paint(
|
||||
// self,
|
||||
// bounds: Bounds<Pixels>,
|
||||
// element_state: &mut Self::State,
|
||||
// cx: &mut WindowContext,
|
||||
// ) {
|
||||
// todo!()
|
||||
// }
|
||||
// }
|
||||
|
||||
// impl<V: 'static, E: 'static + Element> RenderOnce for DrawableElement<V, E> {
|
||||
// type Element = Self;
|
||||
|
||||
// fn element_id(&self) -> Option<ElementId> {
|
||||
// self.element.as_ref()?.element_id()
|
||||
// }
|
||||
|
||||
// fn render_once(self) -> Self::Element {
|
||||
// self
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<E> ElementObject for Option<DrawableElement<E>>
|
||||
where
|
||||
E: Element,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1159,12 +1159,15 @@ impl<'a> WindowContext<'a> {
|
|||
});
|
||||
|
||||
if let Some(active_drag) = self.app.active_drag.take() {
|
||||
self.with_z_index(1, |cx| {
|
||||
let offset = cx.mouse_position() - active_drag.cursor_offset;
|
||||
let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
||||
active_drag.view.draw(offset, available_space, cx);
|
||||
cx.active_drag = Some(active_drag);
|
||||
});
|
||||
if let Some(active_drag) = active_drag.any_drag() {
|
||||
self.with_z_index(1, |cx| {
|
||||
let offset = cx.mouse_position() - active_drag.cursor_offset;
|
||||
let available_space =
|
||||
size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
||||
active_drag.view.draw(offset, available_space, cx);
|
||||
});
|
||||
}
|
||||
self.active_drag = Some(active_drag);
|
||||
} else if let Some(active_tooltip) = self.app.active_tooltip.take() {
|
||||
self.with_z_index(1, |cx| {
|
||||
let available_space = size(AvailableSpace::MinContent, AvailableSpace::MinContent);
|
||||
|
@ -1240,10 +1243,10 @@ impl<'a> WindowContext<'a> {
|
|||
FileDropEvent::Entered { position, files } => {
|
||||
self.window.mouse_position = position;
|
||||
if self.active_drag.is_none() {
|
||||
self.active_drag = Some(AnyDrag {
|
||||
self.active_drag = Some(crate::AnyDragState::AnyDrag(AnyDrag {
|
||||
view: self.build_view(|_| files).into(),
|
||||
cursor_offset: position,
|
||||
});
|
||||
}));
|
||||
}
|
||||
InputEvent::MouseDown(MouseDownEvent {
|
||||
position,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue