zng_ext_input/
drag_drop.rs

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
//! Drag & drop gesture events and service.

use std::{mem, sync::Arc};

use parking_lot::Mutex;
use zng_app::{
    event::{event, event_args, AnyEventArgs},
    static_id,
    update::{EventUpdate, UPDATES},
    view_process::raw_events::{
        RAW_APP_DRAG_ENDED_EVENT, RAW_DRAG_CANCELLED_EVENT, RAW_DRAG_DROPPED_EVENT, RAW_DRAG_HOVERED_EVENT, RAW_DRAG_MOVED_EVENT,
    },
    widget::{
        info::{HitTestInfo, InteractionPath, WidgetInfo, WidgetInfoBuilder},
        WidgetId, WIDGET,
    },
    window::WindowId,
    AppExtension,
};
use zng_app_context::app_local;
use zng_ext_window::{NestedWindowWidgetInfoExt as _, WINDOWS, WINDOWS_DRAG_DROP};
use zng_handle::{Handle, HandleOwner, WeakHandle};
use zng_layout::unit::{DipPoint, DipToPx as _, PxToDip as _};
use zng_state_map::StateId;
use zng_txt::{formatx, Txt};
use zng_var::{var, ArcVar, ReadOnlyArcVar, Var};
use zng_view_api::{mouse::ButtonState, touch::TouchPhase, DragDropId};

use crate::{mouse::MOUSE_INPUT_EVENT, touch::TOUCH_INPUT_EVENT};

pub use zng_view_api::drag_drop::{DragDropData, DragDropEffect};

/// Application extension that provides drag&drop events and service.
///
/// # Events
///
/// Events this extension provides.
///
/// * [`DROP_EVENT`]
/// * [`DRAG_HOVERED_EVENT`]
/// * [`DRAG_MOVE_EVENT`]
/// * [`DRAG_START_EVENT`]
/// * [`DRAG_END_EVENT`]
/// * [`DROP_EVENT`]
///
/// # Services
///
/// Services this extension provides.
///
/// * [`DRAG_DROP`]
#[derive(Default)]
pub struct DragDropManager {
    // last cursor move position (scaled).
    pos: DipPoint,
    // last cursor move over `pos_window` and source device.
    pos_window: Option<WindowId>,
    // last cursor move hit-test (on the pos_window or a nested window).
    hits: Option<HitTestInfo>,
    hovered: Option<InteractionPath>,
}

impl AppExtension for DragDropManager {
    fn event_preview(&mut self, update: &mut EventUpdate) {
        let mut update_sv = false;
        if let Some(args) = RAW_DRAG_DROPPED_EVENT.on(update) {
            // system drop
            let mut sv = DRAG_DROP_SV.write();
            let len = sv.system_dragging.len();
            for data in &args.data {
                sv.system_dragging.retain(|d| d != data);
            }
            update_sv = len != sv.system_dragging.len();

            // view-process can notify multiple drops in sequence with the same ID, so we only notify DROP_EVENT
            // on he next update
            if self.pos_window == Some(args.window_id) {
                if let Some(hovered) = &self.hovered {
                    match &mut sv.pending_drop {
                        Some((id, target, data, allowed)) => {
                            if target != hovered {
                                tracing::error!("drop sequence across different hovered")
                            } else if *id != args.drop_id {
                                tracing::error!("drop_id changed mid sequence")
                            } else if *allowed != args.allowed {
                                tracing::error!("allowed effects changed mid sequence")
                            } else {
                                data.extend(args.data.iter().cloned());
                            }
                        }
                        None => sv.pending_drop = Some((args.drop_id, hovered.clone(), args.data.clone(), args.allowed)),
                    }
                }
            }
            UPDATES.update(None);
        } else if let Some(args) = RAW_DRAG_HOVERED_EVENT.on(update) {
            // system drag hover window
            update_sv = true;
            DRAG_DROP_SV.write().system_dragging.extend(args.data.iter().cloned());
        } else if let Some(args) = RAW_DRAG_MOVED_EVENT.on(update) {
            // code adapted from the MouseManager implementation for mouse hovered
            let moved = self.pos != args.position || self.pos_window != Some(args.window_id);
            if moved {
                self.pos = args.position;
                self.pos_window = Some(args.window_id);

                let mut position = args.position;

                // mouse_move data
                let mut frame_info = match WINDOWS.widget_tree(args.window_id) {
                    Ok(f) => f,
                    Err(_) => {
                        // window not found
                        if let Some(hovered) = self.hovered.take() {
                            DRAG_HOVERED_EVENT.notify(DragHoveredArgs::now(
                                Some(hovered),
                                None,
                                position,
                                HitTestInfo::no_hits(args.window_id),
                            ));
                            self.pos_window = None;
                        }
                        return;
                    }
                };

                let mut pos_hits = frame_info.root().hit_test(position.to_px(frame_info.scale_factor()));

                let target = if let Some(t) = pos_hits.target() {
                    if let Some(w) = frame_info.get(t.widget_id) {
                        if let Some(f) = w.nested_window_tree() {
                            // nested window hit
                            frame_info = f;
                            let factor = frame_info.scale_factor();
                            let pos = position.to_px(factor);
                            let pos = w.inner_transform().inverse().and_then(|t| t.transform_point(pos)).unwrap_or(pos);
                            pos_hits = frame_info.root().hit_test(pos);
                            position = pos.to_dip(factor);
                            pos_hits
                                .target()
                                .and_then(|h| frame_info.get(h.widget_id))
                                .map(|w| w.interaction_path())
                                .unwrap_or_else(|| frame_info.root().interaction_path())
                        } else {
                            w.interaction_path()
                        }
                    } else {
                        tracing::error!("hits target `{}` not found", t.widget_id);
                        frame_info.root().interaction_path()
                    }
                } else {
                    frame_info.root().interaction_path()
                }
                .unblocked();

                self.hits = Some(pos_hits.clone());

                // drag_enter/leave.
                let hovered_args = if self.hovered != target {
                    let prev_target = mem::replace(&mut self.hovered, target.clone());
                    let args = DragHoveredArgs::now(prev_target, target.clone(), position, pos_hits.clone());
                    Some(args)
                } else {
                    None
                };

                // mouse_move
                if let Some(target) = target {
                    let args = DragMoveArgs::now(frame_info.window_id(), args.coalesced_pos.clone(), position, pos_hits, target);
                    DRAG_MOVE_EVENT.notify(args);
                }

                if let Some(args) = hovered_args {
                    DRAG_HOVERED_EVENT.notify(args);
                }
            }
        } else if let Some(args) = RAW_DRAG_CANCELLED_EVENT.on(update) {
            // system drag cancelled of dragged out of all app windows
            let mut sv = DRAG_DROP_SV.write();
            update_sv = !sv.system_dragging.is_empty();
            sv.system_dragging.clear();

            if let Some(prev) = self.hovered.take() {
                self.pos_window = None;
                DRAG_HOVERED_EVENT.notify(DragHoveredArgs::now(
                    Some(prev),
                    None,
                    self.pos,
                    self.hits.take().unwrap_or_else(|| HitTestInfo::no_hits(args.window_id)),
                ));
            }
        } else if let Some(args) = RAW_APP_DRAG_ENDED_EVENT.on(update) {
            let mut sv = DRAG_DROP_SV.write();
            sv.app_dragging.retain(|d| {
                if d.view_id != args.id {
                    return true;
                }

                if !args.applied.is_empty() && !d.allowed.contains(args.applied) {
                    tracing::error!(
                        "drop target applied disallowed effect, allowed={:?}, applied={:?}",
                        d.allowed,
                        args.applied
                    );
                }

                DRAG_END_EVENT.notify(DragEndArgs::now(d.target.clone(), args.applied));

                false
            });
        }

        if update_sv {
            DRAG_DROP.update_var();
        }
    }

    fn event(&mut self, update: &mut EventUpdate) {
        if let Some(args) = MOUSE_INPUT_EVENT.on_unhandled(update) {
            if matches!(args.state, ButtonState::Pressed) {
                if let Some(wgt) = WINDOWS.widget_info(args.target.widget_id()) {
                    if let Some(wgt) = wgt.self_and_ancestors().find(|w| w.is_draggable()) {
                        // unhandled mouse press on draggable
                        args.propagation().stop();
                        let target = wgt.interaction_path();
                        let args = DragStartArgs::now(target.clone());
                        DRAG_START_EVENT.notify(args);
                        DRAG_DROP_SV.write().app_drag = Some(AppDragging {
                            target,
                            data: vec![],
                            handles: vec![],
                            allowed: DragDropEffect::empty(),
                            view_id: DragDropId(0),
                        }); // calls to DRAG_DROP.drag are now valid
                    }
                }
            }
        } else if let Some(args) = TOUCH_INPUT_EVENT.on_unhandled(update) {
            if matches!(args.phase, TouchPhase::Start) {
                if let Some(wgt) = WINDOWS.widget_info(args.target.widget_id()) {
                    if let Some(wgt) = wgt.self_and_ancestors().find(|w| w.is_draggable()) {
                        // unhandled touch start on draggable
                        args.propagation().stop();
                        let target = wgt.interaction_path();
                        let args = DragStartArgs::now(target.clone());
                        DRAG_START_EVENT.notify(args);
                        DRAG_DROP_SV.write().app_drag = Some(AppDragging {
                            target,
                            data: vec![],
                            handles: vec![],
                            allowed: DragDropEffect::empty(),
                            view_id: DragDropId(0),
                        }); // calls to DRAG_DROP.drag are now valid
                    }
                }
            }
        } else if let Some(args) = DRAG_START_EVENT.on(update) {
            // finished notifying draggable drag start
            let mut sv = DRAG_DROP_SV.write();
            let mut data = sv.app_drag.take();
            let mut cancel = args.propagation_handle.is_stopped();
            if !cancel {
                if let Some(d) = &mut data {
                    if d.data.is_empty() {
                        d.data.push(encode_widget_id(args.target.widget_id()));
                        d.allowed = DragDropEffect::all();
                    }
                    match WINDOWS_DRAG_DROP.start_drag_drop(d.target.window_id(), mem::take(&mut d.data), d.allowed) {
                        Ok(id) => {
                            d.view_id = id;
                            sv.app_dragging.push(data.take().unwrap());
                        }
                        Err(e) => {
                            tracing::error!("cannot start drag&drop, {e}");
                            cancel = true;
                        }
                    }
                } else {
                    tracing::warn!("external notification of DRAG_START_EVENT ignored")
                }
            }
            if cancel {
                if let Some(d) = data {
                    DRAG_END_EVENT.notify(DragEndArgs::now(d.target, DragDropEffect::empty()));
                }
            }
        } else if let Some(args) = DROP_EVENT.on(update) {
            let _ = WINDOWS_DRAG_DROP.drag_dropped(args.target.window_id(), args.drop_id, *args.applied.lock());
        }
    }

    fn update_preview(&mut self) {
        let mut sv = DRAG_DROP_SV.write();

        // fulfill drop requests
        if let Some((id, target, data, allowed)) = sv.pending_drop.take() {
            let window_id = self.pos_window.take().unwrap();
            let hits = self.hits.take().unwrap_or_else(|| HitTestInfo::no_hits(window_id));
            DRAG_HOVERED_EVENT.notify(DragHoveredArgs::now(Some(target.clone()), None, self.pos, hits.clone()));
            DROP_EVENT.notify(DropArgs::now(
                target,
                data,
                allowed,
                self.pos,
                hits,
                id,
                Arc::new(Mutex::new(DragDropEffect::empty())),
            ));
        }
    }
}

/// Drag & drop service.
#[allow(non_camel_case_types)]
pub struct DRAG_DROP;
impl DRAG_DROP {
    /// All data current dragging.
    pub fn dragging_data(&self) -> ReadOnlyArcVar<Vec<DragDropData>> {
        DRAG_DROP_SV.read().data.read_only()
    }

    /// Start dragging `data`.
    ///
    /// This method will only work if a [`DRAG_START_EVENT`] is notifying. Handlers of draggable widgets
    /// can provide custom drag data using this method.
    ///
    /// Returns a handle that can be dropped to cancel the drag operation. A [`DRAG_END_EVENT`] notifies
    /// the draggable widget on cancel or drop. Logs an error message and returns a dummy handle on error.
    ///
    /// Note that the `allowed_effects` apply to all data, if a previous handler already set data with an incompatible
    /// effect the call is an error and the data ignored.
    pub fn drag(&self, data: DragDropData, allowed_effects: DragDropEffect) -> DragHandle {
        let mut sv = DRAG_DROP_SV.write();
        if let Some(d) = &mut sv.app_drag {
            if allowed_effects.is_empty() {
                tracing::error!("cannot drag, no `allowed_effects`");
                return DragHandle::dummy();
            }

            if d.allowed.is_empty() {
                d.allowed = allowed_effects;
            } else {
                if !d.allowed.contains(allowed_effects) {
                    tracing::error!("cannot drag, other data already set with incompatible `allowed_effects`");
                    return DragHandle::dummy();
                }
                d.allowed |= allowed_effects
            }

            d.data.push(data);
            let (owner, handle) = DragHandle::new();
            d.handles.push(owner);
            return handle;
        }
        tracing::error!("cannot drag, not in `DRAG_START_EVENT` interval");
        DragHandle::dummy()
    }

    fn update_var(&self) {
        let sv = DRAG_DROP_SV.read();
        sv.data.set(sv.system_dragging.clone());
    }
}

app_local! {
    static DRAG_DROP_SV: DragDropService = DragDropService {
        data: var(vec![]),
        system_dragging: vec![],
        app_drag: None,
        app_dragging: vec![],
        pending_drop: None,
    };
}
struct DragDropService {
    data: ArcVar<Vec<DragDropData>>,

    system_dragging: Vec<DragDropData>,

    app_drag: Option<AppDragging>,
    app_dragging: Vec<AppDragging>,

    pending_drop: Option<(DragDropId, InteractionPath, Vec<DragDropData>, DragDropEffect)>,
}
struct AppDragging {
    target: InteractionPath,
    data: Vec<DragDropData>,
    handles: Vec<HandleOwner<()>>,
    allowed: DragDropEffect,
    view_id: DragDropId,
}

/// Represents dragging data.
///
/// Drop all clones of this handle to cancel the drag operation.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[repr(transparent)]
#[must_use = "dropping the handle cancels the drag operation"]
pub struct DragHandle(Handle<()>);
impl DragHandle {
    fn new() -> (HandleOwner<()>, Self) {
        let (owner, handle) = Handle::new(());
        (owner, Self(handle))
    }

    /// New handle to nothing.
    pub fn dummy() -> Self {
        Self(Handle::dummy(()))
    }

    /// Drops the handle but does **not** cancel the drag operation.
    ///
    /// The drag data stays alive until the user completes or cancels the operation.
    pub fn perm(self) {
        self.0.perm();
    }

    /// If another handle has called [`perm`](Self::perm).
    ///
    /// If `true` operation will run to completion.
    pub fn is_permanent(&self) -> bool {
        self.0.is_permanent()
    }

    /// Drops the handle and forces operation the cancel.
    pub fn cancel(self) {
        self.0.force_drop()
    }

    /// If another handle has called [`cancel`](Self::cancel).
    pub fn is_canceled(&self) -> bool {
        self.0.is_dropped()
    }

    /// Create a weak handle.
    pub fn downgrade(&self) -> WeakDragHandle {
        WeakDragHandle(self.0.downgrade())
    }
}
/// Weak [`DragHandle`].
#[derive(Clone, PartialEq, Eq, Hash, Default, Debug)]
pub struct WeakDragHandle(WeakHandle<()>);
impl WeakDragHandle {
    /// New weak handle that does not upgrade.
    pub fn new() -> Self {
        Self(WeakHandle::new())
    }

    /// Gets the strong handle if it is still subscribed.
    pub fn upgrade(&self) -> Option<DragHandle> {
        self.0.upgrade().map(DragHandle)
    }
}

/// [`WidgetInfo`] extensions for drag & drop service.
pub trait WidgetInfoDragDropExt {
    /// If this widget can be dragged and dropped.
    fn is_draggable(&self) -> bool;
}
impl WidgetInfoDragDropExt for WidgetInfo {
    fn is_draggable(&self) -> bool {
        self.meta().flagged(*IS_DRAGGABLE_ID)
    }
}

/// [`WidgetInfoBuilder`] extensions for drag & drop service.
pub trait WidgetInfoBuilderDragDropExt {
    /// Flag the widget as draggable.
    fn draggable(&mut self);
}
impl WidgetInfoBuilderDragDropExt for WidgetInfoBuilder {
    fn draggable(&mut self) {
        self.flag_meta(*IS_DRAGGABLE_ID);
    }
}

static_id! {
    static ref IS_DRAGGABLE_ID: StateId<()>;
}

event_args! {
    /// Arguments for [`DROP_EVENT`].
    pub struct DropArgs {
        /// Hovered target of the drag&drop gesture.
        pub target: InteractionPath,
        /// Drag&drop data payload.
        pub data: Vec<DragDropData>,
        /// Drop effects that the drag source allows.
        pub allowed: DragDropEffect,
        /// Position of the cursor in the window's content area.
        pub position: DipPoint,
        /// Hit-test result for the cursor point in the window.
        pub hits: HitTestInfo,

        drop_id: DragDropId,
        applied: Arc<Mutex<DragDropEffect>>,

        ..

        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
            list.insert_wgt(&self.target);
        }
    }

    /// Arguments for [`DRAG_HOVERED_EVENT`].
    pub struct DragHoveredArgs {
        /// Previous hovered target.
        pub prev_target: Option<InteractionPath>,
        /// New hovered target.
        pub target: Option<InteractionPath>,
        /// Position of the cursor in the window's content area.
        pub position: DipPoint,
        /// Hit-test result for the cursor point in the window.
        pub hits: HitTestInfo,

        ..

        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
            if let Some(p) = &self.prev_target {
                list.insert_wgt(p);
            }
            if let Some(p) = &self.target {
                list.insert_wgt(p);
            }
        }
    }

    /// [`DRAG_MOVE_EVENT`] arguments.
    pub struct DragMoveArgs {
        /// Id of window that received the event.
        pub window_id: WindowId,

        /// Positions of the cursor in between the previous event and this one.
        ///
        /// Drag move events can be coalesced, i.e. multiple moves packed into a single event.
        pub coalesced_pos: Vec<DipPoint>,

        /// Position of the cursor in the window's content area.
        pub position: DipPoint,

        /// Hit-test result for the cursor point in the window.
        pub hits: HitTestInfo,

        /// Full path to the top-most hit in [`hits`](DragMoveArgs::hits).
        pub target: InteractionPath,

        ..

        /// The [`target`].
        ///
        /// [`target`]: Self::target
        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
            list.insert_wgt(&self.target);
        }
    }

    /// Arguments for [`DRAG_START_EVENT`].
    pub struct DragStartArgs {
        /// Draggable widget that has started dragging.
        pub target: InteractionPath,

        ..

        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
            list.insert_wgt(&self.target);
        }
    }

    /// Arguments for [`DRAG_END_EVENT`].
    pub struct DragEndArgs {
        /// Draggable widget that was dragging.
        pub target: InteractionPath,

        /// Effect applied by the drop target on the data.
        ///
        /// Is empty or a single flag.
        pub applied: DragDropEffect,

        ..

        fn delivery_list(&self, list: &mut UpdateDeliveryList) {
            list.insert_wgt(&self.target);
        }

        /// The `applied` field can only be empty or only have a single flag set.
        fn validate(&self) -> Result<(), Txt> {
            if self.applied.is_empty() && self.applied.len() > 1 {
                return Err("only one or none `DragDropEffect` can be applied".into());
            }
            Ok(())
        }
    }
}
event! {
    /// Drag&drop action finished over some drop target widget.
    pub static DROP_EVENT: DropArgs;
    /// Drag&drop enter or exit a drop target widget.
    pub static DRAG_HOVERED_EVENT: DragHoveredArgs;
    /// Drag&drop is dragging over the target widget.
    pub static DRAG_MOVE_EVENT: DragMoveArgs;
    /// Drag&drop started dragging a draggable widget.
    ///
    /// If propagation is stopped the drag operation is cancelled. Handlers can use
    /// [`DRAG_DROP.drag`] to set the data, otherwise the widget ID will be dragged.
    ///
    /// [`DRAG_DROP.drag`]: DRAG_DROP::drag
    pub static DRAG_START_EVENT: DragStartArgs;

    /// Drag&drop gesture started from the draggable widget has ended.
    pub static DRAG_END_EVENT: DragEndArgs;
}

impl DropArgs {
    /// If the `widget_id` is in the [`target`] is enabled.
    ///
    /// [`target`]: Self::target
    pub fn is_enabled(&self, widget_id: WidgetId) -> bool {
        self.target.interactivity_of(widget_id).map(|i| i.is_enabled()).unwrap_or(false)
    }

    /// If the `widget_id` is in the [`target`] is disabled.
    ///
    /// [`target`]: Self::target
    pub fn is_disabled(&self, widget_id: WidgetId) -> bool {
        self.target.interactivity_of(widget_id).map(|i| i.is_disabled()).unwrap_or(false)
    }

    /// Stop propagation and set the `effect` that was applied to the data.
    ///
    /// Logs an error if propagation is already stopped.
    ///
    /// # Panics
    ///
    /// Panics if `effect` sets more then one flag or is not [`allowed`].
    ///
    /// [`allowed`]: Self::allowed
    pub fn applied(&self, effect: DragDropEffect) {
        assert!(effect.len() > 1, "can only apply one effect");
        assert!(self.allowed.contains(effect), "source does not allow this effect");

        let mut e = self.applied.lock();
        if !self.propagation().is_stopped() {
            self.propagation().stop();
            *e = effect;
        } else {
            tracing::error!("drop already handled");
        }
    }
}

impl DragHoveredArgs {
    /// Gets the [`DRAG_DROP.dragging_data`].
    ///
    /// [`DRAG_DROP.dragging_data`]: DRAG_DROP::dragging_data
    pub fn data(&self) -> ReadOnlyArcVar<Vec<DragDropData>> {
        DRAG_DROP.dragging_data()
    }

    /// Returns `true` if the [`WIDGET`] was not hovered, but now is.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_enter(&self) -> bool {
        !self.was_over() && self.is_over()
    }

    /// Returns `true` if the [`WIDGET`] was hovered, but now isn't.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_leave(&self) -> bool {
        self.was_over() && !self.is_over()
    }

    /// Returns `true` if the [`WIDGET`] is in [`prev_target`].
    ///
    /// [`prev_target`]: Self::prev_target
    /// [`prev_capture`]: Self::prev_capture
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn was_over(&self) -> bool {
        if let Some(t) = &self.prev_target {
            return t.contains(WIDGET.id());
        }

        false
    }

    /// Returns `true` if the [`WIDGET`] is in [`target`].
    ///
    /// [`target`]: Self::target
    /// [`capture`]: Self::capture
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_over(&self) -> bool {
        if let Some(t) = &self.target {
            return t.contains(WIDGET.id());
        }

        false
    }

    /// Returns `true` if the widget was enabled in [`prev_target`].
    ///
    /// [`prev_target`]: Self::prev_target
    pub fn was_enabled(&self, widget_id: WidgetId) -> bool {
        self.prev_target
            .as_ref()
            .and_then(|t| t.interactivity_of(widget_id))
            .map(|itr| itr.is_enabled())
            .unwrap_or(false)
    }

    /// Returns `true` if the widget was disabled in [`prev_target`].
    ///
    /// [`prev_target`]: Self::prev_target
    pub fn was_disabled(&self, widget_id: WidgetId) -> bool {
        self.prev_target
            .as_ref()
            .and_then(|t| t.interactivity_of(widget_id))
            .map(|itr| itr.is_disabled())
            .unwrap_or(false)
    }

    /// Returns `true` if the widget is enabled in [`target`].
    ///
    /// [`target`]: Self::target
    pub fn is_enabled(&self, widget_id: WidgetId) -> bool {
        self.target
            .as_ref()
            .and_then(|t| t.interactivity_of(widget_id))
            .map(|itr| itr.is_enabled())
            .unwrap_or(false)
    }

    /// Returns `true` if the widget is disabled in [`target`].
    ///
    /// [`target`]: Self::target
    pub fn is_disabled(&self, widget_id: WidgetId) -> bool {
        self.target
            .as_ref()
            .and_then(|t| t.interactivity_of(widget_id))
            .map(|itr| itr.is_disabled())
            .unwrap_or(false)
    }

    /// Returns `true` if the [`WIDGET`] was not hovered or was disabled, but now is hovered and enabled.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_enter_enabled(&self) -> bool {
        (!self.was_over() || self.was_disabled(WIDGET.id())) && self.is_over() && self.is_enabled(WIDGET.id())
    }

    /// Returns `true` if the [`WIDGET`] was hovered and enabled, but now is not hovered or is disabled.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_leave_enabled(&self) -> bool {
        self.was_over() && self.was_enabled(WIDGET.id()) && (!self.is_over() || self.is_disabled(WIDGET.id()))
    }

    /// Returns `true` if the [`WIDGET`] was not hovered or was enabled, but now is hovered and disabled.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_enter_disabled(&self) -> bool {
        (!self.was_over() || self.was_enabled(WIDGET.id())) && self.is_over() && self.is_disabled(WIDGET.id())
    }

    /// Returns `true` if the [`WIDGET`] was hovered and disabled, but now is not hovered or is enabled.
    ///
    /// [`WIDGET`]: zng_app::widget::WIDGET
    pub fn is_drag_leave_disabled(&self) -> bool {
        self.was_over() && self.was_disabled(WIDGET.id()) && (!self.is_over() || self.is_enabled(WIDGET.id()))
    }
}

impl DragEndArgs {
    /// Data was dropped on a valid target.
    pub fn was_dropped(&self) -> bool {
        !self.applied.is_empty()
    }

    /// Stopped dragging without dropping on a valid drop target.
    pub fn was_canceled(&self) -> bool {
        self.applied.is_empty()
    }
}

/// Encode an widget ID for drag&drop data.
pub fn encode_widget_id(id: WidgetId) -> DragDropData {
    DragDropData::Text {
        format: formatx!("zng/{}", APP_GUID.read().simple()),
        data: formatx!("wgt-{}", id.get()),
    }
}

/// Decode an widget ID from drag&drop data.
///
/// The ID will only decode if it was encoded by the same app instance.
pub fn decode_widget_id(data: &DragDropData) -> Option<WidgetId> {
    if let DragDropData::Text { format, data } = data {
        if let Some(guid) = format.strip_prefix("zng/") {
            if let Some(id) = data.strip_prefix("wgt-") {
                if guid == APP_GUID.read().simple().to_string() {
                    if let Ok(id) = id.parse::<u64>() {
                        return Some(WidgetId::from_raw(id));
                    }
                }
            }
        }
    }
    None
}

app_local! {
    static APP_GUID: uuid::Uuid = uuid::Uuid::new_v4();
}