use y901::prelude::*;
use y901::ui::{Command, Edges, Flex, FlexItem, Json};
struct DemoHost {
clicks: u32,
}
impl Host for DemoHost {
fn idle(&mut self, _tree: &mut UiTree) {}
fn command(&mut self, tree: &mut UiTree, cmd: Command) -> bool {
let Command::App { name, .. } = &cmd else {
return false;
};
if name != "demo.click" {
return false;
}
self.clicks += 1;
if let Some(label) = tree.node_as_mut::<TextLabel>("label") {
label.set_text(format!("你点了 {} 次", self.clicks));
}
tree.invalidate_all();
true
}
}
fn build() -> UiTree {
let root = Flex::column("root")
.background(Color::from_rgb8(12, 12, 16))
.padding(Edges::all(24.0))
.gap(12.0)
.push(Box::new(
TextLabel::new("label", "还没点过")
.color(Color::rgba(0.85, 0.9, 1.0, 1.0))
.flex_item(FlexItem::height(28.0)),
))
.push(Box::new(
Button::new("btn", "点我")
.on_click("demo.click", Json::obj())
.flex_item(FlexItem::height(36.0)),
));
UiTree::new(Box::new(root))
}
fn main() -> Result<(), String> {
let options = Options {
title: "Y901 · hello".to_string(),
width: 520,
height: 220,
..Default::default()
};
y901::create_runtime().run(Box::new(DemoHost { clicks: 0 }), Box::new(build), options)
}