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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#![doc = include_str!("../readme.md")]

use crossbeam::channel::{SendError, Sender};
use rat_widget::button::ButtonOutcome;
use rat_widget::event::{
    ConsumedEvent, DoubleClickOutcome, EditOutcome, Outcome, ScrollOutcome, TextOutcome,
};
use rat_widget::menuline::MenuOutcome;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use std::fmt::Debug;

pub(crate) mod control_queue;
mod framework;
pub mod poll;
pub mod terminal;
mod threadpool;
pub mod timer;

use crate::control_queue::ControlQueue;
use crate::threadpool::ThreadPool;
use crate::timer::{TimeOut, TimerDef, TimerHandle, Timers};

pub use framework::{run_tui, RunConfig};
pub use threadpool::Cancel;

/// Result of event-handling.
///
/// The macro
/// [rat-event::flow_ok!](https://docs.rs/rat-event/latest/rat_event/macro.flow_ok.html)
/// provides control-flow using this enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[must_use]
pub enum Control<Action> {
    /// Continue handling the current event.
    /// In the event-loop this goes on waiting for a new event.
    Continue,
    /// Break handling the current event.
    /// In the event-loop this does nothing and just waits for a new event.
    Break,
    /// Triggers a repaint in the event-loop.
    Repaint,
    /// The event-loop calls out the action-handlers to take care of it.
    Action(Action),
    /// Quit the application.
    Quit,
}

impl<Action> ConsumedEvent for Control<Action> {
    fn is_consumed(&self) -> bool {
        !matches!(self, Control::Continue)
    }
}

impl<Action> From<Outcome> for Control<Action> {
    fn from(value: Outcome) -> Self {
        match value {
            Outcome::NotUsed => Control::Continue,
            Outcome::Unchanged => Control::Break,
            Outcome::Changed => Control::Repaint,
        }
    }
}

impl<Action> From<MenuOutcome> for Control<Action> {
    fn from(value: MenuOutcome) -> Self {
        match value {
            MenuOutcome::NotUsed => Control::Continue,
            MenuOutcome::Unchanged => Control::Break,
            MenuOutcome::Changed => Control::Repaint,
            MenuOutcome::Selected(_) => Control::Repaint,
            MenuOutcome::Activated(_) => Control::Repaint,
            MenuOutcome::MenuSelected(_, _) => Control::Repaint,
            MenuOutcome::MenuActivated(_, _) => Control::Repaint,
        }
    }
}

impl<Action> From<ButtonOutcome> for Control<Action> {
    fn from(value: ButtonOutcome) -> Self {
        match value {
            ButtonOutcome::NotUsed => Control::Continue,
            ButtonOutcome::Unchanged => Control::Break,
            ButtonOutcome::Changed => Control::Repaint,
            ButtonOutcome::Pressed => Control::Repaint,
        }
    }
}

impl<Action> From<TextOutcome> for Control<Action> {
    fn from(value: TextOutcome) -> Self {
        match value {
            TextOutcome::NotUsed => Control::Continue,
            TextOutcome::Unchanged => Control::Break,
            TextOutcome::Changed => Control::Repaint,
            TextOutcome::TextChanged => Control::Repaint,
        }
    }
}

impl<Action> From<ScrollOutcome> for Control<Action> {
    fn from(value: ScrollOutcome) -> Self {
        match value {
            ScrollOutcome::NotUsed => Control::Continue,
            ScrollOutcome::Unchanged => Control::Break,
            ScrollOutcome::Changed => Control::Repaint,
            ScrollOutcome::Up(_) => Control::Repaint,
            ScrollOutcome::Down(_) => Control::Repaint,
            ScrollOutcome::Left(_) => Control::Repaint,
            ScrollOutcome::Right(_) => Control::Repaint,
            ScrollOutcome::VPos(_) => Control::Repaint,
            ScrollOutcome::HPos(_) => Control::Repaint,
        }
    }
}

impl<Action> From<DoubleClickOutcome> for Control<Action> {
    fn from(value: DoubleClickOutcome) -> Self {
        match value {
            DoubleClickOutcome::NotUsed => Control::Continue,
            DoubleClickOutcome::Unchanged => Control::Break,
            DoubleClickOutcome::Changed => Control::Repaint,
            DoubleClickOutcome::ClickClick(_, _) => Control::Break,
        }
    }
}

impl<Action> From<EditOutcome> for Control<Action> {
    fn from(value: EditOutcome) -> Self {
        match value {
            EditOutcome::NotUsed => Control::Continue,
            EditOutcome::Unchanged => Control::Break,
            EditOutcome::Changed => Control::Repaint,
            EditOutcome::Insert => Control::Break,
            EditOutcome::Remove => Control::Break,
            EditOutcome::Edit => Control::Break,
            EditOutcome::Append => Control::Break,
            EditOutcome::Cancel => Control::Break,
            EditOutcome::Commit => Control::Break,
            EditOutcome::CommitAndAppend => Control::Break,
            EditOutcome::CommitAndEdit => Control::Break,
        }
    }
}

///
/// A trait for application level widgets.
///
/// This trait is an anlog to ratatui's StatefulWidget, and
/// does only the rendering part. It's extended with all the
/// extras needed in an application.
///
#[allow(unused_variables)]
pub trait AppWidget<Global, Action, Error>
where
    Action: 'static + Send + Debug,
    Error: 'static + Send + Debug,
{
    /// Type of the State.
    type State: AppEvents<Global, Action, Error> + Debug;

    /// Renders an application widget.
    fn render(
        &self,
        area: Rect,
        buf: &mut Buffer,
        state: &mut Self::State,
        ctx: &mut RenderContext<'_, Global>,
    ) -> Result<(), Error>;
}

///
/// Eventhandling for application level widgets.
///
/// This one collects all currently defined events.
/// Implement this one on the state struct.
///
#[allow(unused_variables)]
pub trait AppEvents<Global, Action, Error>
where
    Action: 'static + Send + Debug,
    Error: 'static + Send + Debug,
{
    /// Initialize the application. Runs before the first repaint.
    fn init(&mut self, ctx: &mut AppContext<'_, Global, Action, Error>) -> Result<(), Error> {
        Ok(())
    }

    /// Timeout event.
    fn timer(
        &mut self,
        event: &TimeOut,
        ctx: &mut AppContext<'_, Global, Action, Error>,
    ) -> Result<Control<Action>, Error> {
        Ok(Control::Continue)
    }

    /// Crossterm event.
    fn crossterm(
        &mut self,
        event: &crossterm::event::Event,
        ctx: &mut AppContext<'_, Global, Action, Error>,
    ) -> Result<Control<Action>, Error> {
        Ok(Control::Continue)
    }

    /// Run an action.
    fn action(
        &mut self,
        event: &mut Action,
        ctx: &mut AppContext<'_, Global, Action, Error>,
    ) -> Result<Control<Action>, Error> {
        Ok(Control::Continue)
    }

    /// Do error handling.
    fn error(
        &self,
        event: Error,
        ctx: &mut AppContext<'_, Global, Action, Error>,
    ) -> Result<Control<Action>, Error> {
        Ok(Control::Continue)
    }
}

/// A collection of context data used by the application.
#[derive(Debug)]
pub struct AppContext<'a, Global, Action, Error>
where
    Action: 'static + Send + Debug,
    Error: 'static + Send + Debug,
{
    /// Some global state for the application.
    pub g: &'a mut Global,
    /// Current timeout, if any.
    pub timeout: Option<TimeOut>,

    /// Application timers.
    pub(crate) timers: &'a Timers,
    /// Background tasks.
    pub(crate) tasks: &'a ThreadPool<Action, Error>,
    /// Queue foreground tasks.
    queue: &'a ControlQueue<Action, Error>,
}

/// A collection of context data used for rendering.
#[derive(Debug)]
pub struct RenderContext<'a, Global> {
    /// Some global state for the application.
    pub g: &'a mut Global,
    /// Current timeout that triggered the repaint.
    pub timeout: Option<TimeOut>,

    /// Frame counter.
    pub counter: usize,
    /// Frame area.
    pub area: Rect,
    /// Output cursor position. Set after rendering is complete.
    pub cursor: Option<(u16, u16)>,
}

impl<'a, Global, Action, Error> AppContext<'a, Global, Action, Error>
where
    Action: 'static + Send + Debug,
    Error: 'static + Send + Debug,
{
    /// Add a timer.
    #[inline]
    pub fn add_timer(&self, t: TimerDef) -> TimerHandle {
        self.timers.add(t)
    }

    /// Remove a timer.
    #[inline]
    pub fn remove_timer(&self, tag: TimerHandle) {
        self.timers.remove(tag)
    }

    /// Add a background worker task.
    ///
    /// ```rust ignore
    /// let cancel = ctx.spawn(|cancel, send| {
    ///     // ... do stuff
    ///     Ok(Control::Continue)
    /// });
    /// ```
    #[inline]
    pub fn spawn(
        &self,
        task: impl FnOnce(
                Cancel,
                &Sender<Result<Control<Action>, Error>>,
            ) -> Result<Control<Action>, Error>
            + Send
            + 'static,
    ) -> Result<Cancel, SendError<()>>
    where
        Action: 'static + Send + Debug,
        Error: 'static + Send + Debug,
    {
        self.tasks.send(task)
    }

    /// Queue additional results.
    ///
    /// X
    #[inline]
    pub fn queue(&self, ctrl: impl Into<Control<Action>>) {
        self.queue.push(Ok(ctrl.into()))
    }

    /// Queue additional results.
    #[inline]
    pub fn queue_result(&self, ctrl: Result<Control<Action>, Error>) {
        self.queue.push(ctrl)
    }
}

///
/// Event-handler traits and Keybindings.
///
pub mod event {
    pub use rat_widget::event::{ct_event, flow_ok};
}