Skip to main content

strata_editor/
theme.rs

1//! CSS-variable-driven theming system (`--strata-editor-*`) for `<CodeEditorWidget>`.
2//!
3//! Every surface, gutter, selection, diagnostic, and syntax token color resolves through
4//! CSS custom properties (`var(--strata-editor-*, <fallback>)`) so host applications
5//! (such as Obsidian Dark / Daylight Light workspaces) can re-theme live without rebuilding
6//! editor state.
7
8use serde::{Deserialize, Serialize};
9use strata_primitives::color::Rgba;
10
11use crate::diagnostics::DiagnosticSeverity;
12use crate::syntax::TokenType;
13
14/// Complete theme definition for the Monaco-class code editor.
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct EditorTheme {
17    pub name: String,
18    pub is_dark: bool,
19
20    // Surface CSS values (typically `var(--strata-editor-*, <fallback>)`)
21    pub bg: String,
22    pub fg: String,
23    pub gutter_bg: String,
24    pub gutter_fg: String,
25    pub gutter_active_fg: String,
26    pub line_highlight: String,
27    pub selection_bg: String,
28    pub caret: String,
29    pub border: String,
30    pub panel_bg: String,
31    pub panel_border: String,
32    pub find_match_bg: String,
33    pub find_active_bg: String,
34    pub bracket_match_bg: String,
35    pub fold_badge_bg: String,
36    pub fold_badge_fg: String,
37    pub minimap_bg: String,
38    pub minimap_slider: String,
39
40    // Syntax token CSS values
41    pub tok_keyword: String,
42    pub tok_control: String,
43    pub tok_type: String,
44    pub tok_function: String,
45    pub tok_macro: String,
46    pub tok_identifier: String,
47    pub tok_parameter: String,
48    pub tok_property: String,
49    pub tok_string: String,
50    pub tok_string_escape: String,
51    pub tok_number: String,
52    pub tok_boolean: String,
53    pub tok_comment: String,
54    pub tok_doc_comment: String,
55    pub tok_operator: String,
56    pub tok_punctuation: String,
57    pub tok_tag: String,
58    pub tok_attribute: String,
59    pub tok_namespace: String,
60    pub tok_heading: String,
61    pub tok_link: String,
62    pub tok_variable: String,
63
64    // Diagnostic CSS values
65    pub diag_error: String,
66    pub diag_warning: String,
67    pub diag_info: String,
68    pub diag_hint: String,
69}
70
71impl Default for EditorTheme {
72    fn default() -> Self {
73        Self::css_variables_dark()
74    }
75}
76
77impl EditorTheme {
78    /// Default dark theme referencing `--strata-editor-*` CSS variables with dark fallbacks.
79    pub fn css_variables_dark() -> Self {
80        Self {
81            name: "strata-dark".to_string(),
82            is_dark: true,
83            bg: "var(--strata-editor-bg, #0d1016)".to_string(),
84            fg: "var(--strata-editor-fg, #e2e8f0)".to_string(),
85            gutter_bg: "var(--strata-editor-gutter-bg, #0b0e14)".to_string(),
86            gutter_fg: "var(--strata-editor-gutter-fg, #64748b)".to_string(),
87            gutter_active_fg: "var(--strata-editor-gutter-active-fg, #e2e8f0)".to_string(),
88            line_highlight: "var(--strata-editor-line-highlight, rgba(148, 163, 184, 0.08))"
89                .to_string(),
90            selection_bg: "var(--strata-editor-selection-bg, rgba(56, 189, 248, 0.25))".to_string(),
91            caret: "var(--strata-editor-caret, #38bdf8)".to_string(),
92            border: "var(--strata-editor-border, #1e293b)".to_string(),
93            panel_bg: "var(--strata-editor-panel-bg, #111827)".to_string(),
94            panel_border: "var(--strata-editor-panel-border, #334155)".to_string(),
95            find_match_bg: "var(--strata-editor-find-match-bg, rgba(251, 191, 36, 0.22))"
96                .to_string(),
97            find_active_bg: "var(--strata-editor-find-active-bg, rgba(245, 158, 11, 0.45))"
98                .to_string(),
99            bracket_match_bg: "var(--strata-editor-bracket-match-bg, rgba(99, 102, 241, 0.28))"
100                .to_string(),
101            fold_badge_bg: "var(--strata-editor-fold-badge-bg, rgba(51, 65, 85, 0.65))".to_string(),
102            fold_badge_fg: "var(--strata-editor-fold-badge-fg, #cbd5e1)".to_string(),
103            minimap_bg: "var(--strata-editor-minimap-bg, #090c11)".to_string(),
104            minimap_slider: "var(--strata-editor-minimap-slider, rgba(148, 163, 184, 0.16))"
105                .to_string(),
106            tok_keyword: "var(--strata-editor-tok-keyword, #c084fc)".to_string(),
107            tok_control: "var(--strata-editor-tok-control, #e879f9)".to_string(),
108            tok_type: "var(--strata-editor-tok-type, #fbbf24)".to_string(),
109            tok_function: "var(--strata-editor-tok-function, #60a5fa)".to_string(),
110            tok_macro: "var(--strata-editor-tok-macro, #a5b4fc)".to_string(),
111            tok_identifier: "var(--strata-editor-tok-identifier, #e2e8f0)".to_string(),
112            tok_parameter: "var(--strata-editor-tok-parameter, #fed7aa)".to_string(),
113            tok_property: "var(--strata-editor-tok-property, #7dd3fc)".to_string(),
114            tok_string: "var(--strata-editor-tok-string, #34d399)".to_string(),
115            tok_string_escape: "var(--strata-editor-tok-string-escape, #5eead4)".to_string(),
116            tok_number: "var(--strata-editor-tok-number, #fb923c)".to_string(),
117            tok_boolean: "var(--strata-editor-tok-boolean, #fcd34d)".to_string(),
118            tok_comment: "var(--strata-editor-tok-comment, #64748b)".to_string(),
119            tok_doc_comment: "var(--strata-editor-tok-doc-comment, #94a3b8)".to_string(),
120            tok_operator: "var(--strata-editor-tok-operator, #fb7185)".to_string(),
121            tok_punctuation: "var(--strata-editor-tok-punctuation, #94a3b8)".to_string(),
122            tok_tag: "var(--strata-editor-tok-tag, #22d3ee)".to_string(),
123            tok_attribute: "var(--strata-editor-tok-attribute, #7dd3fc)".to_string(),
124            tok_namespace: "var(--strata-editor-tok-namespace, #fde68a)".to_string(),
125            tok_heading: "var(--strata-editor-tok-heading, #67e8f9)".to_string(),
126            tok_link: "var(--strata-editor-tok-link, #93c5fd)".to_string(),
127            tok_variable: "var(--strata-editor-tok-variable, #c4b5fd)".to_string(),
128            diag_error: "var(--strata-editor-diag-error, #f87171)".to_string(),
129            diag_warning: "var(--strata-editor-diag-warning, #fbbf24)".to_string(),
130            diag_info: "var(--strata-editor-diag-info, #38bdf8)".to_string(),
131            diag_hint: "var(--strata-editor-diag-hint, #94a3b8)".to_string(),
132        }
133    }
134
135    /// Default light theme referencing `--strata-editor-*` CSS variables with crisp daylight fallbacks.
136    pub fn css_variables_light() -> Self {
137        Self {
138            name: "strata-light".to_string(),
139            is_dark: false,
140            bg: "var(--strata-editor-bg, #ffffff)".to_string(),
141            fg: "var(--strata-editor-fg, #0f172a)".to_string(),
142            gutter_bg: "var(--strata-editor-gutter-bg, #f8fafc)".to_string(),
143            gutter_fg: "var(--strata-editor-gutter-fg, #94a3b8)".to_string(),
144            gutter_active_fg: "var(--strata-editor-gutter-active-fg, #0f172a)".to_string(),
145            line_highlight: "var(--strata-editor-line-highlight, rgba(15, 23, 42, 0.05))"
146                .to_string(),
147            selection_bg: "var(--strata-editor-selection-bg, rgba(14, 165, 233, 0.22))".to_string(),
148            caret: "var(--strata-editor-caret, #0284c7)".to_string(),
149            border: "var(--strata-editor-border, #e2e8f0)".to_string(),
150            panel_bg: "var(--strata-editor-panel-bg, #f1f5f9)".to_string(),
151            panel_border: "var(--strata-editor-panel-border, #cbd5e1)".to_string(),
152            find_match_bg: "var(--strata-editor-find-match-bg, rgba(245, 158, 11, 0.22))"
153                .to_string(),
154            find_active_bg: "var(--strata-editor-find-active-bg, rgba(217, 119, 6, 0.40))"
155                .to_string(),
156            bracket_match_bg: "var(--strata-editor-bracket-match-bg, rgba(99, 102, 241, 0.18))"
157                .to_string(),
158            fold_badge_bg: "var(--strata-editor-fold-badge-bg, #e2e8f0)".to_string(),
159            fold_badge_fg: "var(--strata-editor-fold-badge-fg, #334155)".to_string(),
160            minimap_bg: "var(--strata-editor-minimap-bg, #f8fafc)".to_string(),
161            minimap_slider: "var(--strata-editor-minimap-slider, rgba(15, 23, 42, 0.10))"
162                .to_string(),
163            tok_keyword: "var(--strata-editor-tok-keyword, #7c3aed)".to_string(),
164            tok_control: "var(--strata-editor-tok-control, #a21caf)".to_string(),
165            tok_type: "var(--strata-editor-tok-type, #b45309)".to_string(),
166            tok_function: "var(--strata-editor-tok-function, #1d4ed8)".to_string(),
167            tok_macro: "var(--strata-editor-tok-macro, #4338ca)".to_string(),
168            tok_identifier: "var(--strata-editor-tok-identifier, #0f172a)".to_string(),
169            tok_parameter: "var(--strata-editor-tok-parameter, #c2410c)".to_string(),
170            tok_property: "var(--strata-editor-tok-property, #0369a1)".to_string(),
171            tok_string: "var(--strata-editor-tok-string, #047857)".to_string(),
172            tok_string_escape: "var(--strata-editor-tok-string-escape, #0f766e)".to_string(),
173            tok_number: "var(--strata-editor-tok-number, #c2410c)".to_string(),
174            tok_boolean: "var(--strata-editor-tok-boolean, #b45309)".to_string(),
175            tok_comment: "var(--strata-editor-tok-comment, #64748b)".to_string(),
176            tok_doc_comment: "var(--strata-editor-tok-doc-comment, #475569)".to_string(),
177            tok_operator: "var(--strata-editor-tok-operator, #be123c)".to_string(),
178            tok_punctuation: "var(--strata-editor-tok-punctuation, #475569)".to_string(),
179            tok_tag: "var(--strata-editor-tok-tag, #0e7490)".to_string(),
180            tok_attribute: "var(--strata-editor-tok-attribute, #0369a1)".to_string(),
181            tok_namespace: "var(--strata-editor-tok-namespace, #a16207)".to_string(),
182            tok_heading: "var(--strata-editor-tok-heading, #0e7490)".to_string(),
183            tok_link: "var(--strata-editor-tok-link, #2563eb)".to_string(),
184            tok_variable: "var(--strata-editor-tok-variable, #6d28d9)".to_string(),
185            diag_error: "var(--strata-editor-diag-error, #dc2626)".to_string(),
186            diag_warning: "var(--strata-editor-diag-warning, #d97706)".to_string(),
187            diag_info: "var(--strata-editor-diag-info, #0284c7)".to_string(),
188            diag_hint: "var(--strata-editor-diag-hint, #64748b)".to_string(),
189        }
190    }
191
192    /// Obsidian dark engineering theme preset.
193    pub fn obsidian_dark() -> Self {
194        let mut theme = Self::css_variables_dark();
195        theme.name = "obsidian-dark".to_string();
196        theme.bg = "var(--strata-editor-bg, #0a0d12)".to_string();
197        theme.gutter_bg = "var(--strata-editor-gutter-bg, #080a0f)".to_string();
198        theme.border = "var(--strata-editor-border, #1e2430)".to_string();
199        theme.caret = "var(--strata-editor-caret, #22d3ee)".to_string();
200        theme
201    }
202
203    /// Daylight light workstation theme preset.
204    pub fn daylight_light() -> Self {
205        let mut theme = Self::css_variables_light();
206        theme.name = "daylight-light".to_string();
207        theme
208    }
209
210    /// Resolves the CSS `color` expression for a syntax [`TokenType`].
211    pub fn token_css(&self, token_type: TokenType) -> &str {
212        match token_type {
213            TokenType::Keyword => &self.tok_keyword,
214            TokenType::ControlFlow => &self.tok_control,
215            TokenType::Type => &self.tok_type,
216            TokenType::Function => &self.tok_function,
217            TokenType::Macro => &self.tok_macro,
218            TokenType::Identifier => &self.tok_identifier,
219            TokenType::Parameter => &self.tok_parameter,
220            TokenType::Property => &self.tok_property,
221            TokenType::String => &self.tok_string,
222            TokenType::StringEscape => &self.tok_string_escape,
223            TokenType::Number => &self.tok_number,
224            TokenType::Boolean => &self.tok_boolean,
225            TokenType::Comment => &self.tok_comment,
226            TokenType::DocComment => &self.tok_doc_comment,
227            TokenType::Operator => &self.tok_operator,
228            TokenType::Punctuation => &self.tok_punctuation,
229            TokenType::Tag => &self.tok_tag,
230            TokenType::Attribute => &self.tok_attribute,
231            TokenType::Namespace => &self.tok_namespace,
232            TokenType::Heading => &self.tok_heading,
233            TokenType::Link => &self.tok_link,
234            TokenType::Variable => &self.tok_variable,
235        }
236    }
237
238    /// Resolves the CSS color expression for a diagnostic severity.
239    pub fn diagnostic_css(&self, severity: DiagnosticSeverity) -> &str {
240        match severity {
241            DiagnosticSeverity::Error => &self.diag_error,
242            DiagnosticSeverity::Warning => &self.diag_warning,
243            DiagnosticSeverity::Info => &self.diag_info,
244            DiagnosticSeverity::Hint => &self.diag_hint,
245        }
246    }
247
248    /// Resolves an RGBA color for rasterizing a token bar inside the Canvas2D minimap.
249    pub fn minimap_token_rgba(&self, token_type: TokenType) -> Rgba {
250        if self.is_dark {
251            match token_type {
252                TokenType::Keyword => Rgba::new(192, 132, 252, 200),
253                TokenType::ControlFlow => Rgba::new(232, 121, 249, 200),
254                TokenType::Type | TokenType::Namespace => Rgba::new(251, 191, 36, 195),
255                TokenType::Function | TokenType::Link => Rgba::new(96, 165, 250, 205),
256                TokenType::Macro => Rgba::new(165, 180, 252, 195),
257                TokenType::String | TokenType::StringEscape => Rgba::new(52, 211, 153, 195),
258                TokenType::Number | TokenType::Boolean => Rgba::new(251, 146, 60, 195),
259                TokenType::Comment | TokenType::DocComment => Rgba::new(100, 116, 139, 130),
260                TokenType::Operator => Rgba::new(251, 113, 133, 180),
261                TokenType::Punctuation => Rgba::new(148, 163, 184, 135),
262                TokenType::Tag | TokenType::Heading => Rgba::new(34, 211, 238, 205),
263                TokenType::Attribute | TokenType::Property => Rgba::new(125, 211, 252, 190),
264                TokenType::Parameter | TokenType::Variable => Rgba::new(196, 181, 253, 185),
265                TokenType::Identifier => Rgba::new(203, 213, 225, 165),
266            }
267        } else {
268            match token_type {
269                TokenType::Keyword => Rgba::new(124, 58, 237, 190),
270                TokenType::ControlFlow => Rgba::new(162, 28, 175, 190),
271                TokenType::Type | TokenType::Namespace => Rgba::new(180, 83, 9, 185),
272                TokenType::Function | TokenType::Link => Rgba::new(29, 78, 216, 190),
273                TokenType::Macro => Rgba::new(67, 56, 202, 185),
274                TokenType::String | TokenType::StringEscape => Rgba::new(4, 120, 87, 185),
275                TokenType::Number | TokenType::Boolean => Rgba::new(194, 65, 12, 185),
276                TokenType::Comment | TokenType::DocComment => Rgba::new(100, 116, 139, 125),
277                TokenType::Operator => Rgba::new(190, 18, 60, 170),
278                TokenType::Punctuation => Rgba::new(71, 85, 105, 135),
279                TokenType::Tag | TokenType::Heading => Rgba::new(14, 116, 144, 190),
280                TokenType::Attribute | TokenType::Property => Rgba::new(3, 105, 161, 180),
281                TokenType::Parameter | TokenType::Variable => Rgba::new(109, 40, 217, 175),
282                TokenType::Identifier => Rgba::new(30, 41, 59, 155),
283            }
284        }
285    }
286
287    pub fn minimap_bg_rgba(&self) -> Rgba {
288        if self.is_dark {
289            Rgba::new(9, 12, 17, 255)
290        } else {
291            Rgba::new(248, 250, 252, 255)
292        }
293    }
294
295    pub fn minimap_slider_rgba(&self) -> Rgba {
296        if self.is_dark {
297            Rgba::new(148, 163, 184, 42)
298        } else {
299            Rgba::new(15, 23, 42, 26)
300        }
301    }
302
303    pub fn minimap_slider_border_rgba(&self) -> Rgba {
304        if self.is_dark {
305            Rgba::new(148, 163, 184, 96)
306        } else {
307            Rgba::new(15, 23, 42, 64)
308        }
309    }
310
311    pub fn minimap_cursor_rgba(&self) -> Rgba {
312        if self.is_dark {
313            Rgba::new(56, 189, 248, 220)
314        } else {
315            Rgba::new(2, 132, 199, 220)
316        }
317    }
318
319    pub fn minimap_search_rgba(&self) -> Rgba {
320        Rgba::new(245, 158, 11, 225)
321    }
322
323    pub fn diagnostic_rgba(&self, severity: DiagnosticSeverity) -> Rgba {
324        match severity {
325            DiagnosticSeverity::Error => Rgba::new(248, 113, 113, 240),
326            DiagnosticSeverity::Warning => Rgba::new(251, 191, 36, 235),
327            DiagnosticSeverity::Info => Rgba::new(56, 189, 248, 225),
328            DiagnosticSeverity::Hint => Rgba::new(148, 163, 184, 190),
329        }
330    }
331
332    /// Exports a CSS custom property declaration block suitable for stylesheet injection.
333    pub fn to_css_variables_block(&self) -> String {
334        if self.is_dark {
335            concat!(
336                "--strata-editor-bg: #0d1016; ",
337                "--strata-editor-fg: #e2e8f0; ",
338                "--strata-editor-gutter-bg: #0b0e14; ",
339                "--strata-editor-gutter-fg: #64748b; ",
340                "--strata-editor-gutter-active-fg: #e2e8f0; ",
341                "--strata-editor-line-highlight: rgba(148, 163, 184, 0.08); ",
342                "--strata-editor-selection-bg: rgba(56, 189, 248, 0.25); ",
343                "--strata-editor-caret: #38bdf8; ",
344                "--strata-editor-border: #1e293b; ",
345                "--strata-editor-tok-keyword: #c084fc; ",
346                "--strata-editor-tok-type: #fbbf24; ",
347                "--strata-editor-tok-function: #60a5fa; ",
348                "--strata-editor-tok-string: #34d399; ",
349                "--strata-editor-tok-number: #fb923c; ",
350                "--strata-editor-tok-comment: #64748b; ",
351                "--strata-editor-diag-error: #f87171; ",
352                "--strata-editor-diag-warning: #fbbf24;"
353            )
354            .to_string()
355        } else {
356            concat!(
357                "--strata-editor-bg: #ffffff; ",
358                "--strata-editor-fg: #0f172a; ",
359                "--strata-editor-gutter-bg: #f8fafc; ",
360                "--strata-editor-gutter-fg: #94a3b8; ",
361                "--strata-editor-gutter-active-fg: #0f172a; ",
362                "--strata-editor-line-highlight: rgba(15, 23, 42, 0.05); ",
363                "--strata-editor-selection-bg: rgba(14, 165, 233, 0.22); ",
364                "--strata-editor-caret: #0284c7; ",
365                "--strata-editor-border: #e2e8f0; ",
366                "--strata-editor-tok-keyword: #7c3aed; ",
367                "--strata-editor-tok-type: #b45309; ",
368                "--strata-editor-tok-function: #1d4ed8; ",
369                "--strata-editor-tok-string: #047857; ",
370                "--strata-editor-tok-number: #c2410c; ",
371                "--strata-editor-tok-comment: #64748b; ",
372                "--strata-editor-diag-error: #dc2626; ",
373                "--strata-editor-diag-warning: #d97706;"
374            )
375            .to_string()
376        }
377    }
378}