1
//! Ratatui rendering. The draw layer is a pure function of
2
//! [`crate::app::App`]: take a `Frame` and an `App`, render the current
3
//! visual state. No I/O of its own; I/O is the transport's job.
4

            
5
mod canvas;
6
mod filter_builder;
7
mod form;
8
mod menu;
9
mod messages;
10
mod modal;
11
mod status_tail;
12

            
13
use std::rc::Rc;
14

            
15
use crate::app::App;
16
use crate::event::Intent;
17
use crate::hits::{Hits, Target};
18
use crate::keymap;
19
use crate::view::{DrawCtx, Tab};
20
use canvas::Offscreen;
21
use ratatui::Frame;
22
use ratatui::buffer::Buffer;
23
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
24
use ratatui::widgets::Paragraph;
25
use status_tail::status_tail;
26

            
27
use tvui::{
28
    Dialog, FrameMode, FrameState, Role, StatusBar, close_area, copy_visible, grow_area, zoom_area,
29
};
30

            
31
/// The columns between the hints and the status tail.
32
const TAIL_GAP: usize = 3;
33

            
34
24516
fn screen_rows(area: Rect) -> Rc<[Rect]> {
35
24516
    Layout::default()
36
24516
        .direction(Direction::Vertical)
37
24516
        .constraints([
38
24516
            Constraint::Length(1),
39
24516
            Constraint::Min(1),
40
24516
            Constraint::Length(1),
41
24516
        ])
42
24516
        .split(area)
43
24516
}
44

            
45
/// The area between the menu bar and the status line.
46
#[must_use]
47
22867
pub fn body_area(screen: Rect) -> Rect {
48
22867
    screen_rows(screen)[1]
49
22867
}
50

            
51
/// Draws `app`, recording in `hits` every area a click can land on, bottom to top.
52
1649
pub fn draw(frame: &mut Frame, app: &App, hits: &mut Hits) {
53
1649
    let area = frame.area();
54
1649
    let chunks = screen_rows(area);
55

            
56
1649
    draw_body(frame, chunks[1], app, hits);
57
1649
    let hints = draw_status(frame, chunks[2], app);
58
1649
    menu::draw_menu(frame, chunks[0], app, hits);
59

            
60
1649
    for layer in app.overlays.layers() {
61
685
        hits.push(area, Target::Block);
62
685
        let mut lower = Hits::default();
63
685
        let layer_hits = if layer.top { &mut *hits } else { &mut lower };
64
685
        modal::draw_modal(frame, area, layer, app, layer_hits);
65
    }
66
1649
    if let Some(pending) = &app.pending_form {
67
4
        hits.push(area, Target::Block);
68
4
        modal::draw_pending(frame, area, pending, app);
69
1645
    }
70
1649
    if app.menu.is_none() {
71
1615
        for (rect, intent) in hints {
72
5942
            hits.push(rect, Target::Hint(intent));
73
        }
74
34
    }
75
1649
}
76

            
77
1649
fn draw_body(frame: &mut Frame, area: Rect, app: &App, hits: &mut Hits) {
78
1649
    tvui::render_background(area, frame.buffer_mut(), &app.theme);
79
1649
    let covered = !app.overlays.is_empty() || app.pending_form.is_some();
80
1649
    let front = app.front_window();
81
1649
    for (id, view, place) in app.visible_windows(area) {
82
1514
        let state = app.frame_state(id);
83
1514
        let mut local = Hits::default();
84
1514
        local.push(place.local(), Target::Block);
85
        let mut ctx = DrawCtx {
86
1514
            is_active: app.console_input_focused() && front == Some(id),
87
1514
            frame: match (state.mode, covered) {
88
658
                (FrameMode::Active, true) => FrameState {
89
658
                    mode: FrameMode::Passive,
90
658
                    ..state
91
658
                },
92
856
                _ => state,
93
            },
94
1514
            title: view.framed_title(&app.window_suffix(id)),
95
1514
            theme: &app.theme,
96
1514
            hits: &mut local,
97
        };
98
1514
        let mut window = Buffer::empty(place.local());
99
1514
        view.draw(&mut window, place.local(), &mut ctx);
100
1514
        copy_visible(&window, place, place.visible_in(area), frame.buffer_mut());
101
1514
        push_frame_hits(&mut local, place.local(), state);
102
1514
        hits.extend_shown(local, Some(id), place, area);
103
    }
104
1649
}
105

            
106
/// The frame's own areas, over whatever the view drew: its icons and the whole top row.
107
1514
fn push_frame_hits(local: &mut Hits, area: Rect, state: FrameState) {
108
1514
    local.push(Rect { height: 1, ..area }, Target::Title);
109
1514
    for (rect, target) in [
110
1514
        (close_area(area, state), Target::Close),
111
1514
        (zoom_area(area, state), Target::Zoom),
112
1514
        (grow_area(area, state), Target::Grow),
113
1514
    ] {
114
4542
        if let Some(rect) = rect {
115
4014
            local.push(rect, target);
116
528
        }
117
    }
118
1514
}
119

            
120
/// Draws the status line and returns the area of each hint, to record over any modal: the
121
/// status line stays live under one.
122
1649
fn draw_status(frame: &mut Frame, area: Rect, app: &App) -> Vec<(Rect, Intent)> {
123
1649
    let style = app.theme.style(Role::Status);
124
1649
    if app.cmdline.active {
125
287
        let buf = app.cmdline.editor.buffer();
126
287
        let text = match app.cmdline.completions.is_empty() {
127
286
            true => format!(":{buf}"),
128
1
            false => format!(":{}  [{}]", buf, app.cmdline.completions.join("  ")),
129
        };
130
287
        frame.render_widget(Paragraph::new(text).style(style), area);
131
287
        return Vec::new();
132
1362
    }
133
1362
    let indicator = format!("[{}]", app.keys.indicator());
134
1362
    if !app.status.is_empty() {
135
6
        let text = format!("{indicator} {}", app.status);
136
6
        frame.render_widget(Paragraph::new(text).style(style), area);
137
6
        return Vec::new();
138
1356
    }
139
1356
    if app.front_window().is_none() && app.overlays.is_empty() && app.menu.is_none() {
140
32
        let text = format!("{indicator} {}", empty_desktop_hint(app));
141
32
        frame.render_widget(Paragraph::new(text).style(style), area);
142
32
        return Vec::new();
143
1324
    }
144
1324
    let hinted = keymap::hinted(app);
145
1324
    let hints: Vec<_> = hinted.iter().map(keymap::Hint::pair).collect();
146
1324
    let bar = StatusBar::new(&app.theme, &hints);
147
1324
    let tail = status_tail(app.user_id, &indicator);
148
1324
    let tail_width = u16::try_from(tail.chars().count() + TAIL_GAP).unwrap_or(u16::MAX);
149
1324
    let width = area.width.saturating_sub(tail_width);
150
1324
    let end = Rect {
151
1324
        x: area.x.saturating_add(width),
152
1324
        width: area.width.saturating_sub(width),
153
1324
        ..area
154
1324
    };
155
1324
    let bar_area = Rect { width, ..area };
156
1324
    let areas: Vec<_> = StatusBar::item_areas(bar_area, &hints)
157
1324
        .into_iter()
158
6054
        .map(|(index, rect)| (rect, hinted[index].intent))
159
        .collect();
160
1324
    frame.render_widget(bar, bar_area);
161
1324
    let text = Paragraph::new(tail)
162
1324
        .style(style)
163
1324
        .alignment(Alignment::Right);
164
1324
    frame.render_widget(text, end);
165
1324
    areas
166
1649
}
167

            
168
/// What the status line says while the desktop is empty: the keys of the menu and of the views.
169
32
fn empty_desktop_hint(app: &App) -> String {
170
32
    let keys = keymap::here(app);
171
64
    let views = [Tab::ALL.first(), Tab::ALL.last()].map(|tab| {
172
64
        let tab = tab.copied()?;
173
64
        keys.key(Intent::SelectTab(tab))
174
64
    });
175
    let parts = [
176
32
        keys.key(Intent::OpenMenu)
177
32
            .map(|menu| format!("{menu} menu")),
178
32
        match views {
179
32
            [Some(first), Some(last)] => Some(format!("{first}\u{2013}{last} open a view")),
180
            _ => None,
181
        },
182
    ];
183
32
    parts
184
32
        .into_iter()
185
32
        .flatten()
186
32
        .collect::<Vec<_>>()
187
32
        .join(" \u{b7} ")
188
32
}
189

            
190
249
fn draw_panel(
191
249
    frame: &mut Offscreen,
192
249
    area: Rect,
193
249
    dialog: Dialog<'_>,
194
249
    (body, at): (Paragraph<'_>, Rect),
195
249
) {
196
249
    frame.render_widget(dialog, area);
197
249
    frame.render_widget(body, at);
198
249
}
199

            
200
#[cfg(test)]
201
mod bar_hits_tests;
202
#[cfg(test)]
203
mod button_tests;
204
#[cfg(test)]
205
mod form_hits_tests;
206
#[cfg(test)]
207
mod frame_tests;
208
#[cfg(test)]
209
mod hits_tests;
210
#[cfg(test)]
211
mod layers_tests;
212
#[cfg(test)]
213
pub(crate) mod pending_form_tests;
214
#[cfg(test)]
215
mod placed_tests;
216
#[cfg(test)]
217
mod tests;
218
#[cfg(test)]
219
mod window_hits_tests;