waft-gestures 0.8.2

Gesture arena and recognizers for Waft
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Flutter counterpart: `gestures/binding.dart`.

use std::collections::{HashMap, VecDeque};
use std::rc::Rc;

use waft_embedder::{Offset, PointerDataPacket, ViewId};
use waft_foundation::{App, Handle, Listener, OptionRetained};

use crate::arena::GestureArenaManager;
use crate::converter::PointerEventConverter;
use crate::debug::{debug_print_hit_test_results, debug_print_mouse_hover_events};
use crate::events::{PointerCancelEvent, PointerEvent};
use crate::hit_test::{HitTestEntry, HitTestResult, HitTestTarget, HitTestable};
use crate::pointer_router::PointerRouter;
use crate::pointer_signal_resolver::PointerSignalResolver;
use waft_foundation::Ref;

/// What Flutter's `RendererBinding` mixin adds on top of `GestureBinding`: it overrides
/// `hitTestInView` to walk the render trees before this binding adds itself, and the start of
/// `dispatchEvent` (the body before `super.dispatchEvent`) to update the mouse tracker.
///
/// Registered with [`GestureBinding::set_overrides`]. A binding that lives in the arena
/// implements [`GestureBindingOverridesObject`] instead; its `Handle` is then this trait.
pub trait GestureBindingOverrides: HitTestable {
    /// Runs at the start of [`GestureBinding::dispatch_event`], before the event is routed.
    fn will_dispatch_event(
        &self,
        app: &mut App,
        event: &PointerEvent,
        hit_test_result: Option<&HitTestResult>,
    );
}

/// The object side of [`GestureBindingOverrides`]: the binding struct implements this, and its
/// `Handle` is then [`HitTestable`] and [`GestureBindingOverrides`].
///
/// Those two take `&self` so [`GestureBinding`] can hold them erased; a crate above cannot
/// implement them for `Handle<ItsType>`, so it implements this one on the type instead.
pub trait GestureBindingOverridesObject: Sized + 'static {
    /// See [`HitTestable::hit_test_in_view`].
    fn hit_test_in_view(
        self: Handle<Self>,
        app: &mut App,
        result: &mut HitTestResult,
        position: Offset,
        view_id: ViewId,
    );

    /// See [`GestureBindingOverrides::will_dispatch_event`].
    fn will_dispatch_event(
        self: Handle<Self>,
        app: &mut App,
        event: &PointerEvent,
        hit_test_result: Option<&HitTestResult>,
    );
}

impl<T: GestureBindingOverridesObject> HitTestable for Handle<T> {
    fn hit_test_in_view(
        &self,
        app: &mut App,
        result: &mut HitTestResult,
        position: Offset,
        view_id: ViewId,
    ) {
        T::hit_test_in_view(*self, app, result, position, view_id);
    }
}

impl<T: GestureBindingOverridesObject> GestureBindingOverrides for Handle<T> {
    fn will_dispatch_event(
        &self,
        app: &mut App,
        event: &PointerEvent,
        hit_test_result: Option<&HitTestResult>,
    ) {
        T::will_dispatch_event(*self, app, event, hit_test_result);
    }
}

impl<T: GestureBindingOverridesObject> HitTestable for Ref<T> {
    fn hit_test_in_view(
        &self,
        app: &mut App,
        result: &mut HitTestResult,
        position: Offset,
        view_id: ViewId,
    ) {
        T::hit_test_in_view(self.handle(), app, result, position, view_id);
    }
}

/// An override object the gesture binding keeps, for one nothing else keeps.
impl<T: GestureBindingOverridesObject> GestureBindingOverrides for Ref<T> {
    fn will_dispatch_event(
        &self,
        app: &mut App,
        event: &PointerEvent,
        hit_test_result: Option<&HitTestResult>,
    ) {
        T::will_dispatch_event(self.handle(), app, event, hit_test_result);
    }
}

/// A binding for the gesture subsystem.
///
/// Dart's `GestureBinding.instance` is [`GestureBinding::instance`], the App's
/// singleton.
#[derive(Default)]
pub struct GestureBinding {
    pending_pointer_events: VecDeque<PointerEvent>,
    /// Filled on first [`GestureBinding::instance`]. `Default` cannot mint Handles.
    pointer_router: Option<Ref<PointerRouter>>,
    gesture_arena: Option<Ref<GestureArenaManager>>,
    pointer_signal_resolver: Option<Ref<PointerSignalResolver>>,
    /// The path each pressed pointer was hit-tested to, its targets retained.
    hit_tests: HashMap<i64, HitTestResult>,
    /// Flutter's `hitTestInView` override point: the renderer binding registers itself here.
    overrides: Option<Rc<dyn GestureBindingOverrides>>,
}

impl GestureBinding {
    /// The current [`GestureBinding`], if one has been created.
    ///
    /// Dart's `GestureBinding.instance`.
    pub fn instance(app: &mut App) -> Handle<GestureBinding> {
        let this: Handle<GestureBinding> = app.singleton();
        if app.get(this).pointer_router.is_none() {
            let router = PointerRouter::new(app);
            let arena = GestureArenaManager::new(app);
            let resolver = PointerSignalResolver::new(app);
            let binding = app.get_mut(this);
            binding.pointer_router = Some(router);
            binding.gesture_arena = Some(arena);
            binding.pointer_signal_resolver = Some(resolver);
        }
        this
    }

    /// A router that routes all pointer events received from the engine.
    pub fn pointer_router(self: Handle<Self>, app: &App) -> Handle<PointerRouter> {
        app.get(self)
            .pointer_router
            .handle()
            .expect("GestureBinding::instance must run first")
    }

    /// The gesture arenas used for disambiguating the meaning of sequences of
    /// pointer events.
    pub fn gesture_arena(self: Handle<Self>, app: &App) -> Handle<GestureArenaManager> {
        app.get(self)
            .gesture_arena
            .handle()
            .expect("GestureBinding::instance must run first")
    }

    /// The resolver used for determining which widget handles a
    /// [`PointerEvent::Scroll`], [`PointerEvent::ScrollInertiaCancel`], or
    /// [`PointerEvent::Scale`] event.
    pub fn pointer_signal_resolver(self: Handle<Self>, app: &App) -> Handle<PointerSignalResolver> {
        app.get(self)
            .pointer_signal_resolver
            .handle()
            .expect("GestureBinding::instance must run first")
    }

    /// Converts a host packet into framework events and flushes them.
    pub fn handle_pointer_data_packet(
        self: Handle<Self>,
        app: &mut App,
        packet: PointerDataPacket,
    ) {
        let platform = app.platform();
        let events: Vec<PointerEvent> = PointerEventConverter::expand(packet.data, |view_id| {
            platform
                .view(view_id)
                .map(|view| view.metrics().device_pixel_ratio)
        })
        .collect();
        app.get_mut(self).pending_pointer_events.extend(events);
        self.flush_pointer_event_queue(app);
    }

    /// Dispatch a [`PointerCancelEvent`] for the given pointer soon.
    ///
    /// The pointer event will be dispatched before the next pointer event and
    /// before the end of the microtask but not within this function call.
    pub fn cancel_pointer(self: Handle<Self>, app: &mut App, pointer: i64) {
        if app.get(self).pending_pointer_events.is_empty() {
            app.schedule_microtask(Listener::handle_method(
                self,
                GestureBinding::flush_pointer_event_queue,
            ));
        }
        app.get_mut(self)
            .pending_pointer_events
            .push_front(PointerEvent::Cancel(PointerCancelEvent {
                pointer,
                ..PointerCancelEvent::default()
            }));
    }

    fn flush_pointer_event_queue(self: Handle<Self>, app: &mut App) {
        while let Some(event) = app.get_mut(self).pending_pointer_events.pop_front() {
            self.handle_pointer_event(app, event);
        }
    }

    /// Dispatch an event to the targets found by a hit test on its position.
    pub fn handle_pointer_event(self: Handle<Self>, app: &mut App, event: PointerEvent) {
        self.handle_pointer_event_immediately(app, event);
    }

    fn handle_pointer_event_immediately(self: Handle<Self>, app: &mut App, event: PointerEvent) {
        let pointer = event.pointer();
        let is_down_or_pan_zoom_start =
            matches!(event, PointerEvent::Down(_) | PointerEvent::PanZoomStart(_));
        let is_up_or_cancel_or_pan_zoom_end = matches!(
            event,
            PointerEvent::Up(_) | PointerEvent::Cancel(_) | PointerEvent::PanZoomEnd(_)
        );
        let is_new_hit_test = matches!(
            event,
            PointerEvent::Down(_)
                | PointerEvent::Hover(_)
                | PointerEvent::Scroll(_)
                | PointerEvent::ScrollInertiaCancel(_)
                | PointerEvent::Scale(_)
                | PointerEvent::PanZoomStart(_)
        );

        // A cached result retains the objects on its path (Dart holds them; see
        // `HitTestResult::keep_targets`), so a target a rebuild destroyed mid-press still
        // receives the pointer's later events, as it does in Flutter.
        let (hit_test_result, cache_after) = if is_new_hit_test {
            debug_assert!(
                !app.get(self).hit_tests.contains_key(&pointer),
                "Pointer unexpectedly has a HitTestResult associated with it."
            );
            let mut hit_test_result = HitTestResult::new();
            self.hit_test_in_view(app, &mut hit_test_result, event.position(), event.view_id());
            (Some(hit_test_result), is_down_or_pan_zoom_start)
        } else if is_up_or_cancel_or_pan_zoom_end {
            (app.get_mut(self).hit_tests.remove(&pointer), false)
        } else if event.down() || matches!(event, PointerEvent::PanZoomUpdate(_)) {
            (app.get_mut(self).hit_tests.remove(&pointer), true)
        } else {
            (None, false)
        };

        if cfg!(debug_assertions)
            && debug_print_hit_test_results()
            && let Some(result) = &hit_test_result
        {
            eprintln!("{event:?}: {result:?}");
        }
        if cfg!(debug_assertions)
            && debug_print_mouse_hover_events()
            && matches!(event, PointerEvent::Hover(_))
        {
            eprintln!("{event:?}");
        }

        if hit_test_result.is_some()
            || matches!(event, PointerEvent::Added(_) | PointerEvent::Removed(_))
        {
            self.dispatch_event(app, event, hit_test_result.as_ref());
        }
        if let Some(mut result) = hit_test_result
            && cache_after
        {
            result.keep_targets(app);
            app.get_mut(self).hit_tests.insert(pointer, result);
        }
    }

    /// Registers the binding layered above this one, as Flutter's `RendererBinding` does by
    /// overriding [`hit_test_in_view`](Self::hit_test_in_view) and
    /// [`dispatch_event`](Self::dispatch_event).
    pub fn set_overrides(
        self: Handle<Self>,
        app: &mut App,
        overrides: Rc<dyn GestureBindingOverrides>,
    ) {
        app.get_mut(self).overrides = Some(overrides);
    }

    /// Determine which [`HitTestTarget`] objects are located at a given position in the
    /// specified view.
    pub fn hit_test_in_view(
        self: Handle<Self>,
        app: &mut App,
        result: &mut HitTestResult,
        position: Offset,
        view_id: ViewId,
    ) {
        let overrides = app.get(self).overrides.clone();
        if let Some(overrides) = overrides {
            overrides.hit_test_in_view(app, result, position, view_id);
        }
        result.add(HitTestEntry::new(self));
    }

    /// Dispatch an event to [`pointer_router`](Self::pointer_router) and the path of a hit test result.
    ///
    /// The `hit_test_result` argument may only be none for [`PointerEvent::Added`]
    /// or [`PointerEvent::Removed`].
    pub fn dispatch_event(
        self: Handle<Self>,
        app: &mut App,
        event: PointerEvent,
        hit_test_result: Option<&HitTestResult>,
    ) {
        let overrides = app.get(self).overrides.clone();
        if let Some(overrides) = overrides {
            overrides.will_dispatch_event(app, &event, hit_test_result);
        }
        let Some(hit_test_result) = hit_test_result else {
            debug_assert!(matches!(
                event,
                PointerEvent::Added(_) | PointerEvent::Removed(_)
            ));
            let router = self.pointer_router(app);
            router.route(app, event);
            return;
        };
        for entry in hit_test_result.path() {
            entry
                .target()
                .handle_event(app, &event.transformed(entry.transform()), entry);
        }
    }

    /// Reset states of [`GestureBinding`].
    ///
    /// This clears the hit test records.
    ///
    /// This is typically called between tests.
    pub fn reset_gesture_binding(self: Handle<Self>, app: &mut App) {
        app.get_mut(self).hit_tests.clear();
    }
}

impl HitTestTarget for Handle<GestureBinding> {
    fn handle_event(&self, app: &mut App, event: &PointerEvent, _entry: &HitTestEntry) {
        let this = *self;
        let router = this.pointer_router(app);
        router.route(app, event.clone());
        let arena = this.gesture_arena(app);
        if matches!(event, PointerEvent::Down(_) | PointerEvent::PanZoomStart(_)) {
            arena.close(app, event.pointer());
        } else if matches!(event, PointerEvent::Up(_) | PointerEvent::PanZoomEnd(_)) {
            arena.sweep(app, event.pointer());
        } else if matches!(
            event,
            PointerEvent::Scroll(_) | PointerEvent::ScrollInertiaCancel(_) | PointerEvent::Scale(_)
        ) {
            this.pointer_signal_resolver(app).resolve(app);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::rc::Rc;
    use waft_foundation::AppCell;

    use crate::events::{PointerDownEvent, PointerMoveEvent, PointerUpEvent};
    use crate::pointer_router::PointerRoute;

    #[test]
    fn down_routes_through_the_binding_and_closes_the_arena() {
        let cell = AppCell::new();
        let mut app = cell.borrow_mut();
        let binding = GestureBinding::instance(&mut app);
        let ran = Rc::new(Cell::new(0));
        let ran_flag = Rc::clone(&ran);
        binding.pointer_router(&app).add_route(
            &mut app,
            1,
            PointerRoute::new(move |_app, _event| {
                ran_flag.set(ran_flag.get() + 1);
            }),
            None,
        );

        binding.handle_pointer_event(
            &mut app,
            PointerEvent::Down(PointerDownEvent {
                pointer: 1,
                ..PointerDownEvent::default()
            }),
        );
        assert_eq!(ran.get(), 1);

        binding.handle_pointer_event(
            &mut app,
            PointerEvent::Move(PointerMoveEvent {
                pointer: 1,
                down: true,
                ..PointerMoveEvent::default()
            }),
        );
        assert_eq!(ran.get(), 2);

        binding.handle_pointer_event(
            &mut app,
            PointerEvent::Up(PointerUpEvent {
                pointer: 1,
                ..PointerUpEvent::default()
            }),
        );
        assert_eq!(ran.get(), 3);
    }
}