sanguine 0.3.1

A library for creating dynamic TUI experiences in Rust
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use std::{
    sync::{atomic::AtomicBool, mpsc::Sender, Arc, RwLock},
    time::Duration,
};

pub use crate::widget::{RenderCtx, UpdateCtx};

use slotmap::SecondaryMap;

use crate::{
    error::{Error, Result},
    event::*,
    layout::*,
    surface::{term::*, *},
    Widget,
};

/// Contains configuration options for the Sanguine application.
pub struct Config {
    /// Whether or not to quit on <kbd>ctrl</kbd>+<kbd>q</kbd> `default: true`
    ///
    /// Set to false if you implement your own exit handling.
    pub ctrl_q_quit: bool,
    /// Whether or not to focus a window when the mouse hovers over it `default: false`
    pub focus_follows_hover: bool,
}

impl Config {
    /// Create a new Config struct with the given options
    pub fn new() -> Self {
        Default::default()
    }

    /// Set whether or not to quit on <kbd>ctrl</kbd>+<kbd>q</kbd>
    pub fn ctrl_q_quit(mut self, ctrl_q_quit: bool) -> Self {
        self.ctrl_q_quit = ctrl_q_quit;
        self
    }

    /// Set whether or not to focus a window when the mouse hovers over it
    pub fn focus_follows_hover(mut self, focus_follows_hover: bool) -> Self {
        self.focus_follows_hover = focus_follows_hover;
        self
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            ctrl_q_quit: true,
            focus_follows_hover: false,
        }
    }
}

pub type GlobalHandler<S, U> =
    dyn Fn(&mut App<S, U>, &Event<U>, Arc<Sender<UserEvent<U>>>) -> Result<bool>;

/// The main application struct, responsible for managing the layout tree,
/// keeping track of focus, and rendering the widgets.
///
/// The generic type U is the type of user events that can be sent to widgets. It can be used to
/// define custom message-passing behavior between widgets.
pub struct App<S = (), U = ()> {
    /// The layout tree
    layout: Layout<U, S>,
    /// The post-render widget rects for mouse events
    rendered: SecondaryMap<NodeId, Vec<(Rect, Arc<RwLock<dyn Widget<U, S>>>)>>,
    /// The actual terminal used for rendering
    term: BufferedTerminal<UnixTerminal>,
    /// The size of the terminal
    size: Rect,
    /// The focused node in the tree, if any
    focus: Option<NodeId>,
    /// Sender for user events, given to widgets when `Widget::update` is called
    event_tx: Arc<std::sync::mpsc::Sender<UserEvent<U>>>,
    /// Receiver for user events, only used internally
    event_rx: std::sync::mpsc::Receiver<UserEvent<U>>,
    /// Used to signal the exit internally
    exit: Arc<AtomicBool>,
    /// Global event handler, which intercepts events before they are propagated to the focused
    /// widget. If the handler returns `Ok(true)`, the event is considered handled and is not
    /// propagated to the widget that would otherwise receive it.
    global_event_handler: Box<GlobalHandler<S, U>>,
    /// Configuration struct
    config: Config,
    /// User state
    state: S,
}

impl<S, U> Drop for App<S, U> {
    fn drop(&mut self) {
        // Restore cursor visibility and leave alternate screen when app exits
        self.term
            .add_change(Change::CursorVisibility(CursorVisibility::Visible));
        self.term.terminal().exit_alternate_screen().unwrap();
    }
}

impl<S: Default + 'static, U: 'static> App<S, U> {
    /// Create a new Sanguine application with the provided layout and no global event handler.
    pub fn new(layout: Layout<U, S>, config: Config) -> Result<Self> {
        let term = Capabilities::new_from_env()
            .and_then(|caps| {
                UnixTerminal::new(caps).and_then(|mut t| {
                    t.set_raw_mode()?;
                    t.enter_alternate_screen()?;
                    BufferedTerminal::new(t)
                })
            })
            .map_err(|_| Error::TerminalError)?;
        let (event_tx, event_rx) = std::sync::mpsc::channel();

        Ok(App {
            global_event_handler: Box::new(|_, _, _| Ok(false)),
            size: Rect::from_size(term.dimensions()),
            event_tx: Arc::new(event_tx),
            exit: Arc::new(AtomicBool::new(false)),
            rendered: SecondaryMap::new(),
            focus: None,
            layout,
            term,
            event_rx,
            config,
            state: Default::default(),
        })
    }

    /// Create a new Sanguine app with the provided global event handler. The global event handler
    /// intercepts events before they are sent to widgets. It can return true to prevent the event
    /// from propagating to widgets, or false to allow propagation.
    pub fn new_with_handler(
        layout: Layout<U, S>,
        config: Config,
        handler: impl Fn(&mut App<S, U>, &Event<U>, Arc<Sender<UserEvent<U>>>) -> Result<bool> + 'static,
    ) -> Result<Self> {
        let term = Capabilities::new_from_env()
            .and_then(|caps| {
                UnixTerminal::new(caps).and_then(|mut t| {
                    t.set_raw_mode()?;
                    t.enter_alternate_screen()?;
                    BufferedTerminal::new(t)
                })
            })
            .map_err(|_| Error::TerminalError)?;
        let (event_tx, event_rx) = std::sync::mpsc::channel();

        Ok(App {
            global_event_handler: Box::new(handler),
            size: Rect::from_size(term.dimensions()),
            event_tx: Arc::new(event_tx),
            exit: Arc::new(AtomicBool::new(false)),
            rendered: SecondaryMap::new(),
            focus: None,
            layout,
            term,
            event_rx,
            config,
            state: Default::default(),
        })
    }
}

impl<S: 'static, U: 'static> App<S, U> {
    pub fn new_with_state(layout: Layout<U, S>, config: Config, state: S) -> Result<Self> {
        let term = Capabilities::new_from_env()
            .and_then(|caps| {
                UnixTerminal::new(caps).and_then(|mut t| {
                    t.set_raw_mode()?;
                    t.enter_alternate_screen()?;
                    BufferedTerminal::new(t)
                })
            })
            .map_err(|_| Error::TerminalError)?;
        let (event_tx, event_rx) = std::sync::mpsc::channel();

        Ok(App {
            global_event_handler: Box::new(|_, _, _| Ok(false)),
            size: Rect::from_size(term.dimensions()),
            event_tx: Arc::new(event_tx),
            exit: Arc::new(AtomicBool::new(false)),
            rendered: SecondaryMap::new(),
            focus: None,
            layout,
            term,
            event_rx,
            config,
            state,
        })
    }

    pub fn with_state(mut self, state: S) -> Self {
        self.state = state;
        self
    }

    pub fn with_handler(
        mut self,
        handler: impl Fn(&mut App<S, U>, &Event<U>, Arc<Sender<UserEvent<U>>>) -> Result<bool> + 'static,
    ) -> Self {
        self.global_event_handler = Box::new(handler);
        self
    }

    pub fn handler(
        &mut self,
        handler: impl Fn(&mut App<S, U>, &Event<U>, Arc<Sender<UserEvent<U>>>) -> Result<bool> + 'static,
    ) {
        self.global_event_handler = Box::new(handler);
    }

    fn render_ctx(&self, node: NodeId) -> Result<(Arc<RwLock<dyn Widget<U, S>>>, &Rect)> {
        Ok((
            // Retrieve widget trait object from node
            self.layout
                .widget(node)
                .ok_or(Error::WidgetNotFound(node))?,
            // Retrieve computed layout for window
            self.layout
                .layout(node)
                .ok_or(Error::WidgetNotFound(node))?,
        ))
    }

    fn global_event(&mut self, event: &Event<U>) -> Result<bool> {
        if self.config.ctrl_q_quit {
            if let Event::Key(KeyEvent {
                key: KeyCode::Char('q'),
                modifiers: Modifiers::CTRL,
            }) = event
            {
                self.event_tx
                    .send(UserEvent::Exit)
                    .map_err(|_| Error::SignalSendFail)?
            }
        }

        // Safety: The function pointer is stored in self so the borrow checker doesn't like
        // us calling it with a mutable reference to self. However, the function pointer won't be changed
        // so it should be safe to call with a mutable reference to self.
        let evt = &self.global_event_handler as *const GlobalHandler<S, U>;
        unsafe { (*evt)(self, event, self.event_tx.clone()) }
    }

    fn process_event(&mut self, event: Event<U>) -> Result<()> {
        match &event {
            Event::Resize { cols, rows } => {
                self.size = Rect::from_size((*cols, *rows));
                self.term.resize(*cols, *rows);
                self.term.repaint().map_err(|_| Error::TerminalError)?;
                self.term.flush().map_err(|_| Error::TerminalError)?;
                self.layout.mark_dirty();
            }
            Event::Mouse(MouseEvent {
                mut x,
                mut y,
                mouse_buttons,
                modifiers,
            }) => {
                y -= 1;
                x -= 1;
                if !self.global_event(&event)? {
                    let Some(node) = self.layout.node_at_pos((x, y)) else {
                        return Ok(());
                    };
                    if let Some(focus) = self.focus {
                        let focus = if focus != node {
                            // Send hover events to the hovered node, but focus the window if the mouse is clicked
                            if *mouse_buttons != MouseButtons::NONE {
                                // If the node under the mouse is different from the focused node,
                                // focus the new node and consume the event
                                self.focus = Some(node);
                                return Ok(());
                            }
                            node
                        } else {
                            focus
                        };

                        // check if there are inner widgets that the event should be sent to
                        let children = self.rendered.get(focus).cloned().unwrap_or(vec![]);
                        let child = children
                            .iter()
                            .filter(|(rect, _)| rect.contains(x as f32, y as f32))
                            .next();

                        // If the node under the mouse is the same as the focused node,
                        // send the event to the focused node
                        let (mut widget, mut layout) =
                            self.render_ctx(focus).map(|(w, l)| (w, l.clone())).unwrap();

                        if let Some((child_layout, child_widget)) = child {
                            layout = Rect {
                                x: /* layout.x +  */child_layout.x,
                                y: /* layout.y +  */child_layout.y,
                                width: child_layout.width,
                                height: child_layout.height,
                            };
                            widget = child_widget.clone();
                        } else if children.len() > 0 {
                            return Ok(());
                        }

                        let offset_event = Event::Mouse(MouseEvent {
                            x: x - layout.x as u16,
                            y: y - layout.y as u16,
                            mouse_buttons: *mouse_buttons,
                            modifiers: *modifiers,
                        });

                        widget
                            .write()
                            .map_err(|_| Error::WidgetWriteLockError(focus))?
                            .update(
                                &mut UpdateCtx::new(
                                    focus,
                                    layout,
                                    &mut self.layout,
                                    self.event_tx.clone(),
                                    &mut self.state,
                                ),
                                offset_event,
                            )?;
                    } else if *mouse_buttons == MouseButtons::LEFT
                        || self.config.focus_follows_hover
                    {
                        // If there's no focus, focus the node under the mouse
                        self.focus = Some(node);
                    }
                }
            }
            Event::User(UserEvent::Exit) => {
                self.exit.store(true, std::sync::atomic::Ordering::SeqCst);
            }
            // Anything that doesn't need special handling (keys, paste, user events)
            _ => {
                // Handle global events
                if !self.global_event(&event)? {
                    let Some(focus) = self.focus else {
                        // If there's no focus, we can't do anything
                        let Some(leaf) = self.layout.leaves().first().cloned() else {
                            return Ok(());
                        };
                        self.set_focus(leaf)?;
                        return Ok(())
                    };
                    // Retrieve widget trait object from node
                    let Some(widget) = self
                        .layout
                        .widget(focus) else {
                            return Ok(());
                        };

                    // Retrieve computed layout for window
                    let Some(layout) = self
                        .layout
                        .layout(focus)
                        .cloned() else {
                            return Ok(());
                        };
                    let tx = self.event_tx.clone();

                    widget
                        .write()
                        .map_err(|_| Error::WidgetWriteLockError(focus))?
                        .update(
                            &mut UpdateCtx::new(
                                focus,
                                layout,
                                &mut self.layout,
                                tx,
                                &mut self.state,
                            ),
                            event,
                        )?;
                };
            }
        }

        Ok(())
    }

    fn handle_user_events(&mut self) -> Result<()> {
        if let Ok(event) = self.event_rx.try_recv() {
            self.process_event(Event::User(event))?;
        }
        Ok(())
    }

    fn handle_input_events(&mut self) -> Result<()> {
        while let Some(event) = self
            .term
            .terminal()
            .poll_input(Some(Duration::from_millis(15)))
            .map_err(|_| Error::PollInputFailed)?
        {
            use termwiz::input::InputEvent;
            let translated = match event {
                InputEvent::Key(k) => Event::Key(k),
                InputEvent::Mouse(m) => Event::Mouse(m),
                InputEvent::Resized { rows, cols } => Event::Resize { rows, cols },
                InputEvent::Paste(s) => Event::Paste(s),
                _ => continue,
            };
            self.process_event(translated)?;
        }
        Ok(())
    }

    /// Calls a closure, passing in a mutable reference to the layout.
    pub fn update_layout<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Layout<U, S>) -> R,
        R: Sized,
    {
        f(&mut self.layout)
    }

    /// Calls a closure, passing in an immutable reference to the layout.
    pub fn inspect_layout<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&Layout<U, S>) -> R,
        R: Sized,
    {
        f(&self.layout)
    }

    /// Handles and propagates events, returning whether or not the app should continue running.
    ///
    /// This should be used as the condition (or part of the condition) for an application's render loop.
    pub fn handle_events(&mut self) -> Result<bool> {
        self.handle_user_events()?;
        self.handle_input_events()?;
        Ok(!self.exit.load(std::sync::atomic::Ordering::SeqCst))
    }

    /// Sets the focus to the given node.
    pub fn set_focus(&mut self, node: NodeId) -> Result<()> {
        if self.layout.is_container(node) {
            return Err(Error::ExpectedLeaf(node));
        }
        self.focus = Some(node);
        Ok(())
    }

    /// Get the id of the currently focused node, if any
    pub fn get_focus(&self) -> Option<NodeId> {
        self.focus
    }

    /// Cycle focus to the next window
    pub fn cycle_focus(&mut self) -> Result<()> {
        let current = self.get_focus().ok_or(Error::NoFocus)?;
        let next = self.inspect_layout(|l| {
            l.leaves()
                .into_iter()
                .cycle()
                .skip_while(|v| *v != current)
                .nth(1)
                .ok_or(Error::NoFocus)
        })?;
        self.set_focus(next)?;
        Ok(())
    }

    /// Focus the window in the given direction from the currently focused one
    pub fn focus_direction(&mut self, direction: Direction) -> Result<()> {
        let current = self.get_focus().ok_or(Error::NoFocus)?;
        let available = self.inspect_layout(|l| l.adjacent_on_side(current, direction));
        let Some(next) = available.iter().next() else {
            return Ok(());
        };
        self.set_focus(*next)?;
        Ok(())
    }

    fn render_recursive(
        &mut self,
        owner: NodeId,
        inner_widget: Option<Arc<RwLock<dyn Widget<U, S>>>>,
        inner_layout: Option<Rect>,
        mut screen: &mut Surface,
    ) {
        let layout = match inner_layout {
            Some(layout) => layout,
            None => {
                if let Some(layout) = self.layout.layout(owner) {
                    layout.clone()
                } else {
                    return;
                }
            }
        };
        let widget = match inner_widget.clone() {
            Some(widget) => widget,
            None => {
                if let Some(widget) = self.layout.widget(owner) {
                    widget.clone()
                } else {
                    return;
                }
            }
        };

        // Draw onto widget screen for composition
        let mut widget_screen = Surface::new(layout.width as usize, layout.height as usize);

        // Render widget onto widget screen
        let focused = self.focus.map(|f| f == owner).unwrap_or(false);
        let inner_widgets = match widget.read() {
            Ok(widget) => widget.render(
                &RenderCtx::new(focused, &self.layout, &self.state),
                &mut widget_screen,
            ),
            Err(_) => return,
        };

        // Draw widget onto background screen
        screen.draw_from_screen(&widget_screen, layout.x as usize, layout.y as usize);
        if inner_widget.is_some() {
            self.rendered.get_mut(owner).unwrap().push((
                Rect {
                    x: layout.x,
                    y: layout.y,
                    width: layout.width,
                    height: layout.height,
                },
                widget,
            ));
        } else {
            self.rendered.insert(owner, vec![]);
        }

        if let Some(inner_widgets) = inner_widgets {
            inner_widgets.into_iter().for_each(|(rect, widget)| {
                self.render_recursive(
                    owner,
                    Some(widget.clone()),
                    Some(Rect {
                        x: layout.x + rect.x,
                        y: layout.y + rect.y,
                        width: rect.width,
                        height: rect.height,
                    }),
                    &mut screen,
                );
                self.rendered.get_mut(owner).unwrap().push((
                    Rect {
                        x: layout.x + rect.x,
                        y: layout.y + rect.y,
                        width: rect.width,
                        height: rect.height,
                    },
                    widget,
                ));
            });
        }
    }

    /// Render the entire application to the terminal
    pub fn render(&mut self) -> Result<()> {
        self.rendered.clear();
        self.layout.compute(&self.size);

        // Create temporary background screen
        let mut screen = Surface::new(self.size.width as usize, self.size.height as usize);

        let leaves = self.layout.leaves();
        let floats = self.layout.floats();

        for node in leaves.into_iter().chain(floats) {
            self.render_recursive(node, None, None, &mut screen);
        }

        // Draw contents of background screen to terminal
        self.term.draw_from_screen(&screen, 0, 0);

        if let Some(focus) = self.focus {
            if let Some(layout) = self.layout.layout(focus) {
                if let Some(cursor) = self.layout.widget(focus).unwrap().read().unwrap().cursor() {
                    if let Some(child) = cursor.0 {
                        let child = self.rendered.get(focus).unwrap().get(child).unwrap();
                        // let cursor = child.1.read().unwrap().cursor().unwrap();
                        self.term.add_changes(vec![
                            Change::CursorVisibility(CursorVisibility::Visible),
                            Change::CursorPosition {
                                x: Position::Absolute((child.0.x) as usize + cursor.1),
                                y: Position::Absolute((child.0.y) as usize + cursor.2),
                            },
                        ]);
                    } else {
                        self.term.add_changes(vec![
                            Change::CursorVisibility(CursorVisibility::Visible),
                            Change::CursorPosition {
                                x: Position::Absolute(layout.x as usize + cursor.1),
                                y: Position::Absolute(layout.y as usize + cursor.2),
                            },
                        ]);
                    }
                } else {
                    self.term
                        .add_changes(vec![Change::CursorVisibility(CursorVisibility::Hidden)]);
                }
            }
        }

        // Compute optimized diff and flush
        self.term
            .flush()
            .map_err(|_| Error::external("could not flush terminal"))?;

        Ok(())
    }
}