dioxus-dnd 3.1.0

Modular, accessible drag-and-drop for Dioxus: sortable lists, kanban boards, trees, grids, file drops, multi-select, touch support and more
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
418
419
420
421
422
423
//! A formal state machine for pointer-driven drag gestures.
//!
//! The lifecycle every synthesized drag goes through - press, threshold
//! promotion, tracking, release or abort - is modeled here as a pure
//! transition function over explicit states and events, so every edge
//! (stray pointer ids, release before the threshold, cancellation mid-drag)
//! is an exhaustive match arm with a test, not an ad-hoc `if`.
//!
//! [`crate::core::Draggable`] drives this machine; you can drive it yourself
//! to build custom pointer interactions with the same rigor:
//!
//! ```rust
//! use dioxus_dnd::core::{transition, GestureEffect, GesturePhase, GestureEvent, Point};
//!
//! let mut phase = GesturePhase::Idle;
//! let (next, fx) = transition(phase, GestureEvent::Down { at: Point::new(10.0, 10.0), pointer_id: 1 }, 8.0);
//! phase = next;
//! assert_eq!(fx, GestureEffect::None); // pressed, not yet a drag
//!
//! let (next, fx) = transition(phase, GestureEvent::Move { at: Point::new(30.0, 10.0), pointer_id: 1 }, 8.0);
//! assert!(matches!(fx, GestureEffect::Begin { .. })); // crossed the threshold
//! # let _ = next;
//! ```

use super::types::Point;

/// Where a pointer gesture currently stands.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GesturePhase {
    /// No interaction in progress.
    Idle,
    /// Pointer is down on a draggable but hasn't traveled past the
    /// threshold - could still resolve as a tap.
    Pressed {
        /// Where the press started (client coordinates).
        origin: Point,
        /// The pointer that owns this gesture.
        pointer_id: i32,
    },
    /// An active drag.
    Dragging {
        /// Where the press started.
        origin: Point,
        /// The pointer that owns this gesture.
        pointer_id: i32,
    },
}

/// An input to the machine.
///
/// Non-exhaustive because the gesture vocabulary provably grows (`Hold`
/// arrived in 2.5); feed events in freely, but never match this
/// exhaustively - treat unknown inputs as inert, like the machine does.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum GestureEvent {
    /// Pointer pressed.
    Down { at: Point, pointer_id: i32 },
    /// Pointer moved.
    Move { at: Point, pointer_id: i32 },
    /// Pointer released.
    Up { at: Point, pointer_id: i32 },
    /// The press's hold timer elapsed while the pointer stayed put: a
    /// long-press. Promotes a matching [`GesturePhase::Pressed`] straight to
    /// a drag; inert in every other phase, so a stale timer firing after the
    /// gesture already resolved is harmless.
    Hold { pointer_id: i32 },
    /// The platform cancelled the gesture (`pointercancel`).
    Cancel,
}

/// How a press gets promoted to a drag - the policy half of the touch
/// auto-sensor.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Promotion {
    /// Travel in any direction past the threshold begins the drag. The right
    /// policy for mouse and pen, and for touch surfaces that own every
    /// gesture (`touch-action: none`).
    #[default]
    Distance,
    /// Touch sharing the viewport with native vertical scrolling
    /// (`touch-action: pan-y`): a [`GestureEvent::Hold`] or a
    /// sideways-dominant pull (|dx| > |dy|) past the threshold begins the
    /// drag, while a vertical-dominant pull resolves the press as scroll
    /// intent - the machine returns to `Idle` and the browser's pan takes
    /// the gesture (an exact diagonal counts as scroll).
    HoldOrSideways,
}

/// What the caller should do after a transition.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GestureEffect {
    /// Nothing - including events from foreign pointer ids, which the
    /// machine deliberately ignores.
    None,
    /// The threshold was crossed: begin the drag. `origin` is where the
    /// press started (use it for the grab offset), `at` is the current
    /// pointer position.
    Begin { origin: Point, at: Point },
    /// An active drag moved: track the pointer and update hover.
    Track { at: Point },
    /// An active drag released: attempt the drop at `at`.
    Drop { at: Point },
    /// The press resolved as a tap (released before the threshold).
    Tap,
    /// An active drag was aborted: clean up drag state.
    Abort,
}

/// Advance the machine with the default [`Promotion::Distance`] policy.
/// Pure: same inputs, same outputs, no side effects.
///
/// `threshold` is the travel distance (CSS px) that promotes a press to a
/// drag; releases inside it resolve as [`GestureEffect::Tap`].
pub fn transition(
    phase: GesturePhase,
    event: GestureEvent,
    threshold: f64,
) -> (GesturePhase, GestureEffect) {
    transition_with(phase, event, threshold, Promotion::Distance)
}

/// Advance the machine under an explicit [`Promotion`] policy. Pure: same
/// inputs, same outputs, no side effects.
///
/// The policy only shapes how a [`GesturePhase::Pressed`] press becomes a
/// drag; everything after `Begin` is policy-independent.
pub fn transition_with(
    phase: GesturePhase,
    event: GestureEvent,
    threshold: f64,
    promotion: Promotion,
) -> (GesturePhase, GestureEffect) {
    use GestureEffect as Fx;
    use GesturePhase as P;

    match (phase, event) {
        // --- starting -----------------------------------------------------
        (P::Idle, GestureEvent::Down { at, pointer_id }) => (
            P::Pressed {
                origin: at,
                pointer_id,
            },
            Fx::None,
        ),
        // A second pointer pressing mid-gesture doesn't steal it.
        (p @ (P::Pressed { .. } | P::Dragging { .. }), GestureEvent::Down { .. }) => (p, Fx::None),

        // --- pressed: promote, tap, or wait --------------------------------
        (
            P::Pressed { origin, pointer_id },
            GestureEvent::Move {
                at,
                pointer_id: pid,
            },
        ) if pid == pointer_id => {
            let d = at - origin;
            if (d.x * d.x + d.y * d.y).sqrt() >= threshold {
                match promotion {
                    Promotion::Distance => {
                        (P::Dragging { origin, pointer_id }, Fx::Begin { origin, at })
                    }
                    Promotion::HoldOrSideways if d.x.abs() > d.y.abs() => {
                        (P::Dragging { origin, pointer_id }, Fx::Begin { origin, at })
                    }
                    // Vertical-dominant travel is scroll intent: yield the
                    // gesture. The browser pan (when the surface can scroll)
                    // arrives as `pointercancel`, which finds Idle and stays
                    // silent.
                    Promotion::HoldOrSideways => (P::Idle, Fx::None),
                }
            } else {
                (P::Pressed { origin, pointer_id }, Fx::None)
            }
        }
        // A long-press is a promotion regardless of policy: drivers that
        // never arm a timer simply never feed `Hold`. The drag begins at the
        // press origin, so the grab offset is exactly where the finger sat.
        (P::Pressed { origin, pointer_id }, GestureEvent::Hold { pointer_id: pid })
            if pid == pointer_id =>
        {
            (
                P::Dragging { origin, pointer_id },
                Fx::Begin { origin, at: origin },
            )
        }
        (
            P::Pressed { pointer_id, .. },
            GestureEvent::Up {
                pointer_id: pid, ..
            },
        ) if pid == pointer_id => (P::Idle, Fx::Tap),

        // --- dragging: track, drop ----------------------------------------
        (
            P::Dragging { origin, pointer_id },
            GestureEvent::Move {
                at,
                pointer_id: pid,
            },
        ) if pid == pointer_id => (P::Dragging { origin, pointer_id }, Fx::Track { at }),
        (
            P::Dragging { pointer_id, .. },
            GestureEvent::Up {
                at,
                pointer_id: pid,
            },
        ) if pid == pointer_id => (P::Idle, Fx::Drop { at }),

        // --- cancellation ---------------------------------------------------
        (P::Dragging { .. }, GestureEvent::Cancel) => (P::Idle, Fx::Abort),
        (P::Pressed { .. }, GestureEvent::Cancel) => (P::Idle, Fx::None),
        (P::Idle, GestureEvent::Cancel) => (P::Idle, Fx::None),

        // --- everything else is deliberately inert -------------------------
        // Foreign pointer ids, moves/ups while idle: ignored, not errors -
        // browsers deliver stray events and a UI library shrugs them off.
        (p, _) => (p, Fx::None),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const T: f64 = 8.0;

    fn down(x: f64, y: f64, pid: i32) -> GestureEvent {
        GestureEvent::Down {
            at: Point::new(x, y),
            pointer_id: pid,
        }
    }
    fn mv(x: f64, y: f64, pid: i32) -> GestureEvent {
        GestureEvent::Move {
            at: Point::new(x, y),
            pointer_id: pid,
        }
    }
    fn up(x: f64, y: f64, pid: i32) -> GestureEvent {
        GestureEvent::Up {
            at: Point::new(x, y),
            pointer_id: pid,
        }
    }

    #[test]
    fn full_drag_lifecycle() {
        let (p, fx) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        assert_eq!(fx, GestureEffect::None);

        // sub-threshold wiggle stays pressed
        let (p, fx) = transition(p, mv(3.0, 3.0, 1), T);
        assert!(matches!(p, GesturePhase::Pressed { .. }));
        assert_eq!(fx, GestureEffect::None);

        // crossing the threshold begins the drag with the press origin
        let (p, fx) = transition(p, mv(10.0, 0.0, 1), T);
        assert_eq!(
            fx,
            GestureEffect::Begin {
                origin: Point::new(0.0, 0.0),
                at: Point::new(10.0, 0.0)
            }
        );

        let (p, fx) = transition(p, mv(20.0, 5.0, 1), T);
        assert_eq!(
            fx,
            GestureEffect::Track {
                at: Point::new(20.0, 5.0)
            }
        );

        let (p, fx) = transition(p, up(20.0, 5.0, 1), T);
        assert_eq!(p, GesturePhase::Idle);
        assert_eq!(
            fx,
            GestureEffect::Drop {
                at: Point::new(20.0, 5.0)
            }
        );
    }

    #[test]
    fn release_before_threshold_is_a_tap() {
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (p, fx) = transition(p, up(2.0, 2.0, 1), T);
        assert_eq!(p, GesturePhase::Idle);
        assert_eq!(fx, GestureEffect::Tap);
    }

    #[test]
    fn foreign_pointer_ids_are_ignored() {
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        // a second finger moves and lifts: gesture unaffected
        let (p, fx) = transition(p, mv(100.0, 100.0, 2), T);
        assert!(matches!(p, GesturePhase::Pressed { .. }));
        assert_eq!(fx, GestureEffect::None);
        let (p, fx) = transition(p, up(100.0, 100.0, 2), T);
        assert!(matches!(p, GesturePhase::Pressed { .. }));
        assert_eq!(fx, GestureEffect::None);
        // a second finger pressing doesn't steal ownership
        let (p, fx) = transition(p, down(50.0, 50.0, 2), T);
        assert!(matches!(p, GesturePhase::Pressed { pointer_id: 1, .. }));
        assert_eq!(fx, GestureEffect::None);
    }

    #[test]
    fn cancel_paths() {
        // cancel mid-drag aborts
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (p, _) = transition(p, mv(20.0, 0.0, 1), T);
        let (p, fx) = transition(p, GestureEvent::Cancel, T);
        assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::Abort));
        // cancel while merely pressed is silent
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (p, fx) = transition(p, GestureEvent::Cancel, T);
        assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::None));
    }

    #[test]
    fn stray_events_while_idle_are_inert() {
        for ev in [mv(9.0, 9.0, 1), up(9.0, 9.0, 1)] {
            let (p, fx) = transition(GesturePhase::Idle, ev, T);
            assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::None));
        }
    }

    #[test]
    fn exact_threshold_promotes() {
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (_, fx) = transition(p, mv(8.0, 0.0, 1), T);
        assert!(matches!(fx, GestureEffect::Begin { .. }));
    }

    fn hold(pid: i32) -> GestureEvent {
        GestureEvent::Hold { pointer_id: pid }
    }

    fn step_auto(p: GesturePhase, ev: GestureEvent) -> (GesturePhase, GestureEffect) {
        transition_with(p, ev, T, Promotion::HoldOrSideways)
    }

    #[test]
    fn hold_promotes_at_the_press_origin() {
        let (p, _) = step_auto(GesturePhase::Idle, down(5.0, 5.0, 1));
        let (p, fx) = step_auto(p, hold(1));
        assert!(matches!(p, GesturePhase::Dragging { .. }));
        assert_eq!(
            fx,
            GestureEffect::Begin {
                origin: Point::new(5.0, 5.0),
                at: Point::new(5.0, 5.0)
            }
        );
        // and the drag then tracks normally
        let (_, fx) = step_auto(p, mv(5.0, 40.0, 1));
        assert_eq!(
            fx,
            GestureEffect::Track {
                at: Point::new(5.0, 40.0)
            }
        );
    }

    #[test]
    fn stale_or_foreign_hold_is_inert() {
        // foreign pointer id while pressed
        let (p, _) = step_auto(GesturePhase::Idle, down(0.0, 0.0, 1));
        let (p2, fx) = step_auto(p, hold(2));
        assert_eq!((p2, fx), (p, GestureEffect::None));
        // firing while idle (gesture already resolved)
        let (p, fx) = step_auto(GesturePhase::Idle, hold(1));
        assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::None));
        // firing mid-drag changes nothing
        let (p, _) = step_auto(GesturePhase::Idle, down(0.0, 0.0, 1));
        let (p, _) = step_auto(p, hold(1));
        let (p2, fx) = step_auto(p, hold(1));
        assert_eq!((p2, fx), (p, GestureEffect::None));
        // hold under the Distance policy still promotes - the policy shapes
        // Move promotion only, and Distance drivers never feed Hold anyway
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (_, fx) = transition(p, hold(1), T);
        assert!(matches!(fx, GestureEffect::Begin { .. }));
    }

    #[test]
    fn sideways_pull_promotes_under_auto() {
        let (p, _) = step_auto(GesturePhase::Idle, down(0.0, 0.0, 1));
        let (_, fx) = step_auto(p, mv(9.0, 4.0, 1));
        assert_eq!(
            fx,
            GestureEffect::Begin {
                origin: Point::new(0.0, 0.0),
                at: Point::new(9.0, 4.0)
            }
        );
    }

    #[test]
    fn vertical_pull_yields_to_scroll_under_auto() {
        let (p, _) = step_auto(GesturePhase::Idle, down(0.0, 0.0, 1));
        // sub-threshold drift keeps waiting
        let (p, fx) = step_auto(p, mv(1.0, 5.0, 1));
        assert!(matches!(p, GesturePhase::Pressed { .. }));
        assert_eq!(fx, GestureEffect::None);
        // vertical-dominant travel past the threshold resolves as scroll
        let (p, fx) = step_auto(p, mv(2.0, 12.0, 1));
        assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::None));
        // an exact diagonal counts as scroll, not drag
        let (p, _) = step_auto(GesturePhase::Idle, down(0.0, 0.0, 1));
        let (p, fx) = step_auto(p, mv(10.0, 10.0, 1));
        assert_eq!((p, fx), (GesturePhase::Idle, GestureEffect::None));
    }

    #[test]
    fn distance_policy_promotes_vertical_pulls_unchanged() {
        let (p, _) = transition(GesturePhase::Idle, down(0.0, 0.0, 1), T);
        let (_, fx) = transition(p, mv(0.0, 12.0, 1), T);
        assert!(matches!(fx, GestureEffect::Begin { .. }));
    }
}