teksilo-core 0.9.0

Core of the Teksilo GUI framework — widget trait, arena, layout engine, event dispatch, focus, signals and theming.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

use std::time::{Duration, Instant};

use teksilo_canvas::Point;

use crate::event::{ButtonMask, PointerButton};

use super::{GestureEvent, GestureRecognizer, GestureResult, RawPointerEvent, TapEvent, distance};

/// Recognizes a double-tap (two taps within a time window and distance,
/// using the same button).
///
/// Default `accept` is [`ButtonMask::PRIMARY`]; presses on other buttons
/// are ignored. Within the recognized sequence, both taps must match
/// the press button — a `Primary` then `Secondary` sequence resets to
/// the new tap as a fresh "first" rather than firing `DoubleTap`.
#[derive(Debug)]
pub struct DoubleTapRecognizer {
    max_distance: f32,
    max_interval: Duration,
    accept: ButtonMask,
    first_tap_position: Option<Point>,
    first_tap_time: Option<Instant>,
    first_tap_button: Option<PointerButton>,
    down_position: Option<Point>,
    down_button: Option<PointerButton>,
}

impl DoubleTapRecognizer {
    pub fn new() -> Self {
        Self {
            max_distance: 10.0,
            max_interval: Duration::from_millis(300),
            accept: ButtonMask::PRIMARY,
            first_tap_position: None,
            first_tap_time: None,
            first_tap_button: None,
            down_position: None,
            down_button: None,
        }
    }

    pub fn max_distance(mut self, d: f32) -> Self {
        self.max_distance = d;
        self
    }

    pub fn max_interval(mut self, interval: Duration) -> Self {
        self.max_interval = interval;
        self
    }

    /// Restrict (or extend) the set of buttons that can fire this
    /// recognizer. Default is [`ButtonMask::PRIMARY`].
    pub fn accept_buttons(mut self, mask: impl Into<ButtonMask>) -> Self {
        self.accept = mask.into();
        self
    }

    /// Convenience: accept any pointer button.
    pub fn accept_any_button(self) -> Self {
        self.accept_buttons(ButtonMask::ALL)
    }

    /// Feed an event with an explicit timestamp (for testability without real clocks).
    pub fn process_at(&mut self, event: &RawPointerEvent, now: Instant) -> GestureResult {
        match event {
            RawPointerEvent::Down {
                position, button, ..
            } => {
                if !self.accept.contains(*button) {
                    return GestureResult::Pending;
                }
                // Cross-tap button-match: if we have a first tap from a
                // different button, the new press starts fresh — reset
                // the accumulated state to avoid spuriously firing a
                // mixed-button DoubleTap.
                if let Some(first_button) = self.first_tap_button
                    && first_button != *button
                {
                    self.first_tap_position = None;
                    self.first_tap_time = None;
                    self.first_tap_button = None;
                }
                self.down_position = Some(*position);
                self.down_button = Some(*button);
                GestureResult::Pending
            }
            RawPointerEvent::Move { position } => {
                if let Some(down) = self.down_position
                    && distance(*position, down) > self.max_distance
                {
                    return GestureResult::Failed;
                }
                GestureResult::Pending
            }
            RawPointerEvent::Up {
                position,
                button,
                modifiers,
            } => {
                let Some(down) = self.down_position else {
                    return GestureResult::Failed;
                };
                let Some(down_button) = self.down_button else {
                    return GestureResult::Failed;
                };
                self.down_position = None;
                self.down_button = None;
                if *button != down_button {
                    return GestureResult::Failed;
                }
                if distance(*position, down) > self.max_distance {
                    return GestureResult::Failed;
                }

                if let (Some(first_pos), Some(first_time), Some(first_button)) = (
                    self.first_tap_position,
                    self.first_tap_time,
                    self.first_tap_button,
                ) {
                    // Second tap — check distance, time interval, AND
                    // button match against the first tap.
                    if first_button == *button
                        && distance(*position, first_pos) <= self.max_distance
                        && now.duration_since(first_time) <= self.max_interval
                    {
                        self.reset();
                        return GestureResult::Recognized(GestureEvent::DoubleTap(TapEvent {
                            position: *position,
                            button: *button,
                            modifiers: *modifiers,
                        }));
                    }
                    // Out of window or button mismatch — treat as new
                    // first tap.
                    self.first_tap_position = Some(*position);
                    self.first_tap_time = Some(now);
                    self.first_tap_button = Some(*button);
                    GestureResult::Pending
                } else {
                    // First tap — record and wait for second.
                    self.first_tap_position = Some(*position);
                    self.first_tap_time = Some(now);
                    self.first_tap_button = Some(*button);
                    GestureResult::Pending
                }
            }
        }
    }
}

impl Default for DoubleTapRecognizer {
    fn default() -> Self {
        Self::new()
    }
}

impl GestureRecognizer for DoubleTapRecognizer {
    fn process(&mut self, event: &RawPointerEvent) -> GestureResult {
        self.process_at(event, Instant::now())
    }

    fn reset(&mut self) {
        self.first_tap_position = None;
        self.first_tap_time = None;
        self.first_tap_button = None;
        self.down_position = None;
        self.down_button = None;
    }

    fn priority(&self) -> u32 {
        15 // Higher than tap — double-tap should win over single tap
    }

    fn resets_on_peer_recognition(&self) -> bool {
        // Cooperative with `TripleTapRecognizer`: when we fire a DoubleTap
        // at click 2, the triple-tap recognizer may still be mid-sequence
        // waiting for click 3. The arena must not wipe triple-tap state
        // because of our win, and symmetrically we don't want our state
        // wiped by a triple-tap's win either (though we've already reset
        // ourselves internally by then).
        false
    }
}

/// Recognizes a triple tap (three taps within a time window and
/// distance, all using the same button).
///
/// State machine mirrors `DoubleTapRecognizer` with one extra step:
/// Idle → FirstTapLanded → SecondTapLanded → Recognized(TripleTap).
/// Defaults match `DoubleTapRecognizer` (300 ms / 10 px / Primary only)
/// so the two fire as a natural escalating pair. Mixed-button sequences
/// reset to a fresh first tap.
#[derive(Debug)]
pub struct TripleTapRecognizer {
    max_distance: f32,
    max_interval: Duration,
    accept: ButtonMask,
    first_tap_position: Option<Point>,
    first_tap_time: Option<Instant>,
    first_tap_button: Option<PointerButton>,
    second_tap_position: Option<Point>,
    second_tap_time: Option<Instant>,
    second_tap_button: Option<PointerButton>,
    down_position: Option<Point>,
    down_button: Option<PointerButton>,
}

impl TripleTapRecognizer {
    pub fn new() -> Self {
        Self {
            max_distance: 10.0,
            max_interval: Duration::from_millis(300),
            accept: ButtonMask::PRIMARY,
            first_tap_position: None,
            first_tap_time: None,
            first_tap_button: None,
            second_tap_position: None,
            second_tap_time: None,
            second_tap_button: None,
            down_position: None,
            down_button: None,
        }
    }

    pub fn max_distance(mut self, d: f32) -> Self {
        self.max_distance = d;
        self
    }

    pub fn max_interval(mut self, interval: Duration) -> Self {
        self.max_interval = interval;
        self
    }

    /// Restrict (or extend) the set of buttons that can fire this
    /// recognizer. Default is [`ButtonMask::PRIMARY`].
    pub fn accept_buttons(mut self, mask: impl Into<ButtonMask>) -> Self {
        self.accept = mask.into();
        self
    }

    /// Convenience: accept any pointer button.
    pub fn accept_any_button(self) -> Self {
        self.accept_buttons(ButtonMask::ALL)
    }

    /// Feed an event with an explicit timestamp (for testability without real clocks).
    pub fn process_at(&mut self, event: &RawPointerEvent, now: Instant) -> GestureResult {
        match event {
            RawPointerEvent::Down {
                position, button, ..
            } => {
                if !self.accept.contains(*button) {
                    return GestureResult::Pending;
                }
                // Cross-tap button-match: if any accumulated tap used a
                // different button, drop everything and start fresh.
                let mismatch = self.first_tap_button.map(|b| b != *button).unwrap_or(false)
                    || self
                        .second_tap_button
                        .map(|b| b != *button)
                        .unwrap_or(false);
                if mismatch {
                    self.first_tap_position = None;
                    self.first_tap_time = None;
                    self.first_tap_button = None;
                    self.second_tap_position = None;
                    self.second_tap_time = None;
                    self.second_tap_button = None;
                }
                self.down_position = Some(*position);
                self.down_button = Some(*button);
                GestureResult::Pending
            }
            RawPointerEvent::Move { position } => {
                if let Some(down) = self.down_position
                    && distance(*position, down) > self.max_distance
                {
                    return GestureResult::Failed;
                }
                GestureResult::Pending
            }
            RawPointerEvent::Up {
                position,
                button,
                modifiers,
            } => {
                let Some(down) = self.down_position else {
                    return GestureResult::Failed;
                };
                let Some(down_button) = self.down_button else {
                    return GestureResult::Failed;
                };
                self.down_position = None;
                self.down_button = None;
                if *button != down_button {
                    return GestureResult::Failed;
                }
                if distance(*position, down) > self.max_distance {
                    return GestureResult::Failed;
                }

                // Third tap landed — this is the third if both prior
                // timings AND buttons are in window/match.
                if let (
                    Some(first_pos),
                    Some(first_time),
                    Some(first_button),
                    Some(second_pos),
                    Some(second_time),
                    Some(second_button),
                ) = (
                    self.first_tap_position,
                    self.first_tap_time,
                    self.first_tap_button,
                    self.second_tap_position,
                    self.second_tap_time,
                    self.second_tap_button,
                ) {
                    if first_button == *button
                        && second_button == *button
                        && distance(*position, second_pos) <= self.max_distance
                        && now.duration_since(second_time) <= self.max_interval
                        && distance(second_pos, first_pos) <= self.max_distance
                        && second_time.duration_since(first_time) <= self.max_interval
                    {
                        self.reset();
                        return GestureResult::Recognized(GestureEvent::TripleTap(TapEvent {
                            position: *position,
                            button: *button,
                            modifiers: *modifiers,
                        }));
                    }
                    // Out of window or button mismatch: fold this tap
                    // forward as a fresh first.
                    self.first_tap_position = Some(*position);
                    self.first_tap_time = Some(now);
                    self.first_tap_button = Some(*button);
                    self.second_tap_position = None;
                    self.second_tap_time = None;
                    self.second_tap_button = None;
                    return GestureResult::Pending;
                }

                // First or second tap.
                if let (Some(first_pos), Some(first_time), Some(first_button)) = (
                    self.first_tap_position,
                    self.first_tap_time,
                    self.first_tap_button,
                ) {
                    // Second tap — if in window AND button matches, promote.
                    if first_button == *button
                        && distance(*position, first_pos) <= self.max_distance
                        && now.duration_since(first_time) <= self.max_interval
                    {
                        self.second_tap_position = Some(*position);
                        self.second_tap_time = Some(now);
                        self.second_tap_button = Some(*button);
                        return GestureResult::Pending;
                    }
                    // Out of window or mismatch — treat as fresh first.
                    self.first_tap_position = Some(*position);
                    self.first_tap_time = Some(now);
                    self.first_tap_button = Some(*button);
                    self.second_tap_position = None;
                    self.second_tap_time = None;
                    self.second_tap_button = None;
                    return GestureResult::Pending;
                }

                // No prior tap — record as first.
                self.first_tap_position = Some(*position);
                self.first_tap_time = Some(now);
                self.first_tap_button = Some(*button);
                self.second_tap_position = None;
                self.second_tap_time = None;
                self.second_tap_button = None;
                GestureResult::Pending
            }
        }
    }
}

impl Default for TripleTapRecognizer {
    fn default() -> Self {
        Self::new()
    }
}

impl GestureRecognizer for TripleTapRecognizer {
    fn process(&mut self, event: &RawPointerEvent) -> GestureResult {
        self.process_at(event, Instant::now())
    }

    fn reset(&mut self) {
        self.first_tap_position = None;
        self.first_tap_time = None;
        self.first_tap_button = None;
        self.second_tap_position = None;
        self.second_tap_time = None;
        self.second_tap_button = None;
        self.down_position = None;
        self.down_button = None;
    }

    fn priority(&self) -> u32 {
        // Higher than DoubleTap so that when both would fire on the same
        // up event (shouldn't happen in practice — TripleTap only fires
        // after three taps and DoubleTap only at tap 2) TripleTap wins.
        20
    }

    fn resets_on_peer_recognition(&self) -> bool {
        // Cooperative with `DoubleTapRecognizer` — see the matching
        // override on DoubleTapRecognizer. The arena must not wipe our
        // accumulated first/second tap state when DoubleTap fires at
        // click 2, or click 3 would never promote us to TripleTap.
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::Modifiers;
    use crate::gesture::test_helpers::*;

    // --- DoubleTapRecognizer ---

    #[test]
    fn double_tap_recognized_within_interval() {
        let mut rec = DoubleTapRecognizer::new().max_interval(Duration::from_millis(500));
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        // First tap
        rec.process_at(&down(p), t0);
        rec.process_at(&up(p), t0 + Duration::from_millis(50));

        // Second tap within interval
        rec.process_at(
            &down(Point::new(11.0, 10.0)),
            t0 + Duration::from_millis(200),
        );
        let result = rec.process_at(&up(Point::new(11.0, 10.0)), t0 + Duration::from_millis(250));
        assert!(matches!(
            result,
            GestureResult::Recognized(GestureEvent::DoubleTap(_))
        ));
    }

    #[test]
    fn double_tap_fails_if_too_slow() {
        let mut rec = DoubleTapRecognizer::new().max_interval(Duration::from_millis(300));
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down(p), t0);
        rec.process_at(&up(p), t0 + Duration::from_millis(50));

        rec.process_at(&down(p), t0 + Duration::from_millis(400));
        let result = rec.process_at(&up(p), t0 + Duration::from_millis(450));
        // Should be Pending (treated as new first tap), not Recognized
        assert!(matches!(result, GestureResult::Pending));
    }

    #[test]
    fn double_tap_fails_if_too_far() {
        let mut rec = DoubleTapRecognizer::new().max_distance(5.0);
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down(p), t0);
        rec.process_at(&up(p), t0 + Duration::from_millis(50));

        // Second tap too far from first
        rec.process_at(
            &down(Point::new(50.0, 50.0)),
            t0 + Duration::from_millis(100),
        );
        let result = rec.process_at(&up(Point::new(50.0, 50.0)), t0 + Duration::from_millis(150));
        // Treated as new first tap
        assert!(matches!(result, GestureResult::Pending));
    }

    #[test]
    fn double_tap_button_mismatch_fails_at_second_down() {
        // First tap Primary, second tap Secondary → no DoubleTap. The
        // second tap is recorded as a fresh first instead.
        let mut rec = DoubleTapRecognizer::new().accept_any_button();
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down_btn(p, PointerButton::Primary), t0);
        rec.process_at(
            &up_btn(p, PointerButton::Primary),
            t0 + Duration::from_millis(50),
        );

        rec.process_at(
            &down_btn(p, PointerButton::Secondary),
            t0 + Duration::from_millis(150),
        );
        let result = rec.process_at(
            &up_btn(p, PointerButton::Secondary),
            t0 + Duration::from_millis(200),
        );
        assert!(matches!(result, GestureResult::Pending));
    }

    #[test]
    fn double_tap_carries_modifiers_from_second_up() {
        let mut rec = DoubleTapRecognizer::new();
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down(p), t0);
        rec.process_at(&up(p), t0 + Duration::from_millis(50));

        rec.process_at(&down(p), t0 + Duration::from_millis(150));
        let result = rec.process_at(
            &up_full(p, PointerButton::Primary, Modifiers::SHIFT),
            t0 + Duration::from_millis(200),
        );
        match result {
            GestureResult::Recognized(GestureEvent::DoubleTap(event)) => {
                assert!(event.modifiers.shift());
            }
            other => panic!("expected DoubleTap with shift, got {:?}", other),
        }
    }

    // --- TripleTapRecognizer ---

    #[test]
    fn triple_tap_recognized_within_intervals() {
        let mut rec = TripleTapRecognizer::new();
        let t0 = Instant::now();
        let p = Point::new(10.0, 10.0);

        // Three taps all within window, all at (10, 10).
        for i in 0..3 {
            let offset = Duration::from_millis(200 * i as u64);
            rec.process_at(&down(p), t0 + offset);
            let result = rec.process_at(&up(p), t0 + offset + Duration::from_millis(50));
            if i < 2 {
                assert!(matches!(result, GestureResult::Pending));
            } else {
                assert!(matches!(
                    result,
                    GestureResult::Recognized(GestureEvent::TripleTap(_))
                ));
            }
        }
    }

    #[test]
    fn triple_tap_fails_if_third_is_too_slow() {
        let mut rec = TripleTapRecognizer::new().max_interval(Duration::from_millis(300));
        let t0 = Instant::now();
        let stamp = |ms| t0 + Duration::from_millis(ms);
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down(p), stamp(0));
        rec.process_at(&up(p), stamp(50));
        rec.process_at(&down(p), stamp(200));
        rec.process_at(&up(p), stamp(250));

        // Third tap > 300 ms after the second — does not recognize.
        rec.process_at(&down(p), stamp(700));
        let result = rec.process_at(&up(p), stamp(750));
        assert!(matches!(result, GestureResult::Pending));
    }

    #[test]
    fn triple_tap_fails_if_third_is_too_far() {
        let mut rec = TripleTapRecognizer::new().max_distance(5.0);
        let t0 = Instant::now();
        let stamp = |ms| t0 + Duration::from_millis(ms);
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down(p), stamp(0));
        rec.process_at(&up(p), stamp(50));
        rec.process_at(&down(p), stamp(100));
        rec.process_at(&up(p), stamp(150));

        // Third tap > 5 px from the second.
        rec.process_at(&down(Point::new(30.0, 10.0)), stamp(200));
        let result = rec.process_at(&up(Point::new(30.0, 10.0)), stamp(250));
        assert!(matches!(result, GestureResult::Pending));
    }

    #[test]
    fn triple_tap_button_mismatch_fails_at_third_down() {
        // Third tap with a different button → no TripleTap. The
        // second-tap state collapses and the new tap becomes a fresh
        // first.
        let mut rec = TripleTapRecognizer::new().accept_any_button();
        let t0 = Instant::now();
        let stamp = |ms| t0 + Duration::from_millis(ms);
        let p = Point::new(10.0, 10.0);

        rec.process_at(&down_btn(p, PointerButton::Primary), stamp(0));
        rec.process_at(&up_btn(p, PointerButton::Primary), stamp(50));
        rec.process_at(&down_btn(p, PointerButton::Primary), stamp(150));
        rec.process_at(&up_btn(p, PointerButton::Primary), stamp(200));

        rec.process_at(&down_btn(p, PointerButton::Secondary), stamp(300));
        let result = rec.process_at(&up_btn(p, PointerButton::Secondary), stamp(350));
        assert!(matches!(result, GestureResult::Pending));
    }
}