use std::{cell::Cell, collections::HashMap};
use gpui::{App, ElementId, Entity, EntityId, EventEmitter, WeakEntity, Window, WindowId};
use crate::InteractionState;
#[derive(Default)]
pub struct InteractionSource {
windows: HashMap<WindowId, HashMap<ElementId, (EntityId, InteractionState)>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct InteractionUpdate {
pub window: WindowId,
pub id: ElementId,
pub state: Option<InteractionState>,
}
impl EventEmitter<InteractionUpdate> for InteractionSource {}
impl InteractionSource {
pub fn new() -> Self {
Self::default()
}
pub fn state(&self, window: WindowId, id: &ElementId) -> Option<InteractionState> {
self.windows.get(&window)?.get(id).map(|(_, state)| *state)
}
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>>,
}
#[derive(Clone)]
pub struct InteractionTracker(WeakEntity<Attachment>);
impl InteractionTracker {
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();
});
}
}