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
#![feature(trait_upcasting)]

use ratatui::prelude::{Buffer, Rect};
use std::{
    any::Any,
    fmt,
    hash::{Hash, Hasher},
    mem::replace,
    num::NonZeroU64,
};
use twox_hash::XxHash64;

mod jobs;
pub use jobs::*;
mod compositor;
pub use compositor::*;

/// LayerId describes elevation of the component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct LayerId(pub i16);

impl LayerId {
    /// Background: `-1_000`
    pub const BACKGROUND: Self = Self(-1_000);
    /// Middle: `0`
    pub const MIDDLE: Self = Self(0);
    /// Foreground: `1_000`
    pub const FOREGROUND: Self = Self(1_000);
    /// Popup: `2_000`
    pub const POPUP: Self = Self(2_000);
    /// Overlay: `5_000`
    pub const OVERLAY: Self = Self(5_000);
    /// Topmost: `10_000`
    pub const TOPMOST: Self = Self(10_000);
}

/// Id of the component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Id(NonZeroU64);

impl Id {
    /// Creates new Id from hashable.
    pub fn new(source: impl Hash) -> Self {
        let mut hasher = XxHash64::default();
        source.hash(&mut hasher);

        NonZeroU64::new(hasher.finish()).map(Self).expect("id is 0")
    }

    /// Combines id with another source of randomness.
    pub fn with(&self, more: impl Hash) -> Self {
        let mut hasher = XxHash64::default();
        self.0.hash(&mut hasher);
        more.hash(&mut hasher);

        NonZeroU64::new(hasher.finish()).map(Self).expect("id is 0")
    }
}

/// Event that can occur during runtime.
#[non_exhaustive]
pub enum Event<E> {
    /// User event
    User(E),
    /// Event from the terminal
    Terminal(crossterm::event::Event),
    /// Next tick occured without intermediate event
    Tick,
    /// Exists compositor when emitted
    Exit,
    /// No event, used as a placeholder when event was taken
    None,
}

impl<T> Event<T> {
    /// Checks if event is user.
    #[inline]
    pub fn is_user(&self) -> bool {
        matches!(self, Self::User(_))
    }

    /// Converts into user event ref on success.
    #[inline]
    pub fn as_user(&self) -> Option<&T> {
        match self {
            Event::User(e) => Some(e),
            _ => None,
        }
    }

    /// Converts into user event mut ref on success.
    #[inline]
    pub fn as_mut_user(&mut self) -> Option<&mut T> {
        match self {
            Event::User(e) => Some(e),
            _ => None,
        }
    }

    /// Converts into custom event on success.
    #[inline]
    pub fn into_user(self) -> Result<T, Self> {
        match self {
            Event::User(e) => Ok(e),
            _ => Err(self),
        }
    }

    /// Checks if event is from terminal.
    #[inline]
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Terminal(_))
    }

    /// Converts into terminal event ref on success.
    #[inline]
    pub fn as_terminal(&self) -> Option<&crossterm::event::Event> {
        match self {
            Event::Terminal(e) => Some(e),
            _ => None,
        }
    }

    /// Converts into terminal event mut ref on success.
    #[inline]
    pub fn as_mut_terminal(&mut self) -> Option<&mut crossterm::event::Event> {
        match self {
            Event::Terminal(e) => Some(e),
            _ => None,
        }
    }

    /// Converts into terminal event on success.
    #[inline]
    pub fn into_terminal(self) -> Result<crossterm::event::Event, Self> {
        match self {
            Event::Terminal(e) => Ok(e),
            _ => Err(self),
        }
    }
}

impl<T: Clone> Clone for Event<T> {
    fn clone(&self) -> Self {
        match self {
            Event::Terminal(e) => Self::Terminal(e.clone()),
            Event::User(e) => Self::User(e.clone()),
            Event::Tick => Self::Tick,
            Event::Exit => Self::Exit,
            Event::None => Self::None,
        }
    }
}

impl<T: fmt::Debug> fmt::Debug for Event<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Event::Terminal(e) => f.debug_tuple("Crossterm").field(e).finish(),
            Event::User(e) => f.debug_tuple("User").field(e).finish(),
            Event::Tick => write!(f, "Tick"),
            Event::Exit => write!(f, "Exit"),
            Event::None => write!(f, "None"),
        }
    }
}

/// Provides access to the [`Event`], default result is `Ignored`
pub struct EventAccess<E = ()> {
    event: Event<E>,
}

impl<E> EventAccess<E> {
    /// Peeks the event
    #[inline]
    pub fn peek(&self) -> &Event<E> {
        &self.event
    }

    /// Consumes the event, sets old event to `None`
    #[inline]
    pub fn consume(&mut self) -> Event<E> {
        replace(&mut self.event, Event::None)
    }

    /// Replaces old event with the one supplied, returns old event
    #[inline]
    pub fn replace(&mut self, event: Event<E>) -> Event<E> {
        replace(&mut self.event, event)
    }

    /// Checks if event was consumed
    #[inline]
    pub fn is_consumed(&self) -> bool {
        matches!(self.event, Event::None)
    }
}

impl<E: Clone> EventAccess<E> {
    /// Clones the event, doesn't modify the result
    #[inline]
    pub fn cloned(&self) -> Event<E> {
        self.event.clone()
    }
}

/// UI component
pub trait Component<S = (), E = ()>: Any {
    /// Id of this component
    fn id(&self) -> Id;

    /// Function to draw the inner ui.
    /// If component is root the `area` equals to the whole screen.
    fn view(&self, area: Rect, buf: &mut Buffer, state: &S);

    fn handle_event(&mut self, _event: &mut EventAccess<E>, _cx: &mut Context<S, E>) {}
}

/// Forwards `handle_event` to multiple child components.
#[macro_export]
macro_rules! forward_handle_event {
    (@ret $($tail:tt)*) => {
        if $crate::forward_handle_event!($($tail)*) {
            return;
        }
    };
    ($event:expr, $cx:expr, $($comp:expr),*) => {
        'forward: {
            $(
                $comp.handle_event($event, $cx);
                if $event.is_consumed() {
                    break 'forward true;
                }
            )*

            false
        }
    };
}

/// Forwards `view` to multiple child components.
#[macro_export]
macro_rules! forward_view {
    ($area:expr, $buf:expr, $state:expr, $($comp:expr),*) => {
        {
            let mut any = false;

            $(
                if $comp.should_update($state) {
                    $comp.view($area, $buf, $state);
                    any = true;
                }
            )*

            any
        }
    };
}

/// Helpful macro to avoid fully qualified syntax.
#[macro_export]
macro_rules! id {
    ($self:expr) => {
        <_ as $crate::Component>::id($self)
    };
    ($state:tt, $self:expr) => {
        <_ as $crate::Component<$state>>::id($self)
    };
    ($state:tt, $event:tt, $self:expr) => {
        <_ as $crate::Component<$state, $event>>::id($self)
    };
}