509 lines
17 KiB
Rust
509 lines
17 KiB
Rust
//! Top-level draw orchestration. Composes sidebar / tab bar / pane / overlays
|
|
//! into one frame, and returns a [`RegionMap`] that the input router uses to
|
|
//! hit-test mouse clicks.
|
|
|
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
|
use ratatui::style::Style;
|
|
use ratatui::text::Span;
|
|
use ratatui::widgets::{Block, Borders, Paragraph};
|
|
use ratatui::Frame;
|
|
|
|
use crate::input::Region;
|
|
use crate::keymap::Mode;
|
|
use crate::overlays::{HelpOverlay, KillConfirm, Toast};
|
|
use crate::palette;
|
|
use crate::pane::Pane;
|
|
use crate::sidebar::Sidebar;
|
|
use crate::state::{AppState, Focus, OverlayKind, SidebarItem};
|
|
use crate::tabs::TabBar;
|
|
use crate::worktrees::{ReclaimConfirm, WorktreesView};
|
|
|
|
/// Tagged regions for mouse hit-testing. The input router resolves a mouse
|
|
/// coordinate to one of these via [`RegionMap::region_at`].
|
|
#[derive(Debug, Default, Clone)]
|
|
pub struct RegionMap {
|
|
pub sidebar: Option<Rect>,
|
|
pub sidebar_items: Vec<SidebarItem>,
|
|
pub tab_bar: Option<Rect>,
|
|
pub pane: Option<Rect>,
|
|
pub modal: Option<Rect>,
|
|
}
|
|
|
|
impl RegionMap {
|
|
pub fn region_at(&self, col: u16, row: u16) -> Region {
|
|
if let Some(r) = self.modal {
|
|
if inside(r, col, row) {
|
|
return Region::Modal;
|
|
}
|
|
}
|
|
if let Some(r) = self.tab_bar {
|
|
if inside(r, col, row) {
|
|
return Region::Tab;
|
|
}
|
|
}
|
|
if let Some(r) = self.sidebar {
|
|
if inside(r, col, row) {
|
|
return Region::SidebarRow;
|
|
}
|
|
}
|
|
if let Some(r) = self.pane {
|
|
if inside(r, col, row) {
|
|
return Region::Pane;
|
|
}
|
|
}
|
|
Region::Unknown
|
|
}
|
|
|
|
/// Resolve a mouse row against the rendered sidebar sequence. The title
|
|
/// occupies one inner row; groups occupy one row and visible cards two.
|
|
pub fn sidebar_item_at(&self, row: u16) -> Option<SidebarItem> {
|
|
let area = self.sidebar?;
|
|
let mut cursor = area.y.saturating_add(2);
|
|
for item in &self.sidebar_items {
|
|
let height = match item {
|
|
SidebarItem::Group(_) => 1,
|
|
SidebarItem::Session(_) => 2,
|
|
};
|
|
if row >= cursor && row < cursor.saturating_add(height) {
|
|
return Some(item.clone());
|
|
}
|
|
cursor = cursor.saturating_add(height);
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
fn inside(r: Rect, col: u16, row: u16) -> bool {
|
|
col >= r.x && col < r.x + r.width && row >= r.y && row < r.y + r.height
|
|
}
|
|
|
|
/// Bundle of references the renderer reads from. Built by `App::redraw` each
|
|
/// frame.
|
|
#[derive(Debug)]
|
|
pub struct RenderInputs<'a> {
|
|
pub sidebar: &'a Sidebar,
|
|
pub home_dir: &'a str,
|
|
pub tab_bar: &'a TabBar,
|
|
pub pane: Option<&'a Pane>,
|
|
/// Inline spawn form when the active tab's pane is a draft (issue #63).
|
|
pub draft: Option<&'a crate::draft::DraftPane>,
|
|
pub state: &'a AppState,
|
|
pub help: &'a HelpOverlay,
|
|
/// Retained-worktree entries for the worktrees view (#85).
|
|
pub worktrees: Option<&'a WorktreesView>,
|
|
}
|
|
|
|
/// Draw one frame. Returns the `RegionMap` the input router will consult for
|
|
/// mouse events the next time it canonicalises an event.
|
|
pub fn draw(f: &mut Frame, inputs: RenderInputs<'_>) -> RegionMap {
|
|
let mut map = RegionMap::default();
|
|
let area = f.area();
|
|
|
|
// Split the screen into top (everything) + 1-row status bar at the bottom.
|
|
let outer = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([Constraint::Min(0), Constraint::Length(1)])
|
|
.split(area);
|
|
let body_area = outer[0];
|
|
let status_area = outer[1];
|
|
|
|
let cols = if inputs.state.sidebar_visible {
|
|
Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Length(28), Constraint::Min(0)])
|
|
.split(body_area)
|
|
} else {
|
|
Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Length(0), Constraint::Min(0)])
|
|
.split(body_area)
|
|
};
|
|
let sidebar_area = cols[0];
|
|
let right = cols[1];
|
|
|
|
if inputs.state.sidebar_visible {
|
|
inputs.sidebar.render_with_focus_and_collapsed_home(
|
|
f,
|
|
sidebar_area,
|
|
inputs.state.focus == Focus::Sidebar,
|
|
&inputs.state.collapsed_sidebar_groups,
|
|
inputs.home_dir,
|
|
);
|
|
map.sidebar = Some(sidebar_area);
|
|
map.sidebar_items = inputs
|
|
.sidebar
|
|
.rendered_items_with_collapsed(&inputs.state.collapsed_sidebar_groups);
|
|
}
|
|
|
|
// The pane always owns its full border, so focus changes only recolour
|
|
// chrome and never change the rectangles handed to the input router.
|
|
let rows = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints([Constraint::Length(1), Constraint::Min(0)])
|
|
.split(right);
|
|
let tab_area = rows[0];
|
|
let pane_area = rows[1];
|
|
|
|
inputs.tab_bar.render(f, tab_area);
|
|
map.tab_bar = Some(tab_area);
|
|
|
|
let pane_focused = inputs.state.focus == Focus::Pane;
|
|
let pane_block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.border_style(focus_style(pane_focused));
|
|
let pane_inner = pane_block.inner(pane_area);
|
|
f.render_widget(pane_block, pane_area);
|
|
if let Some(p) = inputs.pane {
|
|
p.render(f, pane_inner);
|
|
}
|
|
if let Some(d) = inputs.draft {
|
|
d.render(f, pane_inner, pane_focused);
|
|
}
|
|
// `RegionMap::pane` is also the PTY resize source. Keep it aligned with
|
|
// the cells the emulator actually owns, excluding the persistent border.
|
|
map.pane = Some(pane_inner);
|
|
|
|
// Status bar — always visible, never eclipsed by overlays so the user can
|
|
// always see the current mode + key hints.
|
|
render_status_bar(f, status_area, inputs.state);
|
|
|
|
match inputs.state.overlay() {
|
|
Some(OverlayKind::KillConfirm(id)) => {
|
|
// #83: surface what the watcher last saw in the worktree, in
|
|
// InTheWay's own words, before the operator commits to the kill.
|
|
let blockers = inputs
|
|
.sidebar
|
|
.sessions
|
|
.iter()
|
|
.find(|s| &s.id == id)
|
|
.and_then(|s| s.blocked_work)
|
|
.filter(|b| !b.is_clear())
|
|
.map(|b| b.to_string());
|
|
let kc = KillConfirm {
|
|
label: id.to_string(),
|
|
blockers,
|
|
};
|
|
kc.render(f, area);
|
|
map.modal = Some(area);
|
|
}
|
|
Some(OverlayKind::Help) => {
|
|
inputs.help.render(f, area);
|
|
map.modal = Some(area);
|
|
}
|
|
Some(OverlayKind::Worktrees) => {
|
|
if let Some(view) = inputs.worktrees {
|
|
view.render(f, area);
|
|
map.modal = Some(area);
|
|
}
|
|
}
|
|
Some(OverlayKind::ReclaimConfirm {
|
|
branch, blockers, ..
|
|
}) => {
|
|
let rc = ReclaimConfirm {
|
|
branch: branch.clone(),
|
|
blockers: blockers.clone(),
|
|
};
|
|
rc.render(f, area);
|
|
map.modal = Some(area);
|
|
}
|
|
Some(OverlayKind::Toast(text)) => {
|
|
let t = Toast { text: text.clone() };
|
|
t.render(f, area);
|
|
}
|
|
None => {}
|
|
}
|
|
|
|
map
|
|
}
|
|
|
|
/// Border style for a panel based on whether it currently has focus. Both
|
|
/// forms are palette roles (`border_focused` / `border_unfocused`).
|
|
fn focus_style(focused: bool) -> Style {
|
|
palette::active().border_style(focused)
|
|
}
|
|
|
|
/// Render the always-visible status bar at the bottom of the frame. Surfaces
|
|
/// mode + focused panel + the most useful key hints so the user is never
|
|
/// guessing what state they're in.
|
|
fn render_status_bar(f: &mut Frame, area: Rect, state: &AppState) {
|
|
let mode_label = match state.mode {
|
|
Mode::Normal => "[NORMAL]",
|
|
Mode::Insert => "[INSERT]",
|
|
};
|
|
let focus_label = match state.focus {
|
|
Focus::Sidebar => "sidebar",
|
|
Focus::Pane => "pane",
|
|
};
|
|
let hint = match state.mode {
|
|
Mode::Normal => "j/k: nav • enter: attach • s: new draft • ?: help • ^Wq: quit",
|
|
Mode::Insert => "^a: back to normal",
|
|
};
|
|
let mode_style = palette::active().mode_style(state.mode == Mode::Insert);
|
|
let line = ratatui::text::Line::from(vec![
|
|
Span::styled(format!(" {mode_label} "), mode_style),
|
|
Span::raw(format!(" {focus_label} • {hint}")),
|
|
]);
|
|
f.render_widget(Paragraph::new(line), area);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::input::Region;
|
|
|
|
#[test]
|
|
fn region_map_resolves_known_areas() {
|
|
let map = RegionMap {
|
|
sidebar: Some(Rect {
|
|
x: 0,
|
|
y: 0,
|
|
width: 10,
|
|
height: 20,
|
|
}),
|
|
sidebar_items: Vec::new(),
|
|
tab_bar: Some(Rect {
|
|
x: 10,
|
|
y: 0,
|
|
width: 30,
|
|
height: 3,
|
|
}),
|
|
pane: Some(Rect {
|
|
x: 10,
|
|
y: 3,
|
|
width: 30,
|
|
height: 17,
|
|
}),
|
|
modal: None,
|
|
};
|
|
assert_eq!(map.region_at(5, 5), Region::SidebarRow);
|
|
assert_eq!(map.region_at(15, 1), Region::Tab);
|
|
assert_eq!(map.region_at(15, 5), Region::Pane);
|
|
assert_eq!(map.region_at(50, 50), Region::Unknown);
|
|
}
|
|
|
|
#[test]
|
|
fn modal_eclipses_other_regions() {
|
|
let map = RegionMap {
|
|
sidebar: Some(Rect {
|
|
x: 0,
|
|
y: 0,
|
|
width: 10,
|
|
height: 20,
|
|
}),
|
|
sidebar_items: Vec::new(),
|
|
tab_bar: Some(Rect {
|
|
x: 10,
|
|
y: 0,
|
|
width: 30,
|
|
height: 3,
|
|
}),
|
|
pane: Some(Rect {
|
|
x: 10,
|
|
y: 3,
|
|
width: 30,
|
|
height: 17,
|
|
}),
|
|
modal: Some(Rect {
|
|
x: 5,
|
|
y: 5,
|
|
width: 25,
|
|
height: 10,
|
|
}),
|
|
};
|
|
// Even though (15, 10) is inside `pane`, the modal claims it.
|
|
assert_eq!(map.region_at(15, 10), Region::Modal);
|
|
assert_eq!(map.region_at(3, 12), Region::SidebarRow); // outside modal
|
|
}
|
|
|
|
#[test]
|
|
fn sidebar_mouse_rows_follow_group_and_two_line_card_heights() {
|
|
use std::path::PathBuf;
|
|
use uuid::Uuid;
|
|
|
|
let group_a = SidebarItem::Group(PathBuf::from("/a"));
|
|
let card_a = SidebarItem::Session(Uuid::from_u128(1));
|
|
let group_b = SidebarItem::Group(PathBuf::from("/b"));
|
|
let card_b = SidebarItem::Session(Uuid::from_u128(2));
|
|
let map = RegionMap {
|
|
sidebar: Some(Rect::new(0, 0, 28, 10)),
|
|
sidebar_items: vec![group_a.clone(), card_a.clone(), group_b.clone(), card_b],
|
|
..RegionMap::default()
|
|
};
|
|
assert_eq!(map.sidebar_item_at(2), Some(group_a));
|
|
assert_eq!(map.sidebar_item_at(3), Some(card_a.clone()));
|
|
assert_eq!(map.sidebar_item_at(4), Some(card_a));
|
|
assert_eq!(map.sidebar_item_at(5), Some(group_b));
|
|
|
|
let collapsed = RegionMap {
|
|
sidebar: Some(Rect::new(0, 0, 28, 10)),
|
|
sidebar_items: vec![
|
|
SidebarItem::Group(PathBuf::from("/a")),
|
|
SidebarItem::Group(PathBuf::from("/b")),
|
|
SidebarItem::Session(Uuid::from_u128(2)),
|
|
],
|
|
..RegionMap::default()
|
|
};
|
|
assert_eq!(
|
|
collapsed.sidebar_item_at(3),
|
|
Some(SidebarItem::Group(PathBuf::from("/b")))
|
|
);
|
|
}
|
|
|
|
/// Issue #63: an empty pane is a draft — the frame must show the spawn
|
|
/// form where the terminal would otherwise be.
|
|
#[test]
|
|
fn a_draft_pane_renders_the_spawn_form_in_its_pane_area() {
|
|
use crate::overlays::HelpOverlay;
|
|
use crate::state::PaneRef;
|
|
use std::collections::HashSet;
|
|
|
|
let mut state = AppState::new();
|
|
state.open_draft_pane();
|
|
let draft = match &state.tabs[0].pane {
|
|
Some(PaneRef::Draft(d)) => d,
|
|
other => panic!("expected a draft pane, got {other:?}"),
|
|
};
|
|
let sidebar = Sidebar {
|
|
sessions: Vec::new(),
|
|
highlight: None,
|
|
attached: HashSet::new(),
|
|
};
|
|
let tab_bar = TabBar {
|
|
tab_names: vec!["1".into()],
|
|
active_idx: 0,
|
|
};
|
|
let help = HelpOverlay { lines: Vec::new() };
|
|
let inputs = RenderInputs {
|
|
sidebar: &sidebar,
|
|
home_dir: "",
|
|
tab_bar: &tab_bar,
|
|
pane: None,
|
|
draft: Some(draft),
|
|
state: &state,
|
|
help: &help,
|
|
worktrees: None,
|
|
};
|
|
|
|
let backend = ratatui::backend::TestBackend::new(80, 24);
|
|
let mut terminal = ratatui::Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|f| {
|
|
draw(f, inputs);
|
|
})
|
|
.unwrap();
|
|
let buf = terminal.backend().buffer().clone();
|
|
let mut out = String::new();
|
|
for y in 0..buf.area.height {
|
|
for x in 0..buf.area.width {
|
|
out.push_str(buf[(x, y)].symbol());
|
|
}
|
|
out.push('\n');
|
|
}
|
|
for s in [
|
|
"draft",
|
|
"cmd:",
|
|
"dir:",
|
|
"name:",
|
|
"[ Spawn ]",
|
|
"Other directory…",
|
|
] {
|
|
assert!(out.contains(s), "missing {s:?}:\n{out}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn panel_rectangles_are_stable_when_focus_moves() {
|
|
use crate::overlays::HelpOverlay;
|
|
use crate::sidebar::Sidebar;
|
|
use std::collections::HashSet;
|
|
|
|
let sidebar = Sidebar {
|
|
sessions: Vec::new(),
|
|
highlight: None,
|
|
attached: HashSet::new(),
|
|
};
|
|
let tab_bar = TabBar {
|
|
tab_names: vec!["1".into()],
|
|
active_idx: 0,
|
|
};
|
|
let help = HelpOverlay { lines: Vec::new() };
|
|
let draw_map = |focus| {
|
|
let mut state = AppState::new();
|
|
state.focus = focus;
|
|
let inputs = RenderInputs {
|
|
sidebar: &sidebar,
|
|
home_dir: "",
|
|
tab_bar: &tab_bar,
|
|
pane: None,
|
|
draft: None,
|
|
state: &state,
|
|
help: &help,
|
|
worktrees: None,
|
|
};
|
|
let backend = ratatui::backend::TestBackend::new(80, 24);
|
|
let mut terminal = ratatui::Terminal::new(backend).unwrap();
|
|
let mut map = None;
|
|
terminal.draw(|f| map = Some(draw(f, inputs))).unwrap();
|
|
map.unwrap()
|
|
};
|
|
|
|
let sidebar_focus = draw_map(Focus::Sidebar);
|
|
let pane_focus = draw_map(Focus::Pane);
|
|
assert_eq!(sidebar_focus.sidebar, pane_focus.sidebar);
|
|
assert_eq!(sidebar_focus.tab_bar, pane_focus.tab_bar);
|
|
assert_eq!(sidebar_focus.pane, pane_focus.pane);
|
|
assert_eq!(sidebar_focus.pane, Some(Rect::new(29, 2, 50, 20)));
|
|
}
|
|
|
|
#[test]
|
|
fn panel_border_colours_follow_focus_without_restyling_the_pane() {
|
|
use crate::overlays::HelpOverlay;
|
|
use crate::sidebar::Sidebar;
|
|
use std::collections::HashSet;
|
|
|
|
let sidebar = Sidebar {
|
|
sessions: Vec::new(),
|
|
highlight: None,
|
|
attached: HashSet::new(),
|
|
};
|
|
let tab_bar = TabBar {
|
|
tab_names: vec!["1".into()],
|
|
active_idx: 0,
|
|
};
|
|
let help = HelpOverlay { lines: Vec::new() };
|
|
let render = |focus| {
|
|
let mut state = AppState::new();
|
|
state.focus = focus;
|
|
let inputs = RenderInputs {
|
|
sidebar: &sidebar,
|
|
home_dir: "",
|
|
tab_bar: &tab_bar,
|
|
pane: None,
|
|
draft: None,
|
|
state: &state,
|
|
help: &help,
|
|
worktrees: None,
|
|
};
|
|
let backend = ratatui::backend::TestBackend::new(80, 24);
|
|
let mut terminal = ratatui::Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|f| {
|
|
draw(f, inputs);
|
|
})
|
|
.unwrap();
|
|
terminal.backend().buffer().clone()
|
|
};
|
|
|
|
let sidebar_focused = render(Focus::Sidebar);
|
|
let pane_focused = render(Focus::Pane);
|
|
let p = palette::active();
|
|
assert_eq!(sidebar_focused[(0, 0)].style().fg, Some(p.border_focused));
|
|
assert_eq!(pane_focused[(0, 0)].style().fg, Some(p.border_unfocused));
|
|
assert_eq!(
|
|
sidebar_focused[(28, 1)].style().fg,
|
|
Some(p.border_unfocused)
|
|
);
|
|
assert_eq!(pane_focused[(28, 1)].style().fg, Some(p.border_focused));
|
|
}
|
|
}
|