1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::error::Error;

use ratatui::widgets::Paragraph;
use widgetui::*;

#[derive(State)]
pub struct CoolState {
    pub q_count: i32,
}

impl Default for CoolState {
    fn default() -> Self {
        Self { q_count: 8 }
    }
}

pub fn widget(
    mut frame: ResMut<WidgetFrame>,
    mut events: ResMut<Events>,
    mut state: ResMut<CoolState>,
) -> WidgetResult {
    if events.key(crossterm::event::KeyCode::Char('q')) {
        state.q_count -= 1;
        if state.q_count <= 0 {
            events.register_exit();
            return Ok(());
        }
    }

    let size = frame.size();

    frame.render_widget(
        Paragraph::new(format!("Press `q` {} more times", state.q_count)),
        size,
    );

    Ok(())
}

#[set]
pub fn CoolSet(app: App) -> App {
    app.widgets(widget).states(CoolState::default())
}

fn main() -> Result<(), Box<dyn Error>> {
    App::new(100)?.sets(CoolSet).run()
}