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
//! Color theme for the IDE chrome.
//!
//! croft mimics VS Code, where the user picks a "Color Theme". The first-party
//! themes (Croft Black `#000000` and Croft Dark `#1e222e`) ship as data in a
//! bundled extension manifest (`assets/extensions/themes/extension.toml`, baked
//! into the binary), and a third party adds a theme by dropping another
//! `[[themes]]` block into `~/.config/croft/extensions/`. A `Theme` is just the
//! parsed palette; [`Theme::BLACK`] is a const fallback so croft is never
//! themeless even if every manifest is missing or unparseable.
//!
//! The active theme drives every surface that paints an explicit background
//! instead of inheriting the iTerm2 session background: the editor canvases,
//! the baked activity-bar icons, the welcome hero raster, and the `SetColors`
//! sequence. Surfaces that paint `Color::Reset` inherit the session background,
//! so flipping `SetColors` recolors them for free.
use std::sync::OnceLock;
use ratatui::style::Color;
/// VS Code's default dark terminal ANSI palette (the `terminal.ansi*` colors
/// of the Dark+ theme): black, red, green, yellow, blue, magenta, cyan,
/// white, then the bright variants. The base for every croft theme that does
/// not override it with a 16-entry `ansi` array in its `[[themes]]` manifest
/// block, so terminal panes render the same on every host terminal instead
/// of inheriting whatever palette the host happens to use.
pub const VSCODE_ANSI: [(u8, u8, u8); 16] = [
(0x00, 0x00, 0x00),
(0xcd, 0x31, 0x31),
(0x0d, 0xbc, 0x79),
(0xe5, 0xe5, 0x10),
(0x24, 0x72, 0xc8),
(0xbc, 0x3f, 0xbc),
(0x11, 0xa8, 0xcd),
(0xe5, 0xe5, 0xe5),
(0x66, 0x66, 0x66),
(0xf1, 0x4c, 0x4c),
(0x23, 0xd1, 0x8b),
(0xf5, 0xf5, 0x43),
(0x3b, 0x8e, 0xea),
(0xd6, 0x70, 0xd6),
(0x29, 0xb8, 0xdb),
(0xe5, 0xe5, 0xe5),
];
/// The eight syntax-highlight colors a theme paints code with, shared by the
/// tree-sitter highlighter and the LSP semantic-token overlay (both funnel
/// through `highlight::style_for_name`). Grouped by role the way editor themes
/// publish their palettes: comment, keyword, string, constant (numbers /
/// booleans / consts / parameters), function (calls / methods / properties),
/// type (types / namespaces), tag (attributes / JSX tags / builtins), and the
/// default foreground for plain identifiers, operators, and punctuation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SyntaxPalette {
pub comment: (u8, u8, u8),
pub keyword: (u8, u8, u8),
pub string: (u8, u8, u8),
pub constant: (u8, u8, u8),
pub function: (u8, u8, u8),
pub type_: (u8, u8, u8),
pub tag: (u8, u8, u8),
pub fg: (u8, u8, u8),
}
impl SyntaxPalette {
/// The historical Base16-Ocean-Dark palette, inlined from the old hardcoded
/// `style_for_name` arms. The default for Croft Black / Croft Dark (Blue)
/// and for any theme that omits per-color `syn_*` fields, so no theme is
/// ever code-colorless and the built-ins render byte-for-byte as before.
pub const BASE16: SyntaxPalette = SyntaxPalette {
comment: (0x65, 0x73, 0x7e),
keyword: (0xb4, 0x8e, 0xad),
string: (0xa3, 0xbe, 0x8c),
constant: (0xd0, 0x87, 0x70),
function: (0x8f, 0xa1, 0xb3),
type_: (0xeb, 0xcb, 0x8b),
tag: (0xbf, 0x61, 0x6a),
fg: (0xc0, 0xc5, 0xce),
};
}
/// A complete IDE color palette. `Copy` and cheap to pass by value: the strings
/// are interned to `&'static` when loaded from a manifest, and the colors are
/// plain bytes. Equality is by value, so a registry-loaded theme compares equal
/// to the matching [`Theme::BLACK`]/[`Theme::DARK_BLUE`] const.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Theme {
id: &'static str,
label: &'static str,
bg: (u8, u8, u8),
accent: (u8, u8, u8),
selection: (u8, u8, u8),
search: (u8, u8, u8),
button: (u8, u8, u8),
gradient: bool,
osk_key: (u8, u8, u8),
osk_special: (u8, u8, u8),
osk_armed: (u8, u8, u8),
/// The 16 ANSI terminal colors (black..bright white) panes render with.
ansi: [(u8, u8, u8); 16],
/// The code-highlight palette (defaults to [`SyntaxPalette::BASE16`]).
syntax: SyntaxPalette,
/// Tab-strip chrome (the editor tab bar). Explicit for the two built-ins
/// (byte-for-byte the historical constants); derived from the palette in
/// [`Theme::from_decl`] for themes that don't declare them.
tab_strip: (u8, u8, u8),
tab_inactive: (u8, u8, u8),
tab_active: (u8, u8, u8),
tab_hover: (u8, u8, u8),
tab_pill: (u8, u8, u8),
}
impl Theme {
/// Croft Black — the OLED-friendly default, and the const fallback used when
/// no manifest theme is available. Values mirror the bundled manifest (the
/// gradient brand consts from `src/gradient.rs` inlined).
pub const BLACK: Theme = Theme {
id: "black",
label: "Croft Black",
bg: (0x00, 0x00, 0x00),
accent: (0x4f, 0xb1, 0xa6),
selection: (0x26, 0x4f, 0x4a),
search: (0x14, 0x14, 0x14),
button: (0x0e, 0x7e, 0x76),
gradient: true,
osk_key: (0x20, 0x24, 0x2b),
osk_special: (0x14, 0x16, 0x1b),
osk_armed: (0x0e, 0x7e, 0x76),
ansi: VSCODE_ANSI,
syntax: SyntaxPalette::BASE16,
tab_strip: (0x1f, 0x24, 0x36),
tab_inactive: (0x2a, 0x2f, 0x3e),
// The teal selection fill (gradient::POPUP_SEL_BG) as the active chip,
// with the brighter teal hover pill; both inlined from the old
// brand-branch constants in src/widgets/editor.rs.
tab_active: (0x26, 0x4f, 0x4a),
tab_hover: (0x2f, 0x35, 0x50),
tab_pill: (0x3c, 0x8a, 0x7e),
};
/// Croft Dark (Blue) — the historical look. Const mirror of the manifest,
/// for call sites and tests that name it directly.
pub const DARK_BLUE: Theme = Theme {
id: "dark-blue",
label: "Croft Dark (Blue)",
bg: (0x1e, 0x22, 0x2e),
accent: (0x4e, 0x9a, 0xff),
selection: (0x09, 0x4d, 0x77),
search: (0x23, 0x27, 0x2f),
button: (0x09, 0x67, 0xb8),
gradient: false,
osk_key: (0x3a, 0x40, 0x52),
osk_special: (0x2c, 0x31, 0x40),
osk_armed: (0x00, 0x7a, 0xcc),
ansi: VSCODE_ANSI,
syntax: SyntaxPalette::BASE16,
// The historical navy chip look.
tab_strip: (0x1f, 0x24, 0x36),
tab_inactive: (0x2a, 0x2f, 0x3e),
tab_active: (0x1e, 0x3a, 0x6e),
tab_hover: (0x34, 0x50, 0x7f),
tab_pill: (0x4e, 0x9a, 0xff),
};
/// Every available theme in pick-list order, loaded once from the bundled +
/// user extension manifests, skipping any whose contributing extension is
/// disabled in the Extensions panel (read once at first access, so a toggle
/// takes effect on the next launch — matching the other extension toggles).
/// The gear menu's theme picker renders this.
pub fn all() -> &'static [Theme] {
static REGISTRY: OnceLock<Vec<Theme>> = OnceLock::new();
REGISTRY.get_or_init(|| {
let mut sources: Vec<String> = crate::lsp::manifest::BUNDLED_MANIFESTS
.iter()
.map(|s| s.to_string())
.collect();
// Under test the registry must be hermetic: reading the developer's
// real ~/.config/croft (user extensions + disabled set) made this
// process-wide cache depend on machine state and on which test
// touched it first. Tests see only the bundled themes.
let disabled = if cfg!(test) {
std::collections::BTreeSet::new()
} else {
sources.extend(crate::lsp::manifest::read_extension_sources(
&crate::lsp::manifest::user_extensions_dir(),
));
crate::prefs::Prefs::load_or_default().disabled_extensions
};
let themes: Vec<Theme> = sources
.iter()
.filter_map(|s| crate::lsp::manifest::parse(s).ok())
.filter(|m| !disabled.contains(&m.id))
.flat_map(|m| m.themes)
.map(|d| Theme::from_decl(&d))
.collect();
// Never empty: fall back to the baked-in const so the picker and
// `default`/`from_id` always have something even if the themes
// extension is disabled.
if themes.is_empty() {
vec![Theme::BLACK]
} else {
themes
}
})
}
/// Build a theme from a parsed manifest entry, interning its strings and
/// parsing its `#rrggbb` colors (a bad color degrades to black, not a crash).
fn from_decl(d: &crate::lsp::manifest::ThemeDecl) -> Theme {
let accent = parse_hex(&d.accent);
let selection = parse_hex(&d.selection);
let search = parse_hex(&d.search);
// Tab-strip chrome defaults, for themes that declare no explicit tab
// colors: the strip wears the theme's secondary panel fill, an
// inactive tab lifts a hair off it, the active tab wears the
// selection fill, hover tints the strip toward the accent, and the
// close pill is the accent itself.
let tab_strip = hex_or(&d.tab_strip, search);
Theme {
id: intern(&d.id),
label: intern(&d.label),
bg: parse_hex(&d.background),
accent,
selection,
search,
button: parse_hex(&d.button),
tab_strip,
tab_inactive: hex_or(&d.tab_inactive, blend((0xff, 0xff, 0xff), 0.06, tab_strip)),
tab_active: hex_or(&d.tab_active, selection),
tab_hover: hex_or(&d.tab_hover, blend(accent, 0.25, tab_strip)),
tab_pill: hex_or(&d.tab_close_pill, accent),
gradient: d.gradient,
osk_key: parse_hex(&d.osk_key),
osk_special: parse_hex(&d.osk_special),
osk_armed: parse_hex(&d.osk_armed),
ansi: if d.ansi.len() == 16 {
let mut p = VSCODE_ANSI;
for (slot, hex) in p.iter_mut().zip(&d.ansi) {
*slot = parse_hex(hex);
}
p
} else {
VSCODE_ANSI
},
syntax: SyntaxPalette {
// Each syn_* is optional: an empty string keeps the Base16
// default for that role, so a theme can override just a few.
comment: hex_or(&d.syn_comment, SyntaxPalette::BASE16.comment),
keyword: hex_or(&d.syn_keyword, SyntaxPalette::BASE16.keyword),
string: hex_or(&d.syn_string, SyntaxPalette::BASE16.string),
constant: hex_or(&d.syn_constant, SyntaxPalette::BASE16.constant),
function: hex_or(&d.syn_function, SyntaxPalette::BASE16.function),
type_: hex_or(&d.syn_type, SyntaxPalette::BASE16.type_),
tag: hex_or(&d.syn_tag, SyntaxPalette::BASE16.tag),
fg: hex_or(&d.syn_fg, SyntaxPalette::BASE16.fg),
},
}
}
/// The default theme: Croft Black when present, else the first registered,
/// else the const fallback. Used for a fresh install and as the revert
/// target when the active theme's extension is disabled/removed.
pub fn default_theme() -> Theme {
let all = Theme::all();
all.iter()
.find(|t| t.id == "black")
.copied()
.or_else(|| all.first().copied())
.unwrap_or(Theme::BLACK)
}
/// Resolve a persisted `id` to a theme, falling back to the default for
/// anything unrecognized (forward/backward-compatible prefs).
pub fn from_id(id: &str) -> Self {
Theme::all()
.iter()
.find(|t| t.id == id)
.copied()
.unwrap_or_else(Theme::default_theme)
}
/// Stable on-disk identifier, persisted in the prefs file.
pub fn id(self) -> &'static str {
self.id
}
/// Human-facing label shown in the theme picker.
pub fn label(self) -> &'static str {
self.label
}
/// Whether this theme uses the gradient brand chrome (teal accents, the
/// focused-pane gradient border, popup gradients) vs the flat-accent look.
/// Replaces the old hardcoded `theme == Black` checks.
pub fn gradient(self) -> bool {
self.gradient
}
/// The 16 ANSI terminal colors (black..bright white) the theme's panes
/// render Named/Indexed 0-15 cell colors with.
pub fn ansi(self) -> [(u8, u8, u8); 16] {
self.ansi
}
/// The code-highlight palette. The app pushes this into the highlighter
/// (`highlight::set_syntax_palette`) on every theme switch so tree-sitter
/// and semantic-token colors follow the active theme.
pub fn syntax(self) -> SyntaxPalette {
self.syntax
}
/// Primary accent (selected-row text, active chrome).
pub fn accent(self) -> Color {
rgb(self.accent)
}
/// Primary accent as raw sRGB bytes, baked into the activity-bar change
/// badge's inline-image canvas (VS Code `activityBarBadge.background`).
pub fn accent_rgb(self) -> (u8, u8, u8) {
self.accent
}
/// Selected-row fill in lists/popups.
pub fn selection(self) -> Color {
rgb(self.selection)
}
/// Soft translucent accent fill behind an active toolbar control (VS Code's
/// lit-filter highlight): the theme accent blended over the panel
/// background so a non-default filter/toggle reads as lit without a heavy
/// opaque box.
pub fn accent_chip_bg(self) -> Color {
self.blend_over_bg(self.accent, 0.18)
}
/// Filter/search input fill.
pub fn search_bg(self) -> Color {
rgb(self.search)
}
/// Primary-button / lit-toggle fill.
pub fn button(self) -> Color {
rgb(self.button)
}
/// On-screen-keyboard normal key cap fill.
pub fn osk_key_bg(self) -> Color {
rgb(self.osk_key)
}
/// On-screen-keyboard special key cap fill.
pub fn osk_special_bg(self) -> Color {
rgb(self.osk_special)
}
/// On-screen-keyboard armed (held) key fill.
pub fn osk_armed_bg(self) -> Color {
rgb(self.osk_armed)
}
/// The editor/panel background as raw sRGB bytes. Used both as the
/// `SetColors` session fill and as the alpha-blend / canvas-fill target
/// behind the baked OSC-1337 images so they merge seamlessly.
pub fn editor_bg_rgb(self) -> (u8, u8, u8) {
self.bg
}
/// The editor/panel background as a ratatui color.
pub fn editor_bg(self) -> Color {
rgb(self.bg)
}
/// The selection-highlight background as raw sRGB bytes (minimap selection
/// band).
pub fn selection_rgb(self) -> (u8, u8, u8) {
self.selection
}
/// Alpha-composite `fg` over this theme's background at `alpha` (0.0..=1.0),
/// returning an opaque color. Terminal cells can't render real
/// transparency, so we pre-blend against the known per-theme background —
/// the trick that reproduces VS Code's translucent scrollbar slider.
fn blend_over_bg(self, fg: (u8, u8, u8), alpha: f32) -> Color {
let (br, bgc, bb) = self.bg;
let mix = |f: u8, b: u8| (f as f32 * alpha + b as f32 * (1.0 - alpha)).round() as u8;
Color::Rgb(mix(fg.0, br), mix(fg.1, bgc), mix(fg.2, bb))
}
/// Background box under a matched bracket pair. VS Code paints
/// `editorBracketMatch.border` (a subtle outline) which a terminal cell
/// can't carry, so a faint grey fill blended over each theme's background
/// stands in, staying legible on both dark themes.
pub fn bracket_match_bg(self) -> Color {
self.blend_over_bg((0x9a, 0x9a, 0x9a), 0.35)
}
/// Depth-cycled foregrounds for bracket-pair colorization (VS Code
/// `editorBracketHighlight.foreground1..3`). The dark defaults are VS
/// Code's gold / orchid / blue; a light-background theme gets VS Code
/// Light's blue / green / brown, picked by the background's luminance so
/// manifest themes inherit a legible set either way.
pub fn bracket_pair_color(self, depth: usize) -> Color {
let (r, g, b) = self.bg;
let luma = 0.299 * f32::from(r) + 0.587 * f32::from(g) + 0.114 * f32::from(b);
let cycle: [(u8, u8, u8); 3] = if luma > 128.0 {
[(0x04, 0x31, 0xfa), (0x31, 0x93, 0x31), (0x7b, 0x38, 0x14)]
} else {
[(0xff, 0xd7, 0x00), (0xda, 0x70, 0xd6), (0x17, 0x9f, 0xff)]
};
let (cr, cg, cb) = cycle[depth % cycle.len()];
Color::Rgb(cr, cg, cb)
}
/// Foreground of an unmatched closing bracket (VS Code
/// `editorBracketHighlight.unexpectedBracket.foreground`).
pub fn bracket_unexpected_fg(self) -> Color {
Color::Rgb(0xff, 0x12, 0x12)
}
/// Foreground of rendered whitespace glyphs (VS Code
/// `editorWhitespace.foreground`): dimmer than the indent guides so an
/// "all" render stays readable, blended over each theme's background.
pub fn whitespace_fg(self) -> Color {
self.blend_over_bg((0x96, 0x96, 0x96), 0.30)
}
/// Foreground of an indentation guide (VS Code
/// `editorIndentGuide.background1`, #404040 on the dark default): a grey
/// blended over each theme's background so it stays a whisper above the
/// editor surface on dark and light themes alike.
pub fn indent_guide(self) -> Color {
self.blend_over_bg((0x88, 0x88, 0x88), 0.38)
}
/// Foreground of the active indentation guide — the guide of the block
/// containing the cursor (VS Code `editorIndentGuide.activeBackground1`,
/// #707070 on the dark default). Same grey, blended stronger.
pub fn indent_guide_active(self) -> Color {
self.blend_over_bg((0x88, 0x88, 0x88), 0.80)
}
/// Background tint under read occurrences of the symbol at the caret
/// (VS Code `editor.wordHighlightBackground`, #575757 at 72% alpha),
/// blended over each theme's background.
pub fn occurrence_bg(self) -> Color {
self.blend_over_bg((0x57, 0x57, 0x57), 0.72)
}
/// Background tint under write occurrences of the symbol at the caret
/// (VS Code `editor.wordHighlightStrongBackground`, #004972 at 72%): the
/// assignment site reads stronger than the uses.
pub fn occurrence_write_bg(self) -> Color {
self.blend_over_bg((0x00, 0x49, 0x72), 0.72)
}
/// Background of the pinned sticky-scroll header rows (VS Code
/// `editorStickyScroll.background`): the editor background lifted a hair so
/// the pinned scope headers read as a distinct band above the content.
pub fn sticky_scroll_bg(self) -> Color {
self.blend_over_bg((0xff, 0xff, 0xff), 0.06)
}
/// Git gutter bar for an added line (VS Code `editorGutter.addedBackground`,
/// a vivid green). The add/modify/delete decorations are semantic status
/// colours, identical across both dark themes and legible on either
/// background, so they are fixed here rather than carried per-theme.
pub fn git_added(self) -> Color {
Color::Rgb(0x2e, 0xa0, 0x43)
}
/// Git gutter bar for a modified line (VS Code `editorGutter.modifiedBackground`).
pub fn git_modified(self) -> Color {
Color::Rgb(0x0c, 0x7d, 0xc4)
}
/// Git gutter bar marking a deletion (VS Code `editorGutter.deletedBackground`).
pub fn git_deleted(self) -> Color {
Color::Rgb(0xf8, 0x51, 0x49)
}
/// The deletion/failure red as raw sRGB bytes, baked into the Testing
/// failing-count badge canvas. Same value as [`Theme::git_deleted`].
pub fn git_deleted_rgb(self) -> (u8, u8, u8) {
(0xf8, 0x51, 0x49)
}
/// Explorer name foreground for a git-ignored file or directory (VS Code
/// `gitDecoration.ignoredResourceForeground`, #8C8C8C on dark themes).
/// White at 55% over the theme background lands exactly on that grey for
/// the Black theme and stays a legible muted grey on every dark bg.
pub fn ignored_fg(self) -> Color {
self.blend_over_bg((0xff, 0xff, 0xff), 0.55)
}
/// The scrollbar track. VS Code paints no track, so we match the editor
/// background and the lane melts away on every theme.
pub fn scrollbar_track(self) -> Color {
self.editor_bg()
}
/// The scrollbar thumb when its pane is unfocused (VS Code's resting
/// `scrollbarSlider.background`, #797979 at 40% alpha).
pub fn scrollbar_thumb(self) -> Color {
self.blend_over_bg((0x79, 0x79, 0x79), 0.40)
}
/// The scrollbar thumb when its pane is focused (VS Code's brighter hover
/// value, #646464 at 70% alpha).
pub fn scrollbar_thumb_focused(self) -> Color {
self.blend_over_bg((0x64, 0x64, 0x64), 0.70)
}
/// The editor tab bar's background (VS Code
/// `editorGroupHeader.tabsBackground`), including the gap right of the
/// last tab.
pub fn tab_strip_bg(self) -> Color {
rgb(self.tab_strip)
}
/// An inactive tab's body fill.
pub fn tab_inactive_bg(self) -> Color {
rgb(self.tab_inactive)
}
/// The active tab's body fill.
pub fn tab_active_bg(self) -> Color {
rgb(self.tab_active)
}
/// An inactive tab's body while the pointer rests on it (VS Code
/// `tab.hoverBackground`); the active tab is already prominent and never
/// lifts.
pub fn tab_hover_bg(self) -> Color {
rgb(self.tab_hover)
}
/// The pill behind the close cross / pin glyph while the pointer is on
/// that cell (VS Code `toolbar.hoverBackground`). Must stay distinct from
/// [`Theme::tab_active_bg`] or the hover is invisible on the active tab.
pub fn tab_close_pill_bg(self) -> Color {
rgb(self.tab_pill)
}
}
impl Default for Theme {
fn default() -> Self {
Theme::BLACK
}
}
fn rgb(c: (u8, u8, u8)) -> Color {
Color::Rgb(c.0, c.1, c.2)
}
/// Alpha-composite `fg` over `bg` at `alpha`, as raw bytes. The tuple-level
/// sibling of [`Theme::blend_over_bg`], for derivations at parse time.
fn blend(fg: (u8, u8, u8), alpha: f32, bg: (u8, u8, u8)) -> (u8, u8, u8) {
let mix = |f: u8, b: u8| (f as f32 * alpha + b as f32 * (1.0 - alpha)).round() as u8;
(mix(fg.0, bg.0), mix(fg.1, bg.1), mix(fg.2, bg.2))
}
/// Parse `#rrggbb` (case-insensitive); anything malformed degrades to black so
/// a typo'd theme manifest can't crash croft.
fn parse_hex(s: &str) -> (u8, u8, u8) {
let h = s.strip_prefix('#').unwrap_or(s);
if h.len() == 6
&& let (Ok(r), Ok(g), Ok(b)) = (
u8::from_str_radix(&h[0..2], 16),
u8::from_str_radix(&h[2..4], 16),
u8::from_str_radix(&h[4..6], 16),
)
{
return (r, g, b);
}
(0, 0, 0)
}
/// Parse an optional `#rrggbb`, falling back to `default` when the string is
/// empty (the field was omitted in the manifest) — lets a theme override only
/// the syntax colors it cares about and inherit Base16 for the rest.
fn hex_or(s: &str, default: (u8, u8, u8)) -> (u8, u8, u8) {
if s.is_empty() { default } else { parse_hex(s) }
}
/// Leak a manifest string to `&'static` (bounded by the installed theme count,
/// loaded once). Same rationale as the LSP manifest interner.
fn intern(s: &str) -> &'static str {
Box::leak(s.to_string().into_boxed_str())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dark_blue_keeps_the_historical_background() {
assert_eq!(Theme::DARK_BLUE.editor_bg_rgb(), (0x1e, 0x22, 0x2e));
}
#[test]
fn black_is_true_black() {
assert_eq!(Theme::BLACK.editor_bg_rgb(), (0x00, 0x00, 0x00));
}
#[test]
fn parse_hex_reads_rrggbb_and_degrades_to_black() {
assert_eq!(parse_hex("#1e222e"), (0x1e, 0x22, 0x2e));
assert_eq!(parse_hex("000000"), (0, 0, 0));
assert_eq!(parse_hex("nonsense"), (0, 0, 0));
}
#[test]
fn bundled_manifest_themes_match_the_const_palettes() {
// The data move must be byte-for-byte: the baked-in manifest reproduces
// the historical const palettes exactly.
assert_eq!(
Theme::from_id("black"),
Theme::BLACK,
"manifest == const BLACK"
);
assert_eq!(
Theme::from_id("dark-blue"),
Theme::DARK_BLUE,
"manifest == const DARK_BLUE"
);
}
#[test]
fn built_in_tab_chrome_is_byte_identical_to_the_legacy_constants() {
// The tab strip's colors were hardcoded constants in
// src/widgets/editor.rs before they became theme data; the two
// built-ins must keep rendering exactly as before the move.
let b = Theme::BLACK;
assert_eq!(b.tab_strip_bg(), Color::Rgb(0x1f, 0x24, 0x36));
assert_eq!(b.tab_inactive_bg(), Color::Rgb(0x2a, 0x2f, 0x3e));
assert_eq!(b.tab_active_bg(), Color::Rgb(0x26, 0x4f, 0x4a));
assert_eq!(b.tab_hover_bg(), Color::Rgb(0x2f, 0x35, 0x50));
assert_eq!(b.tab_close_pill_bg(), Color::Rgb(0x3c, 0x8a, 0x7e));
let d = Theme::DARK_BLUE;
assert_eq!(d.tab_strip_bg(), Color::Rgb(0x1f, 0x24, 0x36));
assert_eq!(d.tab_inactive_bg(), Color::Rgb(0x2a, 0x2f, 0x3e));
assert_eq!(d.tab_active_bg(), Color::Rgb(0x1e, 0x3a, 0x6e));
assert_eq!(d.tab_hover_bg(), Color::Rgb(0x34, 0x50, 0x7f));
assert_eq!(d.tab_close_pill_bg(), Color::Rgb(0x4e, 0x9a, 0xff));
}
#[test]
fn manifest_themes_without_tab_colors_derive_them_from_the_palette() {
// A theme that declares no tab_* fields still gets full tab chrome:
// strip from its secondary panel fill, active tab from its selection,
// close pill from its accent. No theme can fall back to another
// theme's (navy) chrome.
let s = Theme::from_id("solarized-dark");
assert_eq!(s.id(), "solarized-dark");
assert_eq!(s.tab_strip_bg(), s.search_bg());
assert_eq!(s.tab_active_bg(), s.selection());
assert_eq!(s.tab_close_pill_bg(), s.accent());
assert_ne!(
s.tab_strip_bg(),
Theme::DARK_BLUE.tab_strip_bg(),
"solarized must not wear the built-ins' navy strip"
);
}
#[test]
fn registry_lists_both_first_party_themes() {
let ids: Vec<&str> = Theme::all().iter().map(|t| t.id()).collect();
assert!(ids.contains(&"black"));
assert!(ids.contains(&"dark-blue"));
}
#[test]
fn editor_inspired_themes_all_load() {
// Every bundled [[themes]] block must parse (a bad hex or missing field
// silently drops the theme via `.ok()`), so pin the full roster.
let ids: Vec<&str> = Theme::all().iter().map(|t| t.id()).collect();
for id in [
"one-dark-pro",
"dracula",
"monokai",
"nord",
"gruvbox-dark",
"tokyo-night",
"catppuccin-mocha",
"solarized-dark",
"github-dark",
"darcula",
] {
assert!(ids.contains(&id), "theme `{id}` missing from registry");
}
// These are imports of external editor palettes, not croft's own brand:
// they must use the flat-accent look, never the teal gradient chrome.
for t in Theme::all() {
if t.id() != "black" {
assert!(!t.gradient(), "`{}` must not use gradient chrome", t.id());
}
}
}
#[test]
fn imported_themes_carry_their_own_syntax_palette() {
// The whole point of the fix: switching theme must recolor code. Pin a
// couple of signature token colors so a regression to Base16 is caught.
let one_dark = Theme::from_id("one-dark-pro");
assert_eq!(one_dark.syntax().keyword, (0xc6, 0x78, 0xdd)); // purple
assert_ne!(one_dark.syntax(), SyntaxPalette::BASE16);
let dracula = Theme::from_id("dracula");
assert_eq!(dracula.syntax().string, (0xf1, 0xfa, 0x8c)); // yellow
assert_ne!(dracula.syntax().keyword, one_dark.syntax().keyword);
// Built-ins keep the historical Base16 code colors (no regression).
assert_eq!(Theme::BLACK.syntax(), SyntaxPalette::BASE16);
assert_eq!(Theme::DARK_BLUE.syntax(), SyntaxPalette::BASE16);
}
#[test]
fn imported_themes_carry_their_own_ansi_terminal_palette() {
// The terminal palette must follow the theme too, not fall back to the
// shared VS Code default. Pin a couple of signature ANSI slots.
let dracula = Theme::from_id("dracula").ansi();
assert_eq!(dracula[5], (0xff, 0x79, 0xc6)); // magenta = Dracula pink
assert_ne!(dracula, VSCODE_ANSI);
let gruvbox = Theme::from_id("gruvbox-dark").ansi();
assert_eq!(gruvbox[1], (0xcc, 0x24, 0x1d)); // red
assert_ne!(gruvbox, dracula);
// Built-ins keep the VS Code default ANSI (no regression).
assert_eq!(Theme::BLACK.ansi(), VSCODE_ANSI);
}
#[test]
fn omitted_syntax_fields_fall_back_to_base16() {
assert_eq!(hex_or("", (1, 2, 3)), (1, 2, 3));
assert_eq!(hex_or("#0a0b0c", (1, 2, 3)), (0x0a, 0x0b, 0x0c));
}
#[test]
fn id_round_trips_through_from_id() {
for theme in Theme::all() {
assert_eq!(Theme::from_id(theme.id()), *theme);
}
}
#[test]
fn unknown_id_falls_back_to_default() {
assert_eq!(Theme::from_id("chartreuse"), Theme::default_theme());
}
#[test]
fn default_is_black() {
assert_eq!(Theme::default_theme(), Theme::BLACK);
assert_eq!(Theme::default(), Theme::BLACK);
}
#[test]
fn black_uses_gradient_brand_chrome_dark_blue_does_not() {
assert!(Theme::BLACK.gradient());
assert!(!Theme::DARK_BLUE.gradient());
}
}