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
use std::{cell::Cell, collections::HashMap};
use gpui::{App, ElementId, Entity, EntityId, EventEmitter, WeakEntity, Window, WindowId};
use crate::InteractionState;
/// Observes the presentation state of controls with a shared GPUI entity.
///
/// A source can observe multiple controls. Give each control a distinct
/// element ID within each window that uses the source.
#[derive(Default)]
pub struct InteractionSource {
windows: HashMap<WindowId, HashMap<ElementId, (EntityId, InteractionState)>>,
}
/// One deferred change from an observed control.
///
/// For a fast sequence, the event's snapshot can precede the latest state
/// returned by [`InteractionSource::state`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct InteractionUpdate {
/// Window containing the observed control.
pub window: WindowId,
/// Element identifier of the observed control within [`Self::window`].
pub id: ElementId,
/// `None` means the control was detached from this source.
pub state: Option<InteractionState>,
}
impl EventEmitter<InteractionUpdate> for InteractionSource {}
impl InteractionSource {
/// Creates an empty source for use in a caller-owned GPUI entity.
pub fn new() -> Self {
Self::default()
}
/// Reads the most recently delivered state for a control in a window.
pub fn state(&self, window: WindowId, id: &ElementId) -> Option<InteractionState> {
self.windows.get(&window)?.get(id).map(|(_, state)| *state)
}
/// Binds an observer to a control during its rendering.
///
/// Record the control's initial state during rendering and later changes
/// from its existing input handlers. The tracker expires when the control
/// stops rendering with this source.
pub fn track(
source: &Entity<Self>,
id: &ElementId,
window: &mut Window,
cx: &mut App,
) -> InteractionTracker {
let window_id = window.window_handle().window_id();
let source_id = source.entity_id();
let attachment = window.with_id((id.clone(), "material-interaction-source"), |window| {
window.use_keyed_state(("source", source_id), cx, |_, cx| {
cx.on_release(|this: &mut Attachment, cx| {
let source = this.source.clone();
let window = this.window;
let id = this.id.clone();
let owner = this.owner;
cx.defer(move |cx| {
source
.update(cx, |source, cx| {
if source.detach(window, &id, owner) {
cx.emit(InteractionUpdate {
window,
id,
state: None,
});
cx.notify();
}
})
.ok();
});
})
.detach();
Attachment {
source: source.downgrade(),
window: window_id,
id: id.clone(),
owner: cx.entity_id(),
last: Cell::new(None),
}
})
});
InteractionTracker(attachment.downgrade())
}
fn detach(&mut self, window: WindowId, id: &ElementId, owner: EntityId) -> bool {
let Some(controls) = self.windows.get_mut(&window) else {
return false;
};
if controls
.get(id)
.is_none_or(|(current, _)| *current != owner)
{
return false;
}
controls.remove(id);
if controls.is_empty() {
self.windows.remove(&window);
}
true
}
}
struct Attachment {
source: WeakEntity<InteractionSource>,
window: WindowId,
id: ElementId,
owner: EntityId,
last: Cell<Option<InteractionState>>,
}
/// A control's binding to an interaction source.
/// The tracker observes an existing input path and has no effect on activation.
#[derive(Clone)]
pub struct InteractionTracker(WeakEntity<Attachment>);
impl InteractionTracker {
/// Records one changed snapshot. Events are delivered in recording order
/// after the current GPUI operation, even when several changes share a draw.
pub fn record(&self, state: InteractionState, cx: &mut App) {
let Some(attachment) = self.0.upgrade() else {
return;
};
if attachment.read(cx).last.replace(Some(state)) == Some(state) {
return;
}
let attachment = self.0.clone();
cx.defer(move |cx| {
let Some(attachment) = attachment.upgrade() else {
return;
};
let attachment = attachment.read(cx);
let source = attachment.source.clone();
let window = attachment.window;
let id = attachment.id.clone();
let owner = attachment.owner;
source
.update(cx, |source, cx| {
source
.windows
.entry(window)
.or_default()
.insert(id.clone(), (owner, state));
cx.emit(InteractionUpdate {
window,
id,
state: Some(state),
});
cx.notify();
})
.ok();
});
}
}