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

@@ -6,7 +6,7 @@ use crate::{
WidgetInstance, Widgets,
},
render::{Primitive, PrimitiveHandle, Primitives},
util::Id,
util::{HashMap, Id},
};
struct State {
@@ -25,6 +25,7 @@ pub struct Painter<'a, Ctx: 'static> {
pub(super) primitives: &'a mut Primitives,
textures: &'a mut Textures,
text: &'a mut TextData,
labels: &'a HashMap<Id, String>,
screen_size: Vec2,
/// state of what's currently being drawn
state: State,
@@ -41,6 +42,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
text: &'a mut TextData,
textures: &'a mut Textures,
screen_size: Vec2,
labels: &'a HashMap<Id, String>,
) -> Self {
Self {
widgets: nodes,
@@ -51,6 +53,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
text,
textures,
screen_size,
labels,
state: State {
region: UiRegion::full(),
children: Vec::new(),
@@ -123,7 +126,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
id,
WidgetInstance {
region,
primitives: (start_i..end_i).into(),
span: start_i..end_i,
children: child_state.children,
parent: child_state.parent,
},
@@ -168,21 +171,51 @@ impl<'a, Ctx> Painter<'a, Ctx> {
}
let instance = self.free(id);
self.state.id = instance.parent;
self.primitives.prepare(instance.primitives.into());
self.primitives.prepare(instance.span.clone());
self.draw_raw_at(id, instance.region);
let delta = self.primitives.apply(instance.primitives.into());
if let Some(parent) = self.state.id.take() {
self.shift_end(parent, delta);
let start = instance.span.start;
let delta = self.primitives.apply(instance.span);
if delta != 0
&& let Some(parent) = self.state.id.take()
{
self.shift_parent(parent, start, delta);
}
}
fn shift_end(&mut self, id: Id, delta: isize) {
let instance = self.active.widgets.get_mut(&id).unwrap();
let end = &mut instance.primitives.end;
fn shift_parent(&mut self, parent: Id, start: usize, delta: isize) {
let instance = self.active.widgets.get_mut(&parent).unwrap();
let end = &mut instance.span.end;
*end = end.strict_add_signed(delta);
if let Some(parent) = &instance.parent {
let parent = parent.duplicate();
self.shift_end(parent, delta);
// ids are supposed to be unique so theoretically no cloning is needed
// leaving for now for testing
let parent = instance.parent.as_ref().map(|p| p.duplicate());
for child in instance
.children
.iter()
.map(|id| id.duplicate())
.collect::<Vec<_>>()
{
self.shift_child(&child, start, delta);
}
if let Some(parent) = parent {
self.shift_parent(parent, start, delta);
}
}
fn shift_child(&mut self, child: &Id, start: usize, delta: isize) {
let instance = self.active.widgets.get_mut(child).unwrap();
if instance.span.start <= start {
return;
}
instance.span.start = instance.span.start.strict_add_signed(delta);
instance.span.end = instance.span.end.strict_add_signed(delta);
for child in instance
.children
.iter()
.map(|id| id.duplicate())
.collect::<Vec<_>>()
{
self.shift_child(&child, start, delta);
}
}