1use std::collections::HashMap;
10use std::rc::Rc;
11use std::time::Duration;
12
13use indexmap::IndexMap;
14use waft_embedder::{Matrix4, Offset, PointerDeviceKind};
15use waft_foundation::{App, Handle, Listener, OptionRetained};
16use waft_scheduler::SchedulerBinding;
17
18use crate::arena::GestureDisposition;
19use crate::constants::{K_MAX_FLING_VELOCITY, K_MIN_FLING_VELOCITY};
20use crate::drag_details::{
21 DragDownDetails, DragEndDetails, DragStartDetails, DragUpdateDetails, GestureDragDownCallback,
22 GestureDragStartCallback, GestureDragUpdateCallback,
23};
24use crate::events::{
25 K_PRIMARY_BUTTON, PointerDownEvent, PointerEvent, PointerPanZoomStartEvent, compute_hit_slop,
26 compute_pan_slop, transform_delta_via_positions,
27};
28use crate::gesture_settings::DeviceGestureSettings;
29use crate::recognizer::{
30 DragStartBehavior, GestureRecognizer, GestureRecognizerData, MultitouchDragStrategy,
31 OffsetPair, OneSequenceData, OneSequenceGestureRecognizer, OneSequenceLeafData, RecognizerLeaf,
32 RecognizerLeafData,
33};
34use crate::team::GestureArenaTeam;
35use crate::velocity::{Velocity, VelocityEstimate};
36use crate::velocity_tracker::{AnyVelocityTracker, VelocityTracker};
37use waft_foundation::Ref;
38
39#[derive(Clone, Copy, PartialEq, Eq)]
40enum DragState {
41 Ready,
42 Possible,
43 Accepted,
44}
45
46pub type GestureDragEndCallback = waft_foundation::ValueChanged<DragEndDetails>;
54
55pub type GestureDragCancelCallback = Listener;
60
61pub type GestureVelocityTrackerBuilder = Rc<dyn Fn(&PointerEvent) -> Box<dyn AnyVelocityTracker>>;
65
66fn default_builder(event: &PointerEvent) -> Box<dyn AnyVelocityTracker> {
67 Box::new(VelocityTracker::with_kind(event.kind()))
68}
69
70fn default_button_accept_behavior(buttons: i64) -> bool {
72 buttons == K_PRIMARY_BUTTON
73}
74
75fn sign(value: f64) -> f64 {
78 if value > 0.0 {
79 1.0
80 } else if value < 0.0 {
81 -1.0
82 } else {
83 value
84 }
85}
86
87pub struct DragData {
89 drag_start_behavior: DragStartBehavior,
90 multitouch_drag_strategy: MultitouchDragStrategy,
91 on_down: Option<GestureDragDownCallback>,
92 on_start: Option<GestureDragStartCallback>,
93 on_update: Option<GestureDragUpdateCallback>,
94 on_end: Option<GestureDragEndCallback>,
95 on_cancel: Option<GestureDragCancelCallback>,
96 min_fling_distance: Option<f64>,
97 min_fling_velocity: Option<f64>,
98 max_fling_velocity: Option<f64>,
99 only_accept_drag_on_threshold: bool,
100 velocity_tracker_builder: GestureVelocityTrackerBuilder,
101 state: DragState,
102 initial_position: OffsetPair,
105 pending_drag_offset: OffsetPair,
106 last_position: OffsetPair,
107 last_pending_event_timestamp: Option<Duration>,
108 initial_buttons: Option<i64>,
111 last_transform: Option<Matrix4>,
112 global_distance_moved: f64,
113 has_drag_threshold_been_met: bool,
114 velocity_trackers: HashMap<i64, Box<dyn AnyVelocityTracker>>,
115 move_delta_before_frame: IndexMap<i64, Offset>,
119 frame_time_stamp: Option<Duration>,
122 last_updated_delta_for_pan: Offset,
123 accepted_active_pointers: Vec<i64>,
124 active_pointer: Option<i64>,
129}
130
131impl DragData {
132 pub fn new() -> DragData {
135 DragData {
136 drag_start_behavior: DragStartBehavior::Start,
137 multitouch_drag_strategy: MultitouchDragStrategy::LatestPointer,
138 on_down: None,
139 on_start: None,
140 on_update: None,
141 on_end: None,
142 on_cancel: None,
143 min_fling_distance: None,
144 min_fling_velocity: None,
145 max_fling_velocity: None,
146 only_accept_drag_on_threshold: false,
147 velocity_tracker_builder: Rc::new(default_builder),
148 state: DragState::Ready,
149 initial_position: OffsetPair::ZERO,
150 pending_drag_offset: OffsetPair::ZERO,
151 last_position: OffsetPair::ZERO,
152 last_pending_event_timestamp: None,
153 initial_buttons: None,
154 last_transform: None,
155 global_distance_moved: 0.0,
156 has_drag_threshold_been_met: false,
157 velocity_trackers: HashMap::new(),
158 move_delta_before_frame: IndexMap::new(),
159 frame_time_stamp: None,
160 last_updated_delta_for_pan: Offset::ZERO,
161 accepted_active_pointers: Vec::new(),
162 active_pointer: None,
163 }
164 }
165}
166
167impl Default for DragData {
168 fn default() -> DragData {
169 DragData::new()
170 }
171}
172
173pub trait DragLeafData: OneSequenceLeafData {
175 fn drag(&self) -> &DragData;
177
178 fn drag_mut(&mut self) -> &mut DragData;
180}
181
182#[derive(Clone, Copy, Debug, PartialEq, Eq)]
185pub enum DragDirection {
186 Horizontal,
188 Vertical,
190}
191
192pub trait DragLeaf: RecognizerLeaf + DragLeafData {
194 fn is_fling_gesture(
200 self: Handle<Self>,
201 app: &App,
202 estimate: &VelocityEstimate,
203 kind: PointerDeviceKind,
204 ) -> bool;
205
206 fn consider_fling(
209 self: Handle<Self>,
210 app: &App,
211 estimate: &VelocityEstimate,
212 kind: PointerDeviceKind,
213 ) -> Option<DragEndDetails>;
214
215 fn get_delta_for_details(self: Handle<Self>, app: &App, delta: Offset) -> Offset;
222
223 fn get_primary_value_from_offset(self: Handle<Self>, app: &App, value: Offset) -> Option<f64>;
227
228 fn get_primary_drag_axis(self: Handle<Self>, app: &App) -> Option<DragDirection> {
232 let _ = app;
233 None
234 }
235
236 fn has_sufficient_global_distance_to_accept(
242 self: Handle<Self>,
243 app: &App,
244 pointer_device_kind: PointerDeviceKind,
245 device_touch_slop: Option<f64>,
246 ) -> bool;
247}
248
249pub trait DragGestureRecognizer: Sized + 'static {
269 fn drag_start_behavior(self: Handle<Self>, app: &App) -> DragStartBehavior;
281
282 fn set_drag_start_behavior(self: Handle<Self>, app: &mut App, value: DragStartBehavior);
284
285 fn multitouch_drag_strategy(self: Handle<Self>, app: &App) -> MultitouchDragStrategy;
289
290 fn set_multitouch_drag_strategy(
292 self: Handle<Self>,
293 app: &mut App,
294 value: MultitouchDragStrategy,
295 );
296
297 fn set_on_down(self: Handle<Self>, app: &mut App, callback: Option<GestureDragDownCallback>);
303
304 fn set_on_start(self: Handle<Self>, app: &mut App, callback: Option<GestureDragStartCallback>);
312
313 fn set_on_update(
328 self: Handle<Self>,
329 app: &mut App,
330 callback: Option<GestureDragUpdateCallback>,
331 );
332
333 fn set_on_end(self: Handle<Self>, app: &mut App, callback: Option<GestureDragEndCallback>);
340
341 fn set_on_cancel(
344 self: Handle<Self>,
345 app: &mut App,
346 callback: Option<GestureDragCancelCallback>,
347 );
348
349 fn min_fling_distance(self: Handle<Self>, app: &App) -> Option<f64>;
356
357 fn set_min_fling_distance(self: Handle<Self>, app: &mut App, value: Option<f64>);
359
360 fn min_fling_velocity(self: Handle<Self>, app: &App) -> Option<f64>;
366
367 fn set_min_fling_velocity(self: Handle<Self>, app: &mut App, value: Option<f64>);
369
370 fn max_fling_velocity(self: Handle<Self>, app: &App) -> Option<f64>;
374
375 fn set_max_fling_velocity(self: Handle<Self>, app: &mut App, value: Option<f64>);
377
378 fn only_accept_drag_on_threshold(self: Handle<Self>, app: &App) -> bool;
395
396 fn set_only_accept_drag_on_threshold(self: Handle<Self>, app: &mut App, value: bool);
398
399 fn velocity_tracker_builder(self: Handle<Self>, app: &App) -> GestureVelocityTrackerBuilder;
411
412 fn set_velocity_tracker_builder(
414 self: Handle<Self>,
415 app: &mut App,
416 builder: GestureVelocityTrackerBuilder,
417 );
418
419 fn last_position(self: Handle<Self>, app: &App) -> OffsetPair;
424
425 fn debug_last_pending_event_timestamp(self: Handle<Self>, app: &App) -> Option<Duration>;
431
432 fn global_distance_moved(self: Handle<Self>, app: &App) -> f64;
438
439 fn is_fling_gesture(
441 self: Handle<Self>,
442 app: &App,
443 estimate: &VelocityEstimate,
444 kind: PointerDeviceKind,
445 ) -> bool;
446
447 fn consider_fling(
450 self: Handle<Self>,
451 app: &App,
452 estimate: &VelocityEstimate,
453 kind: PointerDeviceKind,
454 ) -> Option<DragEndDetails>;
455
456 fn has_sufficient_global_distance_to_accept(
459 self: Handle<Self>,
460 app: &App,
461 pointer_device_kind: PointerDeviceKind,
462 device_touch_slop: Option<f64>,
463 ) -> bool;
464
465 fn is_pointer_allowed(self: Handle<Self>, app: &App, event: &PointerDownEvent) -> bool;
468
469 fn add_allowed_pointer(self: Handle<Self>, app: &mut App, event: PointerDownEvent);
471
472 fn add_allowed_pointer_pan_zoom(
474 self: Handle<Self>,
475 app: &mut App,
476 event: PointerPanZoomStartEvent,
477 );
478
479 fn handle_event(self: Handle<Self>, app: &mut App, event: PointerEvent);
481
482 fn accept_gesture(self: Handle<Self>, app: &mut App, pointer: i64);
484
485 fn reject_gesture(self: Handle<Self>, app: &mut App, pointer: i64);
487
488 fn did_stop_tracking_last_pointer(self: Handle<Self>, app: &mut App, pointer: i64);
491
492 fn dispose(self: Handle<Self>, app: &mut App);
494}
495
496impl<L: DragLeaf> DragGestureRecognizer for L {
497 fn drag_start_behavior(self: Handle<Self>, app: &App) -> DragStartBehavior {
498 app.get(self).drag().drag_start_behavior
499 }
500
501 fn set_drag_start_behavior(self: Handle<Self>, app: &mut App, value: DragStartBehavior) {
502 app.get_mut(self).drag_mut().drag_start_behavior = value;
503 }
504
505 fn multitouch_drag_strategy(self: Handle<Self>, app: &App) -> MultitouchDragStrategy {
506 app.get(self).drag().multitouch_drag_strategy
507 }
508
509 fn set_multitouch_drag_strategy(
510 self: Handle<Self>,
511 app: &mut App,
512 value: MultitouchDragStrategy,
513 ) {
514 app.get_mut(self).drag_mut().multitouch_drag_strategy = value;
515 }
516
517 fn set_on_down(self: Handle<Self>, app: &mut App, callback: Option<GestureDragDownCallback>) {
518 app.get_mut(self).drag_mut().on_down = callback;
519 }
520
521 fn set_on_start(self: Handle<Self>, app: &mut App, callback: Option<GestureDragStartCallback>) {
522 app.get_mut(self).drag_mut().on_start = callback;
523 }
524
525 fn set_on_update(
526 self: Handle<Self>,
527 app: &mut App,
528 callback: Option<GestureDragUpdateCallback>,
529 ) {
530 app.get_mut(self).drag_mut().on_update = callback;
531 }
532
533 fn set_on_end(self: Handle<Self>, app: &mut App, callback: Option<GestureDragEndCallback>) {
534 app.get_mut(self).drag_mut().on_end = callback;
535 }
536
537 fn set_on_cancel(
538 self: Handle<Self>,
539 app: &mut App,
540 callback: Option<GestureDragCancelCallback>,
541 ) {
542 app.get_mut(self).drag_mut().on_cancel = callback;
543 }
544
545 fn min_fling_distance(self: Handle<Self>, app: &App) -> Option<f64> {
546 app.get(self).drag().min_fling_distance
547 }
548
549 fn set_min_fling_distance(self: Handle<Self>, app: &mut App, value: Option<f64>) {
550 app.get_mut(self).drag_mut().min_fling_distance = value;
551 }
552
553 fn min_fling_velocity(self: Handle<Self>, app: &App) -> Option<f64> {
554 app.get(self).drag().min_fling_velocity
555 }
556
557 fn set_min_fling_velocity(self: Handle<Self>, app: &mut App, value: Option<f64>) {
558 app.get_mut(self).drag_mut().min_fling_velocity = value;
559 }
560
561 fn max_fling_velocity(self: Handle<Self>, app: &App) -> Option<f64> {
562 app.get(self).drag().max_fling_velocity
563 }
564
565 fn set_max_fling_velocity(self: Handle<Self>, app: &mut App, value: Option<f64>) {
566 app.get_mut(self).drag_mut().max_fling_velocity = value;
567 }
568
569 fn only_accept_drag_on_threshold(self: Handle<Self>, app: &App) -> bool {
570 app.get(self).drag().only_accept_drag_on_threshold
571 }
572
573 fn set_only_accept_drag_on_threshold(self: Handle<Self>, app: &mut App, value: bool) {
574 app.get_mut(self).drag_mut().only_accept_drag_on_threshold = value;
575 }
576
577 fn velocity_tracker_builder(self: Handle<Self>, app: &App) -> GestureVelocityTrackerBuilder {
578 Rc::clone(&app.get(self).drag().velocity_tracker_builder)
579 }
580
581 fn set_velocity_tracker_builder(
582 self: Handle<Self>,
583 app: &mut App,
584 builder: GestureVelocityTrackerBuilder,
585 ) {
586 app.get_mut(self).drag_mut().velocity_tracker_builder = builder;
587 }
588
589 fn last_position(self: Handle<Self>, app: &App) -> OffsetPair {
590 app.get(self).drag().last_position
591 }
592
593 fn debug_last_pending_event_timestamp(self: Handle<Self>, app: &App) -> Option<Duration> {
594 let mut last_pending_event_timestamp = None;
595 if cfg!(debug_assertions) {
596 last_pending_event_timestamp = app.get(self).drag().last_pending_event_timestamp;
597 }
598 last_pending_event_timestamp
599 }
600
601 fn global_distance_moved(self: Handle<Self>, app: &App) -> f64 {
602 app.get(self).drag().global_distance_moved
603 }
604
605 fn is_fling_gesture(
606 self: Handle<Self>,
607 app: &App,
608 estimate: &VelocityEstimate,
609 kind: PointerDeviceKind,
610 ) -> bool {
611 DragLeaf::is_fling_gesture(self, app, estimate, kind)
612 }
613
614 fn consider_fling(
615 self: Handle<Self>,
616 app: &App,
617 estimate: &VelocityEstimate,
618 kind: PointerDeviceKind,
619 ) -> Option<DragEndDetails> {
620 DragLeaf::consider_fling(self, app, estimate, kind)
621 }
622
623 fn has_sufficient_global_distance_to_accept(
624 self: Handle<Self>,
625 app: &App,
626 pointer_device_kind: PointerDeviceKind,
627 device_touch_slop: Option<f64>,
628 ) -> bool {
629 DragLeaf::has_sufficient_global_distance_to_accept(
630 self,
631 app,
632 pointer_device_kind,
633 device_touch_slop,
634 )
635 }
636
637 fn is_pointer_allowed(self: Handle<Self>, app: &App, event: &PointerDownEvent) -> bool {
638 let recognizer = app.get(self).drag();
639 if recognizer.initial_buttons.is_none() {
640 if recognizer.on_down.is_none()
641 && recognizer.on_start.is_none()
642 && recognizer.on_update.is_none()
643 && recognizer.on_end.is_none()
644 && recognizer.on_cancel.is_none()
645 {
646 return false;
647 }
648 } else if Some(event.buttons) != recognizer.initial_buttons {
649 return false;
651 }
652 GestureRecognizer::is_pointer_allowed(self, app, event)
653 }
654
655 fn add_allowed_pointer(self: Handle<Self>, app: &mut App, event: PointerDownEvent) {
656 OneSequenceGestureRecognizer::add_allowed_pointer(self, app, &event);
657 if app.get(self).drag().state == DragState::Ready {
658 app.get_mut(self).drag_mut().initial_buttons = Some(event.buttons);
659 }
660 add_pointer(self, app, &PointerEvent::Down(event));
661 }
662
663 fn add_allowed_pointer_pan_zoom(
664 self: Handle<Self>,
665 app: &mut App,
666 event: PointerPanZoomStartEvent,
667 ) {
668 self.start_tracking_pointer(app, event.pointer, event.transform);
669 if app.get(self).drag().state == DragState::Ready {
670 app.get_mut(self).drag_mut().initial_buttons = Some(K_PRIMARY_BUTTON);
671 }
672 add_pointer(self, app, &PointerEvent::PanZoomStart(event));
673 }
674
675 fn handle_event(self: Handle<Self>, app: &mut App, event: PointerEvent) {
676 debug_assert!(app.get(self).drag().state != DragState::Ready);
677 if !event.synthesized()
678 && matches!(
679 event,
680 PointerEvent::Down(_)
681 | PointerEvent::Move(_)
682 | PointerEvent::PanZoomStart(_)
683 | PointerEvent::PanZoomUpdate(_)
684 )
685 {
686 let position = match &event {
687 PointerEvent::PanZoomStart(_) => Offset::ZERO,
688 PointerEvent::PanZoomUpdate(pan_zoom) => pan_zoom.pan,
689 _ => event.local_position(),
690 };
691 let time_stamp = event.time_stamp();
692 let now = app.clock();
693 app.get_mut(self)
694 .drag_mut()
695 .velocity_trackers
696 .get_mut(&event.pointer())
697 .unwrap()
698 .add_position(now, time_stamp, position);
699 }
700 if let PointerEvent::Move(moved) = &event
701 && Some(moved.buttons) != app.get(self).drag().initial_buttons
702 {
703 give_up_pointer(self, app, moved.pointer);
704 return;
705 }
706 if matches!(
707 event,
708 PointerEvent::Move(_) | PointerEvent::PanZoomUpdate(_)
709 ) && should_track_move_event(self, app, event.pointer())
710 {
711 let (delta, local_delta, position, local_position) = match &event {
712 PointerEvent::Move(moved) => (
713 moved.delta,
714 moved.local_delta(),
715 moved.position,
716 moved.local_position(),
717 ),
718 PointerEvent::PanZoomUpdate(pan_zoom) => (
719 pan_zoom.pan_delta,
720 pan_zoom.local_pan_delta(),
721 pan_zoom.position + pan_zoom.pan,
722 pan_zoom.local_position() + pan_zoom.local_pan(),
723 ),
724 _ => unreachable!(),
725 };
726 app.get_mut(self).drag_mut().last_position = OffsetPair::new(local_position, position);
727 let resolved_delta =
728 resolve_local_delta_for_multitouch(self, app, event.pointer(), local_delta);
729 match app.get(self).drag().state {
730 DragState::Ready | DragState::Possible => {
731 let pending = app.get(self).drag().pending_drag_offset;
732 app.get_mut(self).drag_mut().pending_drag_offset =
733 pending + OffsetPair::new(local_delta, delta);
734 app.get_mut(self).drag_mut().last_pending_event_timestamp =
735 Some(event.time_stamp());
736 app.get_mut(self).drag_mut().last_transform = event.transform();
737 let moved_locally = DragLeaf::get_delta_for_details(self, app, local_delta);
738 let local_to_global_transform =
739 event.transform().and_then(|transform| transform.invert());
740 let primary = DragLeaf::get_primary_value_from_offset(self, app, moved_locally);
741 app.get_mut(self).drag_mut().global_distance_moved +=
742 transform_delta_via_positions(
743 local_position,
744 None,
745 moved_locally,
746 local_to_global_transform,
747 )
748 .distance()
749 * sign(primary.unwrap_or(1.0));
750 let touch_slop = app
751 .get(self)
752 .recognizer()
753 .gesture_settings
754 .and_then(|settings| settings.touch_slop);
755 if DragLeaf::has_sufficient_global_distance_to_accept(
756 self,
757 app,
758 event.kind(),
759 touch_slop,
760 ) {
761 app.get_mut(self).drag_mut().has_drag_threshold_been_met = true;
762 if app
763 .get(self)
764 .drag()
765 .accepted_active_pointers
766 .contains(&event.pointer())
767 {
768 check_drag(self, app, event.pointer());
769 } else {
770 self.resolve(app, GestureDisposition::Accepted);
771 }
772 }
773 }
774 DragState::Accepted => {
775 let delta_for_details =
776 DragLeaf::get_delta_for_details(self, app, resolved_delta);
777 let primary_delta =
778 DragLeaf::get_primary_value_from_offset(self, app, resolved_delta);
779 check_update(
780 self,
781 app,
782 Some(event.time_stamp()),
783 delta_for_details,
784 primary_delta,
785 position,
786 Some(local_position),
787 event.pointer(),
788 );
789 }
790 }
791 record_move_delta_for_multitouch(self, app, event.pointer(), local_delta);
792 }
793 if matches!(
794 event,
795 PointerEvent::Up(_) | PointerEvent::Cancel(_) | PointerEvent::PanZoomEnd(_)
796 ) {
797 give_up_pointer(self, app, event.pointer());
798 }
799 }
800
801 fn accept_gesture(self: Handle<Self>, app: &mut App, pointer: i64) {
802 debug_assert!(
803 !app.get(self)
804 .drag()
805 .accepted_active_pointers
806 .contains(&pointer)
807 );
808 app.get_mut(self)
809 .drag_mut()
810 .accepted_active_pointers
811 .push(pointer);
812 app.get_mut(self).drag_mut().active_pointer = Some(pointer);
813 let drag = app.get(self).drag();
814 if !drag.only_accept_drag_on_threshold || drag.has_drag_threshold_been_met {
815 check_drag(self, app, pointer);
816 }
817 }
818
819 fn reject_gesture(self: Handle<Self>, app: &mut App, pointer: i64) {
820 give_up_pointer(self, app, pointer);
821 }
822
823 fn did_stop_tracking_last_pointer(self: Handle<Self>, app: &mut App, pointer: i64) {
824 debug_assert!(app.get(self).drag().state != DragState::Ready);
825 match app.get(self).drag().state {
826 DragState::Ready => {}
827 DragState::Possible => {
828 self.resolve(app, GestureDisposition::Rejected);
829 check_cancel(self, app);
830 }
831 DragState::Accepted => check_end(self, app, pointer),
832 }
833 let drag = app.get_mut(self).drag_mut();
834 drag.has_drag_threshold_been_met = false;
835 drag.velocity_trackers.clear();
836 drag.initial_buttons = None;
837 drag.state = DragState::Ready;
838 }
839
840 fn dispose(self: Handle<Self>, app: &mut App) {
841 app.get_mut(self).drag_mut().velocity_trackers.clear();
842 OneSequenceGestureRecognizer::dispose(self, app);
843 }
844}
845
846fn add_pointer<L: DragLeaf>(this: Handle<L>, app: &mut App, event: &PointerEvent) {
848 let builder = Rc::clone(&app.get(this).drag().velocity_tracker_builder);
849 let tracker = builder(event);
850 app.get_mut(this)
851 .drag_mut()
852 .velocity_trackers
853 .insert(event.pointer(), tracker);
854 match app.get(this).drag().state {
855 DragState::Ready => {
856 let initial_position = OffsetPair::new(event.local_position(), event.position());
857 let drag = app.get_mut(this).drag_mut();
858 drag.state = DragState::Possible;
859 drag.initial_position = initial_position;
860 drag.last_position = initial_position;
861 drag.pending_drag_offset = OffsetPair::ZERO;
862 drag.global_distance_moved = 0.0;
863 drag.last_pending_event_timestamp = Some(event.time_stamp());
864 drag.last_transform = event.transform();
865 check_down(this, app);
866 }
867 DragState::Possible => {}
868 DragState::Accepted => this.resolve(app, GestureDisposition::Accepted),
869 }
870}
871
872fn should_track_move_event<L: DragLeaf>(this: Handle<L>, app: &App, pointer: i64) -> bool {
873 let drag = app.get(this).drag();
874 match drag.multitouch_drag_strategy {
875 MultitouchDragStrategy::SumAllPointers
876 | MultitouchDragStrategy::AverageBoundaryPointers => true,
877 MultitouchDragStrategy::LatestPointer => {
878 drag.active_pointer.is_none() || Some(pointer) == drag.active_pointer
879 }
880 }
881}
882
883fn record_move_delta_for_multitouch<L: DragLeaf>(
884 this: Handle<L>,
885 app: &mut App,
886 pointer: i64,
887 local_delta: Offset,
888) {
889 if app.get(this).drag().multitouch_drag_strategy
890 != MultitouchDragStrategy::AverageBoundaryPointers
891 {
892 debug_assert!(app.get(this).drag().frame_time_stamp.is_none());
893 debug_assert!(app.get(this).drag().move_delta_before_frame.is_empty());
894 return;
895 }
896
897 if cfg!(debug_assertions) {
898 let frame_time_stamp = app.get(this).drag().frame_time_stamp;
899 debug_assert_eq!(
900 frame_time_stamp,
901 Some(SchedulerBinding::current_system_frame_time_stamp(app))
902 );
903 }
904
905 if app.get(this).drag().state != DragState::Accepted || local_delta == Offset::ZERO {
906 return;
907 }
908
909 let entry = app
910 .get_mut(this)
911 .drag_mut()
912 .move_delta_before_frame
913 .entry(pointer)
914 .or_insert(Offset::ZERO);
915 *entry = *entry + local_delta;
916}
917
918fn get_sum_delta<L: DragLeaf>(
919 this: Handle<L>,
920 app: &App,
921 pointer: i64,
922 positive: bool,
923 axis: DragDirection,
924) -> f64 {
925 let Some(offset) = app.get(this).drag().move_delta_before_frame.get(&pointer) else {
926 return 0.0;
927 };
928 if positive {
929 if axis == DragDirection::Vertical {
930 offset.dy().max(0.0)
931 } else {
932 offset.dx().max(0.0)
933 }
934 } else if axis == DragDirection::Vertical {
935 offset.dy().min(0.0)
936 } else {
937 offset.dx().min(0.0)
938 }
939}
940
941fn get_max_sum_delta_pointer<L: DragLeaf>(
942 this: Handle<L>,
943 app: &App,
944 positive: bool,
945 axis: DragDirection,
946) -> Option<i64> {
947 if app.get(this).drag().move_delta_before_frame.is_empty() {
948 return None;
949 }
950
951 let pointers: Vec<i64> = app
952 .get(this)
953 .drag()
954 .move_delta_before_frame
955 .keys()
956 .copied()
957 .collect();
958 let mut ret: Option<i64> = None;
959 let mut max = 0.0;
960 for pointer in pointers {
961 let sum = get_sum_delta(this, app, pointer, positive, axis);
962 match ret {
963 None => {
964 ret = Some(pointer);
965 max = sum;
966 }
967 Some(_) => {
968 if positive {
969 if sum > max {
970 ret = Some(pointer);
971 max = sum;
972 }
973 } else if sum < max {
974 ret = Some(pointer);
975 max = sum;
976 }
977 }
978 }
979 }
980 debug_assert!(ret.is_some());
981 ret
982}
983
984fn resolve_local_delta_for_multitouch<L: DragLeaf>(
985 this: Handle<L>,
986 app: &mut App,
987 pointer: i64,
988 local_delta: Offset,
989) -> Offset {
990 if app.get(this).drag().multitouch_drag_strategy
991 != MultitouchDragStrategy::AverageBoundaryPointers
992 {
993 if app.get(this).drag().frame_time_stamp.is_some() {
994 let drag = app.get_mut(this).drag_mut();
995 drag.move_delta_before_frame.clear();
996 drag.frame_time_stamp = None;
997 drag.last_updated_delta_for_pan = Offset::ZERO;
998 }
999 return local_delta;
1000 }
1001
1002 let current_system_frame_time_stamp = SchedulerBinding::current_system_frame_time_stamp(app);
1003 if app.get(this).drag().frame_time_stamp != Some(current_system_frame_time_stamp) {
1004 let drag = app.get_mut(this).drag_mut();
1005 drag.move_delta_before_frame.clear();
1006 drag.last_updated_delta_for_pan = Offset::ZERO;
1007 drag.frame_time_stamp = Some(current_system_frame_time_stamp);
1008 }
1009
1010 let axis = DragLeaf::get_primary_drag_axis(this, app);
1011
1012 if app.get(this).drag().state != DragState::Accepted
1013 || local_delta == Offset::ZERO
1014 || (app.get(this).drag().move_delta_before_frame.is_empty() && axis.is_some())
1015 {
1016 return local_delta;
1017 }
1018
1019 let (dx, dy);
1020 if axis == Some(DragDirection::Horizontal) {
1021 dx = resolve_delta(this, app, pointer, DragDirection::Horizontal, local_delta);
1022 debug_assert!(dx.abs() <= local_delta.dx().abs());
1023 dy = 0.0;
1024 } else if axis == Some(DragDirection::Vertical) {
1025 dx = 0.0;
1026 dy = resolve_delta(this, app, pointer, DragDirection::Vertical, local_delta);
1027 debug_assert!(dy.abs() <= local_delta.dy().abs());
1028 } else {
1029 let average_x =
1030 resolve_delta_for_pan_gesture(this, app, DragDirection::Horizontal, local_delta);
1031 let average_y =
1032 resolve_delta_for_pan_gesture(this, app, DragDirection::Vertical, local_delta);
1033 let updated_delta =
1034 Offset::new(average_x, average_y) - app.get(this).drag().last_updated_delta_for_pan;
1035 app.get_mut(this).drag_mut().last_updated_delta_for_pan = Offset::new(average_x, average_y);
1036 dx = updated_delta.dx();
1037 dy = updated_delta.dy();
1038 }
1039
1040 Offset::new(dx, dy)
1041}
1042
1043fn resolve_delta<L: DragLeaf>(
1044 this: Handle<L>,
1045 app: &App,
1046 pointer: i64,
1047 axis: DragDirection,
1048 local_delta: Offset,
1049) -> f64 {
1050 let positive = if axis == DragDirection::Horizontal {
1051 local_delta.dx() > 0.0
1052 } else {
1053 local_delta.dy() > 0.0
1054 };
1055 let delta = if axis == DragDirection::Horizontal {
1056 local_delta.dx()
1057 } else {
1058 local_delta.dy()
1059 };
1060 let max_sum_delta_pointer = get_max_sum_delta_pointer(this, app, positive, axis);
1061 debug_assert!(max_sum_delta_pointer.is_some());
1062
1063 if max_sum_delta_pointer == Some(pointer) {
1064 delta
1065 } else {
1066 let max_sum_delta =
1067 get_sum_delta(this, app, max_sum_delta_pointer.unwrap(), positive, axis);
1068 let cur_pointer_sum_delta = get_sum_delta(this, app, pointer, positive, axis);
1069 if positive {
1070 if cur_pointer_sum_delta + delta > max_sum_delta {
1071 cur_pointer_sum_delta + delta - max_sum_delta
1072 } else {
1073 0.0
1074 }
1075 } else if cur_pointer_sum_delta + delta < max_sum_delta {
1076 cur_pointer_sum_delta + delta - max_sum_delta
1077 } else {
1078 0.0
1079 }
1080 }
1081}
1082
1083fn resolve_delta_for_pan_gesture<L: DragLeaf>(
1084 this: Handle<L>,
1085 app: &App,
1086 axis: DragDirection,
1087 local_delta: Offset,
1088) -> f64 {
1089 let delta = if axis == DragDirection::Horizontal {
1090 local_delta.dx()
1091 } else {
1092 local_delta.dy()
1093 };
1094 let pointer_count = app.get(this).drag().accepted_active_pointers.len();
1095 debug_assert!(pointer_count >= 1);
1096
1097 let mut sum = delta;
1098 for offset in app.get(this).drag().move_delta_before_frame.values() {
1099 if axis == DragDirection::Horizontal {
1100 sum += offset.dx();
1101 } else {
1102 sum += offset.dy();
1103 }
1104 }
1105 sum / pointer_count as f64
1106}
1107
1108fn give_up_pointer<L: DragLeaf>(this: Handle<L>, app: &mut App, pointer: i64) {
1109 OneSequenceGestureRecognizer::stop_tracking_pointer(this, app, pointer);
1110 let accepted = app
1113 .get(this)
1114 .drag()
1115 .accepted_active_pointers
1116 .iter()
1117 .position(|&p| p == pointer);
1118 match accepted {
1119 Some(index) => {
1120 app.get_mut(this)
1121 .drag_mut()
1122 .accepted_active_pointers
1123 .remove(index);
1124 }
1125 None => {
1126 OneSequenceGestureRecognizer::resolve_pointer(
1127 this,
1128 app,
1129 pointer,
1130 GestureDisposition::Rejected,
1131 );
1132 }
1133 }
1134
1135 let drag = app.get_mut(this).drag_mut();
1136 drag.move_delta_before_frame.shift_remove(&pointer);
1137 if drag.active_pointer == Some(pointer) {
1138 drag.active_pointer = drag.accepted_active_pointers.first().copied();
1139 }
1140}
1141
1142fn check_down<L: DragLeaf>(this: Handle<L>, app: &mut App) {
1143 if let Some(callback) = app.get(this).drag().on_down.clone() {
1144 let initial_position = app.get(this).drag().initial_position;
1145 let details = DragDownDetails::new(initial_position.global, Some(initial_position.local));
1146 GestureRecognizer::invoke_callback(this, app, "onDown", |app| callback(app, details));
1147 }
1148}
1149
1150fn check_drag<L: DragLeaf>(this: Handle<L>, app: &mut App, pointer: i64) {
1151 if app.get(this).drag().state == DragState::Accepted {
1152 return;
1153 }
1154 app.get_mut(this).drag_mut().state = DragState::Accepted;
1155 let delta = app.get(this).drag().pending_drag_offset;
1156 let timestamp = app.get(this).drag().last_pending_event_timestamp;
1157 let transform = app.get(this).drag().last_transform;
1158 let local_update_delta = match app.get(this).drag().drag_start_behavior {
1159 DragStartBehavior::Start => {
1160 let initial_position = app.get(this).drag().initial_position;
1161 app.get_mut(this).drag_mut().initial_position = initial_position + delta;
1162 Offset::ZERO
1163 }
1164 DragStartBehavior::Down => DragLeaf::get_delta_for_details(this, app, delta.local),
1165 };
1166 let drag = app.get_mut(this).drag_mut();
1167 drag.pending_drag_offset = OffsetPair::ZERO;
1168 drag.last_pending_event_timestamp = None;
1169 drag.last_transform = None;
1170 check_start(this, app, timestamp, pointer);
1171 if local_update_delta != Offset::ZERO && app.get(this).drag().on_update.is_some() {
1172 let local_to_global = transform.and_then(|transform| transform.invert());
1173 let corrected_local_position =
1174 app.get(this).drag().initial_position.local + local_update_delta;
1175 let global_update_delta = transform_delta_via_positions(
1176 corrected_local_position,
1177 None,
1178 local_update_delta,
1179 local_to_global,
1180 );
1181 let update_delta = OffsetPair::new(local_update_delta, global_update_delta);
1182 let corrected_position = app.get(this).drag().initial_position + update_delta;
1184 let primary_delta = DragLeaf::get_primary_value_from_offset(this, app, local_update_delta);
1185 check_update(
1186 this,
1187 app,
1188 timestamp,
1189 local_update_delta,
1190 primary_delta,
1191 corrected_position.global,
1192 Some(corrected_position.local),
1193 pointer,
1194 );
1195 }
1196 this.resolve(app, GestureDisposition::Accepted);
1200}
1201
1202fn check_start<L: DragLeaf>(
1203 this: Handle<L>,
1204 app: &mut App,
1205 timestamp: Option<Duration>,
1206 pointer: i64,
1207) {
1208 if let Some(callback) = app.get(this).drag().on_start.clone() {
1209 let initial_position = app.get(this).drag().initial_position;
1210 let details = DragStartDetails::new(
1211 initial_position.global,
1212 Some(initial_position.local),
1213 timestamp,
1214 Some(GestureRecognizer::kind_for_pointer(this, app, pointer)),
1215 );
1216 GestureRecognizer::invoke_callback(this, app, "onStart", |app| callback(app, details));
1217 }
1218}
1219
1220#[allow(clippy::too_many_arguments)]
1221fn check_update<L: DragLeaf>(
1222 this: Handle<L>,
1223 app: &mut App,
1224 source_time_stamp: Option<Duration>,
1225 delta: Offset,
1226 primary_delta: Option<f64>,
1227 global_position: Offset,
1228 local_position: Option<Offset>,
1229 pointer: i64,
1230) {
1231 if let Some(callback) = app.get(this).drag().on_update.clone() {
1232 let details = DragUpdateDetails::new(
1233 global_position,
1234 local_position,
1235 source_time_stamp,
1236 delta,
1237 primary_delta,
1238 Some(GestureRecognizer::kind_for_pointer(this, app, pointer)),
1239 );
1240 GestureRecognizer::invoke_callback(this, app, "onUpdate", |app| callback(app, details));
1241 }
1242}
1243
1244fn check_end<L: DragLeaf>(this: Handle<L>, app: &mut App, pointer: i64) {
1245 let Some(callback) = app.get(this).drag().on_end.clone() else {
1246 return;
1247 };
1248
1249 let (estimate, kind) = {
1250 let tracker = &app.get(this).drag().velocity_trackers[&pointer];
1251 (tracker.get_velocity_estimate(app.clock()), tracker.kind())
1252 };
1253
1254 let details = estimate
1255 .and_then(|estimate| DragLeaf::consider_fling(this, app, &estimate, kind))
1256 .unwrap_or_else(|| {
1257 let last_position = app.get(this).drag().last_position;
1258 DragEndDetails::new(
1259 last_position.global,
1260 Some(last_position.local),
1261 Velocity::ZERO,
1262 Some(0.0),
1263 )
1264 });
1265
1266 GestureRecognizer::invoke_callback(this, app, "onEnd", |app| callback(app, details));
1267}
1268
1269fn check_cancel<L: DragLeaf>(this: Handle<L>, app: &mut App) {
1270 if let Some(callback) = app.get(this).drag().on_cancel.clone() {
1271 GestureRecognizer::invoke_callback(this, app, "onCancel", |app| callback.call(app));
1272 }
1273}
1274
1275macro_rules! drag_gesture_recognizer_leaf {
1279 ($name:ident, $description:literal) => {
1280 impl $name {
1281 pub fn new(app: &mut App) -> Ref<$name> {
1283 let mut recognizer = GestureRecognizerData::new();
1284 recognizer.allowed_buttons_filter = Rc::new(default_button_accept_behavior);
1285 app.create($name {
1286 recognizer,
1287 one_sequence: OneSequenceData::new(),
1288 drag: DragData::new(),
1289 })
1290 }
1291
1292 pub fn supported_devices(
1294 self: Handle<Self>,
1295 app: &mut App,
1296 devices: impl IntoIterator<Item = PointerDeviceKind>,
1297 ) -> Handle<$name> {
1298 app.get_mut(self).recognizer.supported_devices =
1299 Some(devices.into_iter().collect());
1300 self
1301 }
1302
1303 pub fn set_supported_devices(
1306 self: Handle<Self>,
1307 app: &mut App,
1308 devices: Option<std::collections::HashSet<PointerDeviceKind>>,
1309 ) {
1310 app.get_mut(self).recognizer.supported_devices = devices;
1311 }
1312
1313 pub fn allowed_buttons_filter(
1316 self: Handle<Self>,
1317 app: &mut App,
1318 filter: impl Fn(i64) -> bool + 'static,
1319 ) -> Handle<$name> {
1320 app.get_mut(self).recognizer.allowed_buttons_filter = Rc::new(filter);
1321 self
1322 }
1323
1324 pub fn gesture_settings(
1327 self: Handle<Self>,
1328 app: &App,
1329 ) -> Option<DeviceGestureSettings> {
1330 app.get(self).recognizer.gesture_settings
1331 }
1332
1333 pub fn set_gesture_settings(
1335 self: Handle<Self>,
1336 app: &mut App,
1337 settings: Option<DeviceGestureSettings>,
1338 ) {
1339 app.get_mut(self).recognizer.gesture_settings = settings;
1340 }
1341
1342 pub fn team(self: Handle<Self>, app: &App) -> Option<Handle<GestureArenaTeam>> {
1344 app.get(self).one_sequence.team.handle()
1345 }
1346
1347 pub fn set_team(self: Handle<Self>, app: &mut App, value: Ref<GestureArenaTeam>) {
1349 debug_assert!(app.get(self).one_sequence.entries.is_empty());
1350 debug_assert!(app.get(self).one_sequence.tracked_pointers.is_empty());
1351 debug_assert!(app.get(self).one_sequence.team.is_none());
1352 app.get_mut(self).one_sequence.team = Some(value);
1353 }
1354
1355 pub fn add_pointer(self: Handle<Self>, app: &mut App, event: PointerDownEvent) {
1358 GestureRecognizer::add_pointer(self, app, event);
1359 }
1360
1361 pub fn add_pointer_pan_zoom(
1364 self: Handle<Self>,
1365 app: &mut App,
1366 event: PointerPanZoomStartEvent,
1367 ) {
1368 GestureRecognizer::add_pointer_pan_zoom(self, app, event);
1369 }
1370
1371 pub fn debug_description(self: Handle<Self>) -> &'static str {
1374 <Self as RecognizerLeaf>::debug_description(self)
1375 }
1376 }
1377
1378 impl RecognizerLeafData for $name {
1379 fn recognizer(&self) -> &GestureRecognizerData {
1380 &self.recognizer
1381 }
1382 fn recognizer_mut(&mut self) -> &mut GestureRecognizerData {
1383 &mut self.recognizer
1384 }
1385 }
1386
1387 impl OneSequenceLeafData for $name {
1388 fn one_sequence(&self) -> &OneSequenceData {
1389 &self.one_sequence
1390 }
1391 fn one_sequence_mut(&mut self) -> &mut OneSequenceData {
1392 &mut self.one_sequence
1393 }
1394 }
1395
1396 impl DragLeafData for $name {
1397 fn drag(&self) -> &DragData {
1398 &self.drag
1399 }
1400 fn drag_mut(&mut self) -> &mut DragData {
1401 &mut self.drag
1402 }
1403 }
1404
1405 impl RecognizerLeaf for $name {
1406 fn handle_non_allowed_pointer(
1407 self: Handle<Self>,
1408 app: &mut App,
1409 _event: &PointerDownEvent,
1410 ) {
1411 OneSequenceGestureRecognizer::handle_non_allowed_pointer(self, app);
1412 }
1413
1414 fn is_pointer_allowed(self: Handle<Self>, app: &App, event: &PointerDownEvent) -> bool {
1415 DragGestureRecognizer::is_pointer_allowed(self, app, event)
1416 }
1417
1418 fn add_allowed_pointer(self: Handle<Self>, app: &mut App, event: PointerDownEvent) {
1419 DragGestureRecognizer::add_allowed_pointer(self, app, event);
1420 }
1421
1422 fn add_allowed_pointer_pan_zoom(
1423 self: Handle<Self>,
1424 app: &mut App,
1425 event: PointerPanZoomStartEvent,
1426 ) {
1427 DragGestureRecognizer::add_allowed_pointer_pan_zoom(self, app, event);
1428 }
1429
1430 fn handle_event(self: Handle<Self>, app: &mut App, event: PointerEvent) {
1431 DragGestureRecognizer::handle_event(self, app, event);
1432 }
1433
1434 fn accept_gesture(self: Handle<Self>, app: &mut App, pointer: i64) {
1435 DragGestureRecognizer::accept_gesture(self, app, pointer);
1436 }
1437
1438 fn reject_gesture(self: Handle<Self>, app: &mut App, pointer: i64) {
1439 DragGestureRecognizer::reject_gesture(self, app, pointer);
1440 }
1441
1442 fn did_stop_tracking_last_pointer(self: Handle<Self>, app: &mut App, pointer: i64) {
1443 DragGestureRecognizer::did_stop_tracking_last_pointer(self, app, pointer);
1444 }
1445
1446 fn dispose(self: Handle<Self>, app: &mut App) {
1447 DragGestureRecognizer::dispose(self, app);
1448 }
1449
1450 fn debug_description(self: Handle<Self>) -> &'static str {
1451 $description
1452 }
1453 }
1454 };
1455}
1456
1457pub struct VerticalDragGestureRecognizer {
1466 recognizer: GestureRecognizerData,
1467 one_sequence: OneSequenceData,
1468 drag: DragData,
1469}
1470
1471drag_gesture_recognizer_leaf!(VerticalDragGestureRecognizer, "vertical drag");
1472
1473pub struct VerticalDragGestureRecognizerBase;
1475
1476impl VerticalDragGestureRecognizerBase {
1477 pub fn is_fling_gesture<R: DragLeaf>(
1479 this: Handle<R>,
1480 app: &App,
1481 estimate: &VelocityEstimate,
1482 kind: PointerDeviceKind,
1483 ) -> bool {
1484 let min_velocity = app
1485 .get(this)
1486 .drag()
1487 .min_fling_velocity
1488 .unwrap_or(K_MIN_FLING_VELOCITY);
1489 let min_distance =
1490 app.get(this).drag().min_fling_distance.unwrap_or_else(|| {
1491 compute_hit_slop(kind, app.get(this).recognizer().gesture_settings)
1492 });
1493 estimate.pixels_per_second.dy().abs() > min_velocity
1494 && estimate.offset.dy().abs() > min_distance
1495 }
1496
1497 pub fn consider_fling<R: DragLeaf>(
1499 this: Handle<R>,
1500 app: &App,
1501 estimate: &VelocityEstimate,
1502 kind: PointerDeviceKind,
1503 ) -> Option<DragEndDetails> {
1504 if !VerticalDragGestureRecognizerBase::is_fling_gesture(this, app, estimate, kind) {
1505 return None;
1506 }
1507 let max_velocity = app
1508 .get(this)
1509 .drag()
1510 .max_fling_velocity
1511 .unwrap_or(K_MAX_FLING_VELOCITY);
1512 let dy = estimate
1513 .pixels_per_second
1514 .dy()
1515 .clamp(-max_velocity, max_velocity);
1516 let last_position = app.get(this).drag().last_position;
1517 Some(DragEndDetails::new(
1518 last_position.global,
1519 Some(last_position.local),
1520 Velocity::new(Offset::new(0.0, dy)),
1521 Some(dy),
1522 ))
1523 }
1524
1525 pub fn has_sufficient_global_distance_to_accept<R: DragLeaf>(
1527 this: Handle<R>,
1528 app: &App,
1529 pointer_device_kind: PointerDeviceKind,
1530 _device_touch_slop: Option<f64>,
1531 ) -> bool {
1532 app.get(this).drag().global_distance_moved.abs()
1533 > compute_hit_slop(
1534 pointer_device_kind,
1535 app.get(this).recognizer().gesture_settings,
1536 )
1537 }
1538
1539 pub fn get_delta_for_details<R: DragLeaf>(
1541 _this: Handle<R>,
1542 _app: &App,
1543 delta: Offset,
1544 ) -> Offset {
1545 Offset::new(0.0, delta.dy())
1546 }
1547
1548 pub fn get_primary_value_from_offset<R: DragLeaf>(
1550 _this: Handle<R>,
1551 _app: &App,
1552 value: Offset,
1553 ) -> Option<f64> {
1554 Some(value.dy())
1555 }
1556
1557 pub fn get_primary_drag_axis<R: DragLeaf>(
1559 _this: Handle<R>,
1560 _app: &App,
1561 ) -> Option<DragDirection> {
1562 Some(DragDirection::Vertical)
1563 }
1564}
1565
1566impl DragLeaf for VerticalDragGestureRecognizer {
1567 fn is_fling_gesture(
1568 self: Handle<Self>,
1569 app: &App,
1570 estimate: &VelocityEstimate,
1571 kind: PointerDeviceKind,
1572 ) -> bool {
1573 VerticalDragGestureRecognizerBase::is_fling_gesture(self, app, estimate, kind)
1574 }
1575
1576 fn consider_fling(
1577 self: Handle<Self>,
1578 app: &App,
1579 estimate: &VelocityEstimate,
1580 kind: PointerDeviceKind,
1581 ) -> Option<DragEndDetails> {
1582 VerticalDragGestureRecognizerBase::consider_fling(self, app, estimate, kind)
1583 }
1584
1585 fn has_sufficient_global_distance_to_accept(
1586 self: Handle<Self>,
1587 app: &App,
1588 pointer_device_kind: PointerDeviceKind,
1589 device_touch_slop: Option<f64>,
1590 ) -> bool {
1591 VerticalDragGestureRecognizerBase::has_sufficient_global_distance_to_accept(
1592 self,
1593 app,
1594 pointer_device_kind,
1595 device_touch_slop,
1596 )
1597 }
1598
1599 fn get_delta_for_details(self: Handle<Self>, app: &App, delta: Offset) -> Offset {
1600 VerticalDragGestureRecognizerBase::get_delta_for_details(self, app, delta)
1601 }
1602
1603 fn get_primary_value_from_offset(self: Handle<Self>, app: &App, value: Offset) -> Option<f64> {
1604 VerticalDragGestureRecognizerBase::get_primary_value_from_offset(self, app, value)
1605 }
1606
1607 fn get_primary_drag_axis(self: Handle<Self>, app: &App) -> Option<DragDirection> {
1608 VerticalDragGestureRecognizerBase::get_primary_drag_axis(self, app)
1609 }
1610}
1611
1612pub struct HorizontalDragGestureRecognizer {
1621 recognizer: GestureRecognizerData,
1622 one_sequence: OneSequenceData,
1623 drag: DragData,
1624}
1625
1626drag_gesture_recognizer_leaf!(HorizontalDragGestureRecognizer, "horizontal drag");
1627
1628pub struct HorizontalDragGestureRecognizerBase;
1630
1631impl HorizontalDragGestureRecognizerBase {
1632 pub fn is_fling_gesture<R: DragLeaf>(
1634 this: Handle<R>,
1635 app: &App,
1636 estimate: &VelocityEstimate,
1637 kind: PointerDeviceKind,
1638 ) -> bool {
1639 let min_velocity = app
1640 .get(this)
1641 .drag()
1642 .min_fling_velocity
1643 .unwrap_or(K_MIN_FLING_VELOCITY);
1644 let min_distance =
1645 app.get(this).drag().min_fling_distance.unwrap_or_else(|| {
1646 compute_hit_slop(kind, app.get(this).recognizer().gesture_settings)
1647 });
1648 estimate.pixels_per_second.dx().abs() > min_velocity
1649 && estimate.offset.dx().abs() > min_distance
1650 }
1651
1652 pub fn consider_fling<R: DragLeaf>(
1654 this: Handle<R>,
1655 app: &App,
1656 estimate: &VelocityEstimate,
1657 kind: PointerDeviceKind,
1658 ) -> Option<DragEndDetails> {
1659 if !HorizontalDragGestureRecognizerBase::is_fling_gesture(this, app, estimate, kind) {
1660 return None;
1661 }
1662 let max_velocity = app
1663 .get(this)
1664 .drag()
1665 .max_fling_velocity
1666 .unwrap_or(K_MAX_FLING_VELOCITY);
1667 let dx = estimate
1668 .pixels_per_second
1669 .dx()
1670 .clamp(-max_velocity, max_velocity);
1671 let last_position = app.get(this).drag().last_position;
1672 Some(DragEndDetails::new(
1673 last_position.global,
1674 Some(last_position.local),
1675 Velocity::new(Offset::new(dx, 0.0)),
1676 Some(dx),
1677 ))
1678 }
1679
1680 pub fn has_sufficient_global_distance_to_accept<R: DragLeaf>(
1682 this: Handle<R>,
1683 app: &App,
1684 pointer_device_kind: PointerDeviceKind,
1685 _device_touch_slop: Option<f64>,
1686 ) -> bool {
1687 app.get(this).drag().global_distance_moved.abs()
1688 > compute_hit_slop(
1689 pointer_device_kind,
1690 app.get(this).recognizer().gesture_settings,
1691 )
1692 }
1693
1694 pub fn get_delta_for_details<R: DragLeaf>(
1696 _this: Handle<R>,
1697 _app: &App,
1698 delta: Offset,
1699 ) -> Offset {
1700 Offset::new(delta.dx(), 0.0)
1701 }
1702
1703 pub fn get_primary_value_from_offset<R: DragLeaf>(
1705 _this: Handle<R>,
1706 _app: &App,
1707 value: Offset,
1708 ) -> Option<f64> {
1709 Some(value.dx())
1710 }
1711
1712 pub fn get_primary_drag_axis<R: DragLeaf>(
1714 _this: Handle<R>,
1715 _app: &App,
1716 ) -> Option<DragDirection> {
1717 Some(DragDirection::Horizontal)
1718 }
1719}
1720
1721impl DragLeaf for HorizontalDragGestureRecognizer {
1722 fn is_fling_gesture(
1723 self: Handle<Self>,
1724 app: &App,
1725 estimate: &VelocityEstimate,
1726 kind: PointerDeviceKind,
1727 ) -> bool {
1728 HorizontalDragGestureRecognizerBase::is_fling_gesture(self, app, estimate, kind)
1729 }
1730
1731 fn consider_fling(
1732 self: Handle<Self>,
1733 app: &App,
1734 estimate: &VelocityEstimate,
1735 kind: PointerDeviceKind,
1736 ) -> Option<DragEndDetails> {
1737 HorizontalDragGestureRecognizerBase::consider_fling(self, app, estimate, kind)
1738 }
1739
1740 fn has_sufficient_global_distance_to_accept(
1741 self: Handle<Self>,
1742 app: &App,
1743 pointer_device_kind: PointerDeviceKind,
1744 device_touch_slop: Option<f64>,
1745 ) -> bool {
1746 HorizontalDragGestureRecognizerBase::has_sufficient_global_distance_to_accept(
1747 self,
1748 app,
1749 pointer_device_kind,
1750 device_touch_slop,
1751 )
1752 }
1753
1754 fn get_delta_for_details(self: Handle<Self>, app: &App, delta: Offset) -> Offset {
1755 HorizontalDragGestureRecognizerBase::get_delta_for_details(self, app, delta)
1756 }
1757
1758 fn get_primary_value_from_offset(self: Handle<Self>, app: &App, value: Offset) -> Option<f64> {
1759 HorizontalDragGestureRecognizerBase::get_primary_value_from_offset(self, app, value)
1760 }
1761
1762 fn get_primary_drag_axis(self: Handle<Self>, app: &App) -> Option<DragDirection> {
1763 HorizontalDragGestureRecognizerBase::get_primary_drag_axis(self, app)
1764 }
1765}
1766
1767pub struct PanGestureRecognizer {
1769 recognizer: GestureRecognizerData,
1770 one_sequence: OneSequenceData,
1771 drag: DragData,
1772}
1773
1774drag_gesture_recognizer_leaf!(PanGestureRecognizer, "pan");
1775
1776impl DragLeaf for PanGestureRecognizer {
1777 fn is_fling_gesture(
1778 self: Handle<Self>,
1779 app: &App,
1780 estimate: &VelocityEstimate,
1781 kind: PointerDeviceKind,
1782 ) -> bool {
1783 let min_velocity = app
1784 .get(self)
1785 .drag
1786 .min_fling_velocity
1787 .unwrap_or(K_MIN_FLING_VELOCITY);
1788 let min_distance =
1789 app.get(self).drag.min_fling_distance.unwrap_or_else(|| {
1790 compute_hit_slop(kind, app.get(self).recognizer.gesture_settings)
1791 });
1792 estimate.pixels_per_second.distance_squared() > min_velocity * min_velocity
1793 && estimate.offset.distance_squared() > min_distance * min_distance
1794 }
1795
1796 fn consider_fling(
1797 self: Handle<Self>,
1798 app: &App,
1799 estimate: &VelocityEstimate,
1800 kind: PointerDeviceKind,
1801 ) -> Option<DragEndDetails> {
1802 if !DragLeaf::is_fling_gesture(self, app, estimate, kind) {
1803 return None;
1804 }
1805 let velocity = Velocity::new(estimate.pixels_per_second).clamp_magnitude(
1806 app.get(self)
1807 .drag
1808 .min_fling_velocity
1809 .unwrap_or(K_MIN_FLING_VELOCITY),
1810 app.get(self)
1811 .drag
1812 .max_fling_velocity
1813 .unwrap_or(K_MAX_FLING_VELOCITY),
1814 );
1815 let last_position = app.get(self).drag.last_position;
1816 Some(DragEndDetails::new(
1817 last_position.global,
1818 Some(last_position.local),
1819 velocity,
1820 None,
1821 ))
1822 }
1823
1824 fn has_sufficient_global_distance_to_accept(
1825 self: Handle<Self>,
1826 app: &App,
1827 pointer_device_kind: PointerDeviceKind,
1828 _device_touch_slop: Option<f64>,
1829 ) -> bool {
1830 app.get(self).drag.global_distance_moved.abs()
1831 > compute_pan_slop(
1832 pointer_device_kind,
1833 app.get(self).recognizer.gesture_settings,
1834 )
1835 }
1836
1837 fn get_delta_for_details(self: Handle<Self>, _app: &App, delta: Offset) -> Offset {
1838 delta
1839 }
1840
1841 fn get_primary_value_from_offset(
1842 self: Handle<Self>,
1843 _app: &App,
1844 _value: Offset,
1845 ) -> Option<f64> {
1846 None
1847 }
1848}
1849
1850#[cfg(test)]
1851mod tests {
1852 use std::cell::{Cell, RefCell};
1853 use waft_foundation::AppCell;
1854
1855 use super::*;
1856 use crate::binding::GestureBinding;
1857 use crate::constants::K_MAX_FLING_VELOCITY;
1858 use crate::events::{
1859 PointerMoveEvent, PointerPanZoomEndEvent, PointerPanZoomUpdateEvent, PointerUpEvent,
1860 };
1861 use crate::tap::TapGestureRecognizer;
1862
1863 struct TestPointer {
1866 pointer: i64,
1867 kind: PointerDeviceKind,
1868 location: Offset,
1869 pan: Offset,
1870 }
1871
1872 impl TestPointer {
1873 fn new(pointer: i64) -> TestPointer {
1874 TestPointer::with_kind(pointer, PointerDeviceKind::Touch)
1875 }
1876
1877 fn with_kind(pointer: i64, kind: PointerDeviceKind) -> TestPointer {
1878 TestPointer {
1879 pointer,
1880 kind,
1881 location: Offset::ZERO,
1882 pan: Offset::ZERO,
1883 }
1884 }
1885
1886 fn down(&mut self, position: Offset, time_stamp: Duration) -> PointerDownEvent {
1887 self.location = position;
1888 PointerDownEvent {
1889 pointer: self.pointer,
1890 kind: self.kind,
1891 position,
1892 time_stamp,
1893 buttons: K_PRIMARY_BUTTON,
1894 ..PointerDownEvent::default()
1895 }
1896 }
1897
1898 fn move_to(&mut self, position: Offset, time_stamp: Duration) -> PointerMoveEvent {
1899 let delta = position - self.location;
1900 self.location = position;
1901 PointerMoveEvent {
1902 pointer: self.pointer,
1903 kind: self.kind,
1904 position,
1905 delta,
1906 time_stamp,
1907 down: true,
1908 buttons: K_PRIMARY_BUTTON,
1909 ..PointerMoveEvent::default()
1910 }
1911 }
1912
1913 fn up(&mut self, time_stamp: Duration) -> PointerUpEvent {
1914 PointerUpEvent {
1915 pointer: self.pointer,
1916 kind: self.kind,
1917 position: self.location,
1918 time_stamp,
1919 ..PointerUpEvent::default()
1920 }
1921 }
1922
1923 fn pan_zoom_start(&mut self, position: Offset) -> PointerPanZoomStartEvent {
1924 self.location = position;
1925 self.pan = Offset::ZERO;
1926 PointerPanZoomStartEvent {
1927 pointer: self.pointer,
1928 kind: self.kind,
1929 position,
1930 ..PointerPanZoomStartEvent::default()
1931 }
1932 }
1933
1934 fn pan_zoom_update(&mut self, position: Offset, pan: Offset) -> PointerPanZoomUpdateEvent {
1935 let pan_delta = pan - self.pan;
1936 self.location = position;
1937 self.pan = pan;
1938 PointerPanZoomUpdateEvent {
1939 pointer: self.pointer,
1940 kind: self.kind,
1941 position,
1942 pan,
1943 pan_delta,
1944 ..PointerPanZoomUpdateEvent::default()
1945 }
1946 }
1947
1948 fn pan_zoom_end(&mut self) -> PointerPanZoomEndEvent {
1949 PointerPanZoomEndEvent {
1950 pointer: self.pointer,
1951 kind: self.kind,
1952 position: self.location,
1953 ..PointerPanZoomEndEvent::default()
1954 }
1955 }
1956 }
1957
1958 fn close_arena(app: &mut App, pointer: i64) {
1959 GestureBinding::instance(app)
1960 .gesture_arena(app)
1961 .close(app, pointer);
1962 }
1963
1964 fn route(app: &mut App, event: PointerEvent) {
1965 GestureBinding::instance(app)
1966 .pointer_router(app)
1967 .route(app, event);
1968 app.drain_microtasks();
1969 }
1970
1971 fn flag() -> (Rc<Cell<bool>>, Rc<Cell<bool>>) {
1972 let value = Rc::new(Cell::new(false));
1973 (Rc::clone(&value), value)
1974 }
1975
1976 #[test]
1977 fn should_recognize_pan() {
1978 let cell = AppCell::new();
1979 let mut app = cell.borrow_mut();
1980 let pan = PanGestureRecognizer::new(&mut app);
1981 let tap = TapGestureRecognizer::new(&mut app);
1982
1983 let (did_start_pan, start_flag) = flag();
1984 pan.set_on_start(
1985 &mut app,
1986 Some(Rc::new(move |_app, _details| start_flag.set(true))),
1987 );
1988 let updated_scroll_delta = Rc::new(Cell::new(None));
1989 let delta_sink = Rc::clone(&updated_scroll_delta);
1990 pan.set_on_update(
1991 &mut app,
1992 Some(Rc::new(move |_app, details: DragUpdateDetails| {
1993 delta_sink.set(Some(details.delta))
1994 })),
1995 );
1996 let (did_end_pan, end_flag) = flag();
1997 pan.set_on_end(
1998 &mut app,
1999 Some(Rc::new(move |_app, _details| end_flag.set(true))),
2000 );
2001 let (did_tap, tap_flag) = flag();
2002 tap.set_on_tap(
2003 &mut app,
2004 Some(Listener::new(move |_app| tap_flag.set(true))),
2005 );
2006
2007 let mut pointer = TestPointer::new(5);
2008 let down = pointer.down(Offset::new(10.0, 10.0), Duration::ZERO);
2009 pan.add_pointer(&mut app, down.clone());
2010 tap.add_pointer(&mut app, down.clone());
2011 close_arena(&mut app, 5);
2012 assert!(!did_start_pan.get());
2013 assert!(updated_scroll_delta.get().is_none());
2014 assert!(!did_end_pan.get());
2015 assert!(!did_tap.get());
2016
2017 route(&mut app, PointerEvent::Down(down));
2018 assert!(!did_start_pan.get());
2019
2020 let moved = pointer.move_to(Offset::new(20.0, 20.0), Duration::ZERO);
2023 route(&mut app, PointerEvent::Move(moved));
2024 assert!(!did_start_pan.get(), "14 < 18");
2025
2026 let moved = pointer.move_to(Offset::new(20.0, 30.0), Duration::ZERO);
2027 route(&mut app, PointerEvent::Move(moved));
2028 assert!(did_start_pan.get(), "22 > 18");
2029 did_start_pan.set(false);
2030 assert!(!did_end_pan.get());
2031 assert!(!did_tap.get());
2032
2033 let moved = pointer.move_to(Offset::new(20.0, 25.0), Duration::ZERO);
2034 route(&mut app, PointerEvent::Move(moved));
2035 assert!(!did_start_pan.get());
2036 assert_eq!(updated_scroll_delta.get(), Some(Offset::new(0.0, -5.0)));
2037 updated_scroll_delta.set(None);
2038 assert!(!did_end_pan.get());
2039
2040 let up = pointer.up(Duration::ZERO);
2041 route(&mut app, PointerEvent::Up(up));
2042 assert!(updated_scroll_delta.get().is_none());
2043 assert!(did_end_pan.get());
2044 assert!(!did_tap.get());
2045
2046 DragGestureRecognizer::dispose(pan.handle(), &mut app);
2047 tap.dispose(&mut app);
2048 }
2049
2050 #[test]
2051 fn should_report_most_recent_point_to_on_start_by_default() {
2052 let cell = AppCell::new();
2053 let mut app = cell.borrow_mut();
2054 let drag = HorizontalDragGestureRecognizer::new(&mut app);
2055 let competing_drag = VerticalDragGestureRecognizer::new(&mut app);
2056 competing_drag.set_on_start(&mut app, Some(Rc::new(|_app, _details| {})));
2057
2058 let position_at_on_start = Rc::new(Cell::new(None));
2059 let start_sink = Rc::clone(&position_at_on_start);
2060 drag.set_on_start(
2061 &mut app,
2062 Some(Rc::new(move |_app, details: DragStartDetails| {
2063 start_sink.set(Some(details.global_position))
2064 })),
2065 );
2066 let update_offset = Rc::new(Cell::new(None));
2067 let update_sink = Rc::clone(&update_offset);
2068 drag.set_on_update(
2069 &mut app,
2070 Some(Rc::new(move |_app, details: DragUpdateDetails| {
2071 update_sink.set(Some(details.global_position))
2072 })),
2073 );
2074
2075 let mut pointer = TestPointer::new(5);
2076 let down = pointer.down(Offset::new(10.0, 10.0), Duration::ZERO);
2077 drag.add_pointer(&mut app, down.clone());
2078 competing_drag.add_pointer(&mut app, down.clone());
2079 close_arena(&mut app, 5);
2080 route(&mut app, PointerEvent::Down(down));
2081
2082 let moved = pointer.move_to(Offset::new(30.0, 0.0), Duration::ZERO);
2083 route(&mut app, PointerEvent::Move(moved));
2084
2085 assert_eq!(position_at_on_start.get(), Some(Offset::new(30.0, 0.0)));
2086 assert_eq!(update_offset.get(), None);
2087
2088 DragGestureRecognizer::dispose(drag.handle(), &mut app);
2089 DragGestureRecognizer::dispose(competing_drag.handle(), &mut app);
2090 }
2091
2092 #[test]
2093 fn should_recognize_drag_with_the_down_start_behavior() {
2094 let cell = AppCell::new();
2095 let mut app = cell.borrow_mut();
2096 let drag = HorizontalDragGestureRecognizer::new(&mut app);
2097 drag.set_drag_start_behavior(&mut app, DragStartBehavior::Down);
2098
2099 let (did_start_drag, start_flag) = flag();
2100 drag.set_on_start(
2101 &mut app,
2102 Some(Rc::new(move |_app, _details| start_flag.set(true))),
2103 );
2104 let updated_delta = Rc::new(Cell::new(None));
2105 let delta_sink = Rc::clone(&updated_delta);
2106 drag.set_on_update(
2107 &mut app,
2108 Some(Rc::new(move |_app, details: DragUpdateDetails| {
2109 delta_sink.set(details.primary_delta)
2110 })),
2111 );
2112 let (did_end_drag, end_flag) = flag();
2113 drag.set_on_end(
2114 &mut app,
2115 Some(Rc::new(move |_app, _details| end_flag.set(true))),
2116 );
2117
2118 let mut pointer = TestPointer::new(5);
2119 let down = pointer.down(Offset::new(10.0, 10.0), Duration::ZERO);
2120 drag.add_pointer(&mut app, down.clone());
2121 close_arena(&mut app, 5);
2122 assert!(!did_start_drag.get());
2123 assert!(updated_delta.get().is_none());
2124 assert!(!did_end_drag.get());
2125
2126 route(&mut app, PointerEvent::Down(down));
2127 assert!(did_start_drag.get());
2128 assert!(updated_delta.get().is_none());
2129 assert!(!did_end_drag.get());
2130
2131 let moved = pointer.move_to(Offset::new(20.0, 25.0), Duration::ZERO);
2132 route(&mut app, PointerEvent::Move(moved));
2133 did_start_drag.set(false);
2134 assert_eq!(updated_delta.get(), Some(10.0));
2135 updated_delta.set(None);
2136
2137 let moved = pointer.move_to(Offset::new(20.0, 25.0), Duration::ZERO);
2138 route(&mut app, PointerEvent::Move(moved));
2139 assert!(!did_start_drag.get());
2140 assert_eq!(updated_delta.get(), Some(0.0));
2141 updated_delta.set(None);
2142 assert!(!did_end_drag.get());
2143
2144 let up = pointer.up(Duration::ZERO);
2145 route(&mut app, PointerEvent::Up(up));
2146 assert!(!did_start_drag.get());
2147 assert!(updated_delta.get().is_none());
2148 assert!(did_end_drag.get());
2149
2150 DragGestureRecognizer::dispose(drag.handle(), &mut app);
2151 }
2152
2153 #[test]
2154 fn the_vertical_recognizer_wins_a_vertical_move_and_the_horizontal_one_cancels() {
2155 let cell = AppCell::new();
2156 let mut app = cell.borrow_mut();
2157 let vertical = VerticalDragGestureRecognizer::new(&mut app);
2158 let horizontal = HorizontalDragGestureRecognizer::new(&mut app);
2159
2160 let log = Rc::new(RefCell::new(Vec::<&'static str>::new()));
2161 let vertical_log = Rc::clone(&log);
2162 vertical.set_on_start(
2163 &mut app,
2164 Some(Rc::new(move |_app, _details| {
2165 vertical_log.borrow_mut().push("vertical start")
2166 })),
2167 );
2168 let horizontal_log = Rc::clone(&log);
2169 horizontal.set_on_start(
2170 &mut app,
2171 Some(Rc::new(move |_app, _details| {
2172 horizontal_log.borrow_mut().push("horizontal start")
2173 })),
2174 );
2175 let cancel_log = Rc::clone(&log);
2176 horizontal.set_on_cancel(
2177 &mut app,
2178 Some(Listener::new(move |_app| {
2179 cancel_log.borrow_mut().push("horizontal cancel")
2180 })),
2181 );
2182
2183 let mut pointer = TestPointer::new(5);
2184 let down = pointer.down(Offset::new(10.0, 10.0), Duration::ZERO);
2185 vertical.add_pointer(&mut app, down.clone());
2186 horizontal.add_pointer(&mut app, down.clone());
2187 close_arena(&mut app, 5);
2188 route(&mut app, PointerEvent::Down(down));
2189 assert!(log.borrow().is_empty());
2190
2191 let moved = pointer.move_to(Offset::new(10.0, 50.0), Duration::ZERO);
2192 route(&mut app, PointerEvent::Move(moved));
2193 assert_eq!(*log.borrow(), vec!["horizontal cancel", "vertical start"]);
2195
2196 DragGestureRecognizer::dispose(vertical.handle(), &mut app);
2197 DragGestureRecognizer::dispose(horizontal.handle(), &mut app);
2198 }
2199
2200 #[test]
2201 fn clamp_max_velocity() {
2202 let cell = AppCell::new();
2203 let mut app = cell.borrow_mut();
2204 let drag = HorizontalDragGestureRecognizer::new(&mut app);
2205 drag.set_drag_start_behavior(&mut app, DragStartBehavior::Down);
2206
2207 let velocity = Rc::new(Cell::new(Velocity::ZERO));
2208 let primary_velocity = Rc::new(Cell::new(None));
2209 let velocity_sink = Rc::clone(&velocity);
2210 let primary_sink = Rc::clone(&primary_velocity);
2211 drag.set_on_end(
2212 &mut app,
2213 Some(Rc::new(move |_app, details: DragEndDetails| {
2214 velocity_sink.set(details.velocity);
2215 primary_sink.set(details.primary_velocity);
2216 })),
2217 );
2218
2219 let mut pointer = TestPointer::new(5);
2220 let down = pointer.down(Offset::new(10.0, 25.0), Duration::from_millis(10));
2221 drag.add_pointer(&mut app, down.clone());
2222 close_arena(&mut app, 5);
2223 route(&mut app, PointerEvent::Down(down));
2224 for step in 0..11 {
2225 let moved = pointer.move_to(
2226 Offset::new(20.0 + 10.0 * step as f64, 25.0),
2227 Duration::from_millis(10 + step),
2228 );
2229 route(&mut app, PointerEvent::Move(moved));
2230 }
2231 let up = pointer.up(Duration::from_millis(20));
2232 route(&mut app, PointerEvent::Up(up));
2233
2234 let dx = velocity.get().pixels_per_second.dx();
2235 assert!(
2236 (0.99 * K_MAX_FLING_VELOCITY..=K_MAX_FLING_VELOCITY).contains(&dx),
2237 "clamped to at most kMaxFlingVelocity, got {dx}"
2238 );
2239 assert!(velocity.get().pixels_per_second.dy().abs() < 1e-9);
2240 assert_eq!(primary_velocity.get(), Some(dx));
2241
2242 DragGestureRecognizer::dispose(drag.handle(), &mut app);
2243 }
2244
2245 fn drag_callbacks_on_a_drag_that_never_moves(
2246 only_accept_drag_on_threshold: bool,
2247 ) -> Vec<&'static str> {
2248 let cell = AppCell::new();
2249 let mut app = cell.borrow_mut();
2250 let vertical_drag = VerticalDragGestureRecognizer::new(&mut app);
2251 vertical_drag.set_only_accept_drag_on_threshold(&mut app, only_accept_drag_on_threshold);
2252 let log = Rc::new(RefCell::new(Vec::<&'static str>::new()));
2253 let start_log = Rc::clone(&log);
2254 vertical_drag.set_on_start(
2255 &mut app,
2256 Some(Rc::new(move |_app, _details| {
2257 start_log.borrow_mut().push("onStart")
2258 })),
2259 );
2260 let update_log = Rc::clone(&log);
2261 vertical_drag.set_on_update(
2262 &mut app,
2263 Some(Rc::new(move |_app, _details| {
2264 update_log.borrow_mut().push("onUpdate")
2265 })),
2266 );
2267 let end_log = Rc::clone(&log);
2268 vertical_drag.set_on_end(
2269 &mut app,
2270 Some(Rc::new(move |_app, _details| {
2271 end_log.borrow_mut().push("onEnd")
2272 })),
2273 );
2274
2275 let mut pointer = TestPointer::new(6);
2276 let down = pointer.down(Offset::new(10.0, 10.0), Duration::ZERO);
2277 vertical_drag.add_pointer(&mut app, down.clone());
2278 close_arena(&mut app, 6);
2279 route(&mut app, PointerEvent::Down(down));
2280 let up = pointer.up(Duration::ZERO);
2281 route(&mut app, PointerEvent::Up(up));
2282 DragGestureRecognizer::dispose(vertical_drag.handle(), &mut app);
2283 log.borrow().clone()
2284 }
2285
2286 #[test]
2287 fn only_accept_drag_on_threshold_suppresses_callbacks_until_the_threshold_is_met() {
2288 assert!(drag_callbacks_on_a_drag_that_never_moves(true).is_empty());
2289 }
2290
2291 #[test]
2292 fn without_only_accept_drag_on_threshold_winning_the_arena_is_enough() {
2293 assert_eq!(
2294 drag_callbacks_on_a_drag_that_never_moves(false),
2295 vec!["onStart", "onEnd"]
2296 );
2297 }
2298
2299 #[test]
2300 fn should_recognize_pan_gestures_from_platform() {
2301 let cell = AppCell::new();
2302 let mut app = cell.borrow_mut();
2303 let pan = PanGestureRecognizer::new(&mut app);
2304 let competing_pan = PanGestureRecognizer::new(&mut app);
2306 competing_pan.set_on_start(&mut app, Some(Rc::new(|_app, _details| {})));
2307
2308 let (did_start_pan, start_flag) = flag();
2309 pan.set_on_start(
2310 &mut app,
2311 Some(Rc::new(move |_app, _details| start_flag.set(true))),
2312 );
2313 let updated_scroll_delta = Rc::new(Cell::new(None));
2314 let delta_sink = Rc::clone(&updated_scroll_delta);
2315 pan.set_on_update(
2316 &mut app,
2317 Some(Rc::new(move |_app, details: DragUpdateDetails| {
2318 delta_sink.set(Some(details.delta))
2319 })),
2320 );
2321 let (did_end_pan, end_flag) = flag();
2322 pan.set_on_end(
2323 &mut app,
2324 Some(Rc::new(move |_app, _details| end_flag.set(true))),
2325 );
2326
2327 let mut pointer = TestPointer::with_kind(2, PointerDeviceKind::Trackpad);
2328 let start = pointer.pan_zoom_start(Offset::new(10.0, 10.0));
2329 pan.add_pointer_pan_zoom(&mut app, start.clone());
2330 competing_pan.add_pointer_pan_zoom(&mut app, start.clone());
2331 close_arena(&mut app, 2);
2332 assert!(!did_start_pan.get());
2333
2334 route(&mut app, PointerEvent::PanZoomStart(start));
2335 assert!(!did_start_pan.get());
2336
2337 let update = pointer.pan_zoom_update(Offset::new(10.0, 10.0), Offset::new(20.0, 20.0));
2339 route(&mut app, PointerEvent::PanZoomUpdate(update));
2340 assert!(!did_start_pan.get(), "28 < 36");
2341
2342 let update = pointer.pan_zoom_update(Offset::new(10.0, 10.0), Offset::new(30.0, 30.0));
2343 route(&mut app, PointerEvent::PanZoomUpdate(update));
2344 assert!(did_start_pan.get(), "42 > 36");
2345 did_start_pan.set(false);
2346 assert!(!did_end_pan.get());
2347
2348 let update = pointer.pan_zoom_update(Offset::new(10.0, 10.0), Offset::new(30.0, 25.0));
2349 route(&mut app, PointerEvent::PanZoomUpdate(update));
2350 assert!(!did_start_pan.get());
2351 assert_eq!(updated_scroll_delta.get(), Some(Offset::new(0.0, -5.0)));
2352 updated_scroll_delta.set(None);
2353 assert!(!did_end_pan.get());
2354
2355 let end = pointer.pan_zoom_end();
2356 route(&mut app, PointerEvent::PanZoomEnd(end));
2357 assert!(updated_scroll_delta.get().is_none());
2358 assert!(did_end_pan.get());
2359
2360 DragGestureRecognizer::dispose(pan.handle(), &mut app);
2361 DragGestureRecognizer::dispose(competing_pan.handle(), &mut app);
2362 }
2363
2364 #[test]
2365 fn a_recognizer_with_no_callbacks_does_not_compete() {
2366 let cell = AppCell::new();
2367 let mut app = cell.borrow_mut();
2368 let drag = VerticalDragGestureRecognizer::new(&mut app);
2369 let down = PointerDownEvent {
2370 pointer: 5,
2371 buttons: K_PRIMARY_BUTTON,
2372 ..PointerDownEvent::default()
2373 };
2374 assert!(!DragGestureRecognizer::is_pointer_allowed(
2375 drag.handle(),
2376 &app,
2377 &down
2378 ));
2379 drag.set_on_start(&mut app, Some(Rc::new(|_app, _details| {})));
2380 assert!(DragGestureRecognizer::is_pointer_allowed(
2381 drag.handle(),
2382 &app,
2383 &down
2384 ));
2385 DragGestureRecognizer::dispose(drag.handle(), &mut app);
2386 }
2387
2388 #[test]
2389 fn debug_descriptions_name_the_axis() {
2390 let cell = AppCell::new();
2391 let mut app = cell.borrow_mut();
2392 let vertical = VerticalDragGestureRecognizer::new(&mut app);
2393 let horizontal = HorizontalDragGestureRecognizer::new(&mut app);
2394 let pan = PanGestureRecognizer::new(&mut app);
2395 assert_eq!(vertical.debug_description(), "vertical drag");
2396 assert_eq!(horizontal.debug_description(), "horizontal drag");
2397 assert_eq!(pan.debug_description(), "pan");
2398 DragGestureRecognizer::dispose(vertical.handle(), &mut app);
2399 DragGestureRecognizer::dispose(horizontal.handle(), &mut app);
2400 DragGestureRecognizer::dispose(pan.handle(), &mut app);
2401 }
2402}