add module system and move sensor into core with it

This commit is contained in:
2025-09-24 16:11:39 -04:00
parent 2adf7a43a1
commit 26c248dcba
13 changed files with 515 additions and 342 deletions

View File

@@ -3,17 +3,19 @@ use image::DynamicImage;
use crate::{
core::{TextEdit, TextEditCtx},
layout::{
Layers, PainterCtx, Sensor, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
Vec2, Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
Layers, Modules, PainterCtx, StaticWidgetId, TextData, TextureHandle, Textures, Vec2,
Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
},
util::{HashMap, Id},
};
use std::{
any::{Any, TypeId},
marker::PhantomData,
ops::{Index, IndexMut},
sync::mpsc::{Receiver, Sender, channel},
};
// TODO: remove generic
pub struct Ui<Ctx> {
root: Option<WidgetId>,
pub(super) widgets: Widgets,
@@ -28,7 +30,8 @@ pub struct Ui<Ctx> {
full_redraw: bool,
pub(crate) active: ActiveWidgets,
pub(super) event_map: SensorMap<Ctx>,
pub modules: Modules,
_pd: PhantomData<Ctx>,
}
impl<Ctx> Ui<Ctx> {
@@ -114,6 +117,7 @@ impl<Ctx> Ui<Ctx> {
&self.widgets,
&mut self.layers,
&mut self.active,
&mut self.modules,
&mut self.textures,
&mut self.text,
self.size,
@@ -137,6 +141,7 @@ impl<Ctx> Ui<Ctx> {
&self.widgets,
&mut self.layers,
&mut self.active,
&mut self.modules,
&mut self.textures,
&mut self.text,
self.size,
@@ -150,7 +155,9 @@ impl<Ctx> Ui<Ctx> {
/// free any resources that don't have references anymore
fn free(&mut self) {
for id in self.recv.try_iter() {
self.event_map.remove(&id);
for m in self.modules.iter_mut() {
m.on_remove(&id);
}
self.widgets.delete(id);
}
self.textures.free();
@@ -168,15 +175,6 @@ impl<Ctx> Ui<Ctx> {
self.active.len()
}
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
self.event_map
.entry(id.id.duplicate())
.or_default()
.sensors
.push(f);
self.widgets.data_mut(&id.id).unwrap().sensor = true;
}
pub fn text(&mut self, id: &WidgetId<TextEdit>) -> TextEditCtx<'_> {
self.updates.push(id.id.duplicate());
TextEditCtx {
@@ -238,10 +236,11 @@ impl<Ctx> Default for Ui<Ctx> {
text: TextData::default(),
full_redraw: false,
active: Default::default(),
event_map: Default::default(),
send,
recv,
size: Vec2::ZERO,
modules: Modules::default(),
_pd: PhantomData,
}
}
}