Skip to content

Commit 866087b

Browse files
committed
BUGFIX: remove components when truncating the tree
1 parent d6343cc commit 866087b

12 files changed

Lines changed: 32 additions & 57 deletions

File tree

anathema-runtime/src/runtime/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl<'rt, 'bp, G: GlobalEventHandler> Frame<'rt, 'bp, G> {
327327
let now = Instant::now();
328328
self.init_new_components();
329329
let elapsed = self.handle_messages(now);
330+
// Pre cycle events
330331
self.poll_events(elapsed, now, backend);
331332
self.drain_deferred_commands();
332333
self.drain_assoc_events();
@@ -340,6 +341,7 @@ impl<'rt, 'bp, G: GlobalEventHandler> Frame<'rt, 'bp, G> {
340341
self.tick_components(self.dt.elapsed());
341342
self.cycle(backend)?;
342343

344+
// Post cycle events
343345
*self.dt = Instant::now();
344346

345347
match self.layout_ctx.stop_runtime {

anathema-state/src/value/map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod test {
132132
}
133133
impl Drop for DM {
134134
fn drop(&mut self) {
135-
eprintln!("- drop: {}", self.0);
135+
// eprintln!("- drop: {}", self.0);
136136
}
137137
}
138138

anathema-store/src/slab/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<I, T> Entry<I, T> {
6969
// Insert an Occupied entry in place of a vacant one.
7070
fn swap(&mut self, value: T) {
7171
debug_assert!(matches!(self, Entry::Vacant(_)));
72-
std::mem::swap(self, &mut Entry::Occupied(value));
72+
*self = Entry::Occupied(value);
7373
}
7474

7575
// Create a new occupied entry

anathema-store/src/slab/generational.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<T> Entry<T> {
144144
// Insert an Occupied entry in place of a vacant one.
145145
fn swap(&mut self, value: T, generation: Gen) {
146146
debug_assert!(matches!(self, Entry::Vacant(_)));
147-
std::mem::swap(self, &mut Entry::Occupied(value, generation));
147+
*self = Entry::Occupied(value, generation);
148148
}
149149

150150
// Create a new occupied entry
@@ -437,7 +437,7 @@ where
437437
Entry::Vacant(key) => {
438438
let _ = write!(&mut s, "{idx}: vacant ");
439439
match key {
440-
Some(key) => writeln!(&mut s, "next key: {:?}", key),
440+
Some(key) => writeln!(&mut s, "next key: {key:?}"),
441441
None => writeln!(&mut s, "no next id"),
442442
}
443443
}
@@ -451,7 +451,7 @@ where
451451
let _ = writeln!(&mut s, "---- next id ----");
452452

453453
let _ = match self.next_id {
454-
Some(key) => writeln!(&mut s, "next key: {:?}", key),
454+
Some(key) => writeln!(&mut s, "next key: {key:?}"),
455455
None => writeln!(&mut s, "no next id"),
456456
};
457457

anathema-store/src/slab/shared/arc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<I, T> Entry<I, T> {
4646
.expect("Arc strong count is always one here")
4747
.replace(inner_value);
4848

49-
swap(self, &mut Entry::Occupied(storage_cell));
49+
*self = Entry::Occupied(storage_cell);
5050
}
5151
_ => unreachable!(),
5252
}
@@ -70,7 +70,7 @@ impl<I, T> Entry<I, T> {
7070
.expect("strong count is always one")
7171
.take()
7272
.expect("occupied variant never contains a None");
73-
swap(self, &mut Entry::Vacant(next_id.take(), store));
73+
*self = Entry::Vacant(next_id.take(), store);
7474
Some(value)
7575
}
7676
_ => unreachable!(),

anathema-store/src/slab/shared/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<I, T> Entry<I, T> {
4444
.expect("Rc strong count is always one here")
4545
.replace(inner_value);
4646

47-
swap(self, &mut Entry::Occupied(storage_cell));
47+
*self = Entry::Occupied(storage_cell);
4848
}
4949
_ => unreachable!(),
5050
}
@@ -68,7 +68,7 @@ impl<I, T> Entry<I, T> {
6868
.expect("strong count is always one")
6969
.take()
7070
.expect("occupied variant never contains a None");
71-
swap(self, &mut Entry::Vacant(next_id.take(), store));
71+
*self = Entry::Vacant(next_id.take(), store);
7272
Some(value)
7373
}
7474
_ => unreachable!(),

anathema-value-resolver/src/functions/number.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(super) fn round<'bp>(args: &[ValueKind<'bp>]) -> ValueKind<'bp> {
6464
};
6565

6666
match &args[0] {
67-
ValueKind::Float(f) => ValueKind::Str(format!("{f:.*}", precision).into()),
67+
ValueKind::Float(f) => ValueKind::Str(format!("{f:.precision$}").into()),
6868
_ => ValueKind::Null,
6969
}
7070
}

anathema-widgets/src/layout/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'frame, 'bp> LayoutCtx<'frame, 'bp> {
8282
}
8383

8484
pub(crate) fn truncate_children(&mut self, tree: &mut TreeView<'_, WidgetContainer<'bp>>) {
85-
tree.truncate_children(&mut &mut |widget| {
85+
tree.truncate_children(&mut |widget| {
8686
self.return_component(widget);
8787
});
8888
}
@@ -95,6 +95,7 @@ impl<'frame, 'bp> LayoutCtx<'frame, 'bp> {
9595

9696
fn return_component(&mut self, widget: WidgetContainer<'_>) {
9797
let WidgetKind::Component(comp) = widget.kind else { return };
98+
self.components.try_remove(comp.widget_id);
9899
let id = comp.component_id;
99100
let state = self.states.remove(comp.state_id).take();
100101
self.component_registry.return_component(id, comp.dyn_component, state);

anathema-widgets/src/nodes/controlflow.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use anathema_state::Change;
22
use anathema_templates::blueprints::Blueprint;
3-
use anathema_value_resolver::{AttributeStorage, Value};
3+
use anathema_value_resolver::Value;
44

5-
use crate::WidgetKind;
5+
use crate::layout::LayoutCtx;
66
use crate::widget::WidgetTreeView;
77

88
#[derive(Debug)]
@@ -11,12 +11,22 @@ pub struct ControlFlow<'bp> {
1111
}
1212

1313
impl<'bp> ControlFlow<'bp> {
14-
pub(crate) fn update(&mut self, change: &Change, branch_id: u16, attribute_storage: &AttributeStorage<'bp>) {
14+
pub(crate) fn update(
15+
&mut self,
16+
change: &Change,
17+
branch_id: u16,
18+
mut tree: WidgetTreeView<'_, 'bp>,
19+
ctx: &mut LayoutCtx<'_, 'bp>,
20+
) {
1521
match change {
1622
Change::Changed | Change::Dropped => {
1723
let Some(el) = self.elses.get_mut(branch_id as usize) else { return };
1824
let Some(cond) = el.cond.as_mut() else { return };
19-
cond.reload(attribute_storage);
25+
let current = cond.as_bool();
26+
cond.reload(ctx.attribute_storage);
27+
if cond.as_bool() != current {
28+
ctx.truncate_children(&mut tree);
29+
}
2030
}
2131
// TODO:
2232
// this could probably happen given something like this
@@ -29,39 +39,6 @@ impl<'bp> ControlFlow<'bp> {
2939
}
3040
}
3141

32-
impl ControlFlow<'_> {
33-
pub(crate) fn has_changed(&self, children: &WidgetTreeView<'_, '_>) -> bool {
34-
let child_count = children.layout_len();
35-
if child_count != 1 {
36-
return true;
37-
}
38-
39-
let branch_id = self.current_branch_id(children);
40-
41-
// Check if another branch id before this has become true,
42-
// if so this has changed.
43-
if self.elses[..branch_id as usize].iter().any(|e| e.is_true()) {
44-
return true;
45-
}
46-
47-
// If the current branch is false, the value has changed,
48-
// as it has to have been true at one point to become
49-
// the current branch.
50-
!self.elses[branch_id as usize].is_true()
51-
}
52-
53-
fn current_branch_id(&self, children: &WidgetTreeView<'_, '_>) -> u16 {
54-
let node_id = children.layout[0].value();
55-
let (_, widget) = children
56-
.values
57-
.get(node_id)
58-
.expect("because the node exists, the value exist");
59-
60-
let WidgetKind::ControlFlowContainer(id) = widget.kind else { unreachable!() };
61-
id
62-
}
63-
}
64-
6542
#[derive(Debug)]
6643
pub struct Else<'bp> {
6744
pub cond: Option<Value<'bp>>,

anathema-widgets/src/nodes/update.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ pub fn update_widget<'bp>(
3434
WidgetKind::For(for_loop) => for_loop.update(change, tree, ctx)?,
3535
WidgetKind::With(with) => with.update(change, tree, ctx.attribute_storage)?,
3636
WidgetKind::Iteration(_) => todo!(),
37-
WidgetKind::ControlFlow(controlflow) => {
38-
controlflow.update(change, value_id.index().into(), ctx.attribute_storage)
39-
}
37+
WidgetKind::ControlFlow(controlflow) => controlflow.update(change, value_id.index().into(), tree, ctx),
4038
WidgetKind::ControlFlowContainer(_) => unreachable!("control flow containers have no values"),
4139
WidgetKind::Component(_) => (),
4240
WidgetKind::Slot => todo!(),

0 commit comments

Comments
 (0)