41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
use std::{any::TypeId, sync::mpsc::Sender};
|
|
|
|
use crate::{
|
|
layout::{EVENT_TYPES, EventManager, WIDGETS, WidgetId, WidgetUpdate},
|
|
util::{HashSet, RefGuardMut},
|
|
};
|
|
|
|
pub struct WidgetData<W: ?Sized> {
|
|
pub id: WidgetId,
|
|
pub send: Option<Sender<WidgetUpdate>>,
|
|
pub label: String,
|
|
pub event_managers: HashSet<TypeId>,
|
|
pub widget: W,
|
|
}
|
|
|
|
impl<W: ?Sized> WidgetData<W> {
|
|
pub(crate) fn event_managers(&self) -> impl Iterator<Item = RefGuardMut<'_, dyn EventManager>> {
|
|
self.event_managers.iter().map(|id| {
|
|
EVENT_TYPES
|
|
.lock()
|
|
.unwrap()
|
|
.get_mut(id)
|
|
.unwrap()
|
|
.clone()
|
|
.get_take_mut()
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> Drop for WidgetData<W> {
|
|
fn drop(&mut self) {
|
|
WIDGETS.remove(self.id);
|
|
for mut m in self.event_managers() {
|
|
// refer to src/layout/event.rs for why this is like this
|
|
let stuff = m.remove(self.id);
|
|
drop(m);
|
|
drop(stuff);
|
|
}
|
|
}
|
|
}
|