Lines
99.27 %
Functions
100 %
Branches
//! Ratatui rendering. The draw layer is a pure function of
//! [`crate::app::App`]: take a `Frame` and an `App`, render the current
//! visual state. No I/O of its own; I/O is the transport's job.
mod canvas;
mod filter_builder;
mod form;
mod menu;
mod messages;
mod modal;
mod status_tail;
use std::rc::Rc;
use crate::app::App;
use crate::event::Intent;
use crate::hits::{Hits, Target};
use crate::keymap;
use crate::view::{DrawCtx, Tab};
use canvas::Offscreen;
use ratatui::Frame;
use ratatui::buffer::Buffer;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::widgets::Paragraph;
use status_tail::status_tail;
use tvui::{
Dialog, FrameMode, FrameState, Role, StatusBar, close_area, copy_visible, grow_area, zoom_area,
};
/// The columns between the hints and the status tail.
const TAIL_GAP: usize = 3;
fn screen_rows(area: Rect) -> Rc<[Rect]> {
Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1),
Constraint::Min(1),
])
.split(area)
}
/// The area between the menu bar and the status line.
#[must_use]
pub fn body_area(screen: Rect) -> Rect {
screen_rows(screen)[1]
/// Draws `app`, recording in `hits` every area a click can land on, bottom to top.
pub fn draw(frame: &mut Frame, app: &App, hits: &mut Hits) {
let area = frame.area();
let chunks = screen_rows(area);
draw_body(frame, chunks[1], app, hits);
let hints = draw_status(frame, chunks[2], app);
menu::draw_menu(frame, chunks[0], app, hits);
for layer in app.overlays.layers() {
hits.push(area, Target::Block);
let mut lower = Hits::default();
let layer_hits = if layer.top { &mut *hits } else { &mut lower };
modal::draw_modal(frame, area, layer, app, layer_hits);
if let Some(pending) = &app.pending_form {
modal::draw_pending(frame, area, pending, app);
if app.menu.is_none() {
for (rect, intent) in hints {
hits.push(rect, Target::Hint(intent));
fn draw_body(frame: &mut Frame, area: Rect, app: &App, hits: &mut Hits) {
tvui::render_background(area, frame.buffer_mut(), &app.theme);
let covered = !app.overlays.is_empty() || app.pending_form.is_some();
let front = app.front_window();
for (id, view, place) in app.visible_windows(area) {
let state = app.frame_state(id);
let mut local = Hits::default();
local.push(place.local(), Target::Block);
let mut ctx = DrawCtx {
is_active: app.console_input_focused() && front == Some(id),
frame: match (state.mode, covered) {
(FrameMode::Active, true) => FrameState {
mode: FrameMode::Passive,
..state
},
_ => state,
title: view.framed_title(&app.window_suffix(id)),
theme: &app.theme,
hits: &mut local,
let mut window = Buffer::empty(place.local());
view.draw(&mut window, place.local(), &mut ctx);
copy_visible(&window, place, place.visible_in(area), frame.buffer_mut());
push_frame_hits(&mut local, place.local(), state);
hits.extend_shown(local, Some(id), place, area);
/// The frame's own areas, over whatever the view drew: its icons and the whole top row.
fn push_frame_hits(local: &mut Hits, area: Rect, state: FrameState) {
local.push(Rect { height: 1, ..area }, Target::Title);
for (rect, target) in [
(close_area(area, state), Target::Close),
(zoom_area(area, state), Target::Zoom),
(grow_area(area, state), Target::Grow),
] {
if let Some(rect) = rect {
local.push(rect, target);
/// Draws the status line and returns the area of each hint, to record over any modal: the
/// status line stays live under one.
fn draw_status(frame: &mut Frame, area: Rect, app: &App) -> Vec<(Rect, Intent)> {
let style = app.theme.style(Role::Status);
if app.cmdline.active {
let buf = app.cmdline.editor.buffer();
let text = match app.cmdline.completions.is_empty() {
true => format!(":{buf}"),
false => format!(":{} [{}]", buf, app.cmdline.completions.join(" ")),
frame.render_widget(Paragraph::new(text).style(style), area);
return Vec::new();
let indicator = format!("[{}]", app.keys.indicator());
if !app.status.is_empty() {
let text = format!("{indicator} {}", app.status);
if app.front_window().is_none() && app.overlays.is_empty() && app.menu.is_none() {
let text = format!("{indicator} {}", empty_desktop_hint(app));
let hinted = keymap::hinted(app);
let hints: Vec<_> = hinted.iter().map(keymap::Hint::pair).collect();
let bar = StatusBar::new(&app.theme, &hints);
let tail = status_tail(app.user_id, &indicator);
let tail_width = u16::try_from(tail.chars().count() + TAIL_GAP).unwrap_or(u16::MAX);
let width = area.width.saturating_sub(tail_width);
let end = Rect {
x: area.x.saturating_add(width),
width: area.width.saturating_sub(width),
..area
let bar_area = Rect { width, ..area };
let areas: Vec<_> = StatusBar::item_areas(bar_area, &hints)
.into_iter()
.map(|(index, rect)| (rect, hinted[index].intent))
.collect();
frame.render_widget(bar, bar_area);
let text = Paragraph::new(tail)
.style(style)
.alignment(Alignment::Right);
frame.render_widget(text, end);
areas
/// What the status line says while the desktop is empty: the keys of the menu and of the views.
fn empty_desktop_hint(app: &App) -> String {
let keys = keymap::here(app);
let views = [Tab::ALL.first(), Tab::ALL.last()].map(|tab| {
let tab = tab.copied()?;
keys.key(Intent::SelectTab(tab))
});
let parts = [
keys.key(Intent::OpenMenu)
.map(|menu| format!("{menu} menu")),
match views {
[Some(first), Some(last)] => Some(format!("{first}\u{2013}{last} open a view")),
_ => None,
];
parts
.flatten()
.collect::<Vec<_>>()
.join(" \u{b7} ")
fn draw_panel(
frame: &mut Offscreen,
area: Rect,
dialog: Dialog<'_>,
(body, at): (Paragraph<'_>, Rect),
) {
frame.render_widget(dialog, area);
frame.render_widget(body, at);
#[cfg(test)]
mod bar_hits_tests;
mod button_tests;
mod form_hits_tests;
mod frame_tests;
mod hits_tests;
mod layers_tests;
pub(crate) mod pending_form_tests;
mod placed_tests;
mod tests;
mod window_hits_tests;