fix reactivity 😭 + visual widget counter

This commit is contained in:
2025-08-24 22:02:50 -04:00
parent 6bb6db32a6
commit 74d01d14d4
9 changed files with 119 additions and 47 deletions

View File

@@ -10,14 +10,14 @@ use crate::{
};
use std::{
any::{Any, TypeId},
ops::{Index, IndexMut},
range::Range,
ops::{Index, IndexMut, Range},
sync::mpsc::{Receiver, Sender, channel},
};
pub struct Ui<Ctx> {
base: Option<WidgetId>,
widgets: Widgets<Ctx>,
labels: HashMap<Id, String>,
updates: Vec<WidgetId>,
recv: Receiver<Id>,
send: Sender<Id>,
@@ -46,6 +46,11 @@ impl<Ctx> Ui<Ctx> {
w.add(self)
}
/// useful for debugging
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
self.labels.insert(id.id.duplicate(), label);
}
pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
self.push(w)
}
@@ -56,8 +61,8 @@ impl<Ctx> Ui<Ctx> {
id
}
pub fn set<W: Widget<Ctx>>(&mut self, i: &WidgetId<W>, w: W) {
self.widgets.insert(i.id.duplicate(), w);
pub fn set<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>, w: W) {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
@@ -108,6 +113,7 @@ impl<Ctx> Ui<Ctx> {
&mut self.text,
&mut self.textures,
self.size,
&self.labels,
);
if let Some(base) = &self.base {
painter.draw(base);
@@ -131,6 +137,7 @@ impl<Ctx> Ui<Ctx> {
where
Ctx: 'static,
{
self.free();
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
@@ -140,6 +147,7 @@ impl<Ctx> Ui<Ctx> {
&mut self.text,
&mut self.textures,
self.size,
&self.labels,
);
for id in self.updates.drain(..) {
painter.redraw(&id.id);
@@ -149,6 +157,7 @@ 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.labels.remove(&id);
self.widgets.delete(id);
}
self.textures.free();
@@ -244,6 +253,7 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
Self {
base: Default::default(),
widgets: Widgets::new(),
labels: Default::default(),
updates: Default::default(),
primitives: Default::default(),
textures: Textures::new(),
@@ -260,7 +270,7 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
pub struct WidgetInstance {
pub region: UiRegion,
pub primitives: Range<usize>,
pub span: Range<usize>,
pub children: Vec<Id>,
pub parent: Option<Id>,
}