presentar-core 0.3.4

Core types and traits for Presentar UI framework
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
#![allow(
    clippy::unwrap_used,
    clippy::disallowed_methods,
    clippy::many_single_char_names
)]
//! Gesture recognition from touch/pointer events.
//!
//! This module provides gesture recognizers that process raw touch and pointer
//! events and emit high-level gesture events like pinch, rotate, pan, tap, etc.

use crate::event::{Event, GestureState, PointerId, PointerType, TouchId};
use crate::geometry::Point;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Configuration for gesture recognition.
#[derive(Debug, Clone)]
pub struct GestureConfig {
    /// Minimum distance to start a pan gesture (in pixels).
    pub pan_threshold: f32,
    /// Maximum time for a tap (in milliseconds).
    pub tap_timeout_ms: u64,
    /// Maximum movement for a tap to still be valid.
    pub tap_slop: f32,
    /// Time required for a long press (in milliseconds).
    pub long_press_ms: u64,
    /// Maximum time between taps for a double tap.
    pub double_tap_ms: u64,
    /// Minimum scale change to start pinch.
    pub pinch_threshold: f32,
    /// Minimum rotation to start rotate gesture (radians).
    pub rotate_threshold: f32,
}

impl Default for GestureConfig {
    fn default() -> Self {
        Self {
            pan_threshold: 10.0,
            tap_timeout_ms: 300,
            tap_slop: 10.0,
            long_press_ms: 500,
            double_tap_ms: 300,
            pinch_threshold: 0.05,
            rotate_threshold: 0.05,
        }
    }
}

/// Active touch point being tracked.
#[derive(Debug, Clone)]
pub struct TouchPoint {
    /// Touch ID.
    pub id: TouchId,
    /// Starting position.
    pub start_position: Point,
    /// Current position.
    pub current_position: Point,
    /// Previous position.
    pub previous_position: Point,
    /// When the touch started.
    pub start_time: Instant,
    /// Pressure (0.0-1.0).
    pub pressure: f32,
}

impl TouchPoint {
    /// Create a new touch point.
    pub fn new(id: TouchId, position: Point, pressure: f32) -> Self {
        let now = Instant::now();
        Self {
            id,
            start_position: position,
            current_position: position,
            previous_position: position,
            start_time: now,
            pressure,
        }
    }

    /// Update the touch point position.
    pub fn update(&mut self, position: Point, pressure: f32) {
        self.previous_position = self.current_position;
        self.current_position = position;
        self.pressure = pressure;
    }

    /// Get the total distance moved from start.
    pub fn total_distance(&self) -> f32 {
        self.start_position.distance(&self.current_position)
    }

    /// Get the delta from previous position.
    pub fn delta(&self) -> Point {
        self.current_position - self.previous_position
    }

    /// Get duration since touch started.
    pub fn duration(&self) -> Duration {
        self.start_time.elapsed()
    }
}

/// State of a recognized gesture.
#[derive(Debug, Clone, Default)]
pub enum RecognizedGesture {
    /// No gesture recognized yet.
    #[default]
    None,
    /// Tap gesture (with tap count).
    Tap { position: Point, count: u8 },
    /// Long press gesture.
    LongPress { position: Point },
    /// Pan/drag gesture.
    Pan {
        delta: Point,
        velocity: Point,
        state: GestureState,
    },
    /// Pinch gesture.
    Pinch {
        scale: f32,
        center: Point,
        state: GestureState,
    },
    /// Rotate gesture.
    Rotate {
        angle: f32,
        center: Point,
        state: GestureState,
    },
}

/// Tracks last tap for double-tap detection.
#[derive(Debug, Clone)]
struct LastTap {
    position: Point,
    time: Instant,
    count: u8,
}

/// Multi-touch gesture recognizer.
#[derive(Debug)]
pub struct GestureRecognizer {
    /// Configuration.
    config: GestureConfig,
    /// Active touch points.
    touches: HashMap<TouchId, TouchPoint>,
    /// Currently recognized gesture.
    current_gesture: RecognizedGesture,
    /// Initial distance between two fingers (for pinch).
    initial_pinch_distance: Option<f32>,
    /// Initial angle between two fingers (for rotate).
    initial_rotation_angle: Option<f32>,
    /// Last tap info for double-tap detection.
    last_tap: Option<LastTap>,
    /// Velocity tracking for pan.
    velocity_samples: Vec<(Point, Instant)>,
}

impl GestureRecognizer {
    /// Create a new gesture recognizer with default config.
    pub fn new() -> Self {
        Self::with_config(GestureConfig::default())
    }

    /// Create a new gesture recognizer with custom config.
    pub fn with_config(config: GestureConfig) -> Self {
        Self {
            config,
            touches: HashMap::new(),
            current_gesture: RecognizedGesture::None,
            initial_pinch_distance: None,
            initial_rotation_angle: None,
            last_tap: None,
            velocity_samples: Vec::new(),
        }
    }

    /// Get the current gesture configuration.
    pub fn config(&self) -> &GestureConfig {
        &self.config
    }

    /// Get the number of active touches.
    pub fn touch_count(&self) -> usize {
        self.touches.len()
    }

    /// Get an active touch by ID.
    pub fn touch(&self, id: TouchId) -> Option<&TouchPoint> {
        self.touches.get(&id)
    }

    /// Process an event and return any recognized gesture.
    pub fn process(&mut self, event: &Event) -> Option<Event> {
        match event {
            Event::TouchStart {
                id,
                position,
                pressure,
            } => self.on_touch_start(*id, *position, *pressure),
            Event::TouchMove {
                id,
                position,
                pressure,
            } => self.on_touch_move(*id, *position, *pressure),
            Event::TouchEnd { id, position } => self.on_touch_end(*id, *position),
            Event::TouchCancel { id } => self.on_touch_cancel(*id),
            _ => None,
        }
    }

    fn on_touch_start(&mut self, id: TouchId, position: Point, pressure: f32) -> Option<Event> {
        let touch = TouchPoint::new(id, position, pressure);
        self.touches.insert(id, touch);

        // Reset gesture state when new touch starts
        if self.touches.len() == 2 {
            self.init_two_finger_tracking();
        }

        None
    }

    fn on_touch_move(&mut self, id: TouchId, position: Point, pressure: f32) -> Option<Event> {
        if let Some(touch) = self.touches.get_mut(&id) {
            touch.update(position, pressure);
        } else {
            return None;
        }

        match self.touches.len() {
            1 => self.recognize_single_finger_move(),
            2 => self.recognize_two_finger_move(),
            _ => None,
        }
    }

    fn on_touch_end(&mut self, id: TouchId, _position: Point) -> Option<Event> {
        let touch = self.touches.remove(&id)?;

        // Check for tap if this was the last touch
        if self.touches.is_empty() {
            let duration = touch.duration();
            let distance = touch.total_distance();

            if duration.as_millis() < u128::from(self.config.tap_timeout_ms)
                && distance < self.config.tap_slop
            {
                return self.handle_tap(touch.start_position);
            }

            // End any active gesture
            self.end_active_gesture()
        } else if self.touches.len() == 1 {
            // Went from 2 to 1 finger - end pinch/rotate
            self.end_two_finger_gesture()
        } else {
            None
        }
    }

    fn on_touch_cancel(&mut self, id: TouchId) -> Option<Event> {
        self.touches.remove(&id);

        if self.touches.is_empty() {
            self.end_active_gesture()
        } else {
            None
        }
    }

    fn init_two_finger_tracking(&mut self) {
        let (dist, angle) = if let Some((t1, t2)) = self.get_two_touches() {
            let dist = t1.current_position.distance(&t2.current_position);
            let angle = (t2.current_position.y - t1.current_position.y)
                .atan2(t2.current_position.x - t1.current_position.x);
            (Some(dist), Some(angle))
        } else {
            (None, None)
        };
        self.initial_pinch_distance = dist;
        self.initial_rotation_angle = angle;
    }

    fn recognize_single_finger_move(&mut self) -> Option<Event> {
        let touch = self.touches.values().next()?;
        let distance = touch.total_distance();

        if distance >= self.config.pan_threshold {
            let delta = touch.delta();
            let velocity = self.calculate_velocity();

            let state = match &self.current_gesture {
                RecognizedGesture::Pan { .. } => GestureState::Changed,
                _ => GestureState::Started,
            };

            self.current_gesture = RecognizedGesture::Pan {
                delta,
                velocity,
                state,
            };

            Some(Event::GesturePan {
                delta,
                velocity,
                state,
            })
        } else {
            None
        }
    }

    fn recognize_two_finger_move(&mut self) -> Option<Event> {
        let (t1, t2) = self.get_two_touches()?;

        let current_distance = t1.current_position.distance(&t2.current_position);
        let initial_distance = self.initial_pinch_distance?;
        let scale = current_distance / initial_distance;

        let current_angle = self.angle_between(&t1.current_position, &t2.current_position);
        let initial_angle = self.initial_rotation_angle?;
        let angle_delta = current_angle - initial_angle;

        let center = Point::new(
            (t1.current_position.x + t2.current_position.x) / 2.0,
            (t1.current_position.y + t2.current_position.y) / 2.0,
        );

        // Determine if this is more pinch or rotate
        let scale_change = (scale - 1.0).abs();
        let angle_change = angle_delta.abs();

        if scale_change > self.config.pinch_threshold || angle_change > self.config.rotate_threshold
        {
            // Prefer the larger change
            if scale_change > angle_change {
                let state = match &self.current_gesture {
                    RecognizedGesture::Pinch { .. } => GestureState::Changed,
                    _ => GestureState::Started,
                };

                self.current_gesture = RecognizedGesture::Pinch {
                    scale,
                    center,
                    state,
                };

                Some(Event::GesturePinch {
                    scale,
                    center,
                    state,
                })
            } else {
                let state = match &self.current_gesture {
                    RecognizedGesture::Rotate { .. } => GestureState::Changed,
                    _ => GestureState::Started,
                };

                self.current_gesture = RecognizedGesture::Rotate {
                    angle: angle_delta,
                    center,
                    state,
                };

                Some(Event::GestureRotate {
                    angle: angle_delta,
                    center,
                    state,
                })
            }
        } else {
            None
        }
    }

    fn handle_tap(&mut self, position: Point) -> Option<Event> {
        let now = Instant::now();

        let count = if let Some(last) = &self.last_tap {
            if now.duration_since(last.time).as_millis() < u128::from(self.config.double_tap_ms)
                && position.distance(&last.position) < self.config.tap_slop
            {
                last.count + 1
            } else {
                1
            }
        } else {
            1
        };

        self.last_tap = Some(LastTap {
            position,
            time: now,
            count,
        });

        Some(Event::GestureTap { position, count })
    }

    fn end_active_gesture(&mut self) -> Option<Event> {
        let result = match &self.current_gesture {
            RecognizedGesture::Pan {
                delta, velocity, ..
            } => Some(Event::GesturePan {
                delta: *delta,
                velocity: *velocity,
                state: GestureState::Ended,
            }),
            _ => None,
        };

        self.current_gesture = RecognizedGesture::None;
        self.velocity_samples.clear();
        result
    }

    fn end_two_finger_gesture(&mut self) -> Option<Event> {
        let result = match &self.current_gesture {
            RecognizedGesture::Pinch { scale, center, .. } => Some(Event::GesturePinch {
                scale: *scale,
                center: *center,
                state: GestureState::Ended,
            }),
            RecognizedGesture::Rotate { angle, center, .. } => Some(Event::GestureRotate {
                angle: *angle,
                center: *center,
                state: GestureState::Ended,
            }),
            _ => None,
        };

        self.current_gesture = RecognizedGesture::None;
        self.initial_pinch_distance = None;
        self.initial_rotation_angle = None;
        result
    }

    fn get_two_touches(&self) -> Option<(&TouchPoint, &TouchPoint)> {
        let mut iter = self.touches.values();
        let t1 = iter.next()?;
        let t2 = iter.next()?;
        Some((t1, t2))
    }

    fn angle_between(&self, p1: &Point, p2: &Point) -> f32 {
        (p2.y - p1.y).atan2(p2.x - p1.x)
    }

    fn calculate_velocity(&mut self) -> Point {
        let now = Instant::now();

        // Keep only recent samples (last 100ms)
        self.velocity_samples
            .retain(|(_, time)| now.duration_since(*time).as_millis() < 100);

        if let Some(touch) = self.touches.values().next() {
            self.velocity_samples.push((touch.current_position, now));
        }

        if self.velocity_samples.len() < 2 {
            return Point::ORIGIN;
        }

        let (first_pos, first_time) = self.velocity_samples.first().expect("checked len >= 2");
        let (last_pos, last_time) = self.velocity_samples.last().expect("checked len >= 2");

        let dt = last_time.duration_since(*first_time).as_secs_f32();
        if dt < 0.001 {
            return Point::ORIGIN;
        }

        Point::new(
            (last_pos.x - first_pos.x) / dt,
            (last_pos.y - first_pos.y) / dt,
        )
    }

    /// Check if a long press has occurred.
    /// Call this periodically (e.g., from a timer) to detect long press.
    pub fn check_long_press(&mut self) -> Option<Event> {
        if self.touches.len() != 1 {
            return None;
        }

        let touch = self.touches.values().next()?;

        if touch.duration().as_millis() >= u128::from(self.config.long_press_ms)
            && touch.total_distance() < self.config.tap_slop
            && matches!(self.current_gesture, RecognizedGesture::None)
        {
            self.current_gesture = RecognizedGesture::LongPress {
                position: touch.start_position,
            };

            Some(Event::GestureLongPress {
                position: touch.start_position,
            })
        } else {
            None
        }
    }

    /// Reset the recognizer state.
    pub fn reset(&mut self) {
        self.touches.clear();
        self.current_gesture = RecognizedGesture::None;
        self.initial_pinch_distance = None;
        self.initial_rotation_angle = None;
        self.velocity_samples.clear();
    }
}

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

/// Pointer gesture recognizer that unifies mouse, touch, and pen input.
#[derive(Debug)]
pub struct PointerGestureRecognizer {
    /// Active pointers.
    pointers: HashMap<PointerId, PointerInfo>,
    /// Configuration.
    config: GestureConfig,
    /// Primary pointer ID.
    primary_pointer: Option<PointerId>,
}

/// Information about an active pointer.
#[derive(Debug, Clone)]
pub struct PointerInfo {
    /// Pointer ID.
    pub id: PointerId,
    /// Pointer type.
    pub pointer_type: PointerType,
    /// Starting position.
    pub start_position: Point,
    /// Current position.
    pub current_position: Point,
    /// Start time.
    pub start_time: Instant,
    /// Is primary pointer.
    pub is_primary: bool,
    /// Pressure (0.0-1.0).
    pub pressure: f32,
}

impl PointerGestureRecognizer {
    /// Create a new pointer gesture recognizer.
    pub fn new() -> Self {
        Self::with_config(GestureConfig::default())
    }

    /// Create with custom config.
    pub fn with_config(config: GestureConfig) -> Self {
        Self {
            pointers: HashMap::new(),
            config,
            primary_pointer: None,
        }
    }

    /// Get the gesture configuration.
    pub fn config(&self) -> &GestureConfig {
        &self.config
    }

    /// Get the number of active pointers.
    pub fn pointer_count(&self) -> usize {
        self.pointers.len()
    }

    /// Get the primary pointer.
    pub fn primary(&self) -> Option<&PointerInfo> {
        self.primary_pointer.and_then(|id| self.pointers.get(&id))
    }

    /// Process a pointer event.
    pub fn process(&mut self, event: &Event) -> Option<Event> {
        match event {
            Event::PointerDown {
                pointer_id,
                pointer_type,
                position,
                pressure,
                is_primary,
                ..
            } => {
                let info = PointerInfo {
                    id: *pointer_id,
                    pointer_type: *pointer_type,
                    start_position: *position,
                    current_position: *position,
                    start_time: Instant::now(),
                    is_primary: *is_primary,
                    pressure: *pressure,
                };

                if *is_primary || self.primary_pointer.is_none() {
                    self.primary_pointer = Some(*pointer_id);
                }

                self.pointers.insert(*pointer_id, info);
                None
            }
            Event::PointerMove {
                pointer_id,
                position,
                pressure,
                ..
            } => {
                if let Some(info) = self.pointers.get_mut(pointer_id) {
                    info.current_position = *position;
                    info.pressure = *pressure;
                }
                None
            }
            Event::PointerUp { pointer_id, .. } | Event::PointerCancel { pointer_id } => {
                self.pointers.remove(pointer_id);
                if self.primary_pointer == Some(*pointer_id) {
                    self.primary_pointer = self.pointers.keys().next().copied();
                }
                None
            }
            _ => None,
        }
    }

    /// Reset the recognizer.
    pub fn reset(&mut self) {
        self.pointers.clear();
        self.primary_pointer = None;
    }
}

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

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

    // GestureConfig tests
    #[test]
    fn test_gesture_config_default() {
        let config = GestureConfig::default();
        assert_eq!(config.pan_threshold, 10.0);
        assert_eq!(config.tap_timeout_ms, 300);
        assert_eq!(config.tap_slop, 10.0);
        assert_eq!(config.long_press_ms, 500);
        assert_eq!(config.double_tap_ms, 300);
        assert!((config.pinch_threshold - 0.05).abs() < 0.001);
        assert!((config.rotate_threshold - 0.05).abs() < 0.001);
    }

    #[test]
    fn test_gesture_config_custom() {
        let config = GestureConfig {
            pan_threshold: 20.0,
            tap_timeout_ms: 200,
            tap_slop: 15.0,
            long_press_ms: 800,
            double_tap_ms: 400,
            pinch_threshold: 0.1,
            rotate_threshold: 0.1,
        };
        assert_eq!(config.pan_threshold, 20.0);
        assert_eq!(config.long_press_ms, 800);
    }

    // TouchPoint tests
    #[test]
    fn test_touch_point_new() {
        let point = TouchPoint::new(TouchId::new(1), Point::new(100.0, 200.0), 0.5);
        assert_eq!(point.id, TouchId(1));
        assert_eq!(point.start_position, Point::new(100.0, 200.0));
        assert_eq!(point.current_position, Point::new(100.0, 200.0));
        assert_eq!(point.pressure, 0.5);
    }

    #[test]
    fn test_touch_point_update() {
        let mut point = TouchPoint::new(TouchId::new(1), Point::new(100.0, 200.0), 0.5);
        point.update(Point::new(150.0, 250.0), 0.8);

        assert_eq!(point.current_position, Point::new(150.0, 250.0));
        assert_eq!(point.previous_position, Point::new(100.0, 200.0));
        assert_eq!(point.pressure, 0.8);
    }

    #[test]
    fn test_touch_point_total_distance() {
        let mut point = TouchPoint::new(TouchId::new(1), Point::new(0.0, 0.0), 0.5);
        point.update(Point::new(3.0, 4.0), 0.5);

        assert!((point.total_distance() - 5.0).abs() < 0.001);
    }

    #[test]
    fn test_touch_point_delta() {
        let mut point = TouchPoint::new(TouchId::new(1), Point::new(0.0, 0.0), 0.5);
        point.update(Point::new(10.0, 20.0), 0.5);
        point.update(Point::new(15.0, 25.0), 0.5);

        let delta = point.delta();
        assert_eq!(delta.x, 5.0);
        assert_eq!(delta.y, 5.0);
    }

    // GestureRecognizer tests
    #[test]
    fn test_recognizer_new() {
        let recognizer = GestureRecognizer::new();
        assert_eq!(recognizer.touch_count(), 0);
    }

    #[test]
    fn test_recognizer_with_config() {
        let config = GestureConfig {
            pan_threshold: 20.0,
            ..Default::default()
        };
        let recognizer = GestureRecognizer::with_config(config);
        assert_eq!(recognizer.config().pan_threshold, 20.0);
    }

    #[test]
    fn test_recognizer_touch_start() {
        let mut recognizer = GestureRecognizer::new();

        let event = Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        };

        recognizer.process(&event);
        assert_eq!(recognizer.touch_count(), 1);

        let touch = recognizer.touch(TouchId::new(1)).unwrap();
        assert_eq!(touch.start_position, Point::new(100.0, 200.0));
    }

    #[test]
    fn test_recognizer_touch_move() {
        let mut recognizer = GestureRecognizer::new();

        // Start touch
        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // Move touch
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0),
            pressure: 0.6,
        });

        let touch = recognizer.touch(TouchId::new(1)).unwrap();
        assert_eq!(touch.current_position, Point::new(150.0, 250.0));
    }

    #[test]
    fn test_recognizer_touch_end() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchEnd {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
        });

        assert_eq!(recognizer.touch_count(), 0);
    }

    #[test]
    fn test_recognizer_tap() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // End immediately (tap)
        let result = recognizer.process(&Event::TouchEnd {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
        });

        assert!(matches!(result, Some(Event::GestureTap { count: 1, .. })));
    }

    #[test]
    fn test_recognizer_pan() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // Move beyond pan threshold
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0), // 50px moved
            pressure: 0.5,
        });

        assert!(matches!(
            result,
            Some(Event::GesturePan {
                state: GestureState::Started,
                ..
            })
        ));
    }

    #[test]
    fn test_recognizer_pan_continued() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // First move - starts pan
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0),
            pressure: 0.5,
        });

        // Second move - continues pan
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(200.0, 300.0),
            pressure: 0.5,
        });

        assert!(matches!(
            result,
            Some(Event::GesturePan {
                state: GestureState::Changed,
                ..
            })
        ));
    }

    #[test]
    fn test_recognizer_two_touches() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        assert_eq!(recognizer.touch_count(), 2);
    }

    #[test]
    fn test_recognizer_pinch() {
        let mut recognizer = GestureRecognizer::with_config(GestureConfig {
            pinch_threshold: 0.01,
            ..Default::default()
        });

        // Start with two fingers 100px apart
        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        // Move fingers apart to 200px
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(50.0, 200.0),
            pressure: 0.5,
        });

        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(2),
            position: Point::new(250.0, 200.0),
            pressure: 0.5,
        });

        assert!(matches!(result, Some(Event::GesturePinch { .. })));
    }

    #[test]
    fn test_recognizer_reset() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        assert_eq!(recognizer.touch_count(), 1);

        recognizer.reset();
        assert_eq!(recognizer.touch_count(), 0);
    }

    #[test]
    fn test_recognizer_touch_cancel() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchCancel {
            id: TouchId::new(1),
        });

        assert_eq!(recognizer.touch_count(), 0);
    }

    #[test]
    fn test_recognizer_ignores_non_touch_events() {
        let mut recognizer = GestureRecognizer::new();

        let result = recognizer.process(&Event::MouseMove {
            position: Point::new(100.0, 200.0),
        });

        assert!(result.is_none());
        assert_eq!(recognizer.touch_count(), 0);
    }

    // PointerGestureRecognizer tests
    #[test]
    fn test_pointer_recognizer_new() {
        let recognizer = PointerGestureRecognizer::new();
        assert_eq!(recognizer.pointer_count(), 0);
        assert!(recognizer.primary().is_none());
    }

    #[test]
    fn test_pointer_recognizer_pointer_down() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        assert_eq!(recognizer.pointer_count(), 1);

        let primary = recognizer.primary().unwrap();
        assert_eq!(primary.id, PointerId(1));
        assert_eq!(primary.pointer_type, PointerType::Touch);
        assert!(primary.is_primary);
    }

    #[test]
    fn test_pointer_recognizer_pointer_move() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Mouse,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        recognizer.process(&Event::PointerMove {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Mouse,
            position: Point::new(150.0, 250.0),
            pressure: 0.6,
            is_primary: true,
        });

        let primary = recognizer.primary().unwrap();
        assert_eq!(primary.current_position, Point::new(150.0, 250.0));
    }

    #[test]
    fn test_pointer_recognizer_pointer_up() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Pen,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        recognizer.process(&Event::PointerUp {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Pen,
            position: Point::new(100.0, 200.0),
            is_primary: true,
            button: None,
        });

        assert_eq!(recognizer.pointer_count(), 0);
        assert!(recognizer.primary().is_none());
    }

    #[test]
    fn test_pointer_recognizer_multiple_pointers() {
        let mut recognizer = PointerGestureRecognizer::new();

        // First pointer (primary)
        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        // Second pointer (not primary)
        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(2),
            pointer_type: PointerType::Touch,
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
            is_primary: false,
            button: None,
        });

        assert_eq!(recognizer.pointer_count(), 2);

        let primary = recognizer.primary().unwrap();
        assert_eq!(primary.id, PointerId(1));
    }

    #[test]
    fn test_pointer_recognizer_primary_changes_on_remove() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(2),
            pointer_type: PointerType::Touch,
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
            is_primary: false,
            button: None,
        });

        // Remove primary
        recognizer.process(&Event::PointerUp {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            is_primary: true,
            button: None,
        });

        assert_eq!(recognizer.pointer_count(), 1);
        // Primary should now be the remaining pointer
        assert!(recognizer.primary().is_some());
    }

    #[test]
    fn test_pointer_recognizer_reset() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        recognizer.reset();

        assert_eq!(recognizer.pointer_count(), 0);
        assert!(recognizer.primary().is_none());
    }

    #[test]
    fn test_pointer_recognizer_cancel() {
        let mut recognizer = PointerGestureRecognizer::new();

        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: true,
            button: None,
        });

        recognizer.process(&Event::PointerCancel {
            pointer_id: PointerId::new(1),
        });

        assert_eq!(recognizer.pointer_count(), 0);
    }

    // RecognizedGesture tests
    #[test]
    fn test_recognized_gesture_default() {
        let gesture = RecognizedGesture::default();
        assert!(matches!(gesture, RecognizedGesture::None));
    }

    // PointerInfo tests
    #[test]
    fn test_pointer_info_clone() {
        let info = PointerInfo {
            id: PointerId::new(1),
            pointer_type: PointerType::Mouse,
            start_position: Point::new(100.0, 200.0),
            current_position: Point::new(150.0, 250.0),
            start_time: Instant::now(),
            is_primary: true,
            pressure: 0.8,
        };

        let cloned = info.clone();
        assert_eq!(cloned.id, info.id);
        assert_eq!(cloned.pointer_type, info.pointer_type);
        assert_eq!(cloned.pressure, info.pressure);
    }

    // =========================================================================
    // Additional Edge Case Tests
    // =========================================================================

    #[test]
    fn test_gesture_config_debug() {
        let config = GestureConfig::default();
        let debug = format!("{config:?}");
        assert!(debug.contains("GestureConfig"));
    }

    #[test]
    fn test_gesture_config_clone() {
        let config = GestureConfig {
            pan_threshold: 25.0,
            ..Default::default()
        };
        let cloned = config.clone();
        assert_eq!(cloned.pan_threshold, 25.0);
    }

    #[test]
    fn test_touch_point_debug() {
        let point = TouchPoint::new(TouchId::new(1), Point::new(100.0, 200.0), 0.5);
        let debug = format!("{point:?}");
        assert!(debug.contains("TouchPoint"));
    }

    #[test]
    fn test_touch_point_clone() {
        let point = TouchPoint::new(TouchId::new(1), Point::new(100.0, 200.0), 0.5);
        let cloned = point.clone();
        assert_eq!(cloned.id, point.id);
        assert_eq!(cloned.start_position, point.start_position);
    }

    #[test]
    fn test_touch_point_duration() {
        let point = TouchPoint::new(TouchId::new(1), Point::new(100.0, 200.0), 0.5);
        let duration = point.duration();
        assert!(duration.as_millis() < 100); // Should be very short
    }

    #[test]
    fn test_recognized_gesture_debug() {
        let gesture = RecognizedGesture::Tap {
            position: Point::new(50.0, 50.0),
            count: 2,
        };
        let debug = format!("{gesture:?}");
        assert!(debug.contains("Tap"));
    }

    #[test]
    fn test_recognized_gesture_clone() {
        let gesture = RecognizedGesture::Pan {
            delta: Point::new(10.0, 20.0),
            velocity: Point::new(100.0, 200.0),
            state: GestureState::Started,
        };
        let cloned = gesture.clone();
        assert!(matches!(cloned, RecognizedGesture::Pan { .. }));
    }

    #[test]
    fn test_recognized_gesture_all_variants() {
        let gestures = vec![
            RecognizedGesture::None,
            RecognizedGesture::Tap {
                position: Point::ORIGIN,
                count: 1,
            },
            RecognizedGesture::LongPress {
                position: Point::ORIGIN,
            },
            RecognizedGesture::Pan {
                delta: Point::ORIGIN,
                velocity: Point::ORIGIN,
                state: GestureState::Started,
            },
            RecognizedGesture::Pinch {
                scale: 1.0,
                center: Point::ORIGIN,
                state: GestureState::Changed,
            },
            RecognizedGesture::Rotate {
                angle: 0.5,
                center: Point::ORIGIN,
                state: GestureState::Ended,
            },
        ];

        for gesture in gestures {
            let debug = format!("{gesture:?}");
            assert!(!debug.is_empty());
        }
    }

    #[test]
    fn test_gesture_recognizer_default() {
        let recognizer = GestureRecognizer::default();
        assert_eq!(recognizer.touch_count(), 0);
        assert_eq!(recognizer.config().pan_threshold, 10.0);
    }

    #[test]
    fn test_gesture_recognizer_debug() {
        let recognizer = GestureRecognizer::new();
        let debug = format!("{recognizer:?}");
        assert!(debug.contains("GestureRecognizer"));
    }

    #[test]
    fn test_gesture_recognizer_touch_move_unknown_id() {
        let mut recognizer = GestureRecognizer::new();

        // Move without starting touch
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(99),
            position: Point::new(150.0, 250.0),
            pressure: 0.5,
        });

        assert!(result.is_none());
    }

    #[test]
    fn test_gesture_recognizer_touch_end_unknown_id() {
        let mut recognizer = GestureRecognizer::new();

        // End without starting touch
        let result = recognizer.process(&Event::TouchEnd {
            id: TouchId::new(99),
            position: Point::new(100.0, 200.0),
        });

        assert!(result.is_none());
    }

    #[test]
    fn test_gesture_recognizer_pan_end() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // Move to start pan
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0),
            pressure: 0.5,
        });

        // End touch
        let result = recognizer.process(&Event::TouchEnd {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0),
        });

        assert!(matches!(
            result,
            Some(Event::GesturePan {
                state: GestureState::Ended,
                ..
            })
        ));
    }

    #[test]
    fn test_gesture_recognizer_rotate() {
        let mut recognizer = GestureRecognizer::with_config(GestureConfig {
            rotate_threshold: 0.01,
            pinch_threshold: 1.0, // High threshold to prefer rotation
            ..Default::default()
        });

        // Start with two fingers
        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        // Rotate (move one finger up, other down, same distance apart)
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(100.0, 150.0),
            pressure: 0.5,
        });

        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(2),
            position: Point::new(200.0, 250.0),
            pressure: 0.5,
        });

        assert!(matches!(result, Some(Event::GestureRotate { .. })));
    }

    #[test]
    fn test_gesture_recognizer_two_finger_end() {
        let mut recognizer = GestureRecognizer::with_config(GestureConfig {
            pinch_threshold: 0.01,
            ..Default::default()
        });

        // Start pinch
        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        // Move to trigger pinch
        recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(50.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchMove {
            id: TouchId::new(2),
            position: Point::new(250.0, 200.0),
            pressure: 0.5,
        });

        // End one finger
        let result = recognizer.process(&Event::TouchEnd {
            id: TouchId::new(1),
            position: Point::new(50.0, 200.0),
        });

        assert!(matches!(
            result,
            Some(Event::GesturePinch {
                state: GestureState::Ended,
                ..
            })
        ));
    }

    #[test]
    fn test_gesture_recognizer_three_touches() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(3),
            position: Point::new(300.0, 200.0),
            pressure: 0.5,
        });

        assert_eq!(recognizer.touch_count(), 3);

        // Move with 3 touches should return None (not handled)
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(150.0, 250.0),
            pressure: 0.5,
        });

        assert!(result.is_none());
    }

    #[test]
    fn test_pointer_recognizer_debug() {
        let recognizer = PointerGestureRecognizer::new();
        let debug = format!("{recognizer:?}");
        assert!(debug.contains("PointerGestureRecognizer"));
    }

    #[test]
    fn test_pointer_recognizer_default() {
        let recognizer = PointerGestureRecognizer::default();
        assert_eq!(recognizer.pointer_count(), 0);
    }

    #[test]
    fn test_pointer_recognizer_with_config() {
        let config = GestureConfig {
            pan_threshold: 30.0,
            ..Default::default()
        };
        let recognizer = PointerGestureRecognizer::with_config(config);
        assert_eq!(recognizer.config().pan_threshold, 30.0);
    }

    #[test]
    fn test_pointer_recognizer_ignores_non_pointer_events() {
        let mut recognizer = PointerGestureRecognizer::new();

        let result = recognizer.process(&Event::MouseMove {
            position: Point::new(100.0, 200.0),
        });

        assert!(result.is_none());
        assert_eq!(recognizer.pointer_count(), 0);
    }

    #[test]
    fn test_pointer_recognizer_move_unknown_pointer() {
        let mut recognizer = PointerGestureRecognizer::new();

        let result = recognizer.process(&Event::PointerMove {
            pointer_id: PointerId::new(99),
            pointer_type: PointerType::Touch,
            position: Point::new(150.0, 250.0),
            pressure: 0.5,
            is_primary: true,
        });

        assert!(result.is_none());
    }

    #[test]
    fn test_pointer_info_debug() {
        let info = PointerInfo {
            id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            start_position: Point::new(100.0, 200.0),
            current_position: Point::new(100.0, 200.0),
            start_time: Instant::now(),
            is_primary: true,
            pressure: 0.5,
        };
        let debug = format!("{info:?}");
        assert!(debug.contains("PointerInfo"));
    }

    #[test]
    fn test_pointer_recognizer_first_non_primary_becomes_primary() {
        let mut recognizer = PointerGestureRecognizer::new();

        // First pointer is not marked as primary, but should become primary
        recognizer.process(&Event::PointerDown {
            pointer_id: PointerId::new(1),
            pointer_type: PointerType::Touch,
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
            is_primary: false,
            button: None,
        });

        // Since no primary existed, this should be set as primary
        assert!(recognizer.primary().is_some());
    }

    #[test]
    fn test_gesture_recognizer_below_pan_threshold() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        // Small move below threshold
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(102.0, 202.0), // Only ~2.8px moved
            pressure: 0.5,
        });

        assert!(result.is_none());
    }

    #[test]
    fn test_gesture_recognizer_below_pinch_threshold() {
        let mut recognizer = GestureRecognizer::new();

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(1),
            position: Point::new(100.0, 200.0),
            pressure: 0.5,
        });

        recognizer.process(&Event::TouchStart {
            id: TouchId::new(2),
            position: Point::new(200.0, 200.0),
            pressure: 0.5,
        });

        // Tiny movement that doesn't trigger pinch/rotate
        let result = recognizer.process(&Event::TouchMove {
            id: TouchId::new(1),
            position: Point::new(99.0, 200.0),
            pressure: 0.5,
        });

        assert!(result.is_none());
    }
}