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
use std::{cell::RefMut, error::Error};

use ratatui::{
    prelude::{Constraint, Direction, Layout},
    widgets::Paragraph,
};
use widgetui::*;

struct TestChunk;

pub fn chunk_generator(frame: &mut WidgetFrame, mut chunks: RefMut<Chunks>) -> WidgetResult {
    let chunk = layout! {
        frame.size(),
        constraint!(%50),
        constraint!(#1) => {constraint!(#3), constraint!(%100), constraint!(#3)},
        constraint!(%50)
    }[1][1];

    chunks.register_chunk::<TestChunk>(chunk);

    Ok(())
}

pub fn render(
    frame: &mut WidgetFrame,
    chunks: RefMut<Chunks>,
    mut events: RefMut<Events>,
) -> WidgetResult {
    let chunk = chunks.get_chunk::<TestChunk>()?;

    frame.render_widget(Paragraph::new("Hello, world!"), chunk);

    if events.key(crossterm::event::KeyCode::Char('q')) {
        events.register_exit();
    }

    Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
    App::new(100)?
        .handle_panics()
        .with_widget(chunk_generator)
        .with_widget(render)
        .run()
}