widget state

This commit is contained in:
2026-01-19 20:39:58 -05:00
parent 06dd015092
commit 7bafb04a34
6 changed files with 137 additions and 206 deletions

View File

@@ -15,19 +15,22 @@ type Rsc = DefaultRsc<State>;
struct Test {
#[root]
root: WeakWidget<Rect>,
cur: WeakState<bool>,
}
impl Test {
pub fn new(rsc: &mut Rsc) -> Self {
let root = rect(Color::RED).add(rsc);
Self { root }
let cur = rsc.create_state(root, false);
Self { root, cur }
}
pub fn toggle(&self, rsc: &mut Rsc) {
let rect = (self.root)(rsc);
if rect.color == Color::RED {
rect.color = Color::BLUE;
let cur = &mut rsc[self.cur];
*cur = !*cur;
if *cur {
rsc[self.root].color = Color::BLUE;
} else {
rect.color = Color::RED;
rsc[self.root].color = Color::RED;
}
}
}