waft-widgets 0.17.0

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

use std::any::Any;
use std::fmt;
use std::rc::Rc;
use std::time::Duration;

use waft_embedder::{Offset, ViewId};
use waft_foundation::{App, Handle, HandleId, Listener, OptionRetained};
use waft_gestures::*;
use waft_painting::Axis;
use waft_rendering::{BoxHitTestEntry, HitTestBehavior, RenderMetaData};
use waft_services::HapticFeedback;

use crate::*;
use waft_foundation::Ref;

/// Signature for determining whether a drag target will accept the given data.
pub type DragTargetWillAccept<T> = Rc<dyn Fn(&mut App, Option<Rc<T>>) -> bool>;

/// Signature for determining whether a drag target will accept the given drag details.
pub type DragTargetWillAcceptWithDetails<T> = Rc<dyn Fn(&mut App, DragTargetDetails<T>) -> bool>;

/// Signature for causing a drag target to accept the given data.
pub type DragTargetAccept<T> = Rc<dyn Fn(&mut App, Rc<T>)>;

/// Signature for causing a drag target to accept the given drag details.
pub type DragTargetAcceptWithDetails<T> = Rc<dyn Fn(&mut App, DragTargetDetails<T>)>;

/// Signature for building a drag target from its candidate and rejected data.
pub type DragTargetBuilder<T> =
    Rc<dyn Fn(&mut App, BuildContext, Vec<Option<Rc<T>>>, Vec<Option<Rc<dyn Any>>>) -> WidgetRef>;

/// Signature for when a Draggable is dragged across the screen.
pub type DragUpdateCallback = Rc<dyn Fn(&mut App, DragUpdateDetails)>;

/// Signature for a draggable dropped without being accepted by a target.
pub type DraggableCanceledCallback = Rc<dyn Fn(&mut App, Velocity, Offset)>;

/// Signature for when the draggable is dropped.
pub type DragEndCallback = Rc<dyn Fn(&mut App, DraggableDetails)>;

/// Signature for when a Draggable leaves a DragTarget.
pub type DragTargetLeave<T> = Rc<dyn Fn(&mut App, Option<Rc<T>>)>;

/// Signature for when a Draggable moves within a DragTarget.
pub type DragTargetMove<T> = Rc<dyn Fn(&mut App, DragTargetDetails<T>)>;

/// Signature for the strategy that determines the drag start point.
pub type DragAnchorStrategy<T> =
    Rc<dyn Fn(&mut App, &Draggable<T>, BuildContext, Offset) -> Offset>;

/// Display the feedback anchored at the position of the original child.
pub fn child_drag_anchor_strategy<T: 'static>(
    app: &mut App,
    _draggable: &Draggable<T>,
    context: BuildContext,
    position: Offset,
) -> Offset {
    context
        .find_render_object(app)
        .unwrap()
        .as_box()
        .unwrap()
        .global_to_local(app, position, None)
}

/// Display the feedback anchored at the position of the touch that started the drag.
pub fn pointer_drag_anchor_strategy<T: 'static>(
    _app: &mut App,
    _draggable: &Draggable<T>,
    _context: BuildContext,
    _position: Offset,
) -> Offset {
    Offset::ZERO
}

/// A widget that can be dragged from to a DragTarget.
///
/// Active drags and their feedback survive removal of the source widget.
pub struct Draggable<T: 'static> {
    /// Controls how one widget replaces another widget in the tree.
    pub key: Option<KeyRef>,

    /// The data that was dropped onto this `DragTarget`.
    pub data: Option<Rc<T>>,

    /// The `Axis` to restrict this draggable's movement, if specified.
    ///
    /// When axis is set to `Axis.horizontal`, this widget can only be dragged
    /// horizontally. Behavior is similar for `Axis.vertical`.
    ///
    /// Defaults to allowing drag on both `Axis.horizontal` and `Axis.vertical`.
    ///
    /// When null, allows drag on both `Axis.horizontal` and `Axis.vertical`.
    ///
    /// For the direction of gestures this widget competes with to start a drag
    /// event, see `Draggable.affinity`.
    pub axis: Option<Axis>,

    /// The widget below this widget in the tree.
    ///
    /// This widget displays `child` when zero drags are under way. If
    /// `childWhenDragging` is non-null, this widget instead displays
    /// `childWhenDragging` when one or more drags are underway. Otherwise, this
    /// widget always displays `child`.
    ///
    /// The `feedback` widget is shown under the pointer when a drag is under way.
    ///
    /// To limit the number of simultaneous drags on multitouch devices, see
    /// `maxSimultaneousDrags`.
    ///
    /// {@macro flutter.widgets.ProxyWidget.child}
    pub child: WidgetRef,

    /// The widget to display instead of `child` when one or more drags are under way.
    ///
    /// If this is null, then this widget will always display `child` (and so the
    /// drag source representation will not change while a drag is under
    /// way).
    ///
    /// The `feedback` widget is shown under the pointer when a drag is under way.
    ///
    /// To limit the number of simultaneous drags on multitouch devices, see
    /// `maxSimultaneousDrags`.
    pub child_when_dragging: Option<WidgetRef>,

    /// The widget to show under the pointer when a drag is under way.
    ///
    /// See `child` and `childWhenDragging` for information about what is shown
    /// at the location of the `Draggable` itself when a drag is under way.
    pub feedback: WidgetRef,

    /// The feedbackOffset can be used to set the hit test target point for the
    /// purposes of finding a drag target. It is especially useful if the feedback
    /// is transformed compared to the child.
    pub feedback_offset: Offset,

    /// A strategy that is used by this draggable to get the anchor offset when it
    /// is dragged.
    ///
    /// The anchor offset refers to the distance between the users' fingers and
    /// the `feedback` widget when this draggable is dragged.
    ///
    /// This property's value is a function that implements `DragAnchorStrategy`.
    /// There are two built-in functions that can be used:
    ///
    ///  * `childDragAnchorStrategy`, which displays the feedback anchored at the
    ///    position of the original child.
    ///
    ///  * `pointerDragAnchorStrategy`, which displays the feedback anchored at the
    ///    position of the touch that started the drag.
    ///
    /// Defaults to `childDragAnchorStrategy`.
    pub drag_anchor_strategy: DragAnchorStrategy<T>,

    /// Whether the semantics of the `feedback` widget is ignored when building
    /// the semantics tree.
    ///
    /// This value should be set to false when the `feedback` widget is intended
    /// to be the same object as the `child`. Placing a `GlobalKey` on this
    /// widget will ensure semantic focus is kept on the element as it moves in
    /// and out of the feedback position.
    ///
    /// Defaults to true.
    pub ignoring_feedback_semantics: bool,

    /// Whether the `feedback` widget is ignored during hit testing.
    ///
    /// Regardless of whether this widget is ignored during hit testing, it will
    /// still consume space during layout and be visible during painting.
    ///
    /// Defaults to true.
    pub ignoring_feedback_pointer: bool,

    /// Controls how this widget competes with other gestures to initiate a drag.
    ///
    /// If affinity is null, this widget initiates a drag as soon as it recognizes
    /// a tap down gesture, regardless of any directionality. If affinity is
    /// horizontal (or vertical), then this widget will compete with other
    /// horizontal (or vertical, respectively) gestures.
    ///
    /// For example, if this widget is placed in a vertically scrolling region and
    /// has horizontal affinity, pointer motion in the vertical direction will
    /// result in a scroll and pointer motion in the horizontal direction will
    /// result in a drag. Conversely, if the widget has a null or vertical
    /// affinity, pointer motion in any direction will result in a drag rather
    /// than in a scroll because the draggable widget, being the more specific
    /// widget, will out-compete the `Scrollable` for vertical gestures.
    ///
    /// For the directions this widget can be dragged in after the drag event
    /// starts, see `Draggable.axis`.
    pub affinity: Option<Axis>,

    /// How many simultaneous drags to support.
    ///
    /// When null, no limit is applied. Set this to 1 if you want to only allow
    /// the drag source to have one item dragged at a time. Set this to 0 if you
    /// want to prevent the draggable from actually being dragged.
    ///
    /// If you set this property to 1, consider supplying an "empty" widget for
    /// `childWhenDragging` to create the illusion of actually moving `child`.
    pub max_simultaneous_drags: Option<usize>,

    /// Called when the draggable starts being dragged.
    pub on_drag_started: Option<Listener>,

    /// Called when the draggable is dragged.
    ///
    /// This function will only be called while this widget is still mounted to
    /// the tree (i.e. `State.mounted` is true), and if this widget has actually moved.
    pub on_drag_update: Option<DragUpdateCallback>,

    /// Called when the draggable is dropped without being accepted by a `DragTarget`.
    ///
    /// This function might be called after this widget has been removed from the
    /// tree. For example, if a drag was in progress when this widget was removed
    /// from the tree and the drag ended up being canceled, this callback will
    /// still be called. For this reason, implementations of this callback might
    /// need to check `State.mounted` to check whether the state receiving the
    /// callback is still in the tree.
    pub on_draggable_canceled: Option<DraggableCanceledCallback>,

    /// Called when the draggable is dropped and accepted by a `DragTarget`.
    ///
    /// This function might be called after this widget has been removed from the
    /// tree. For example, if a drag was in progress when this widget was removed
    /// from the tree and the drag ended up completing, this callback will
    /// still be called. For this reason, implementations of this callback might
    /// need to check `State.mounted` to check whether the state receiving the
    /// callback is still in the tree.
    pub on_drag_completed: Option<Listener>,

    /// Called when the draggable is dropped.
    ///
    /// The velocity and offset at which the pointer was moving when it was
    /// dropped is available in the `DraggableDetails`. Also included in the
    /// `details` is whether the draggable's `DragTarget` accepted it.
    ///
    /// This function will only be called while this widget is still mounted to
    /// the tree (i.e. `State.mounted` is true).
    pub on_drag_end: Option<DragEndCallback>,

    /// Whether the feedback widget will be put on the root `Overlay`.
    ///
    /// When false, the feedback widget will be put on the closest `Overlay`. When
    /// true, the `feedback` widget will be put on the farthest (aka root)
    /// `Overlay`.
    ///
    /// Defaults to false.
    pub root_overlay: bool,

    /// How to behave during hit testing.
    ///
    /// Defaults to `HitTestBehavior.translucent`.
    pub hit_test_behavior: HitTestBehavior,

    /// {@macro flutter.gestures.multidrag._allowedButtonsFilter}
    pub allowed_buttons_filter: Option<AllowedButtonsFilter>,
}

impl<T: 'static> Clone for Draggable<T> {
    fn clone(&self) -> Self {
        Self {
            key: self.key.clone(),
            data: self.data.clone(),
            axis: self.axis,
            child: self.child.clone(),
            child_when_dragging: self.child_when_dragging.clone(),
            feedback: self.feedback.clone(),
            feedback_offset: self.feedback_offset,
            drag_anchor_strategy: self.drag_anchor_strategy.clone(),
            ignoring_feedback_semantics: self.ignoring_feedback_semantics,
            ignoring_feedback_pointer: self.ignoring_feedback_pointer,
            affinity: self.affinity,
            max_simultaneous_drags: self.max_simultaneous_drags,
            on_drag_started: self.on_drag_started.clone(),
            on_drag_update: self.on_drag_update.clone(),
            on_draggable_canceled: self.on_draggable_canceled.clone(),
            on_drag_completed: self.on_drag_completed.clone(),
            on_drag_end: self.on_drag_end.clone(),
            root_overlay: self.root_overlay,
            hit_test_behavior: self.hit_test_behavior,
            allowed_buttons_filter: self.allowed_buttons_filter.clone(),
        }
    }
}

impl<T: 'static> fmt::Debug for Draggable<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Draggable").finish_non_exhaustive()
    }
}

impl<T: 'static> Draggable<T> {
    /// Creates a draggable with its normal child and drag feedback.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<C, F>(child: impl IntoWidget<C>, feedback: impl IntoWidget<F>) -> Self {
        Self {
            key: None,
            data: None,
            axis: None,
            child: child.into_widget(),
            child_when_dragging: None,
            feedback: feedback.into_widget(),
            feedback_offset: Offset::ZERO,
            drag_anchor_strategy: Rc::new(child_drag_anchor_strategy::<T>),
            ignoring_feedback_semantics: true,
            ignoring_feedback_pointer: true,
            affinity: None,
            max_simultaneous_drags: None,
            on_drag_started: None,
            on_drag_update: None,
            on_draggable_canceled: None,
            on_drag_completed: None,
            on_drag_end: None,
            root_overlay: false,
            hit_test_behavior: HitTestBehavior::DeferToChild,
            allowed_buttons_filter: None,
        }
    }

    /// Sets `key` for this draggable.
    pub fn key(mut self, value: KeyRef) -> Self {
        self.key = Some(value);
        self
    }

    /// Sets `data` for this draggable.
    pub fn data(mut self, value: T) -> Self {
        self.data = Some(Rc::new(value));
        self
    }

    /// Sets `axis` for this draggable.
    pub fn axis(mut self, value: Axis) -> Self {
        self.axis = Some(value);
        self
    }

    /// Sets `child_when_dragging` for this draggable.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn child_when_dragging<K>(mut self, value: impl IntoWidget<K>) -> Self {
        self.child_when_dragging = Some(value.into_widget());
        self
    }

    /// Sets `feedback_offset` for this draggable.
    pub fn feedback_offset(mut self, value: Offset) -> Self {
        self.feedback_offset = value;
        self
    }

    /// Sets `drag_anchor_strategy` for this draggable.
    pub fn drag_anchor_strategy(mut self, value: DragAnchorStrategy<T>) -> Self {
        self.drag_anchor_strategy = value;
        self
    }

    /// Sets `ignoring_feedback_semantics` for this draggable.
    pub fn ignoring_feedback_semantics(mut self, value: bool) -> Self {
        self.ignoring_feedback_semantics = value;
        self
    }

    /// Sets `ignoring_feedback_pointer` for this draggable.
    pub fn ignoring_feedback_pointer(mut self, value: bool) -> Self {
        self.ignoring_feedback_pointer = value;
        self
    }

    /// Sets `affinity` for this draggable.
    pub fn affinity(mut self, value: Axis) -> Self {
        self.affinity = Some(value);
        self
    }

    /// Sets `max_simultaneous_drags` for this draggable.
    pub fn max_simultaneous_drags(mut self, value: usize) -> Self {
        self.max_simultaneous_drags = Some(value);
        self
    }

    /// Sets `on_drag_started` for this draggable.
    pub fn on_drag_started(mut self, value: Listener) -> Self {
        self.on_drag_started = Some(value);
        self
    }

    /// Sets `on_drag_update` for this draggable.
    pub fn on_drag_update(mut self, value: DragUpdateCallback) -> Self {
        self.on_drag_update = Some(value);
        self
    }

    /// Sets `on_draggable_canceled` for this draggable.
    pub fn on_draggable_canceled(mut self, value: DraggableCanceledCallback) -> Self {
        self.on_draggable_canceled = Some(value);
        self
    }

    /// Sets `on_drag_completed` for this draggable.
    pub fn on_drag_completed(mut self, value: Listener) -> Self {
        self.on_drag_completed = Some(value);
        self
    }

    /// Sets `on_drag_end` for this draggable.
    pub fn on_drag_end(mut self, value: DragEndCallback) -> Self {
        self.on_drag_end = Some(value);
        self
    }

    /// Sets `root_overlay` for this draggable.
    pub fn root_overlay(mut self, value: bool) -> Self {
        self.root_overlay = value;
        self
    }

    /// Sets `hit_test_behavior` for this draggable.
    pub fn hit_test_behavior(mut self, value: HitTestBehavior) -> Self {
        self.hit_test_behavior = value;
        self
    }

    /// Sets `allowed_buttons_filter` for this draggable.
    pub fn allowed_buttons_filter(mut self, value: AllowedButtonsFilter) -> Self {
        self.allowed_buttons_filter = Some(value);
        self
    }
}

/// The Draggable superclass interface; a subclass overrides `create_recognizer`.
pub trait DraggableWidget: StatefulWidget<State = DraggableState<Self>> + Clone {
    type Data: 'static;

    fn draggable(&self) -> &Draggable<Self::Data>;

    fn create_recognizer(
        &self,
        app: &mut App,
        on_start: GestureMultiDragStartCallback,
    ) -> MultiDragRecognizerRef {
        let draggable = self.draggable();
        let recognizer: MultiDragRecognizerRef = match draggable.affinity {
            Some(Axis::Horizontal) => HorizontalMultiDragGestureRecognizer::new(app).into_erased(),
            Some(Axis::Vertical) => VerticalMultiDragGestureRecognizer::new(app).into_erased(),
            None => ImmediateMultiDragGestureRecognizer::new(app).into_erased(),
        };
        if let Some(filter) = draggable.allowed_buttons_filter.clone() {
            recognizer.set_allowed_buttons_filter(app, filter);
        }
        recognizer.set_on_start(app, Some(on_start));
        recognizer
    }
}

impl<T: 'static> DraggableWidget for Draggable<T> {
    type Data = T;

    fn draggable(&self) -> &Self {
        self
    }
}

impl<T: 'static> StatefulWidget for Draggable<T> {
    type State = DraggableState<Self>;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> Self::State {
        DraggableState::new()
    }
}

/// Makes its child draggable starting from long press.
pub struct LongPressDraggable<T: 'static> {
    /// The Draggable superclass configuration.
    pub draggable: Draggable<T>,

    /// Whether haptic feedback should be triggered on drag start.
    pub haptic_feedback_on_start: bool,

    /// The duration that a user has to press down before a long press is registered.
    ///
    /// Defaults to `kLongPressTimeout`.
    pub delay: Duration,
}

impl<T: 'static> Clone for LongPressDraggable<T> {
    fn clone(&self) -> Self {
        Self {
            draggable: self.draggable.clone(),
            haptic_feedback_on_start: self.haptic_feedback_on_start,
            delay: self.delay,
        }
    }
}

impl<T: 'static> fmt::Debug for LongPressDraggable<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LongPressDraggable").finish_non_exhaustive()
    }
}

impl<T: 'static> LongPressDraggable<T> {
    /// Creates a draggable with its normal child and drag feedback.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<C, F>(child: impl IntoWidget<C>, feedback: impl IntoWidget<F>) -> Self {
        Self {
            draggable: Draggable::new(child, feedback),
            haptic_feedback_on_start: true,
            delay: K_LONG_PRESS_TIMEOUT,
        }
    }

    /// Sets `delay` for this draggable.
    pub fn delay(mut self, value: Duration) -> Self {
        self.delay = value;
        self
    }

    /// Sets `haptic_feedback_on_start` for this draggable.
    pub fn haptic_feedback_on_start(mut self, value: bool) -> Self {
        self.haptic_feedback_on_start = value;
        self
    }

    /// Sets `key` for this draggable.
    pub fn key(mut self, value: KeyRef) -> Self {
        self.draggable.key = Some(value);
        self
    }

    /// Sets `data` for this draggable.
    pub fn data(mut self, value: T) -> Self {
        self.draggable.data = Some(Rc::new(value));
        self
    }

    /// Sets `axis` for this draggable.
    pub fn axis(mut self, value: Axis) -> Self {
        self.draggable.axis = Some(value);
        self
    }

    /// Sets `child_when_dragging` for this draggable.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn child_when_dragging<K>(mut self, value: impl IntoWidget<K>) -> Self {
        self.draggable.child_when_dragging = Some(value.into_widget());
        self
    }

    /// Sets `feedback_offset` for this draggable.
    pub fn feedback_offset(mut self, value: Offset) -> Self {
        self.draggable.feedback_offset = value;
        self
    }

    /// Sets `drag_anchor_strategy` for this draggable.
    pub fn drag_anchor_strategy(mut self, value: DragAnchorStrategy<T>) -> Self {
        self.draggable.drag_anchor_strategy = value;
        self
    }

    /// Sets `ignoring_feedback_semantics` for this draggable.
    pub fn ignoring_feedback_semantics(mut self, value: bool) -> Self {
        self.draggable.ignoring_feedback_semantics = value;
        self
    }

    /// Sets `ignoring_feedback_pointer` for this draggable.
    pub fn ignoring_feedback_pointer(mut self, value: bool) -> Self {
        self.draggable.ignoring_feedback_pointer = value;
        self
    }

    /// Sets `max_simultaneous_drags` for this draggable.
    pub fn max_simultaneous_drags(mut self, value: usize) -> Self {
        self.draggable.max_simultaneous_drags = Some(value);
        self
    }

    /// Sets `on_drag_started` for this draggable.
    pub fn on_drag_started(mut self, value: Listener) -> Self {
        self.draggable.on_drag_started = Some(value);
        self
    }

    /// Sets `on_drag_update` for this draggable.
    pub fn on_drag_update(mut self, value: DragUpdateCallback) -> Self {
        self.draggable.on_drag_update = Some(value);
        self
    }

    /// Sets `on_draggable_canceled` for this draggable.
    pub fn on_draggable_canceled(mut self, value: DraggableCanceledCallback) -> Self {
        self.draggable.on_draggable_canceled = Some(value);
        self
    }

    /// Sets `on_drag_completed` for this draggable.
    pub fn on_drag_completed(mut self, value: Listener) -> Self {
        self.draggable.on_drag_completed = Some(value);
        self
    }

    /// Sets `on_drag_end` for this draggable.
    pub fn on_drag_end(mut self, value: DragEndCallback) -> Self {
        self.draggable.on_drag_end = Some(value);
        self
    }

    /// Sets `root_overlay` for this draggable.
    pub fn root_overlay(mut self, value: bool) -> Self {
        self.draggable.root_overlay = value;
        self
    }

    /// Sets `hit_test_behavior` for this draggable.
    pub fn hit_test_behavior(mut self, value: HitTestBehavior) -> Self {
        self.draggable.hit_test_behavior = value;
        self
    }

    /// Sets `allowed_buttons_filter` for this draggable.
    pub fn allowed_buttons_filter(mut self, value: AllowedButtonsFilter) -> Self {
        self.draggable.allowed_buttons_filter = Some(value);
        self
    }
}

impl<T: 'static> DraggableWidget for LongPressDraggable<T> {
    type Data = T;

    fn draggable(&self) -> &Draggable<T> {
        &self.draggable
    }

    fn create_recognizer(
        &self,
        app: &mut App,
        on_start: GestureMultiDragStartCallback,
    ) -> MultiDragRecognizerRef {
        let recognizer = DelayedMultiDragGestureRecognizer::new(app);
        recognizer.set_delay(app, self.delay);
        let recognizer: MultiDragRecognizerRef = recognizer.into_erased();
        if let Some(filter) = self.draggable.allowed_buttons_filter.clone() {
            recognizer.set_allowed_buttons_filter(app, filter);
        }
        let haptic = self.haptic_feedback_on_start;
        recognizer.set_on_start(
            app,
            Some(Rc::new(move |app, position| {
                let result = on_start(app, position);
                if result.is_some() && haptic {
                    HapticFeedback::selection_click(app);
                }
                result
            })),
        );
        recognizer
    }
}

impl<T: 'static> StatefulWidget for LongPressDraggable<T> {
    type State = DraggableState<Self>;

    fn key(&self) -> Option<&KeyRef> {
        self.draggable.key.as_ref()
    }

    fn create_state(&self) -> Self::State {
        DraggableState::new()
    }
}

/// Retains the recognizer until every active drag has ended, even after unmount.
pub struct DraggableState<W: DraggableWidget> {
    /// Native widget lifecycle and element association.
    state: StateData<W>,

    /// Stays alive after unmount until every drag listening to pointer events has ended.
    recognizer: Option<MultiDragRecognizerRef>,

    /// Number of active drag avatars retaining this state and recognizer.
    active_count: usize,
}

impl<W: DraggableWidget> Default for DraggableState<W> {
    /// Creates the same inactive state as `new`.
    fn default() -> Self {
        Self::new()
    }
}

impl<W: DraggableWidget> DraggableState<W> {
    /// Creates state for Draggable and widgets overriding its recognizer factory.
    pub fn new() -> Self {
        Self {
            state: StateData::new(),
            recognizer: None,
            active_count: 0,
        }
    }

    fn dispose_recognizer_if_inactive(self: Handle<Self>, app: &mut App) {
        if app.get(self).active_count > 0 {
            return;
        }
        if let Some(recognizer) = app.get_mut(self).recognizer.take() {
            recognizer.dispose(app);
        }
    }

    fn route_pointer(self: Handle<Self>, app: &mut App, event: PointerDownEvent) {
        if self
            .widget(app)
            .draggable()
            .max_simultaneous_drags
            .is_some_and(|max| app.get(self).active_count >= max)
        {
            return;
        }
        app.get(self)
            .recognizer
            .handle()
            .unwrap()
            .add_pointer(app, &event);
    }

    fn start_drag(self: Handle<Self>, app: &mut App, position: Offset) -> Option<Rc<dyn Drag>> {
        let widget = self.widget(app).draggable().clone();
        if widget
            .max_simultaneous_drags
            .is_some_and(|max| app.get(self).active_count >= max)
        {
            return None;
        }
        let context = self.context(app);
        let start = (widget.drag_anchor_strategy)(app, &widget, context, position);
        self.set_state(app, |state| state.active_count += 1);
        let retained = self.keep(app);
        let overlay = Overlay::of(app, context, widget.root_overlay);
        let view_id = View::of(app, context).id();
        let avatar = DragAvatar::new(
            app,
            overlay,
            widget.data.clone().map(|data| data as Rc<dyn Any>),
            widget.axis,
            position,
            start,
            widget.feedback.clone(),
            widget.feedback_offset,
            widget.ignoring_feedback_semantics,
            widget.ignoring_feedback_pointer,
            view_id,
            Rc::new(move |app, details| {
                if self.mounted(app)
                    && let Some(callback) = self.widget(app).draggable().on_drag_update.clone()
                {
                    callback(app, details);
                }
            }),
            Rc::new(move |app, velocity, offset, was_accepted| {
                let _keep_alive = &retained;
                if self.mounted(app) {
                    self.set_state(app, |state| state.active_count -= 1);
                } else {
                    app.get_mut(self).active_count -= 1;
                    self.dispose_recognizer_if_inactive(app);
                }
                let widget = self.widget(app).draggable().clone();
                if self.mounted(app)
                    && let Some(callback) = widget.on_drag_end
                {
                    callback(
                        app,
                        DraggableDetails {
                            was_accepted,
                            velocity,
                            offset,
                        },
                    );
                }
                if was_accepted
                    && let Some(callback) = self.widget(app).draggable().on_drag_completed.clone()
                {
                    callback.call(app);
                }
                if !was_accepted
                    && let Some(callback) =
                        self.widget(app).draggable().on_draggable_canceled.clone()
                {
                    callback(app, velocity, offset);
                }
            }),
        );
        if let Some(callback) = widget.on_drag_started {
            callback.call(app);
        }
        Some(Rc::new(avatar))
    }
}

impl<W: DraggableWidget> State for DraggableState<W> {
    type Widget = W;
    crate::state_accessors!();
    fn init_state(self: Handle<Self>, app: &mut App) {
        let widget = self.widget(app).clone();
        let recognizer = widget.create_recognizer(
            app,
            Rc::new(move |app, position| self.start_drag(app, position)),
        );
        app.get_mut(self).recognizer = Some(recognizer);
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        self.dispose_recognizer_if_inactive(app);
    }

    fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
        let context = self.context(app);
        let settings = MediaQuery::maybe_gesture_settings_of(app, context);
        app.get(self)
            .recognizer
            .handle()
            .unwrap()
            .set_gesture_settings(app, settings);
    }

    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        let widget = self.widget(app).draggable();
        let can_drag = widget
            .max_simultaneous_drags
            .is_none_or(|max| app.get(self).active_count < max);
        let show_child = app.get(self).active_count == 0 || widget.child_when_dragging.is_none();
        let mut listener = crate::Listener::new()
            .behavior(widget.hit_test_behavior)
            .child(if show_child {
                widget.child.clone()
            } else {
                widget.child_when_dragging.clone().unwrap()
            });
        if can_drag {
            listener =
                listener.on_pointer_down(Rc::new(move |app, event| self.route_pointer(app, event)));
        }
        listener.into_widget()
    }
}

/// Details when a draggable is dropped, including acceptance, velocity, and offset.
#[derive(Clone, Copy, Debug)]
pub struct DraggableDetails {
    /// Determines whether the `DragTarget` accepted this draggable.
    pub was_accepted: bool,

    /// The velocity at which the pointer was moving when the specific pointer
    /// event occurred on the draggable.
    pub velocity: Velocity,

    /// The global position when the specific pointer event occurred on
    /// the draggable.
    pub offset: Offset,
}

/// The data and global position at which a pointer event occurred on a drag target.
pub struct DragTargetDetails<T> {
    /// The data that was dropped onto this `DragTarget`.
    pub data: Rc<T>,

    /// The global position when the specific pointer event occurred on
    /// the draggable.
    pub offset: Offset,
}

impl<T> Clone for DragTargetDetails<T> {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            offset: self.offset,
        }
    }
}

/// A widget that receives data when a Draggable is dropped over it.
pub struct DragTarget<T: 'static> {
    /// Identity of this target in its parent's child list.
    pub key: Option<KeyRef>,

    /// Builds the target using all current accepted and rejected drag data.
    pub builder: DragTargetBuilder<T>,

    /// Legacy data-only acceptance predicate; mutually exclusive with the detailed predicate.
    pub on_will_accept: Option<DragTargetWillAccept<T>>,

    /// Determines whether to accept an entering drag, including its position.
    pub on_will_accept_with_details: Option<DragTargetWillAcceptWithDetails<T>>,

    /// Called when non-null data is dropped on this target.
    pub on_accept: Option<DragTargetAccept<T>>,

    /// Called when non-null data is dropped, including its position.
    pub on_accept_with_details: Option<DragTargetAcceptWithDetails<T>>,

    /// Called when a drag leaves this target.
    pub on_leave: Option<DragTargetLeave<T>>,

    /// Called when non-null drag data moves within this target.
    pub on_move: Option<DragTargetMove<T>>,

    /// Defaults to translucent so targets behind rejected targets can receive a drag.
    pub hit_test_behavior: HitTestBehavior,
}

impl<T: 'static> fmt::Debug for DragTarget<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DragTarget").finish_non_exhaustive()
    }
}

impl<T: 'static> DragTarget<T> {
    /// Creates a target whose presentation reflects the current candidate and rejected data.
    pub fn new(
        builder: impl Fn(
            &mut App,
            BuildContext,
            Vec<Option<Rc<T>>>,
            Vec<Option<Rc<dyn Any>>>,
        ) -> WidgetRef
        + 'static,
    ) -> Self {
        Self {
            key: None,
            builder: Rc::new(builder),
            on_will_accept: None,
            on_will_accept_with_details: None,
            on_accept: None,
            on_accept_with_details: None,
            on_leave: None,
            on_move: None,
            hit_test_behavior: HitTestBehavior::Translucent,
        }
    }

    /// Sets the widget identity.
    pub fn key(mut self, value: KeyRef) -> Self {
        self.key = Some(value);
        self
    }

    /// Sets the legacy data-only acceptance predicate.
    pub fn on_will_accept(mut self, value: DragTargetWillAccept<T>) -> Self {
        self.on_will_accept = Some(value);
        self
    }

    /// Sets the predicate for accepting an entering drag.
    pub fn on_will_accept_with_details(
        mut self,
        value: DragTargetWillAcceptWithDetails<T>,
    ) -> Self {
        self.on_will_accept_with_details = Some(value);
        self
    }

    /// Sets the callback for dropped non-null data.
    pub fn on_accept(mut self, value: DragTargetAccept<T>) -> Self {
        self.on_accept = Some(value);
        self
    }

    /// Sets the callback for a drop's data and global position.
    pub fn on_accept_with_details(mut self, value: DragTargetAcceptWithDetails<T>) -> Self {
        self.on_accept_with_details = Some(value);
        self
    }

    /// Sets the callback for a drag leaving the target.
    pub fn on_leave(mut self, value: DragTargetLeave<T>) -> Self {
        self.on_leave = Some(value);
        self
    }

    /// Sets the callback for movement within the target.
    pub fn on_move(mut self, value: DragTargetMove<T>) -> Self {
        self.on_move = Some(value);
        self
    }

    /// Sets how the target participates in hit testing.
    pub fn hit_test_behavior(mut self, value: HitTestBehavior) -> Self {
        self.hit_test_behavior = value;
        self
    }
}

impl<T: 'static> StatefulWidget for DragTarget<T> {
    type State = DragTargetState<T>;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> Self::State {
        debug_assert!(
            self.on_will_accept.is_none() || self.on_will_accept_with_details.is_none(),
            "Don't pass both acceptance callbacks"
        );
        DragTargetState {
            state: StateData::new(),
            candidate_avatars: Vec::new(),
            rejected_avatars: Vec::new(),
        }
    }
}

/// The candidate and rejected avatars currently hovering over one target.
pub struct DragTargetState<T: 'static> {
    /// Native widget lifecycle and element association.
    state: StateData<DragTarget<T>>,

    /// Avatars whose data this target accepted on entry.
    candidate_avatars: Vec<Ref<DragAvatar>>,

    /// Avatars whose data this target rejected on entry.
    rejected_avatars: Vec<Ref<DragAvatar>>,
}

/// Erases the target's data type while retaining its normal mounted-state checks.
trait DragTargetHandler {
    fn id(&self) -> HandleId;

    fn is_expected_data_type(&self, data: &Option<Rc<dyn Any>>) -> bool;

    fn did_enter(&self, app: &mut App, avatar: Handle<DragAvatar>) -> bool;

    fn did_leave(&self, app: &mut App, avatar: Handle<DragAvatar>);

    fn did_drop(&self, app: &mut App, avatar: Handle<DragAvatar>);

    fn did_move(&self, app: &mut App, avatar: Handle<DragAvatar>);
}

/// Keeps a target alive while hit testing and active drags hold its metadata.
struct DragTargetMetadata {
    /// Erased target callbacks stored in the render metadata, which keep the target's state.
    handler: Rc<dyn DragTargetHandler>,
}

impl<T: 'static> DragTargetHandler for Ref<DragTargetState<T>> {
    fn id(&self) -> HandleId {
        self.into()
    }

    fn is_expected_data_type(&self, data: &Option<Rc<dyn Any>>) -> bool {
        data.as_ref().is_none_or(|data| data.is::<T>())
    }

    fn did_enter(&self, app: &mut App, avatar: Handle<DragAvatar>) -> bool {
        debug_assert!(
            !app.get(self)
                .candidate_avatars
                .iter()
                .any(|kept| kept == avatar)
        );
        debug_assert!(
            !app.get(self)
                .rejected_avatars
                .iter()
                .any(|kept| kept == avatar)
        );
        let data = app
            .get(avatar)
            .data
            .clone()
            .map(|data| data.downcast::<T>().unwrap());
        let widget = self.widget(app);
        let accepted = match (
            widget.on_will_accept.clone(),
            widget.on_will_accept_with_details.clone(),
        ) {
            (None, None) => true,
            (Some(callback), _) => callback(app, data.clone()),
            (_, Some(callback)) => data.is_some_and(|data| {
                callback(
                    app,
                    DragTargetDetails {
                        data,
                        offset: app.get(avatar).last_offset.unwrap(),
                    },
                )
            }),
        };
        let kept = avatar.keep(app);
        self.set_state(app, |state| {
            if accepted {
                state.candidate_avatars.push(kept)
            } else {
                state.rejected_avatars.push(kept)
            }
        });
        accepted
    }

    fn did_leave(&self, app: &mut App, avatar: Handle<DragAvatar>) {
        if !self.mounted(app) {
            return;
        }
        self.set_state(app, |state| {
            state
                .candidate_avatars
                .retain(|candidate| candidate != avatar);
            state
                .rejected_avatars
                .retain(|candidate| candidate != avatar);
        });
        if let Some(callback) = self.widget(app).on_leave.clone() {
            let data = app
                .get(avatar)
                .data
                .clone()
                .map(|data| data.downcast::<T>().unwrap());
            callback(app, data);
        }
    }

    fn did_drop(&self, app: &mut App, avatar: Handle<DragAvatar>) {
        debug_assert!(
            app.get(self)
                .candidate_avatars
                .iter()
                .any(|kept| kept == avatar)
        );
        if !self.mounted(app) {
            return;
        }
        self.set_state(app, |state| {
            state
                .candidate_avatars
                .retain(|candidate| candidate != avatar)
        });
        if let Some(data) = app.get(avatar).data.clone() {
            let data = data.downcast::<T>().unwrap();
            if let Some(callback) = self.widget(app).on_accept.clone() {
                callback(app, data.clone());
            }
            if let Some(callback) = self.widget(app).on_accept_with_details.clone() {
                callback(
                    app,
                    DragTargetDetails {
                        data,
                        offset: app.get(avatar).last_offset.unwrap(),
                    },
                );
            }
        }
    }

    fn did_move(&self, app: &mut App, avatar: Handle<DragAvatar>) {
        if !self.mounted(app) {
            return;
        }
        if let Some(data) = app.get(avatar).data.clone()
            && let Some(callback) = self.widget(app).on_move.clone()
        {
            callback(
                app,
                DragTargetDetails {
                    data: data.downcast::<T>().unwrap(),
                    offset: app.get(avatar).last_offset.unwrap(),
                },
            );
        }
    }
}

impl<T: 'static> State for DragTargetState<T> {
    type Widget = DragTarget<T>;
    crate::state_accessors!();

    fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
        let state = app.get(self);
        let candidates = state
            .candidate_avatars
            .iter()
            .map(|avatar| {
                app.get(avatar)
                    .data
                    .clone()
                    .map(|data| data.downcast::<T>().unwrap())
            })
            .collect();
        let rejected = state
            .rejected_avatars
            .iter()
            .map(|avatar| app.get(avatar).data.clone())
            .collect();
        let builder = self.widget(app).builder.clone();
        let child = builder(app, context, candidates, rejected);
        MetaData::new()
            .meta_data(Rc::new(DragTargetMetadata {
                handler: Rc::new(self.keep(app)),
            }))
            .behavior(self.widget(app).hit_test_behavior)
            .child(child)
            .into_widget()
    }
}

/// Whether the pointer ended normally or the drag was canceled.
#[derive(Clone, Copy, PartialEq, Eq)]
enum DragEndKind {
    Dropped,
    Canceled,
}

/// Reports the final velocity, feedback offset and target acceptance.
type OnDragEnd = Rc<dyn Fn(&mut App, Velocity, Offset, bool)>;

/// The feedback overlay and target path for one active pointer.
struct DragAvatar {
    /// The data carried by this draggable.
    data: Option<Rc<dyn Any>>,

    /// The axis restricting drag motion, if specified.
    axis: Option<Axis>,

    /// Pickup offset used to anchor feedback to the pointer.
    drag_start_point: Offset,

    /// The widget displayed beneath the pointer during the drag.
    feedback: WidgetRef,

    /// Offset used when hit testing to find drag targets.
    feedback_offset: Offset,

    /// Called after an update changes the drag position.
    on_drag_update: DragUpdateCallback,

    /// Reports the restricted velocity, final offset and acceptance.
    on_drag_end: OnDragEnd,

    /// Overlay into which the feedback entry is inserted.
    overlay_state: Handle<OverlayState>,

    /// Whether feedback is excluded from the semantics tree.
    ignoring_feedback_semantics: bool,

    /// Whether feedback is excluded from pointer hit testing.
    ignoring_feedback_pointer: bool,

    /// View used to hit test the global drag position.
    view_id: ViewId,

    /// First entered target currently accepting this avatar.
    active_target: Option<Rc<DragTargetMetadata>>,

    /// Targets whose metadata appeared in the previous hit test.
    entered_targets: Vec<Rc<DragTargetMetadata>>,

    /// Current global pointer position, restricted to the configured axis.
    position: Offset,

    /// Last global feedback offset after subtracting the pickup anchor.
    last_offset: Option<Offset>,

    /// Feedback origin in the overlay’s coordinate system.
    overlay_offset: Offset,

    /// Overlay entry retained until the drag finishes.
    entry: Option<Ref<OverlayEntry>>,
}

impl DragAvatar {
    /// Inserts feedback into the overlay and hit-tests the initial pointer position.
    #[allow(clippy::too_many_arguments)]
    fn new(
        app: &mut App,
        overlay_state: Handle<OverlayState>,
        data: Option<Rc<dyn Any>>,
        axis: Option<Axis>,
        initial_position: Offset,
        drag_start_point: Offset,
        feedback: WidgetRef,
        feedback_offset: Offset,
        ignoring_feedback_semantics: bool,
        ignoring_feedback_pointer: bool,
        view_id: ViewId,
        on_drag_update: DragUpdateCallback,
        on_drag_end: OnDragEnd,
    ) -> Ref<Self> {
        let avatar = app.create(Self {
            data,
            axis,
            drag_start_point,
            feedback,
            feedback_offset,
            on_drag_update,
            on_drag_end,
            overlay_state,
            ignoring_feedback_semantics,
            ignoring_feedback_pointer,
            view_id,
            active_target: None,
            entered_targets: Vec::new(),
            position: initial_position,
            last_offset: None,
            overlay_offset: Offset::ZERO,
            entry: None,
        });
        // Dart's `_entry` builds with a tear-off of the avatar, which keeps it while the entry is
        // in the overlay: the ring the entry and this builder make is cut when `finish_drag`
        // takes the entry.
        let kept = avatar.clone();
        let entry = OverlayEntry::new(
            app,
            Rc::new(move |app, context| kept.build(app, context)),
            false,
            false,
            false,
        );
        app.get_mut(&avatar).entry = Some(entry.clone());
        overlay_state.insert(app, entry, None, None);
        avatar.update_drag(app, initial_position);
        avatar
    }

    /// Updates overlay coordinates, then enters, leaves and moves targets in hit-test order.
    fn update_drag(self: Handle<Self>, app: &mut App, global_position: Offset) {
        let state = app.get(self);
        let (overlay, anchor, feedback_offset, view_id) = (
            state.overlay_state,
            state.drag_start_point,
            state.feedback_offset,
            state.view_id,
        );
        app.get_mut(self).last_offset = Some(global_position - anchor);
        if overlay.mounted(app) {
            let render_box = overlay
                .context(app)
                .find_render_object(app)
                .unwrap()
                .as_box()
                .unwrap();
            app.get_mut(self).overlay_offset =
                render_box.global_to_local(app, global_position, None) - anchor;
            app.get(self).entry.handle().unwrap().mark_needs_build(app);
        }
        let mut result = HitTestResult::new();
        GestureBinding::instance(app).hit_test_in_view(
            app,
            &mut result,
            global_position + feedback_offset,
            view_id,
        );
        let mut targets = Vec::new();
        for entry in result.path() {
            let Some(hit) = (entry.target() as &dyn Any).downcast_ref::<BoxHitTestEntry>() else {
                continue;
            };
            let Some(render) = hit.target().as_object().downcast::<RenderMetaData>(app) else {
                continue;
            };
            let Some(metadata) = render
                .meta_data(app)
                .and_then(|value| value.downcast::<DragTargetMetadata>().ok())
            else {
                continue;
            };
            if metadata.handler.is_expected_data_type(&app.get(self).data) {
                targets.push(metadata);
            }
        }
        let state = app.get(self);
        let lists_match = !state.entered_targets.is_empty()
            && targets.len() >= state.entered_targets.len()
            && targets
                .iter()
                .zip(&state.entered_targets)
                .all(|(a, b)| a.handler.id() == b.handler.id());
        if lists_match
            && (state.active_target.is_some() || targets.len() == state.entered_targets.len())
        {
            for target in state.entered_targets.clone() {
                target.handler.did_move(app, self);
            }
            return;
        }
        self.leave_all_entered(app);
        let mut active = None;
        for target in targets {
            app.get_mut(self).entered_targets.push(target.clone());
            if target.handler.did_enter(app, self) {
                active = Some(target);
                break;
            }
        }
        for target in app.get(self).entered_targets.clone() {
            target.handler.did_move(app, self);
        }
        app.get_mut(self).active_target = active;
    }

    /// Sends leave to every previously entered target before clearing the path.
    fn leave_all_entered(self: Handle<Self>, app: &mut App) {
        for target in app.get(self).entered_targets.clone() {
            target.handler.did_leave(app, self);
        }
        app.get_mut(self).entered_targets.clear();
    }

    /// Drops on the active target, releases feedback, and reports the final outcome.
    fn finish_drag(self: Handle<Self>, app: &mut App, kind: DragEndKind, velocity: Velocity) {
        let target = app.get(self).active_target.clone();
        let mut accepted = false;
        if kind == DragEndKind::Dropped
            && let Some(target) = target
        {
            target.handler.did_drop(app, self);
            accepted = true;
            app.get_mut(self)
                .entered_targets
                .retain(|entry| entry.handler.id() != target.handler.id());
        }
        self.leave_all_entered(app);
        app.get_mut(self).active_target = None;
        let entry = app.get_mut(self).entry.take().unwrap();
        entry.remove(app);
        entry.dispose(app);
        let callback = app.get(self).on_drag_end.clone();
        callback(app, velocity, app.get(self).last_offset.unwrap(), accepted);
    }

    /// Positions feedback in overlay coordinates.
    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        let state = app.get(self);
        Positioned::new(
            ExcludeSemantics::new()
                .excluding(state.ignoring_feedback_semantics)
                .child(
                    IgnorePointer::new()
                        .ignoring(state.ignoring_feedback_pointer)
                        .child(state.feedback.clone()),
                ),
        )
        .left(state.overlay_offset.dx())
        .top(state.overlay_offset.dy())
        .into_widget()
    }

    /// Restricts movement to the configured axis without changing hit-test coordinates.
    fn restrict_axis(self: Handle<Self>, app: &App, offset: Offset) -> Offset {
        match app.get(self).axis {
            Some(Axis::Horizontal) => Offset::new(offset.dx(), 0.0),
            Some(Axis::Vertical) => Offset::new(0.0, offset.dy()),
            None => offset,
        }
    }
}

impl DragObject for DragAvatar {
    fn update(self: Handle<Self>, app: &mut App, details: DragUpdateDetails) {
        let old_position = app.get(self).position;
        let position = old_position + self.restrict_axis(app, details.delta);
        app.get_mut(self).position = position;
        self.update_drag(app, position);
        if position != old_position {
            let callback = app.get(self).on_drag_update.clone();
            callback(app, details);
        }
    }

    fn end(self: Handle<Self>, app: &mut App, details: DragEndDetails) {
        let velocity = Velocity::new(self.restrict_axis(app, details.velocity.pixels_per_second));
        self.finish_drag(app, DragEndKind::Dropped, velocity);
    }

    fn cancel(self: Handle<Self>, app: &mut App) {
        self.finish_drag(app, DragEndKind::Canceled, Velocity::ZERO);
    }
}