Checkpoint

This commit is contained in:
Antonio Scandurra
2023-10-18 15:24:35 +02:00
parent eaef1c8b8e
commit acca8ea786
4 changed files with 45 additions and 49 deletions
+10 -10
View File
@@ -9,7 +9,7 @@ use collections::HashMap;
use parking_lot::Mutex; use parking_lot::Mutex;
use refineable::Refineable; use refineable::Refineable;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::sync::Arc; use std::{mem, sync::Arc};
#[derive(Default)] #[derive(Default)]
pub struct DivState { pub struct DivState {
@@ -263,12 +263,12 @@ where
} }
fn paint_event_listeners( fn paint_event_listeners(
&self, &mut self,
bounds: Bounds<Pixels>, bounds: Bounds<Pixels>,
pending_click: Arc<Mutex<Option<MouseDownEvent>>>, pending_click: Arc<Mutex<Option<MouseDownEvent>>>,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
) { ) {
let click_listeners = self.listeners.mouse_click.clone(); let click_listeners = mem::take(&mut self.listeners.mouse_click);
let mouse_down = pending_click.lock().clone(); let mouse_down = pending_click.lock().clone();
if let Some(mouse_down) = mouse_down { if let Some(mouse_down) = mouse_down {
cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
@@ -292,25 +292,25 @@ where
}); });
} }
for listener in self.listeners.mouse_down.iter().cloned() { for listener in mem::take(&mut self.listeners.mouse_down) {
cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| { cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
listener(state, event, &bounds, phase, cx); listener(state, event, &bounds, phase, cx);
}) })
} }
for listener in self.listeners.mouse_up.iter().cloned() { for listener in mem::take(&mut self.listeners.mouse_up) {
cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| { cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
listener(state, event, &bounds, phase, cx); listener(state, event, &bounds, phase, cx);
}) })
} }
for listener in self.listeners.mouse_move.iter().cloned() { for listener in mem::take(&mut self.listeners.mouse_move) {
cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| { cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
listener(state, event, &bounds, phase, cx); listener(state, event, &bounds, phase, cx);
}) })
} }
for listener in self.listeners.scroll_wheel.iter().cloned() { for listener in mem::take(&mut self.listeners.scroll_wheel) {
cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| { cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
listener(state, event, &bounds, phase, cx); listener(state, event, &bounds, phase, cx);
}) })
@@ -351,7 +351,7 @@ where
+ Sync + Sync
+ 'static, + 'static,
) -> Self { ) -> Self {
self.listeners.key_down.push(Arc::new(listener)); self.listeners.key_down.push(Box::new(listener));
self self
} }
} }
@@ -377,8 +377,8 @@ where
) -> Self::ElementState { ) -> Self::ElementState {
cx.with_focus( cx.with_focus(
self.focusability.focus_handle().cloned(), self.focusability.focus_handle().cloned(),
self.listeners.key_down.clone(), mem::take(&mut self.listeners.key_down),
self.listeners.key_up.clone(), mem::take(&mut self.listeners.key_up),
|cx| { |cx| {
for child in &mut self.children { for child in &mut self.children {
child.initialize(view_state, cx); child.initialize(view_state, cx);
+9 -10
View File
@@ -1,9 +1,8 @@
use smallvec::SmallVec;
use crate::{ use crate::{
point, Bounds, DispatchPhase, FocusHandle, Keystroke, Modifiers, Pixels, Point, ViewContext, point, Bounds, DispatchPhase, FocusHandle, Keystroke, Modifiers, Pixels, Point, ViewContext,
}; };
use std::{any::Any, ops::Deref, sync::Arc}; use smallvec::SmallVec;
use std::{any::Any, ops::Deref};
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct KeyDownEvent { pub struct KeyDownEvent {
@@ -209,29 +208,29 @@ pub struct FocusEvent {
pub focused: Option<FocusHandle>, pub focused: Option<FocusHandle>,
} }
pub type MouseDownListener<V> = Arc< pub type MouseDownListener<V> = Box<
dyn Fn(&mut V, &MouseDownEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) dyn Fn(&mut V, &MouseDownEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send + Send
+ Sync + Sync
+ 'static, + 'static,
>; >;
pub type MouseUpListener<V> = Arc< pub type MouseUpListener<V> = Box<
dyn Fn(&mut V, &MouseUpEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) dyn Fn(&mut V, &MouseUpEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send + Send
+ Sync + Sync
+ 'static, + 'static,
>; >;
pub type MouseClickListener<V> = pub type MouseClickListener<V> =
Arc<dyn Fn(&mut V, &MouseClickEvent, &mut ViewContext<V>) + Send + Sync + 'static>; Box<dyn Fn(&mut V, &MouseClickEvent, &mut ViewContext<V>) + Send + Sync + 'static>;
pub type MouseMoveListener<V> = Arc< pub type MouseMoveListener<V> = Box<
dyn Fn(&mut V, &MouseMoveEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) dyn Fn(&mut V, &MouseMoveEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send + Send
+ Sync + Sync
+ 'static, + 'static,
>; >;
pub type ScrollWheelListener<V> = Arc< pub type ScrollWheelListener<V> = Box<
dyn Fn(&mut V, &ScrollWheelEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) dyn Fn(&mut V, &ScrollWheelEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
+ Send + Send
+ Sync + Sync
@@ -239,10 +238,10 @@ pub type ScrollWheelListener<V> = Arc<
>; >;
pub type KeyDownListener<V> = pub type KeyDownListener<V> =
Arc<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static>; Box<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static>;
pub type KeyUpListener<V> = pub type KeyUpListener<V> =
Arc<dyn Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static>; Box<dyn Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static>;
pub struct EventListeners<V: 'static> { pub struct EventListeners<V: 'static> {
pub mouse_down: SmallVec<[MouseDownListener<V>; 2]>, pub mouse_down: SmallVec<[MouseDownListener<V>; 2]>,
+7 -8
View File
@@ -2,7 +2,6 @@ use crate::{
DispatchPhase, Element, EventListeners, MouseButton, MouseClickEvent, MouseDownEvent, DispatchPhase, Element, EventListeners, MouseButton, MouseClickEvent, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ScrollWheelEvent, ViewContext, MouseMoveEvent, MouseUpEvent, ScrollWheelEvent, ViewContext,
}; };
use std::sync::Arc;
pub trait Interactive: Element { pub trait Interactive: Element {
fn listeners(&mut self) -> &mut EventListeners<Self::ViewState>; fn listeners(&mut self) -> &mut EventListeners<Self::ViewState>;
@@ -20,7 +19,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.mouse_down .mouse_down
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble if phase == DispatchPhase::Bubble
&& event.button == button && event.button == button
&& bounds.contains_point(&event.position) && bounds.contains_point(&event.position)
@@ -44,7 +43,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.mouse_up .mouse_up
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble if phase == DispatchPhase::Bubble
&& event.button == button && event.button == button
&& bounds.contains_point(&event.position) && bounds.contains_point(&event.position)
@@ -68,7 +67,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.mouse_down .mouse_down
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Capture if phase == DispatchPhase::Capture
&& event.button == button && event.button == button
&& !bounds.contains_point(&event.position) && !bounds.contains_point(&event.position)
@@ -92,7 +91,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.mouse_up .mouse_up
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Capture if phase == DispatchPhase::Capture
&& event.button == button && event.button == button
&& !bounds.contains_point(&event.position) && !bounds.contains_point(&event.position)
@@ -115,7 +114,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.mouse_move .mouse_move
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
handler(view, event, cx); handler(view, event, cx);
} }
@@ -135,7 +134,7 @@ pub trait Interactive: Element {
{ {
self.listeners() self.listeners()
.scroll_wheel .scroll_wheel
.push(Arc::new(move |view, event, bounds, phase, cx| { .push(Box::new(move |view, event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
handler(view, event, cx); handler(view, event, cx);
} }
@@ -157,7 +156,7 @@ pub trait Click: Interactive {
{ {
self.listeners() self.listeners()
.mouse_click .mouse_click
.push(Arc::new(move |view, event, cx| handler(view, event, cx))); .push(Box::new(move |view, event, cx| handler(view, event, cx)));
self self
} }
} }
+19 -21
View File
@@ -40,8 +40,13 @@ pub enum DispatchPhase {
Capture, Capture,
} }
type MouseEventHandler = type AnyMouseEventListener =
Arc<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>; Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
type AnyFocusChangeListener = Box<dyn Fn(&FocusEvent, &mut WindowContext) + Send + Sync + 'static>;
type AnyKeyDownListener =
Box<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
type AnyKeyUpListener =
Box<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>;
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FocusId(usize); pub struct FocusId(usize);
@@ -91,16 +96,13 @@ pub struct Window {
element_states: HashMap<GlobalElementId, AnyBox>, element_states: HashMap<GlobalElementId, AnyBox>,
z_index_stack: StackingOrder, z_index_stack: StackingOrder,
content_mask_stack: Vec<ContentMask<Pixels>>, content_mask_stack: Vec<ContentMask<Pixels>>,
mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, MouseEventHandler)>>, mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyMouseEventListener)>>,
focus_stack: Vec<FocusStackFrame>, focus_stack: Vec<FocusStackFrame>,
focus_parents_by_child: HashMap<FocusId, FocusId>, focus_parents_by_child: HashMap<FocusId, FocusId>,
containing_focus: HashSet<FocusId>, containing_focus: HashSet<FocusId>,
pub(crate) focus_change_listeners: pub(crate) focus_change_listeners: Vec<AnyFocusChangeListener>,
Vec<Arc<dyn Fn(&FocusEvent, &mut WindowContext) + Send + Sync + 'static>>, key_down_listeners: Vec<AnyKeyDownListener>,
key_down_listeners: key_up_listeners: Vec<AnyKeyUpListener>,
Vec<Arc<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>>,
key_up_listeners:
Vec<Arc<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>>,
propagate_event: bool, propagate_event: bool,
mouse_position: Point<Pixels>, mouse_position: Point<Pixels>,
scale_factor: f32, scale_factor: f32,
@@ -207,12 +209,8 @@ impl ContentMask<Pixels> {
struct FocusStackFrame { struct FocusStackFrame {
handle: FocusHandle, handle: FocusHandle,
key_down_listeners: SmallVec< key_down_listeners: SmallVec<[AnyKeyDownListener; 2]>,
[Arc<dyn Fn(&KeyDownEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>; 2], key_up_listeners: SmallVec<[AnyKeyUpListener; 2]>,
>,
key_up_listeners: SmallVec<
[Arc<dyn Fn(&KeyUpEvent, DispatchPhase, &mut WindowContext) + Send + Sync + 'static>; 2],
>,
} }
pub struct WindowContext<'a, 'w> { pub struct WindowContext<'a, 'w> {
@@ -402,7 +400,7 @@ impl<'a, 'w> WindowContext<'a, 'w> {
.or_default() .or_default()
.push(( .push((
order, order,
Arc::new(move |event: &dyn Any, phase, cx| { Box::new(move |event: &dyn Any, phase, cx| {
handler(event.downcast_ref().unwrap(), phase, cx) handler(event.downcast_ref().unwrap(), phase, cx)
}), }),
)) ))
@@ -1136,7 +1134,7 @@ impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> {
let handle = handle.clone(); let handle = handle.clone();
frame frame
.key_down_listeners .key_down_listeners
.push(Arc::new(move |event, phase, cx| { .push(Box::new(move |event, phase, cx| {
handle handle
.update(cx, |view, cx| listener(view, event, phase, cx)) .update(cx, |view, cx| listener(view, event, phase, cx))
.log_err(); .log_err();
@@ -1146,7 +1144,7 @@ impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> {
let handle = handle.clone(); let handle = handle.clone();
frame frame
.key_up_listeners .key_up_listeners
.push(Arc::new(move |event, phase, cx| { .push(Box::new(move |event, phase, cx| {
handle handle
.update(cx, |view, cx| listener(view, event, phase, cx)) .update(cx, |view, cx| listener(view, event, phase, cx))
.log_err(); .log_err();
@@ -1156,14 +1154,14 @@ impl<'a, 'w, V: Send + Sync + 'static> ViewContext<'a, 'w, V> {
window.focus_stack.push(frame); window.focus_stack.push(frame);
if Some(focus_handle.id) == window.focus { if Some(focus_handle.id) == window.focus {
for frame in &window.focus_stack { for frame in &mut window.focus_stack {
window.containing_focus.insert(frame.handle.id); window.containing_focus.insert(frame.handle.id);
window window
.key_down_listeners .key_down_listeners
.extend_from_slice(&frame.key_down_listeners); .extend(frame.key_down_listeners.drain(..));
window window
.key_up_listeners .key_up_listeners
.extend_from_slice(&frame.key_up_listeners); .extend(frame.key_up_listeners.drain(..));
} }
} }