gpui_material_foundation/
interaction.rs1use std::{cell::Cell, collections::HashMap};
2
3use gpui::{App, ElementId, Entity, EntityId, EventEmitter, WeakEntity, Window, WindowId};
4
5use crate::InteractionState;
6
7#[derive(Default)]
12pub struct InteractionSource {
13 windows: HashMap<WindowId, HashMap<ElementId, (EntityId, InteractionState)>>,
14}
15
16#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct InteractionUpdate {
23 pub window: WindowId,
24 pub id: ElementId,
25 pub state: Option<InteractionState>,
27}
28
29impl EventEmitter<InteractionUpdate> for InteractionSource {}
30
31impl InteractionSource {
32 pub fn new() -> Self {
34 Self::default()
35 }
36
37 pub fn state(&self, window: WindowId, id: &ElementId) -> Option<InteractionState> {
39 self.windows.get(&window)?.get(id).map(|(_, state)| *state)
40 }
41
42 pub fn track(
48 source: &Entity<Self>,
49 id: &ElementId,
50 window: &mut Window,
51 cx: &mut App,
52 ) -> InteractionTracker {
53 let window_id = window.window_handle().window_id();
54 let source_id = source.entity_id();
55 let attachment = window.with_id((id.clone(), "material-interaction-source"), |window| {
56 window.use_keyed_state(("source", source_id), cx, |_, cx| {
57 cx.on_release(|this: &mut Attachment, cx| {
58 let source = this.source.clone();
59 let window = this.window;
60 let id = this.id.clone();
61 let owner = this.owner;
62 cx.defer(move |cx| {
63 source
64 .update(cx, |source, cx| {
65 if source.detach(window, &id, owner) {
66 cx.emit(InteractionUpdate {
67 window,
68 id,
69 state: None,
70 });
71 cx.notify();
72 }
73 })
74 .ok();
75 });
76 })
77 .detach();
78 Attachment {
79 source: source.downgrade(),
80 window: window_id,
81 id: id.clone(),
82 owner: cx.entity_id(),
83 last: Cell::new(None),
84 }
85 })
86 });
87 InteractionTracker(attachment.downgrade())
88 }
89
90 fn detach(&mut self, window: WindowId, id: &ElementId, owner: EntityId) -> bool {
91 let Some(controls) = self.windows.get_mut(&window) else {
92 return false;
93 };
94 if controls
95 .get(id)
96 .is_none_or(|(current, _)| *current != owner)
97 {
98 return false;
99 }
100 controls.remove(id);
101 if controls.is_empty() {
102 self.windows.remove(&window);
103 }
104 true
105 }
106}
107
108struct Attachment {
109 source: WeakEntity<InteractionSource>,
110 window: WindowId,
111 id: ElementId,
112 owner: EntityId,
113 last: Cell<Option<InteractionState>>,
114}
115
116#[derive(Clone)]
119pub struct InteractionTracker(WeakEntity<Attachment>);
120
121impl InteractionTracker {
122 pub fn record(&self, state: InteractionState, cx: &mut App) {
125 let Some(attachment) = self.0.upgrade() else {
126 return;
127 };
128 if attachment.read(cx).last.replace(Some(state)) == Some(state) {
129 return;
130 }
131 let attachment = self.0.clone();
132 cx.defer(move |cx| {
133 let Some(attachment) = attachment.upgrade() else {
134 return;
135 };
136 let attachment = attachment.read(cx);
137 let source = attachment.source.clone();
138 let window = attachment.window;
139 let id = attachment.id.clone();
140 let owner = attachment.owner;
141 source
142 .update(cx, |source, cx| {
143 source
144 .windows
145 .entry(window)
146 .or_default()
147 .insert(id.clone(), (owner, state));
148 cx.emit(InteractionUpdate {
149 window,
150 id,
151 state: Some(state),
152 });
153 cx.notify();
154 })
155 .ok();
156 });
157 }
158}