use guido::prelude::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
static ADD_TRIGGERED: AtomicBool = AtomicBool::new(false);
#[tokio::main]
async fn main() {
App::new().run(|app| {
let items = create_signal(vec![1u64, 2, 3]);
let items_w = items.writer();
create_task(move |ctx| async move {
tokio::time::sleep(Duration::from_secs(2)).await;
if ctx.is_running() && !ADD_TRIGGERED.swap(true, Ordering::SeqCst) {
items_w.update(|list| {
list.push(4);
});
}
});
let view = container()
.layout(Flex::column().spacing(8.0))
.padding(20.0)
.background(Color::rgb(0.1, 0.1, 0.15))
.child(container().child(text("Dynamic Children Test").color(Color::WHITE)))
.child(container().child(
text("An item will be added after 2 seconds...").color(Color::rgb(0.7, 0.7, 0.7)),
))
.child(container().layout(Flex::row().spacing(4.0)).children(keyed(
move || items.get(),
|id| *id,
|id| {
container()
.padding(8.0)
.background(Color::rgb(0.3 + id as f32 * 0.1, 0.3, 0.4))
.corners(4.0)
.child(text(format!("Item {}", id)).color(Color::WHITE))
},
)));
app.add_surface(
SurfaceConfig::new()
.width(600)
.height(200)
.anchor(Anchor::TOP | Anchor::LEFT)
.background_color(Color::rgb(0.1, 0.1, 0.15)),
move || view,
);
});
}