11use anathema_state:: Change ;
22use 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 ;
66use crate :: widget:: WidgetTreeView ;
77
88#[ derive( Debug ) ]
@@ -11,12 +11,22 @@ pub struct ControlFlow<'bp> {
1111}
1212
1313impl < ' 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 ) ]
6643pub struct Else < ' bp > {
6744 pub cond : Option < Value < ' bp > > ,
0 commit comments