返回 CodeWhale
layout.rs
根目录 / crates / tui / src / tui / work_surface / render / layout.rs
1 //! Where the surface goes and how tall it is — the arithmetic [`height`] and
2 //! [`super::render`] must agree on before a single cell is painted.
3
4 use ratatui::layout::Rect;
5
6 use crate::tui::app::App;
7 use crate::tui::work_surface::model::{
8 self, RailPanel, WorkSurfacePlacement, visible_rows_for_panel,
9 };
10
11 use super::{progress_shares_goal_row, top_goal_title, top_todo_progress};
12
13 const SIDE_RAIL_MIN_HOST_WIDTH: u16 = 72;
14 const SIDE_RAIL_MIN_CHAT_WIDTH: u16 = 40;
15
16 fn effective_placement(configured: WorkSurfacePlacement, host_width: u16) -> WorkSurfacePlacement {
17 if configured == WorkSurfacePlacement::Off {
18 return WorkSurfacePlacement::Off;
19 }
20 // Only the side rails need width; strip placements work at any host
21 // width, and a narrow host keeps the user's Bottom default.
22 if matches!(
23 configured,
24 WorkSurfacePlacement::Left | WorkSurfacePlacement::Right
25 ) && host_width < SIDE_RAIL_MIN_HOST_WIDTH
26 {
27 WorkSurfacePlacement::Top
28 } else {
29 configured
30 }
31 }
32
33 /// Responsive work-surface height.
34 ///
35 /// `rail_budget` is the caller's answer to "how many rows can the transcript
36 /// actually spare this frame" — terminal height minus fixed chrome minus the
37 /// transcript's own floor. See [`crate::tui::ui::rail_row_budget`]. The rail
38 /// takes spare rows; it never takes rows the transcript needs.
39 ///
40 /// Every view auto-fits its content the same way: content rows + optional
41 /// goal title + the divider, capped by `top_height` and ambient room. A
42 /// two-item checklist is two rows; eight agents grow to show eight. The only
43 /// Top title is an active goal — never panel chrome. Which view is on screen
44 /// is decided first by [`model::resolve_view`]: the user's explicit pick, or
45 /// the first work view with content.
46 pub fn height(app: &mut App, width: u16, terminal_height: u16, rail_budget: u16) -> u16 {
47 app.work_surface.effective_placement = effective_placement(app.work_surface.placement, width);
48 // Off hides the rail outright: no strip, no side reservation, no stale
49 // interaction state.
50 if app.work_surface.effective_placement == WorkSurfacePlacement::Off {
51 collapse_strip(app);
52 return 0;
53 }
54 model::resolve_view(app);
55 if app.work_surface.dismissed {
56 // New work anywhere in the auto views re-opens the dock — a worker
57 // spawning while the to-do list is on top counts, even though the
58 // resolved view stays TODO.
59 let current_rows = model::auto_work_rows(app);
60 let new_work = app.work_surface.panel != app.work_surface.dismissed_view
61 || current_rows > app.work_surface.dismissed_at_rows;
62 if new_work {
63 app.work_surface.dismissed = false;
64 } else {
65 collapse_strip(app);
66 return 0;
67 }
68 }
69
70 // The strip is something to SHOW, not a fixture: without an explicit
71 // pick it opens only for work — live agents, the to-do list, background
72 // jobs. The persisted `rail_panel` preference (files, context, git,
73 // price…) always has rows to paint, and letting it auto-open made the
74 // dock a permanent band under the composer. It still opens on demand
75 // (cycle, tab click, `/workbar <view>`) and then sticks until Esc.
76 let auto_open =
77 app.work_surface.explicit_view || RailPanel::AUTO_ORDER.contains(&app.work_surface.panel);
78 let rows = if auto_open {
79 visible_rows_for_panel(app)
80 } else {
81 Vec::new()
82 };
83 let strip = app.work_surface.effective_placement.is_strip();
84 let goal_rows = u16::from(strip && top_goal_title(app).is_some());
85 let explicit = app.work_surface.explicit_view;
86 if rows.is_empty() && !explicit {
87 // A live goal alone still deserves a strip: title + divider.
88 if goal_rows == 0 {
89 collapse_strip(app);
90 app.work_surface.latest_rows.clear();
91 app.work_surface.visible_rows = 0;
92 app.work_surface.total_rows = 0;
93 app.work_surface.scroll_offset = 0;
94 return 0;
95 }
96 if !strip {
97 return 0;
98 }
99 let cap = top_cap(app, terminal_height, rail_budget);
100 if cap < model::TOP_HEIGHT_MIN {
101 collapse_strip(app);
102 return 0;
103 }
104 return (goal_rows.saturating_add(2)).clamp(model::TOP_HEIGHT_MIN, cap);
105 }
106 if !strip {
107 return 0;
108 }
109 // The strip auto-fits its content: the view's rows plus the optional
110 // goal title, the pinned progress receipt, and the divider row, bounded
111 // by `top_cap`. An explicitly opened empty view keeps one row for its
112 // "nothing here yet" line so cycling never lands on a blank band.
113 let cap = top_cap(app, terminal_height, rail_budget);
114 if cap < model::TOP_HEIGHT_MIN && (!explicit || cap < 3) {
115 collapse_strip(app);
116 return 0;
117 }
118 if app.work_surface.panel == RailPanel::Terminal {
119 return cap;
120 }
121 let list_rows = rows.len().max(usize::from(explicit));
122 let body_width = if width >= 4 { width - 2 } else { width };
123 let progress = u16::from(
124 top_todo_progress(app, &rows).is_some()
125 && !progress_shares_goal_row(body_width, goal_rows > 0),
126 );
127 let desired = u16::try_from(list_rows)
128 .unwrap_or(u16::MAX)
129 .saturating_add(progress)
130 .saturating_add(goal_rows)
131 .saturating_add(2);
132 desired.clamp(model::TOP_HEIGHT_MIN.min(cap), cap)
133 }
134
135 /// The ceilings the *terminal* imposes, independent of anything the user
136 /// asked for, smallest wins:
137 ///
138 /// - half the terminal: proportional restraint, so a tall rail on a short
139 /// terminal still reads as a strip over a transcript.
140 /// - `rail_budget`: the rows the transcript can actually spare. This is the
141 /// only one that knows the transcript has a floor, and it is the one that
142 /// lets decorative water outrank a panel nobody is watching.
143 ///
144 /// Kept separate from [`top_cap`] because the collapse cliff must be charged
145 /// against ambient room alone. Both are monotone non-decreasing in terminal
146 /// height, which is what keeps the strip from blinking across a resize.
147 fn ambient_cap(terminal_height: u16, rail_budget: u16) -> u16 {
148 terminal_height
149 .saturating_div(2)
150 .clamp(model::TOP_HEIGHT_MIN, model::TOP_HEIGHT_MAX)
151 .min(rail_budget)
152 }
153
154 /// [`ambient_cap`] plus `top_height` — what the user asked for via
155 /// drag-resize / settings. This is the ceiling on how *tall* a strip may
156 /// grow; it is deliberately not the quantity a collapse threshold is
157 /// compared against.
158 fn top_cap(app: &App, terminal_height: u16, rail_budget: u16) -> u16 {
159 app.work_surface
160 .top_height
161 .min(ambient_cap(terminal_height, rail_budget))
162 }
163
164 /// Drop the interaction state that only means anything while a strip is on
165 /// screen. Every path reporting "no strip this frame" must run this: hitboxes
166 /// outlive the rows they described, so a strip that yielded its rows would
167 /// still swallow clicks landing on the transcript that replaced it.
168 pub(crate) fn collapse_strip(app: &mut App) {
169 app.work_surface.last_area = None;
170 app.work_surface.hitboxes.clear();
171 app.work_surface.focused = false;
172 app.work_surface.selected = None;
173 app.work_surface.opened = None;
174 app.work_surface.hovered = None;
175 app.work_surface.resizing = false;
176 app.work_surface.divider_hovered = false;
177 app.work_surface.dock_tabs.clear();
178 app.work_surface.pressed_tab = None;
179 app.work_surface.hovered_tab = None;
180 }
181
182 /// Split the transcript slot for a side rail. Top placement consumes its own
183 /// vertical row before this point, so it returns the chat area unchanged.
184 ///
185 /// Placement and auto-fit are orthogonal but share one rule: **empty work is
186 /// not a rail**. Top expresses that as `height() == 0`. Left/Right express it
187 /// here — no column is reserved when the selected panel has nothing to say.
188 /// When there *is* content, the rail takes the full chat height at the
189 /// configured `side_width` (width is the ceiling, the way `top_height` is the
190 /// ceiling on Top). Narrow terminals that cannot fit the rail fall back to
191 /// Top, where height auto-fit takes over.
192 ///
193 /// `min_chat_width` is the column-axis twin of `height`'s `rail_budget`: the
194 /// columns the transcript must keep. When the idle ocean is on screen that is
195 /// the ambient floor, and a rail that cannot fit beside it hides rather than
196 /// squeezing the water into a strip too narrow to draw.
197 pub fn split_chat(app: &mut App, area: Rect, min_chat_width: u16) -> (Rect, Option<Rect>) {
198 let placement = effective_placement(app.work_surface.placement, area.width);
199 app.work_surface.effective_placement = placement;
200 if placement.is_strip() || placement == WorkSurfacePlacement::Off {
201 return (area, None);
202 }
203 // Same empty-collapse rule as Top: a panel with nothing to show does not
204 // spend columns on a blank (or "No agents") column.
205 if !side_rail_has_content(app) {
206 collapse_strip(app);
207 return (area, None);
208 }
209
210 let min_chat_width = min_chat_width.max(SIDE_RAIL_MIN_CHAT_WIDTH);
211 let rail_width = app
212 .work_surface
213 .side_width
214 .clamp(model::SIDE_WIDTH_MIN, model::SIDE_WIDTH_MAX)
215 .min(area.width.saturating_sub(min_chat_width));
216 if rail_width < model::SIDE_WIDTH_MIN {
217 // Too narrow for a side column — fall back to Top. The caller will
218 // re-ask height() with effective_placement Top so content auto-fits
219 // as a strip instead of vanishing.
220 app.work_surface.effective_placement = WorkSurfacePlacement::Top;
221 collapse_strip(app);
222 return (area, None);
223 }
224
225 let chat_width = area.width.saturating_sub(rail_width);
226 match placement {
227 WorkSurfacePlacement::Left => (
228 Rect {
229 x: area.x.saturating_add(rail_width),
230 width: chat_width,
231 ..area
232 },
233 Some(Rect {
234 width: rail_width,
235 ..area
236 }),
237 ),
238 WorkSurfacePlacement::Right => (
239 Rect {
240 width: chat_width,
241 ..area
242 },
243 Some(Rect {
244 x: area.x.saturating_add(chat_width),
245 width: rail_width,
246 ..area
247 }),
248 ),
249 WorkSurfacePlacement::Top | WorkSurfacePlacement::Bottom | WorkSurfacePlacement::Off => {
250 (area, None)
251 }
252 }
253 }
254
255 /// Whether a Left/Right rail should reserve columns this frame.
256 fn side_rail_has_content(app: &mut App) -> bool {
257 if app.work_surface.dismissed {
258 return false;
259 }
260 model::resolve_view(app);
261 app.work_surface.explicit_view || !visible_rows_for_panel(app).is_empty()
262 }
263
263 lines RUST