Files
osui/examples/hello_world.rs
T
2026-01-24 16:40:03 +01:00

62 lines
1.2 KiB
Rust

use std::sync::Arc;
use osui::prelude::*;
#[derive(Debug, Clone)]
pub struct KeyPress;
impl Event for KeyPress {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
fn main() {
let console = Console::new();
console.thread(move |ctx| loop {
crossterm::event::read().unwrap();
ctx.emit_event_threaded(&KeyPress);
});
console.run(app);
}
fn app(cx: &Arc<Context>) -> View {
let mc_cx = Context::new(my_component);
cx.on_event({
let mc_cx = mc_cx.clone();
move |_, &KeyPress| {
mc_cx.emit_event(&KeyPress);
}
});
mc_cx.refresh();
Arc::new(move |ctx| {
let area = ctx.allocate(0, 0, 10, 10);
ctx.draw_view(area, mc_cx.get_view());
})
}
fn my_component(cx: &Arc<Context>) -> View {
let count = use_state(0);
cx.on_event({
let count = count.clone();
move |_cx, _event: &KeyPress| {
let mut count = count.get();
if *count > 5 {
_cx.refresh();
return;
}
*count += 1;
}
});
Arc::new(move |ctx| {
ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}"));
})
}