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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! Renderer for `App::dock_widgets`. Paints each widget into a
//! corner of the editor body area, stacking inward when multiple
//! widgets share a corner.
//!
//! Slice 1 ships only `DockCorner::BottomLeft` painting + the
//! `DockContent::Text` variant + a close `×`. Other corners
//! match the same shape — the `corner_anchor_y` helper computes
//! the starting y per corner so adding them is a small follow-up.
use ratatui::{
Frame,
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph},
};
use crate::app::App;
use crate::dock::{DockContent, DockCorner, Layout as DockLayout, Opacity};
use crate::ui::theme;
/// Paint all dock widgets into the editor body area. Called from
/// `ui::draw` AFTER the editor / split tree paints, so the dock
/// chrome overlays the editor when they overlap.
pub fn draw(frame: &mut Frame, app: &mut App, editor_area: Rect) {
app.rects.dock_widget_bodies.clear();
app.rects.dock_widget_close_buttons.clear();
app.rects.dock_widget_titles.clear();
app.rects.dock_widget_kebabs.clear();
app.rects.dock_empty_chip = None;
if app.dock_widgets.is_empty() {
// Empty-state discoverability chip — a faint `+ dock` at
// the very bottom-right of the editor body. Click → open
// the kebab-style new-widget picker (a stripped-down
// menu with just the `Add` actions). Disappears the
// moment any widget exists. Only render if there's
// enough room AND no overlay is active that would
// visually compete.
if editor_area.width >= 14 && editor_area.height >= 2 {
paint_empty_state_chip(frame, app, editor_area);
}
return;
}
if editor_area.width < 12 || editor_area.height < 4 {
return;
}
let t = theme::cur();
// ── Inline-mode widgets first: tile horizontally into the
// top + bottom strips reserved by `ui::draw`. The editor
// area parameter is ALREADY shrunk by these strips, so we
// read the strips back from `app.rects`.
let top_strip = app.rects.inline_dock_top_strip;
let bottom_strip = app.rects.inline_dock_bottom_strip;
paint_inline_strip(frame, app, top_strip, /*is_top=*/ true, t);
paint_inline_strip(frame, app, bottom_strip, /*is_top=*/ false, t);
// Group widgets by corner so we know how to stack inside each.
// Iterate the four corners explicitly so painting order is
// deterministic regardless of insertion order.
for &corner in &[
DockCorner::BottomLeft,
DockCorner::BottomRight,
DockCorner::TopLeft,
DockCorner::TopRight,
] {
paint_corner_stack(frame, app, editor_area, corner, t);
}
// Drop-zone preview during a dock drag — painted BEFORE the
// kebab menu so an open menu still overlays it (drags don't
// happen with a menu open anyway, but the layering is safer).
paint_drag_preview(frame, app, editor_area, t);
// Kebab menu — painted last so it overlays all widget chrome.
paint_kebab_menu(frame, app, t);
}
fn paint_corner_stack(
frame: &mut Frame,
app: &mut App,
area: Rect,
corner: DockCorner,
t: crate::ui::theme::Theme,
) {
// Collect widgets pinned to this corner, in the order they
// were inserted. The order determines stack order: BottomLeft
// stacks UPWARD (first widget sits at the very bottom),
// TopLeft stacks DOWNWARD (first widget sits at the very top).
let indexed: Vec<(usize, _)> = app
.dock_widgets
.iter()
.enumerate()
.filter(|(_, w)| w.corner == corner && matches!(w.layout, crate::dock::Layout::Overlay))
.map(|(i, w)| (i, w.clone()))
.collect();
if indexed.is_empty() {
return;
}
// Compute per-widget rect, then cap the total height per
// corner to 50% of editor body so the stack can't smother
// the editor underneath.
let max_stack_h = area.height / 2;
let mut painted_h: u16 = 0;
// For bottom corners we stack from the bottom edge inward
// (upward), so we iterate widgets in REVERSE so the
// visually-topmost widget gets the last (smallest) y. For
// top corners we stack downward, so iterate forward.
let is_bottom = matches!(corner, DockCorner::BottomLeft | DockCorner::BottomRight);
let order: Box<dyn Iterator<Item = &(usize, crate::dock::DockWidget)>> = if is_bottom {
Box::new(indexed.iter().rev())
} else {
Box::new(indexed.iter())
};
for (_, w) in order {
// Clamp the user's fractions to a sane range so a widget
// is never unusably small or oversized.
let w_frac = w.width_frac.clamp(0.15, 0.9);
let h_frac = w.height_frac.clamp(0.15, 0.9);
let widget_w = (area.width as f32 * w_frac) as u16;
let widget_h = (area.height as f32 * h_frac) as u16;
if widget_w < 8 || widget_h < 3 {
continue;
}
// Skip if this widget would push the stack past 50%.
if painted_h.saturating_add(widget_h) > max_stack_h {
break;
}
// Position depends on corner. For bottom corners,
// `painted_h` is the distance already consumed above the
// bottom edge; the new widget sits with its bottom edge at
// `area.y + area.height - painted_h - widget_h`.
let (x, y) = match corner {
DockCorner::BottomLeft => (area.x, area.y + area.height - painted_h - widget_h),
DockCorner::BottomRight => (
area.x + area.width - widget_w,
area.y + area.height - painted_h - widget_h,
),
DockCorner::TopLeft => (area.x, area.y + painted_h),
DockCorner::TopRight => (area.x + area.width - widget_w, area.y + painted_h),
};
let widget_rect = Rect {
x,
y,
width: widget_w,
height: widget_h,
};
// code-reviewer S1-3 — was inlined here; delegate to
// paint_one_widget so Overlay widgets pick up the same
// tail-overflow ▼N badge as inline ones.
paint_one_widget(frame, app, w, widget_rect, t);
painted_h = painted_h.saturating_add(widget_h);
}
}
/// Tile inline widgets horizontally across `strip`. `is_top`
/// controls source ordering: top widgets are TL→TR (left→right by
/// corner, then by insertion); bottom widgets are BL→BR.
fn paint_inline_strip(
frame: &mut Frame,
app: &mut App,
strip: Option<Rect>,
is_top: bool,
t: crate::ui::theme::Theme,
) {
let Some(strip) = strip else {
return;
};
// Collect inline widgets for this edge, in insertion order
// within each corner. Left corner first → right corner
// second, so widths-frac-of-editor lines up with corner
// intent.
let (left_corner, right_corner) = if is_top {
(DockCorner::TopLeft, DockCorner::TopRight)
} else {
(DockCorner::BottomLeft, DockCorner::BottomRight)
};
let mut ordered: Vec<crate::dock::DockWidget> = Vec::new();
for w in &app.dock_widgets {
if matches!(w.layout, crate::dock::Layout::Inline) && w.corner == left_corner {
ordered.push(w.clone());
}
}
for w in &app.dock_widgets {
if matches!(w.layout, crate::dock::Layout::Inline) && w.corner == right_corner {
ordered.push(w.clone());
}
}
if ordered.is_empty() {
return;
}
// Tile left-to-right. Each widget gets its own
// `width_frac × editor_width` slice; if their summed widths
// exceed the strip, later widgets are dropped (rule we
// settled on: first inline at this edge wins; subsequent
// ones silently overflow).
let strip_w = strip.width;
let mut cur_x = strip.x;
for w in &ordered {
let w_frac = w.width_frac.clamp(0.15, 0.9);
let widget_w = (strip_w as f32 * w_frac) as u16;
if widget_w < 8 {
continue;
}
if cur_x + widget_w > strip.x + strip.width {
// Out of room — skip silently.
break;
}
let widget_rect = Rect {
x: cur_x,
y: strip.y,
width: widget_w,
height: strip.height,
};
paint_one_widget(frame, app, w, widget_rect, t);
cur_x += widget_w;
}
}
/// Extracted single-widget paint so `paint_corner_stack` and
/// `paint_inline_strip` share the chrome / body code.
fn paint_one_widget(
frame: &mut Frame,
app: &mut App,
w: &crate::dock::DockWidget,
widget_rect: Rect,
t: crate::ui::theme::Theme,
) {
let translucent = matches!(w.opacity, Opacity::Translucent);
if !translucent {
frame.render_widget(Clear, widget_rect);
}
let block = if translucent {
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(t.comment))
} else {
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(t.comment).bg(t.bg2))
};
let inner = block.inner(widget_rect);
frame.render_widget(block, widget_rect);
// code-reviewer S2-1 — read the LogTail file ONCE outside both
// sub-blocks so the title's tail_hint and the body render share
// the result.
let log_tail_read: Option<String> = match &w.content {
DockContent::LogTail { path, .. } => std::fs::read_to_string(path).ok(),
_ => None,
};
// Title + kebab.
if inner.width > 4 {
let is_dragging = app.dock_drag_id == Some(w.id);
let title_bg = if is_dragging { t.cyan } else { t.bg2 };
let title_fg = if is_dragging { t.bg } else { t.fg };
let kebab_glyph = "⋮";
let drag_hint = if is_dragging { " ⇲ " } else { "" };
// Live-content tail-indicator chip — only meaningful for
// content variants whose data CAN exceed the visible
// body (LogTail today; future ClaudeTail / BuildStatus
// will opt in by reporting their own "lines below").
let tail_hint = match (&w.content, log_tail_read.as_deref()) {
(DockContent::LogTail { .. }, Some(s)) => {
let body_h = inner.height.saturating_sub(1).max(1) as usize;
let total = s.lines().count();
if total > body_h {
let hidden = total - body_h;
Some(format!(" ▼{hidden} "))
} else {
None
}
}
_ => None,
};
let tail_hint_str = tail_hint.as_deref().unwrap_or("");
let title_w = inner.width.saturating_sub(
2 + drag_hint.chars().count() as u16 + tail_hint_str.chars().count() as u16,
);
let title_clipped: String = w.title.chars().take(title_w as usize).collect();
let pad = (title_w as usize).saturating_sub(title_clipped.chars().count());
let title_line = Line::from(vec![
Span::styled(
title_clipped,
Style::default()
.fg(title_fg)
.bg(title_bg)
.add_modifier(Modifier::BOLD),
),
Span::styled(" ".repeat(pad), Style::default().bg(title_bg)),
Span::styled(
tail_hint_str.to_string(),
Style::default().fg(t.comment).bg(title_bg),
),
Span::styled(drag_hint, Style::default().fg(title_fg).bg(title_bg)),
Span::styled(kebab_glyph, Style::default().fg(title_fg).bg(title_bg)),
Span::styled(" ", Style::default().bg(title_bg)),
]);
let title_rect = Rect {
x: inner.x,
y: inner.y,
width: inner.width,
height: 1,
};
frame.render_widget(Paragraph::new(title_line), title_rect);
let kebab_rect = Rect {
x: inner.x + inner.width - 2,
y: inner.y,
width: 1,
height: 1,
};
app.rects.dock_widget_kebabs.push((kebab_rect, w.id));
let title_drag_rect = Rect {
x: inner.x,
y: inner.y,
width: inner.width.saturating_sub(2),
height: 1,
};
app.rects.dock_widget_titles.push((title_drag_rect, w.id));
}
if inner.height >= 2 {
let body_rect = Rect {
x: inner.x,
y: inner.y + 1,
width: inner.width,
height: inner.height - 1,
};
match &w.content {
DockContent::Text(s) => render_text_body(frame, body_rect, s, t),
DockContent::LogTail { path, max_lines } => render_log_tail_body(
frame,
body_rect,
path,
*max_lines,
t,
log_tail_read.as_deref(),
),
}
app.rects.dock_widget_bodies.push((body_rect, w.id));
}
}
/// While a dock widget is being dragged, paint:
/// - a translucent gray overlay over the quadrant the cursor
/// is currently in (where the widget will pin on release),
/// - a small ghost chip near the cursor showing the title.
fn paint_drag_preview(
frame: &mut Frame,
app: &mut App,
editor_area: Rect,
t: crate::ui::theme::Theme,
) {
let Some(drag_id) = app.dock_drag_id else {
return;
};
let Some((cx, cy)) = app.dock_drag_cursor else {
return;
};
// Look up the title + actual size for the ghost chip and
// drop-rect. The drop-rect must match the widget's current
// dimensions — painting the whole quadrant misleads the user
// about the actual landing footprint.
let Some(widget) = app.dock_widgets.iter().find(|w| w.id == drag_id) else {
return;
};
let title = widget.title.clone();
let w_frac = widget.width_frac.clamp(0.15, 0.9);
let h_frac = widget.height_frac.clamp(0.15, 0.9);
let widget_w = (editor_area.width as f32 * w_frac) as u16;
let widget_h = (editor_area.height as f32 * h_frac) as u16;
if editor_area.width < 4 || editor_area.height < 4 || widget_w < 4 || widget_h < 2 {
return;
}
// Resolve cursor → target corner.
let mid_x = editor_area.x + editor_area.width / 2;
let mid_y = editor_area.y + editor_area.height / 2;
let (drop_x, drop_y) = match (cx < mid_x, cy < mid_y) {
// TopLeft
(true, true) => (editor_area.x, editor_area.y),
// TopRight
(false, true) => (editor_area.x + editor_area.width - widget_w, editor_area.y),
// BottomLeft
(true, false) => (editor_area.x, editor_area.y + editor_area.height - widget_h),
// BottomRight
(false, false) => (
editor_area.x + editor_area.width - widget_w,
editor_area.y + editor_area.height - widget_h,
),
};
let drop_rect = Rect {
x: drop_x,
y: drop_y,
width: widget_w,
height: widget_h,
};
// Translucent-ish gray fill — terminals can't do alpha so we
// approximate by overpainting each cell with a single
// shaded glyph + dim fg. This signals "drop zone" without
// hiding what's underneath.
let shade_style = Style::default().fg(t.comment).bg(t.bg_dark);
for row in 0..drop_rect.height {
let y = drop_rect.y + row;
let line = Line::from(Span::styled(
"░".repeat(drop_rect.width as usize),
shade_style,
));
frame.render_widget(
Paragraph::new(line),
Rect {
x: drop_rect.x,
y,
width: drop_rect.width,
height: 1,
},
);
}
// Corner-label so the user can tell which quadrant they're
// targeting (handy when dragging across a busy split tree).
let corner_label = match (cx < mid_x, cy < mid_y) {
(true, true) => " ⤴ Top-left ",
(false, true) => " ⤵ Top-right ",
(true, false) => " ⤷ Bottom-left ",
(false, false) => " ⤶ Bottom-right ",
};
let label_w = corner_label.chars().count() as u16;
if drop_rect.width > label_w + 2 && drop_rect.height >= 1 {
let label_rect = Rect {
x: drop_rect.x + (drop_rect.width - label_w) / 2,
y: drop_rect.y + drop_rect.height / 2,
width: label_w,
height: 1,
};
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
corner_label,
Style::default()
.fg(t.fg)
.bg(t.bg2)
.add_modifier(Modifier::BOLD),
))),
label_rect,
);
}
// Ghost chip near the cursor — `⇲ <title>` so the user sees
// what they're carrying. Offset 1 cell so it doesn't cover
// the cursor itself.
let chip_label = format!(" ⇲ {title} ");
let chip_w = chip_label.chars().count() as u16;
let chip_x = (cx + 2).min(editor_area.x + editor_area.width.saturating_sub(chip_w));
let chip_y = cy.saturating_sub(1).max(editor_area.y);
let chip_rect = Rect {
x: chip_x,
y: chip_y,
width: chip_w.min(editor_area.width),
height: 1,
};
frame.render_widget(Clear, chip_rect);
frame.render_widget(
Paragraph::new(Line::from(Span::styled(
chip_label,
Style::default()
.fg(t.bg)
.bg(t.cyan)
.add_modifier(Modifier::BOLD),
))),
chip_rect,
);
}
/// Paint the empty-state discoverability chip — a faint
/// `+ dock` at the bottom-right of the editor body. Click target
/// stored in `app.rects.dock_empty_chip` so the mouse handler
/// can fire `dock.new_text`.
fn paint_empty_state_chip(frame: &mut Frame, app: &mut App, area: Rect) {
let t = theme::cur();
let label = " + dock ";
let lw = label.chars().count() as u16;
if area.width <= lw + 2 {
return;
}
let chip_rect = Rect {
x: area.x + area.width - lw - 1,
y: area.y + area.height - 1,
width: lw,
height: 1,
};
let line = Line::from(Span::styled(
label,
Style::default().fg(t.comment).bg(t.bg2),
));
frame.render_widget(Paragraph::new(line), chip_rect);
app.rects.dock_empty_chip = Some(chip_rect);
}
/// Paint the open kebab menu (if any). Anchored below the `⋮`
/// glyph that opened it; clamped to screen edges.
fn paint_kebab_menu(frame: &mut Frame, app: &mut App, t: crate::ui::theme::Theme) {
app.rects.dock_kebab_rows = Vec::new();
let Some(menu) = app.dock_kebab_menu.as_ref() else {
return;
};
// Look up the widget so we can mark current values in the menu
// (e.g. the active Layout / Opacity).
let cur_layout = app
.dock_widgets
.iter()
.find(|w| w.id == menu.widget_id)
.map(|w| w.layout);
let cur_opacity = app
.dock_widgets
.iter()
.find(|w| w.id == menu.widget_id)
.map(|w| w.opacity);
let cur_corner = app
.dock_widgets
.iter()
.find(|w| w.id == menu.widget_id)
.map(|w| w.corner);
// Compute panel dimensions.
let max_label = 22u16; // widest item label (incl. "Inline (eats space)")
let w = max_label + 4;
let body_rows = menu.items.len() as u16;
let h = body_rows + 2; // borders
let screen = frame.area();
// Don't paint over the statusline / cmdline strip at the
// bottom — clamp to the statusline's top y if known. Falls
// back to screen height - 1 if the statusline rect isn't
// available yet.
let bottom_limit = app
.rects
.statusline
.map(|r| r.y)
.unwrap_or_else(|| screen.y + screen.height.saturating_sub(1));
let x = menu
.anchor_x
.saturating_sub(w / 2)
.min(screen.x + screen.width.saturating_sub(w));
let preferred_y = menu.anchor_y + 1;
// If the menu would extend past the bottom limit, flip it
// ABOVE the anchor (drop-up). Otherwise drop-down as usual.
let y = if preferred_y + h > bottom_limit {
menu.anchor_y.saturating_sub(h).max(screen.y)
} else {
preferred_y
};
let area = Rect {
x,
y,
width: w.min(screen.width),
height: h.min(screen.height),
};
frame.render_widget(Clear, area);
let block = crate::ui::design_tokens::popup_menu("");
let inner = block.inner(area);
frame.render_widget(block, area);
let mut y = inner.y;
let mut item_rects: Vec<(Rect, usize)> = Vec::new();
// Row-highlight tokens are re-derived from the theme each row —
// Style is Copy, so this stays cheap. Matches menu bar / context
// menu / whichkey selection.
let sel_style = crate::ui::design_tokens::row_highlight_menu();
let sel_bg = sel_style.bg.unwrap_or(t.cyan);
let sel_fg = sel_style.fg.unwrap_or(t.bg_dark);
let sel_mod = Modifier::BOLD;
// `y` advances per drawn row but also gates the early `break` below, so the
// explicit counter is clearer here than a zipped range.
#[allow(clippy::explicit_counter_loop)]
for (idx, item) in menu.items.iter().enumerate() {
if y >= inner.y + inner.height {
break;
}
let is_selected = idx == menu.selected;
let row_rect = Rect {
x: inner.x,
y,
width: inner.width,
height: 1,
};
let (text, fg, bg, indent, modifier) = match item {
crate::dock::KebabMenuItem::Header(h) => {
(h.to_string(), t.comment, t.bg2, "", Modifier::BOLD)
}
crate::dock::KebabMenuItem::Separator => (
"─".repeat(inner.width as usize),
t.comment,
t.bg2,
"",
Modifier::empty(),
),
crate::dock::KebabMenuItem::Resize(p) => (
p.label().to_string(),
if is_selected { sel_fg } else { t.fg },
if is_selected { sel_bg } else { t.bg2 },
" ",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
),
crate::dock::KebabMenuItem::MoveTo(c) => {
let label = match c {
DockCorner::BottomLeft => "Bottom-left",
DockCorner::BottomRight => "Bottom-right",
DockCorner::TopLeft => "Top-left",
DockCorner::TopRight => "Top-right",
};
let marker = if cur_corner == Some(*c) { "● " } else { " " };
(
format!("{marker}{label}"),
if is_selected { sel_fg } else { t.fg },
if is_selected { sel_bg } else { t.bg2 },
"",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
)
}
crate::dock::KebabMenuItem::SetLayout(l) => {
let label = match l {
DockLayout::Overlay => "Overlay",
DockLayout::Inline => "Inline (eats space)",
};
let marker = if cur_layout == Some(*l) { "● " } else { " " };
(
format!("{marker}{label}"),
if is_selected { sel_fg } else { t.fg },
if is_selected { sel_bg } else { t.bg2 },
"",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
)
}
crate::dock::KebabMenuItem::SetOpacity(o) => {
let label = match o {
Opacity::Solid => "Solid",
Opacity::Translucent => "Translucent",
};
let marker = if cur_opacity == Some(*o) {
"● "
} else {
" "
};
(
format!("{marker}{label}"),
if is_selected { sel_fg } else { t.fg },
if is_selected { sel_bg } else { t.bg2 },
"",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
)
}
crate::dock::KebabMenuItem::Rename => (
"Rename…".to_string(),
if is_selected { sel_fg } else { t.fg },
if is_selected { sel_bg } else { t.bg2 },
" ",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
),
crate::dock::KebabMenuItem::Close => (
"Close".to_string(),
if is_selected { sel_fg } else { t.red },
if is_selected { sel_bg } else { t.bg2 },
" ",
if is_selected {
sel_mod
} else {
Modifier::empty()
},
),
};
// Right-pad the row so the bg fills the full menu width
// (otherwise the selection highlight stops at the end of
// the text and looks like a stub).
let text_w = indent.chars().count() + text.chars().count();
let pad = (inner.width as usize).saturating_sub(text_w);
let line = Line::from(vec![
Span::styled(indent.to_string(), Style::default().bg(bg)),
Span::styled(text, Style::default().fg(fg).bg(bg).add_modifier(modifier)),
Span::styled(" ".repeat(pad), Style::default().bg(bg)),
]);
frame.render_widget(Paragraph::new(line), row_rect);
// Only register click rects for selectable items.
if !matches!(
item,
crate::dock::KebabMenuItem::Header(_) | crate::dock::KebabMenuItem::Separator
) {
item_rects.push((row_rect, idx));
}
y += 1;
}
app.rects.dock_kebab_rows = item_rects;
}
/// Render static text into the widget's body. Naive char-boundary
/// line-wrap; long single words just clip at the right edge. Good
/// enough for v1 — proper word-wrap can land later.
fn render_text_body(frame: &mut Frame, body_rect: Rect, text: &str, t: crate::ui::theme::Theme) {
let mut lines: Vec<Line> = Vec::with_capacity(body_rect.height as usize);
let max_w = body_rect.width as usize;
for raw in text.lines() {
let mut remaining = raw;
while !remaining.is_empty() && lines.len() < body_rect.height as usize {
let take = remaining.chars().take(max_w).collect::<String>();
let take_len = take.chars().count();
lines.push(Line::from(Span::styled(
take,
Style::default().fg(t.fg).bg(t.bg2),
)));
remaining = remaining
.char_indices()
.nth(take_len)
.map(|(idx, _)| &remaining[idx..])
.unwrap_or("");
}
if lines.len() >= body_rect.height as usize {
break;
}
}
frame.render_widget(Paragraph::new(lines), body_rect);
}
/// Render the last `max_lines` rows of `path` into the body. Re-
/// reads the file every frame; sufficient for the small log
/// tails this widget targets (tests + build output + AI session
/// jsonl), and avoids the complexity of mtime caching.
///
/// Empty / missing file → render a dim placeholder.
fn render_log_tail_body(
frame: &mut Frame,
body_rect: Rect,
path: &std::path::Path,
max_lines: usize,
t: crate::ui::theme::Theme,
// code-reviewer S2-1 — accept pre-read contents from
// paint_one_widget so we don't read the file twice per frame
// per LogTail widget. None = caller didn't pre-read; fall back
// to reading here so existing call sites still work.
pre_read: Option<&str>,
) {
let max_w = body_rect.width as usize;
let max_h = body_rect.height as usize;
let take_n = max_lines.min(max_h);
let mut lines_out: Vec<Line> = Vec::with_capacity(take_n);
let owned: String;
let s_opt: Option<&str> = if let Some(s) = pre_read {
Some(s)
} else {
match std::fs::read_to_string(path) {
Ok(s) => {
owned = s;
Some(owned.as_str())
}
Err(_) => None,
}
};
match s_opt.ok_or(()) {
Ok(s) => {
let all_lines: Vec<&str> = s.lines().collect();
let start = all_lines.len().saturating_sub(take_n);
for raw in &all_lines[start..] {
// Truncate to width — log tails are usually long-
// line stdout; wrapping wastes vertical space.
let display: String = raw.chars().take(max_w).collect();
lines_out.push(Line::from(Span::styled(
display,
Style::default().fg(t.fg).bg(t.bg2),
)));
}
}
Err(_) => {
// File missing / unreadable — show the path so the
// user knows what we tried to open.
let display: String = format!("(no file: {})", path.display())
.chars()
.take(max_w)
.collect();
lines_out.push(Line::from(Span::styled(
display,
Style::default().fg(t.comment).bg(t.bg2),
)));
}
}
frame.render_widget(Paragraph::new(lines_out), body_rect);
}