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
//! Pass lifecycle: registries are published with the hits they describe, lost
//! sources and missed releases are detected, and results are handed out once.
use super::super::Context;
use crate::components::drag_drop::{DragEnd, DragReason};
impl Context {
pub(crate) fn drag_begin_frame(&mut self) {
self.drag.depth = 0;
if let Some(session) = self.drag.session.as_mut() {
session.seen = false;
}
self.tick_drag_autoscroll();
}
/// Runs after the pass's hits become the published ones, so geometry,
/// registries and the hovered target always describe the same frame.
pub(crate) fn drag_finish_state(&mut self) {
std::mem::swap(&mut self.drag.last_sources, &mut self.drag.sources);
std::mem::swap(&mut self.drag.last_targets, &mut self.drag.targets);
self.drag.sources.clear();
self.drag.targets.clear();
if self
.drag
.result
.as_ref()
.is_some_and(|(_, from)| self.frame >= *from)
{
self.drag.result = None;
}
// The pass had its chance to deliver the drop; unclaimed means the
// target vanished or turned the payload down, so nothing happened.
if let Some(ended) = self.drag.ended.take() {
let claimed = ended.claimed;
self.drag.result = Some((
DragEnd {
reason: if claimed {
DragReason::Dropped
} else {
DragReason::Cancelled
},
source: ended.info.key,
target: claimed.then_some(ended.target.key),
position: ended.position,
insertion: ended.target.insertion.filter(|_| claimed),
},
self.frame + 1,
));
self.request_repaint();
}
if let Some(session) = &self.drag.session {
if !session.seen {
let session = self.drag.session.take().unwrap();
self.drag_conclude(session, DragReason::SourceLost);
} else if !session.keyboard && !self.input.primary_down {
// The release never arrived (lost event, host-side capture loss).
self.drag_cancel(DragReason::Cancelled);
}
}
self.drag_refresh_hover();
self.drag.deadline = self
.drag
.scrolling
.then(|| {
self.frame_time
.checked_add(self.style.motion.frame_interval)
})
.flatten();
}
/// Application-facing id of the source being dragged, once the beginning
/// has been reported.
pub fn dragging(&self) -> Option<crate::Id> {
self.drag
.session
.as_ref()
.filter(|s| s.started)
.map(|s| s.info.key)
}
/// The result of the session that just ended, for the one pass after it
/// ended. Sources receive the same value in [`crate::DragOutput::finished`].
pub fn drag_end(&self) -> Option<DragEnd> {
self.drag
.result
.filter(|(_, from)| self.frame >= *from)
.map(|(end, _)| end)
}
/// Cancel the current drag from application code, for example when a modal
/// dialog opens. Reported as [`DragReason::Cancelled`].
pub fn cancel_drag(&mut self) {
self.drag_cancel(DragReason::Cancelled);
}
}