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
//! Drawing the tree: its rows, icons, marks and guides, the sticky band, the scrollbar,
//! and the dot on a file an agent changed.
use super::*;
/// The unreviewed-file marker (#345). A middle dot rather than a filled
/// circle: it must read as a small annotation beside a filename, not compete
/// with the file-type icon at the start of the row.
pub(super) const AGENT_DOT: char = '\u{b7}';
/// Paints styled text segments across one row of a [`Buffer`] within a fixed
/// column budget, matching `Buffer::set_line` without allocating `Span`s or
/// `String`s (#2062).
struct RowPainter<'a> {
buf: &'a mut Buffer,
x: u16,
y: u16,
remaining: usize,
}
impl<'a> RowPainter<'a> {
fn new(buf: &'a mut Buffer, x: u16, y: u16, max_width: u16) -> Self {
Self {
buf,
x,
y,
remaining: usize::from(max_width),
}
}
fn put(&mut self, text: &str, style: Style) {
if self.remaining == 0 || text.is_empty() {
return;
}
let (new_x, _) = self
.buf
.set_stringn(self.x, self.y, text, self.remaining, style);
let written = usize::from(new_x.saturating_sub(self.x));
if unicode_width::UnicodeWidthStr::width(text) > self.remaining {
self.remaining = 0;
} else {
self.remaining = self.remaining.saturating_sub(written);
}
self.x = new_x;
}
fn put_char(&mut self, ch: char, style: Style) {
let mut utf8 = [0u8; 4];
self.put(ch.encode_utf8(&mut utf8), style);
}
fn put_usize(&mut self, n: usize, style: Style) {
let mut digits = [0u8; 20];
let mut i = digits.len();
let mut v = n;
loop {
i -= 1;
digits[i] = b'0' + (v % 10) as u8;
v /= 10;
if v == 0 {
break;
}
}
// SAFETY: digits[i..] contains only ASCII '0'..='9'.
let s = unsafe { std::str::from_utf8_unchecked(&digits[i..]) };
self.put(s, style);
}
}
/// Extension suffix (`".rs"`) borrowed from `name` when possible, matching
/// `Path::extension` with zero heap allocations on valid UTF-8 filenames.
fn borrowed_suffix<'a>(path: &Path, name: &'a str) -> std::borrow::Cow<'a, str> {
use std::borrow::Cow;
let Some(ext) = path.extension() else {
return Cow::Borrowed("");
};
if let Some(ext_str) = ext.to_str()
&& let Some(dot) = name.len().checked_sub(ext_str.len() + 1)
&& name.as_bytes().get(dot) == Some(&b'.')
&& &name[dot + 1..] == ext_str
{
return Cow::Borrowed(&name[dot..]);
}
Cow::Owned(format!(".{}", ext.to_string_lossy()))
}
impl FileTree {
/// Draw the view into `area` with this frame's [`ViewCx`].
pub fn draw(&mut self, cx: &ViewCx, area: Rect, buf: &mut Buffer) {
let block_style = if cx.focused {
Style::default().fg(cx.theme.ui(Color::Rgb(0x4e, 0x9a, 0xff)))
} else {
Style::default().fg(Color::DarkGray)
};
// Black theme: chipless brand header (panelTitle.activeForeground);
// Croft Dark keeps the historical white-on-navy chip.
let tr_label = crate::i18n::tr("EXPLORER");
let mut title_stack = [0u8; 64];
let title_owned: String;
let label: &str = if tr_label.len() + 2 <= title_stack.len() {
title_stack[0] = b' ';
title_stack[1..1 + tr_label.len()].copy_from_slice(tr_label.as_bytes());
title_stack[1 + tr_label.len()] = b' ';
// SAFETY: tr_label is valid UTF-8 and surrounded by ASCII spaces.
unsafe { std::str::from_utf8_unchecked(&title_stack[..tr_label.len() + 2]) }
} else {
title_owned = format!(" {tr_label} ");
&title_owned
};
let label_width = unicode_width::UnicodeWidthStr::width(label);
let tones = crate::theme::tones(cx.theme);
// Croft Loch: the view title is the one label in the side bar that
// says where focus is, so it is the strong ink while the tree has
// the keyboard and recedes to muted otherwise. Never bold: weight
// is kept for the workspace root below it.
let title_style = if let Some(t) = tones {
Style::default().fg(if cx.focused { t.strong() } else { t.muted() })
} else if cx.theme.gradient() {
Style::default()
.fg(crate::gradient::rgb_color(crate::gradient::PANEL_TITLE_FG))
.add_modifier(Modifier::BOLD)
} else {
Style::default()
.fg(cx.theme.ui(Color::White))
.bg(cx.theme.ui(Color::Rgb(0x1e, 0x3a, 0x6e)))
.add_modifier(Modifier::BOLD)
};
let block = crate::widgets::surface::view_block(cx.theme, true, block_style);
let inner = block.inner(area);
block.render(area, buf);
let stamp_title = |buf: &mut Buffer| {
if area.height > 0 && area.width > 2 {
buf.set_stringn(
area.x + 1,
area.y,
label,
usize::from(area.width - 2),
title_style,
);
}
};
stamp_title(buf);
// Black theme: replace the solid focus border with the orange→green
// gradient (matching the welcome activity box), then re-stamp the
// title the gradient top edge just overwrote. Croft Loch marks focus
// with the accent lip, which starts one cell past the title's own
// padding: the title keeps a breath of space on both sides and the
// lip's accent end sits right beside the name of the focused view.
if tones.is_some() {
if cx.focused {
let lip_x = area
.x
.saturating_add(1)
.saturating_add(u16::try_from(label_width).unwrap_or(u16::MAX))
.min(area.right());
crate::widgets::surface::paint_focus_rule(
buf,
Rect {
x: lip_x,
width: area.right() - lip_x,
..area
},
cx.theme,
);
}
} else if crate::widgets::surface::paint_view_focus(
buf,
area,
cx.theme,
cx.focused,
cx.theme.gradient(),
) {
stamp_title(buf);
}
// The New File / New Folder / Refresh / Collapse toolbar is painted
// later, on the root folder row. The "Views and More Actions" (⋯)
// button sits here on the EXPLORER title line, mirroring VS Code's
// view-container header. Reset the hit-test rects each frame so a
// hidden affordance never registers stale clicks.
self.header_new_file_btn = Rect::default();
self.header_new_folder_btn = Rect::default();
self.header_refresh_btn = Rect::default();
self.header_collapse_btn = Rect::default();
self.header_views_btn = Rect::default();
self.last_area = area;
self.last_scrollbar = Rect::default();
// Paint the ⋯ button at the right end of the title border row. It is
// always available (not focus-gated) so the view-toggle menu is one
// click away even when the tree is unfocused, like VS Code. It wears
// the shared header-action pill — a space-padded `" ⋯ "` inset one cell
// from the rounded corner — so it matches the terminal `+` button
// exactly instead of jamming a naked glyph against the corner.
// Only when the pill clears the (translated) title, which starts one
// cell in from the corner: `x + 1 + label_width <= x + width - 4`.
if usize::from(area.width) >= label_width + 5
&& let Some(rect) = crate::widgets::header_pill::render_action(
buf,
area.x + area.width - 1,
area.y,
crate::widgets::header_pill::MORE_LABEL,
cx.theme,
cx.pointer,
)
{
self.header_views_btn = rect;
}
self.last_inner = inner;
let visible_height = inner.height as usize;
if visible_height == 0 {
self.drawn.clear();
self.sticky.clear();
return;
}
// Every node is visible in tree order (`0..total`), so positions and
// node indices coincide without building a `Vec<usize>` per frame.
let total = self.nodes.len();
let sel_pos = if self.selected < total {
self.selected
} else {
0
};
let mut scroll_pos = if self.scroll < total { self.scroll } else { 0 };
if sel_pos < scroll_pos {
scroll_pos = sel_pos;
} else if sel_pos >= scroll_pos + visible_height {
scroll_pos = sel_pos + 1 - visible_height;
}
// Persist the node index at the top of the viewport so the next frame's
// mapping is stable.
self.scroll = if scroll_pos < total { scroll_pos } else { 0 };
let scrollbar_area = Rect {
x: inner.x + inner.width.saturating_sub(1),
y: inner.y,
width: u16::from(inner.width > 0),
height: inner.height,
};
let scrollbar_metrics =
scrollbar::vertical_metrics(scrollbar_area, total, visible_height, scroll_pos);
if let Some(metrics) = scrollbar_metrics {
self.last_scrollbar = metrics.area;
}
let row_width = inner
.width
.saturating_sub(u16::from(scrollbar_metrics.is_some()));
let pointer = cx.pointer;
let end = (scroll_pos + visible_height).min(total);
self.drawn
.record_borrowed((scroll_pos..end).map(|i| (i, self.nodes[i].path.as_path())));
// Croft Loch's indent guides: the guide of the selected row's parent
// folder is drawn a step brighter down that folder's children (VS
// Code's active indent guide), as `(level, first, end)` node indices.
let active_guide = tones.and_then(|_| self.active_indent_guide());
// Croft Loch's quiet layer: chevrons, the rolled-up dot, the
// root badge and note counts all in the muted ink.
let mark_fg = tones.map_or_else(|| cx.theme.ui(Color::Gray), |t| t.muted());
let drop_bg = cx.theme.ui(Color::Rgb(0x2c, 0x60, 0x2e));
for (row, idx) in (scroll_pos..end).enumerate() {
let node = &self.nodes[idx];
let is_selected = idx == self.selected;
let is_marked = self.marked.contains(&node.path);
let is_drop_target = self.drag_target == Some(idx);
let y = inner.y + row as u16;
let row_rect = Rect {
x: inner.x,
y,
width: row_width,
height: 1,
};
if let Some(t) = tones {
// Croft Loch: the one selected-row anatomy, the band with the
// accent bar standing in the pane's left padding column so
// the names keep their column; greyed while another pane has
// the keyboard. Marked rows wear a softer tint of the band,
// hover a neutral lift.
if is_drop_target {
buf.set_style(row_rect, Style::default().bg(drop_bg));
} else if is_selected {
crate::widgets::surface::paint_selected_row(
buf,
Rect {
x: area.x,
width: row_width.saturating_add(inner.x - area.x),
..row_rect
},
t,
t.chrome,
cx.focused,
);
} else if is_marked {
let tint = crate::gradient::lerp_rgb(t.chrome, t.accent, 0.12);
buf.set_style(
row_rect,
Style::default().bg(crate::gradient::rgb_color(tint)),
);
} else if let Some(bg) =
crate::widgets::hover::row_hover_bg(row_rect, pointer, cx.theme)
{
buf.set_style(row_rect, Style::default().bg(bg));
}
} else {
// Black theme: the selected/marked rows wear the brand's
// muted dark-teal fill (identical to the right-click menu's
// selection), replacing the legacy bright-blue accent. Croft
// Dark keeps the historical blue. The drop target stays
// green either way.
let (sel_bg, mark_bg) = if cx.theme.gradient() {
(
crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG),
cx.theme.ui(Color::Rgb(0x18, 0x35, 0x32)),
)
} else {
(
cx.theme.ui(Color::Rgb(0x09, 0x4d, 0x77)),
cx.theme.ui(Color::Rgb(0x07, 0x33, 0x55)),
)
};
let line_style = if is_drop_target {
Style::default().bg(drop_bg)
} else if is_selected {
Style::default().bg(sel_bg)
} else if is_marked {
Style::default().bg(mark_bg)
} else if let Some(bg) =
crate::widgets::hover::row_hover_bg(row_rect, pointer, cx.theme)
{
Style::default().bg(bg)
} else {
Style::default()
};
buf.set_style(row_rect, line_style);
}
// A changed row's status sits at the right edge (#1513): the
// file's letter, or a dot on a folder with changes inside. The
// name is clipped short of it so a long name never covers it.
let badge = self
.git_decoration(&node.path)
.filter(|_| row_width >= 8 && !self.is_ignored(&node.path));
let text_width = if badge.is_some() {
row_width - 3
} else {
row_width
};
let mut painter = RowPainter::new(buf, inner.x, y, text_width);
match tones {
// Croft Loch: a thin guide under each ancestor folder's
// chevron, in the cells the indent already takes. The
// workspace root's level gets none: every row is under it,
// so its guide would only be a second left edge.
Some(t) if node.depth >= 2 => {
painter.put(" ", Style::default());
for level in 1..node.depth {
let active = active_guide.is_some_and(|(l, first, end)| {
l == level && (first..end).contains(&idx)
});
painter.put(
"\u{258f} ",
Style::default().fg(if active { t.faint() } else { t.hairline() }),
);
}
}
_ => {
for _ in 0..node.depth {
painter.put(" ", Style::default());
}
}
}
let name = node.label_cow();
// Git-ignored and changed rows colour the *name* only — icons
// keep their color, matching VS Code's git decorations.
let name_fg = self.name_fg(cx, &node.path);
if node.is_dir {
let chev = crate::widgets::surface::chevron(cx.theme, node.expanded);
let icon = if node.expanded {
icons::FOLDER_OPEN
} else {
icons::FOLDER_CLOSED
};
let chev_style = Style::default().fg(mark_fg);
painter.put_char(chev, chev_style);
painter.put(" ", chev_style);
let icon_style = Style::default().fg(self.folder_icon_fg(cx, icon.color));
painter.put_char(icon.glyph, icon_style);
painter.put(" ", icon_style);
// Bold marks a folder on a boxed theme. Croft Loch keeps
// weight for the workspace root alone, so the tree reads as
// one calm list under a single heading.
let base = if tones.is_some() && node.depth > 0 {
Style::default().fg(name_fg)
} else {
Style::default().fg(name_fg).add_modifier(Modifier::BOLD)
};
painter.put(&name, base);
// A collapsed folder hiding an unreviewed agent write shows a
// dimmer copy of the file dot (#345), so the lane's changes
// stay findable without expanding every folder. Only while
// collapsed: expanded, the file's own dot is on screen.
if !node.expanded && self.hides_agent_touched(&node.path) {
let dot_style = Style::default().fg(mark_fg);
painter.put(" ", dot_style);
painter.put_char(AGENT_DOT, dot_style);
}
// A lane root wears its branch and its agent after the name
// (#348), dim so the folder name stays the thing the eye
// lands on; trailing for the same reason as the agent dot.
if node.depth == 0
&& let Some(badge) = self.root_badges.get(&node.path)
{
// `Gray`, the dim the theme maps for light grounds;
// `DarkGray` is a named colour `Theme::ui` passes through.
let badge_style = Style::default().fg(mark_fg);
painter.put(" ", badge_style);
painter.put(badge, badge_style);
}
} else {
let suffix = borrowed_suffix(&node.path, &name);
let icon = icons::for_path(&name, &suffix);
painter.put(" ", Style::default());
let icon_style = Style::default().fg(cx.theme.ui(icon.color));
painter.put_char(icon.glyph, icon_style);
painter.put(" ", icon_style);
painter.put(&name, Style::default().fg(name_fg));
// The agent-lane dot (#345), AFTER the name rather than
// before it: a leading marker would shift every name in the
// tree by a column as writes land, so the eye loses the
// alignment it reads the tree by. Trailing, it appears and
// clears without moving anything.
if self.is_agent_touched(&node.path) {
// Croft Loch takes its own palette's yellow rather than
// the named colour, which follows the host terminal.
let dot = match tones {
Some(_) => crate::gradient::rgb_color(cx.theme.ansi()[3]),
None => cx.theme.ui(Color::Yellow),
};
let dot_style = Style::default().fg(dot);
painter.put(" ", dot_style);
painter.put_char(AGENT_DOT, dot_style);
}
if let Some(&n) = self.note_counts.get(&node.path) {
let note_style = Style::default().fg(mark_fg);
painter.put(" \u{270e}", note_style);
painter.put_usize(n, note_style);
}
}
if let Some(dec) = badge {
let glyph = if node.is_dir {
'\u{25cf}'
} else {
dec.letter()
};
let mut utf8 = [0u8; 4];
buf.set_string(
inner.x + row_width - 2,
y,
glyph.encode_utf8(&mut utf8),
Style::default().fg(cx.theme.git_decoration_fg(dec)),
);
}
}
// Explorer header toolbar: New File / New Folder / Refresh / Collapse
// Folders, right-aligned on the root folder row (the "croft" header),
// mirroring VS Code's view actions, which live on the workspace-folder
// header rather than the EXPLORER title. Revealed only while the
// Explorer pane is focused and the root row sits at the top of the
// viewport; hidden otherwise (VS Code's hover/focus reveal). Painted
// after the rows so the pills win over the root row's text/fill.
if cx.focused && self.scroll == 0 && !self.nodes.is_empty() {
use crate::widgets::header_pill;
const GLYPHS: [char; 4] = [
header_pill::NEW_FILE_GLYPH,
header_pill::NEW_FOLDER_GLYPH,
header_pill::REFRESH_GLYPH,
header_pill::COLLAPSE_ALL_GLYPH,
];
// Each pill is a single-cell glyph; `step` adds a one-cell gap so
// the chips read as four distinct buttons. `right_pad` keeps the
// block clear of the scrollbar / right border.
let step: u16 = 2;
let count = GLYPHS.len() as u16;
let block_w = count * step - 1;
let right_pad: u16 = 1;
// Approximate the root label footprint (chevron + space + folder
// icon + space + name) so a narrow panel withholds the toolbar
// rather than overprinting the folder name.
let root_name = self.nodes[0]
.path
.file_name()
.map(|n| n.to_string_lossy().chars().count())
.unwrap_or(0) as u16;
let label_w = 4 + root_name;
if row_width > label_w + block_w + right_pad + 2 {
let start_x = inner.x + row_width - right_pad - block_w;
let y = inner.y;
for (i, &glyph) in GLYPHS.iter().enumerate() {
let x = start_x + i as u16 * step;
let rect = Rect {
x,
y,
width: step,
height: 1,
};
let hovered = crate::widgets::hover::contains(rect, cx.pointer);
header_pill::render(buf, x, y, glyph, cx.theme, hovered);
match i {
0 => self.header_new_file_btn = rect,
1 => self.header_new_folder_btn = rect,
2 => self.header_refresh_btn = rect,
_ => self.header_collapse_btn = rect,
}
}
}
}
// Sticky band (#117): pin the off-screen ancestor directories of the
// top visible row, overpainting the topmost content rows — the
// editor sticky band's model, including its guard: never cover the
// selected row (the scroll clamp above keeps it on screen, so its
// band-relative row bounds the band's height).
if scroll_pos > 0 {
const STICKY_MAX: usize = 3;
let chain = sticky_ancestors(&self.nodes, scroll_pos, STICKY_MAX);
let sel_row = sel_pos.saturating_sub(scroll_pos);
let band = chain
.len()
.min(sel_row)
.min(visible_height.saturating_sub(1));
let shown = &chain[chain.len() - band..];
// Croft Loch lifts the band onto the raised tone: the theme's
// sticky tint is a hair off the chrome and vanished against it.
let bg = tones.map_or_else(|| cx.theme.sticky_scroll_bg(), |t| t.raised());
for (i, &aidx) in shown.iter().enumerate() {
let y = inner.y + i as u16;
for x in inner.x..inner.x + row_width {
buf[(x, y)].set_symbol(" ");
buf[(x, y)].set_style(Style::default().bg(bg));
}
let node = &self.nodes[aidx];
let name = node.label_cow();
let name_fg = self.name_fg(cx, &node.path);
let name_style = if tones.is_some() && node.depth > 0 {
Style::default().fg(name_fg).bg(bg)
} else {
Style::default()
.fg(name_fg)
.bg(bg)
.add_modifier(Modifier::BOLD)
};
let mut painter = RowPainter::new(buf, inner.x, y, row_width);
for _ in 0..node.depth {
painter.put(" ", Style::default());
}
let chev_style = Style::default().fg(mark_fg).bg(bg);
painter.put_char(crate::widgets::surface::chevron(cx.theme, true), chev_style);
painter.put(" ", chev_style);
let icon_style = Style::default()
.fg(self.folder_icon_fg(cx, icons::FOLDER_OPEN.color))
.bg(bg);
painter.put_char(icons::FOLDER_OPEN.glyph, icon_style);
painter.put(" ", icon_style);
painter.put(&name, name_style);
}
self.sticky.record_borrowed(
shown
.iter()
.map(|&aidx| (aidx, self.nodes[aidx].path.as_path())),
);
} else {
self.sticky.clear();
}
if let Some(metrics) = scrollbar_metrics {
scrollbar::render_vertical_on_chrome(buf, metrics, cx.focused, cx.theme);
}
}
}