Merge remote-tracking branch 'origin/main' into perf-2
This commit is contained in:
@@ -1139,8 +1139,10 @@ impl AppContext {
|
||||
self.active_drag.is_some()
|
||||
}
|
||||
|
||||
pub fn active_drag(&self) -> Option<AnyView> {
|
||||
self.active_drag.as_ref().map(|drag| drag.view.clone())
|
||||
pub fn active_drag<T: 'static>(&self) -> Option<&T> {
|
||||
self.active_drag
|
||||
.as_ref()
|
||||
.and_then(|drag| drag.value.downcast_ref())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1296,6 +1298,7 @@ impl<G: 'static> DerefMut for GlobalLease<G> {
|
||||
/// within the window or by dragging into the app from the underlying platform.
|
||||
pub struct AnyDrag {
|
||||
pub view: AnyView,
|
||||
pub value: Box<dyn Any>,
|
||||
pub cursor_offset: Point<Pixels>,
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ use std::{
|
||||
cell::RefCell,
|
||||
cmp::Ordering,
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
mem,
|
||||
rc::Rc,
|
||||
time::Duration,
|
||||
@@ -30,9 +31,18 @@ pub struct GroupStyle {
|
||||
pub style: Box<StyleRefinement>,
|
||||
}
|
||||
|
||||
pub struct DragMoveEvent<W: Render> {
|
||||
pub struct DragMoveEvent<T> {
|
||||
pub event: MouseMoveEvent,
|
||||
pub drag: View<W>,
|
||||
drag: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: 'static> DragMoveEvent<T> {
|
||||
pub fn drag<'b>(&self, cx: &'b AppContext) -> &'b T {
|
||||
cx.active_drag
|
||||
.as_ref()
|
||||
.and_then(|drag| drag.value.downcast_ref::<T>())
|
||||
.expect("DragMoveEvent is only valid when the stored active drag is of the same type.")
|
||||
}
|
||||
}
|
||||
|
||||
impl Interactivity {
|
||||
@@ -133,23 +143,26 @@ impl Interactivity {
|
||||
}));
|
||||
}
|
||||
|
||||
pub fn on_drag_move<W>(
|
||||
pub fn on_drag_move<T>(
|
||||
&mut self,
|
||||
listener: impl Fn(&DragMoveEvent<W>, &mut WindowContext) + 'static,
|
||||
listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
|
||||
) where
|
||||
W: Render,
|
||||
T: 'static,
|
||||
{
|
||||
self.mouse_move_listeners
|
||||
.push(Box::new(move |event, bounds, phase, cx| {
|
||||
if phase == DispatchPhase::Capture
|
||||
&& bounds.drag_target_contains(&event.position, cx)
|
||||
{
|
||||
if let Some(view) = cx.active_drag().and_then(|view| view.downcast::<W>().ok())
|
||||
if cx
|
||||
.active_drag
|
||||
.as_ref()
|
||||
.is_some_and(|drag| drag.value.type_id() == TypeId::of::<T>())
|
||||
{
|
||||
(listener)(
|
||||
&DragMoveEvent {
|
||||
event: event.clone(),
|
||||
drag: view,
|
||||
drag: PhantomData,
|
||||
},
|
||||
cx,
|
||||
);
|
||||
@@ -252,14 +265,11 @@ impl Interactivity {
|
||||
}));
|
||||
}
|
||||
|
||||
pub fn on_drop<W: 'static>(
|
||||
&mut self,
|
||||
listener: impl Fn(&View<W>, &mut WindowContext) + 'static,
|
||||
) {
|
||||
pub fn on_drop<T: 'static>(&mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) {
|
||||
self.drop_listeners.push((
|
||||
TypeId::of::<W>(),
|
||||
Box::new(move |dragged_view, cx| {
|
||||
listener(&dragged_view.downcast().unwrap(), cx);
|
||||
TypeId::of::<T>(),
|
||||
Box::new(move |dragged_value, cx| {
|
||||
listener(dragged_value.downcast_ref().unwrap(), cx);
|
||||
}),
|
||||
));
|
||||
}
|
||||
@@ -272,19 +282,23 @@ impl Interactivity {
|
||||
.push(Box::new(move |event, cx| listener(event, cx)));
|
||||
}
|
||||
|
||||
pub fn on_drag<W>(&mut self, constructor: impl Fn(&mut WindowContext) -> View<W> + 'static)
|
||||
where
|
||||
pub fn on_drag<T, W>(
|
||||
&mut self,
|
||||
value: T,
|
||||
constructor: impl Fn(&T, &mut WindowContext) -> View<W> + 'static,
|
||||
) where
|
||||
Self: Sized,
|
||||
T: 'static,
|
||||
W: 'static + Render,
|
||||
{
|
||||
debug_assert!(
|
||||
self.drag_listener.is_none(),
|
||||
"calling on_drag more than once on the same element is not supported"
|
||||
);
|
||||
self.drag_listener = Some(Box::new(move |cursor_offset, cx| AnyDrag {
|
||||
view: constructor(cx).into(),
|
||||
cursor_offset,
|
||||
}));
|
||||
self.drag_listener = Some((
|
||||
Box::new(value),
|
||||
Box::new(move |value, cx| constructor(value.downcast_ref().unwrap(), cx).into()),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn on_hover(&mut self, listener: impl Fn(&bool, &mut WindowContext) + 'static)
|
||||
@@ -413,12 +427,12 @@ pub trait InteractiveElement: Sized {
|
||||
self
|
||||
}
|
||||
|
||||
fn on_drag_move<W>(
|
||||
fn on_drag_move<T>(
|
||||
mut self,
|
||||
listener: impl Fn(&DragMoveEvent<W>, &mut WindowContext) + 'static,
|
||||
listener: impl Fn(&DragMoveEvent<T>, &mut WindowContext) + 'static,
|
||||
) -> Self
|
||||
where
|
||||
W: Render,
|
||||
T: Render,
|
||||
{
|
||||
self.interactivity().on_drag_move(listener);
|
||||
self
|
||||
@@ -485,14 +499,6 @@ pub trait InteractiveElement: Sized {
|
||||
self
|
||||
}
|
||||
|
||||
fn on_drop<W: 'static>(
|
||||
mut self,
|
||||
listener: impl Fn(&View<W>, &mut WindowContext) + 'static,
|
||||
) -> Self {
|
||||
self.interactivity().on_drop(listener);
|
||||
self
|
||||
}
|
||||
|
||||
fn drag_over<S: 'static>(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
|
||||
self.interactivity()
|
||||
.drag_over_styles
|
||||
@@ -514,6 +520,11 @@ pub trait InteractiveElement: Sized {
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
fn on_drop<T: 'static>(mut self, listener: impl Fn(&T, &mut WindowContext) + 'static) -> Self {
|
||||
self.interactivity().on_drop(listener);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
@@ -574,12 +585,17 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
self
|
||||
}
|
||||
|
||||
fn on_drag<W>(mut self, constructor: impl Fn(&mut WindowContext) -> View<W> + 'static) -> Self
|
||||
fn on_drag<T, W>(
|
||||
mut self,
|
||||
value: T,
|
||||
constructor: impl Fn(&T, &mut WindowContext) -> View<W> + 'static,
|
||||
) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
T: 'static,
|
||||
W: 'static + Render,
|
||||
{
|
||||
self.interactivity().on_drag(constructor);
|
||||
self.interactivity().on_drag(value, constructor);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -635,9 +651,9 @@ pub type ScrollWheelListener =
|
||||
|
||||
pub type ClickListener = Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>;
|
||||
|
||||
pub type DragListener = Box<dyn Fn(Point<Pixels>, &mut WindowContext) -> AnyDrag + 'static>;
|
||||
pub type DragListener = Box<dyn Fn(&dyn Any, &mut WindowContext) -> AnyView + 'static>;
|
||||
|
||||
type DropListener = dyn Fn(AnyView, &mut WindowContext) + 'static;
|
||||
type DropListener = Box<dyn Fn(&dyn Any, &mut WindowContext) + 'static>;
|
||||
|
||||
pub type TooltipBuilder = Rc<dyn Fn(&mut WindowContext) -> AnyView + 'static>;
|
||||
|
||||
@@ -834,9 +850,9 @@ pub struct Interactivity {
|
||||
pub key_down_listeners: Vec<KeyDownListener>,
|
||||
pub key_up_listeners: Vec<KeyUpListener>,
|
||||
pub action_listeners: Vec<(TypeId, ActionListener)>,
|
||||
pub drop_listeners: Vec<(TypeId, Box<DropListener>)>,
|
||||
pub drop_listeners: Vec<(TypeId, DropListener)>,
|
||||
pub click_listeners: Vec<ClickListener>,
|
||||
pub drag_listener: Option<DragListener>,
|
||||
pub drag_listener: Option<(Box<dyn Any>, DragListener)>,
|
||||
pub hover_listener: Option<Box<dyn Fn(&bool, &mut WindowContext)>>,
|
||||
pub tooltip_builder: Option<TooltipBuilder>,
|
||||
|
||||
@@ -1124,8 +1140,10 @@ impl Interactivity {
|
||||
if phase == DispatchPhase::Bubble
|
||||
&& interactive_bounds.drag_target_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()
|
||||
.map(|drag| drag.value.as_ref().type_id())
|
||||
{
|
||||
for (drop_state_type, listener) in &drop_listeners {
|
||||
if *drop_state_type == drag_state_type {
|
||||
@@ -1134,7 +1152,7 @@ impl Interactivity {
|
||||
.take()
|
||||
.expect("checked for type drag state type above");
|
||||
|
||||
listener(drag.view.clone(), cx);
|
||||
listener(drag.value.as_ref(), cx);
|
||||
cx.notify();
|
||||
cx.stop_propagation();
|
||||
}
|
||||
@@ -1148,7 +1166,7 @@ impl Interactivity {
|
||||
}
|
||||
|
||||
let click_listeners = mem::take(&mut self.click_listeners);
|
||||
let drag_listener = mem::take(&mut self.drag_listener);
|
||||
let mut drag_listener = mem::take(&mut self.drag_listener);
|
||||
|
||||
if !click_listeners.is_empty() || drag_listener.is_some() {
|
||||
let pending_mouse_down = element_state
|
||||
@@ -1157,7 +1175,7 @@ impl Interactivity {
|
||||
.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_listener.is_some() {
|
||||
let active_state = element_state
|
||||
.clicked_state
|
||||
.get_or_insert_with(Default::default)
|
||||
@@ -1173,10 +1191,18 @@ impl Interactivity {
|
||||
&& interactive_bounds.visibly_contains(&event.position, cx)
|
||||
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
|
||||
{
|
||||
let (drag_value, drag_listener) = drag_listener
|
||||
.take()
|
||||
.expect("The notify below should invalidate this callback");
|
||||
|
||||
*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);
|
||||
let drag = (drag_listener)(drag_value.as_ref(), cx);
|
||||
cx.active_drag = Some(AnyDrag {
|
||||
view: drag,
|
||||
value: drag_value,
|
||||
cursor_offset,
|
||||
});
|
||||
cx.notify();
|
||||
cx.stop_propagation();
|
||||
}
|
||||
@@ -1470,7 +1496,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 *state_type == drag.value.as_ref().type_id()
|
||||
&& group_bounds.contains(&mouse_position)
|
||||
{
|
||||
style.refine(&group_drag_style.style);
|
||||
@@ -1479,7 +1505,7 @@ impl Interactivity {
|
||||
}
|
||||
|
||||
for (state_type, drag_over_style) in &self.drag_over_styles {
|
||||
if *state_type == drag.view.entity_type()
|
||||
if *state_type == drag.value.as_ref().type_id()
|
||||
&& bounds
|
||||
.intersect(&cx.content_mask().bounds)
|
||||
.contains(&mouse_position)
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::{
|
||||
use anyhow::{Context, Result};
|
||||
use std::{
|
||||
any::TypeId,
|
||||
fmt,
|
||||
hash::{Hash, Hasher},
|
||||
};
|
||||
|
||||
@@ -308,6 +309,20 @@ impl<V: 'static + Render> From<WeakView<V>> for AnyWeakView {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for AnyWeakView {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.model == other.model
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for AnyWeakView {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("AnyWeakView")
|
||||
.field("entity_id", &self.model.entity_id)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> Render for T
|
||||
where
|
||||
T: 'static + FnMut(&mut WindowContext) -> E,
|
||||
|
||||
@@ -822,7 +822,7 @@ impl<'a> WindowContext<'a> {
|
||||
/// a specific need to register a global listener.
|
||||
pub fn on_mouse_event<Event: 'static>(
|
||||
&mut self,
|
||||
handler: impl Fn(&Event, DispatchPhase, &mut WindowContext) + 'static,
|
||||
mut handler: impl FnMut(&Event, DispatchPhase, &mut WindowContext) + 'static,
|
||||
) {
|
||||
let order = self.window.next_frame.z_index_stack.clone();
|
||||
self.window
|
||||
@@ -1400,6 +1400,7 @@ impl<'a> WindowContext<'a> {
|
||||
self.window.mouse_position = position;
|
||||
if self.active_drag.is_none() {
|
||||
self.active_drag = Some(AnyDrag {
|
||||
value: Box::new(files.clone()),
|
||||
view: self.build_view(|_| files).into(),
|
||||
cursor_offset: position,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user