@@ -40,8 +40,8 @@ use super::ImageCacheProvider;
4040use crate :: {
4141 Action , AnyDrag , AnyElement , AnyTooltip , AnyView , App , ClickEvent , DispatchPhase , Display , Element , ElementId , Entity , FocusHandle , Global ,
4242 GlobalElementId , Hitbox , HitboxBehavior , HitboxId , IntoElement , KeyContext , KeyDownEvent , KeyUpEvent , KeyboardButton , KeyboardClickEvent , LayoutId ,
43- ModifiersChangedEvent , MouseButton , MouseClickEvent , MouseDownEvent , MouseMoveEvent , MouseUpEvent , Overflow , ParentElement , Render , ScrollWheelEvent ,
44- Style , StyleRefinement , Styled , Task , TooltipId , Visibility , Window , WindowControlArea
43+ ModifiersChangedEvent , MouseButton , MouseClickEvent , MouseDownEvent , MouseMoveEvent , MousePressureEvent , MouseUpEvent , Overflow , ParentElement , Render ,
44+ ScrollWheelEvent , Style , StyleRefinement , Styled , Task , TooltipId , Visibility , Window , WindowControlArea
4545} ;
4646
4747const DRAG_THRESHOLD : f64 = 2. ;
@@ -126,6 +126,30 @@ impl Interactivity {
126126 } ) ) ;
127127 }
128128
129+ /// Bind the given callback to the mouse pressure event, during the bubble phase
130+ /// the imperative API equivalent to [`InteractiveElement::on_mouse_pressure`].
131+ ///
132+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
133+ pub fn on_mouse_pressure ( & mut self , listener : impl Fn ( & MousePressureEvent , & mut Window , & mut App ) + ' static ) {
134+ self . mouse_pressure_listeners . push ( Box :: new ( move |event, phase, hitbox, window, cx| {
135+ if phase == DispatchPhase :: Bubble && hitbox. is_hovered ( window) {
136+ ( listener) ( event, window, cx)
137+ }
138+ } ) ) ;
139+ }
140+
141+ /// Bind the given callback to the mouse pressure event, during the capture phase
142+ /// the imperative API equivalent to [`InteractiveElement::on_mouse_pressure`].
143+ ///
144+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
145+ pub fn capture_mouse_pressure ( & mut self , listener : impl Fn ( & MousePressureEvent , & mut Window , & mut App ) + ' static ) {
146+ self . mouse_pressure_listeners . push ( Box :: new ( move |event, phase, hitbox, window, cx| {
147+ if phase == DispatchPhase :: Capture && hitbox. is_hovered ( window) {
148+ ( listener) ( event, window, cx)
149+ }
150+ } ) ) ;
151+ }
152+
129153 /// Bind the given callback to the mouse up event for the given button, during the bubble phase.
130154 /// The imperative API equivalent to [`InteractiveElement::on_mouse_up`].
131155 ///
@@ -597,6 +621,24 @@ pub trait InteractiveElement: Sized {
597621 self
598622 }
599623
624+ /// Bind the given callback to the mouse pressure event, during the bubble phase
625+ /// the fluent API equivalent to [`Interactivity::on_mouse_pressure`]
626+ ///
627+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
628+ fn on_mouse_pressure ( mut self , listener : impl Fn ( & MousePressureEvent , & mut Window , & mut App ) + ' static ) -> Self {
629+ self . interactivity ( ) . on_mouse_pressure ( listener) ;
630+ self
631+ }
632+
633+ /// Bind the given callback to the mouse pressure event, during the capture phase
634+ /// the fluent API equivalent to [`Interactivity::on_mouse_pressure`]
635+ ///
636+ /// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
637+ fn capture_mouse_pressure ( mut self , listener : impl Fn ( & MousePressureEvent , & mut Window , & mut App ) + ' static ) -> Self {
638+ self . interactivity ( ) . capture_mouse_pressure ( listener) ;
639+ self
640+ }
641+
600642 /// Bind the given callback to the mouse up event for the given button, during the bubble phase.
601643 /// The fluent API equivalent to [`Interactivity::on_mouse_up`].
602644 ///
@@ -972,17 +1014,13 @@ pub trait StatefulInteractiveElement: InteractiveElement {
9721014
9731015pub ( crate ) type MouseDownListener = Box < dyn Fn ( & MouseDownEvent , DispatchPhase , & Hitbox , & mut Window , & mut App ) + ' static > ;
9741016pub ( crate ) type MouseUpListener = Box < dyn Fn ( & MouseUpEvent , DispatchPhase , & Hitbox , & mut Window , & mut App ) + ' static > ;
975-
9761017pub ( crate ) type MouseMoveListener = Box < dyn Fn ( & MouseMoveEvent , DispatchPhase , & Hitbox , & mut Window , & mut App ) + ' static > ;
977-
1018+ pub ( crate ) type MousePressureListener = Box < dyn Fn ( & MousePressureEvent , DispatchPhase , & Hitbox , & mut Window , & mut App ) + ' static > ;
9781019pub ( crate ) type ScrollWheelListener = Box < dyn Fn ( & ScrollWheelEvent , DispatchPhase , & Hitbox , & mut Window , & mut App ) + ' static > ;
979-
9801020pub ( crate ) type ClickListener = Rc < dyn Fn ( & ClickEvent , & mut Window , & mut App ) + ' static > ;
981-
9821021pub ( crate ) type DragListener = Box < dyn Fn ( & dyn Any , Point < Pixels > , & mut Window , & mut App ) -> AnyView + ' static > ;
9831022
9841023type DropListener = Box < dyn Fn ( & dyn Any , & mut Window , & mut App ) + ' static > ;
985-
9861024type CanDropPredicate = Box < dyn Fn ( & dyn Any , & mut Window , & mut App ) -> bool + ' static > ;
9871025
9881026pub ( crate ) struct TooltipBuilder {
@@ -1227,6 +1265,7 @@ pub struct Interactivity {
12271265 pub ( crate ) group_drag_over_styles : Vec < ( TypeId , GroupStyle ) > ,
12281266 pub ( crate ) mouse_down_listeners : Vec < MouseDownListener > ,
12291267 pub ( crate ) mouse_up_listeners : Vec < MouseUpListener > ,
1268+ pub ( crate ) mouse_pressure_listeners : Vec < MousePressureListener > ,
12301269 pub ( crate ) mouse_move_listeners : Vec < MouseMoveListener > ,
12311270 pub ( crate ) scroll_wheel_listeners : Vec < ScrollWheelListener > ,
12321271 pub ( crate ) key_down_listeners : Vec < KeyDownListener > ,
@@ -1369,6 +1408,7 @@ impl Interactivity {
13691408 || self . hover_listener . is_some ( )
13701409 || !self . mouse_up_listeners . is_empty ( )
13711410 || !self . mouse_down_listeners . is_empty ( )
1411+ || !self . mouse_pressure_listeners . is_empty ( )
13721412 || !self . mouse_move_listeners . is_empty ( )
13731413 || !self . click_listeners . is_empty ( )
13741414 || !self . scroll_wheel_listeners . is_empty ( )
@@ -1583,6 +1623,13 @@ impl Interactivity {
15831623 } )
15841624 }
15851625
1626+ for listener in self . mouse_pressure_listeners . drain ( ..) {
1627+ let hitbox = hitbox. clone ( ) ;
1628+ window. on_mouse_event ( move |event : & MousePressureEvent , phase, window, cx| {
1629+ listener ( event, phase, & hitbox, window, cx) ;
1630+ } )
1631+ }
1632+
15861633 for listener in self . mouse_move_listeners . drain ( ..) {
15871634 let hitbox = hitbox. clone ( ) ;
15881635 window. on_mouse_event ( move |event : & MouseMoveEvent , phase, window, cx| {
0 commit comments