minimal/
minimal.rs

1/**
2This is a super basic implementation of a simple widgetui application
3All this does is render hello world with the 'hello' widget, then
4handles a press of the 'q' key to quit the application
5*/
6use ascii_forge::prelude::*;
7use std::io;
8use widget_forge::prelude::*;
9
10#[derive(Default)]
11pub struct AppData {
12    should_exit: bool,
13}
14
15fn hello(w: &mut Window, _d: &mut AppData) {
16    let center = {
17        let s = w.size();
18        vec2(s.x / 2, s.y / 2)
19    };
20
21    render!(w, (center.x - 13/2, center.y) => [ "Hello, World!".green() ]);
22}
23
24fn quit_handler(w: &mut Window, d: &mut AppData) {
25    if event!(w, Event::Key(e) => e.code == KeyCode::Char('q')) {
26        d.should_exit = true;
27    }
28}
29
30fn main() -> io::Result<()> {
31    let window = Window::init()?;
32    handle_panics();
33    Scene::new(window, AppData::default())
34        // Insert the 2 widgets to run (hello will run first, then quit_handler)
35        .insert_widgets((hello, quit_handler))
36        // Run the application with a poll time of 100ms and a exit condition where AppData::should_exit is true
37        .run(100, |scene| !scene.data().should_exit)?;
38    Ok(())
39}