summaryrefslogtreecommitdiffstats
path: root/src/tuine/runtime.rs
blob: d3c3579a51453d4060ddf2575b4dbf5c3ba38793 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::sync::mpsc::Receiver;

use tui::{backend::Backend, Terminal};

use crate::tuine::Status;

use super::{
    build_layout_tree, Application, DrawContext, Element, Event, LayoutNode, StateContext,
    StateMap, TmpComponent, ViewContext,
};

#[derive(Clone, Copy, Debug)]
pub enum RuntimeEvent<Message> {
    UserInterface(Event),
    Resize { width: u16, height: u16 },
    Custom(Message),
}

#[derive(Default)]
struct AppData {
    state_map: StateMap,
}

pub(crate) fn launch<A, B>(
    mut application: A, receiver: Receiver<RuntimeEvent<A::Message>>, terminal: &mut Terminal<B>,
) -> anyhow::Result<()>
where
    A: Application + 'static,
    B: Backend,
{
    let mut app_data = AppData::default(); // FIXME: This needs to be cleared periodically!
    let mut layout: LayoutNode = LayoutNode::default();

    let mut user_interface = {
        let mut ui = create_user_interface(&mut application, &mut app_data);
        draw(&mut ui, terminal, &mut app_data, &mut layout)?;
        ui
    };

    while !application.is_terminated() {
        if let Ok(event) = receiver.recv() {
            match event {
                RuntimeEvent::UserInterface(event) => {
                    if on_event(
                        &mut application,
                        &mut user_interface,
                        &mut app_data,
                        &mut layout,
                        event,
                    ) {
                        if application.is_terminated() {
                            break;
                        }

                        user_interface = create_user_interface(&mut application, &mut app_data);
                        draw(&mut user_interface, terminal, &mut app_data, &mut layout)?;
                    }
                }
                RuntimeEvent::Custom(message) => {
                    if application.update(message) {
                        user_interface = create_user_interface(&mut application, &mut app_data);
                        draw(&mut user_interface, terminal, &mut app_data, &mut layout)?;
                    }
                }
                RuntimeEvent::Resize {
                    width: _,
                    height: _,
                } => {
                    user_interface = create_user_interface(&mut application, &mut app_data);
                    // FIXME: Also nuke any cache and the like...
                    draw(&mut user_interface, terminal, &mut app_data, &mut layout)?;
                }
            }
        } else {
            break;
        }
    }

    application.destructor();

    Ok(())
}

/// Handles a [`Event`].
fn on_event<A>(
    application: &mut A, user_interface: &mut Element<A::Message>, app_data: &mut AppData,
    layout: &mut LayoutNode, event: Event,
) -> bool
where
    A: Application + 'static,
{
    let mut messages = vec![];
    let mut state_ctx = StateContext::new(&mut app_data.state_map);
    let draw_ctx = DrawContext::root(&layout);

    let event_handled =
        match user_interface.on_event(&mut state_ctx, &draw_ctx, event, &mut messages) {
            Status::Captured => Status::Captured,
            Status::Ignored => application.global_event_handler(event, &mut messages),
        };

    let mut should_redraw = match event_handled {
        Status::Captured => true,
        Status::Ignored => false,
    };

    for msg in messages {
        // debug!("Message: {:?}", msg);
        let msg_result = application.update(msg);
        should_redraw = should_redraw || msg_result;
    }

    should_redraw
}

/// Creates a new [`Element`] representing the root of the user interface.
fn create_user_interface<A>(application: &mut A, app_data: &mut AppData) -> Element<A::Message>
where
    A: Application + 'static,
{
    let mut ctx = ViewContext::new(&mut app_data.state_map);
    application.view(&mut ctx)
}

/// Updates the layout, and draws the given user interface.
fn draw<M, B>(
    user_interface: &mut Element<M>, terminal: &mut Terminal<B>, app_data: &mut AppData,
    layout: &mut LayoutNode,
) -> anyhow::Result<()>
where
    B: Backend,
{
    terminal.draw(|frame| {
        let rect = frame.size();
        *layout = build_layout_tree(rect, &user_interface);
        let mut state_ctx = StateContext::new(&mut app_data.state_map);
        let draw_ctx = DrawContext::root(&layout);

        user_interface.draw(&mut state_ctx, &draw_ctx, frame);
    })?;

    Ok(())
}