teksilo-platform 0.9.0

Platform integration for Teksilo — winit and AccessKit adapters, clipboard, OS theme, native file dialogs, drag-and-drop and menus.
Documentation
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! OS theme detection — reads colors directly from desktop environment
//! configuration files and settings.
//!
//! Supports GNOME, KDE, and Cinnamon on Linux. macOS and Windows return
//! only the light/dark preference (via winit), with no color reading.

#[cfg(target_os = "linux")]
use teksilo_tokens::Color;
use teksilo_tokens::{ColorSchemePreference, OsThemeColors};

/// Query only the OS light/dark preference (lightweight).
/// Used by `ThemeMode::FollowSystem`.
pub fn query_color_scheme() -> ColorSchemePreference {
    platform::query_color_scheme()
}

/// Query full OS theme colors from desktop config files.
/// Used by `ThemeMode::Native`.
pub fn query_os_theme_colors() -> OsThemeColors {
    platform::query_os_theme_colors()
}

// ── Linux ───────────────────────────────────────────────────────────────────
#[cfg(target_os = "linux")]
mod platform {
    use super::*;
    use crate::linux_helpers::{
        Desktop, detect_desktop, read_gsettings, read_portal_rgb, read_portal_u32,
    };

    pub(super) fn query_color_scheme() -> ColorSchemePreference {
        // XDG portal: 0 = no preference, 1 = dark, 2 = light.
        // Works on GNOME and on KDE Plasma 5.27+ (with the
        // `xdg-desktop-portal-kde` package installed). On older or
        // headless KDE setups the portal call returns None and we
        // fall through.
        if let Some(v) = read_portal_u32("org.freedesktop.appearance", "color-scheme") {
            return match v {
                1 => ColorSchemePreference::Dark,
                2 => ColorSchemePreference::Light,
                _ => ColorSchemePreference::NoPreference,
            };
        }

        // Desktop-specific fallback. Picks the right config source
        // for the current DE rather than blindly trying GNOME's
        // gsettings (which is empty on a clean KDE install and
        // would otherwise leave us at `NoPreference`, mapping
        // dark-mode KDE users to a light theme).
        match detect_desktop() {
            Desktop::Kde => {
                // KDE stores the active scheme in `kdeglobals` —
                // `[General] ColorScheme=BreezeDark` for dark,
                // `BreezeLight` (or any name without "dark") for
                // light. Reading the file is cheap; we already
                // do it under `ThemeMode::Native` for the full
                // colour palette.
                let path = dirs_kdeglobals();
                if let Ok(content) = std::fs::read_to_string(&path)
                    && let Some(scheme) = ini_value(&content, "General", "ColorScheme")
                {
                    if scheme.to_lowercase().contains("dark") {
                        return ColorSchemePreference::Dark;
                    }
                    return ColorSchemePreference::Light;
                }
            }
            Desktop::Cinnamon => {
                if let Some(name) = read_gsettings("org.cinnamon.desktop.interface", "gtk-theme") {
                    if name.to_lowercase().contains("dark") {
                        return ColorSchemePreference::Dark;
                    }
                    return ColorSchemePreference::Light;
                }
            }
            Desktop::Gnome | Desktop::Other(_) => {
                if let Some(name) = read_gsettings("org.gnome.desktop.interface", "gtk-theme") {
                    if name.to_lowercase().contains("dark") {
                        return ColorSchemePreference::Dark;
                    }
                    return ColorSchemePreference::Light;
                }
            }
        }

        ColorSchemePreference::NoPreference
    }

    pub(super) fn query_os_theme_colors() -> OsThemeColors {
        let mut colors = OsThemeColors {
            color_scheme: query_color_scheme(),
            ..Default::default()
        };

        match detect_desktop() {
            Desktop::Kde => query_kde(&mut colors),
            Desktop::Cinnamon => query_cinnamon(&mut colors),
            Desktop::Gnome | Desktop::Other(_) => query_gnome(&mut colors),
        }

        colors
    }

    // ── GNOME ────────────────────────────────────────────────────────────

    fn query_gnome(colors: &mut OsThemeColors) {
        // Accent color from XDG portal (RGB doubles)
        if let Some((r, g, b)) = read_portal_rgb("org.freedesktop.appearance", "accent-color") {
            colors.accent = Some(Color::from_rgb(r as f32, g as f32, b as f32));
        }

        // Fallback: GNOME 47 named accent
        if colors.accent.is_none()
            && let Some(name) = read_gsettings("org.gnome.desktop.interface", "accent-color")
        {
            colors.accent = gnome_named_accent(&name);
        }

        // Read surface/selection colors from GTK CSS
        if let Some(theme_name) = read_gsettings("org.gnome.desktop.interface", "gtk-theme") {
            apply_gtk_css_colors(colors, &theme_name);
        }
    }

    /// Map GNOME 47 named accent colors to RGB.
    fn gnome_named_accent(name: &str) -> Option<Color> {
        let hex = match name.to_lowercase().as_str() {
            "blue" => "#3584e4",
            "teal" => "#2190a4",
            "green" => "#3a944a",
            "yellow" => "#c88800",
            "orange" => "#ed5b00",
            "red" => "#e62d42",
            "pink" => "#d56199",
            "purple" => "#9141ac",
            "slate" => "#6f8396",
            _ => return None,
        };
        Some(Color::from_hex(hex))
    }

    // ── KDE ──────────────────────────────────────────────────────────────

    fn query_kde(colors: &mut OsThemeColors) {
        let path = dirs_kdeglobals();
        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(_) => return,
        };

        // Infer dark/light from color scheme name if portal didn't provide it
        if colors.color_scheme == ColorSchemePreference::NoPreference
            && let Some(scheme) = ini_value(&content, "General", "ColorScheme")
        {
            if scheme.to_lowercase().contains("dark") {
                colors.color_scheme = ColorSchemePreference::Dark;
            } else {
                colors.color_scheme = ColorSchemePreference::Light;
            }
        }

        // Window colors
        colors.window_bg = ini_color(&content, "Colors:Window", "BackgroundNormal");
        colors.window_fg = ini_color(&content, "Colors:Window", "ForegroundNormal");
        colors.accent = ini_color(&content, "Colors:Window", "DecorationFocus");

        // Button colors
        colors.button_bg = ini_color(&content, "Colors:Button", "BackgroundNormal");
        colors.button_fg = ini_color(&content, "Colors:Button", "ForegroundNormal");

        // Selection colors
        colors.selection_bg = ini_color(&content, "Colors:Selection", "BackgroundNormal");
        colors.selection_fg = ini_color(&content, "Colors:Selection", "ForegroundNormal");

        // Tooltip colors
        colors.tooltip_bg = ini_color(&content, "Colors:Tooltip", "BackgroundNormal");
        colors.tooltip_fg = ini_color(&content, "Colors:Tooltip", "ForegroundNormal");
    }

    fn dirs_kdeglobals() -> std::path::PathBuf {
        if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
            std::path::PathBuf::from(config_home).join("kdeglobals")
        } else if let Ok(home) = std::env::var("HOME") {
            std::path::PathBuf::from(home).join(".config/kdeglobals")
        } else {
            std::path::PathBuf::from("/dev/null")
        }
    }

    /// Read a value from a simple INI file (section + key).
    fn ini_value<'a>(content: &'a str, section: &str, key: &str) -> Option<&'a str> {
        let section_header = format!("[{}]", section);
        let mut in_section = false;

        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with('[') {
                in_section = trimmed == section_header;
                continue;
            }
            if in_section
                && let Some((k, v)) = trimmed.split_once('=')
                && k.trim() == key
            {
                return Some(v.trim());
            }
        }
        None
    }

    /// Parse a KDE "R,G,B" color value (0-255 integers).
    fn ini_color(content: &str, section: &str, key: &str) -> Option<Color> {
        let value = ini_value(content, section, key)?;
        let parts: Vec<&str> = value.split(',').collect();
        if parts.len() >= 3 {
            let r = parts[0].trim().parse::<u8>().ok()?;
            let g = parts[1].trim().parse::<u8>().ok()?;
            let b = parts[2].trim().parse::<u8>().ok()?;
            Some(Color::from_rgb(
                r as f32 / 255.0,
                g as f32 / 255.0,
                b as f32 / 255.0,
            ))
        } else {
            None
        }
    }

    // ── Cinnamon ─────────────────────────────────────────────────────────

    fn query_cinnamon(colors: &mut OsThemeColors) {
        let theme_name = read_gsettings("org.cinnamon.desktop.interface", "gtk-theme")
            .or_else(|| read_gsettings("org.gnome.desktop.interface", "gtk-theme"));

        if let Some(ref name) = theme_name {
            // Infer accent from Mint-Y theme name suffix
            colors.accent = mint_y_accent(name);

            // Read surface/selection colors from GTK CSS
            apply_gtk_css_colors(colors, name);
        }
    }

    /// Map Mint-Y theme name suffixes to accent colors.
    fn mint_y_accent(theme_name: &str) -> Option<Color> {
        // Theme names like "Mint-Y-Dark-Aqua" → extract the last segment
        let suffix = theme_name.rsplit('-').next()?;
        let hex = match suffix.to_lowercase().as_str() {
            "aqua" => "#1a9e87",
            "blue" => "#0c75de",
            "grey" => "#70737a",
            "orange" => "#dd6516",
            "pink" => "#e54980",
            "purple" => "#7e57c2",
            "red" => "#c0392b",
            "sand" => "#c5a07c",
            "teal" => "#009688",
            // Default Mint-Y (no accent suffix) uses green
            _ if theme_name.starts_with("Mint-Y") => "#92b372",
            _ => return None,
        };
        Some(Color::from_hex(hex))
    }

    // ── Shared GTK CSS parser ────────────────────────────────────────────

    /// Parse `@define-color` declarations from a GTK theme's CSS and apply
    /// well-known color names to the `OsThemeColors` struct.
    fn apply_gtk_css_colors(colors: &mut OsThemeColors, theme_name: &str) {
        let css = load_gtk_css(theme_name);
        if css.is_empty() {
            return;
        }

        let defined = parse_define_colors(&css);

        // Resolve well-known GTK color names to actual Color values.
        // Try both bare names and DE-specific suffixed names (e.g. `_breeze`).
        let resolve = |names: &[&str]| -> Option<Color> {
            for name in names {
                if let Some(c) = resolve_color(&defined, name) {
                    return Some(c);
                }
            }
            None
        };

        if colors.window_bg.is_none() {
            colors.window_bg = resolve(&["theme_bg_color"]);
        }
        if colors.window_fg.is_none() {
            colors.window_fg = resolve(&["theme_fg_color"]);
        }
        if colors.selection_bg.is_none() {
            colors.selection_bg = resolve(&["theme_selected_bg_color"]);
        }
        if colors.selection_fg.is_none() {
            colors.selection_fg = resolve(&["theme_selected_fg_color"]);
        }
        if colors.button_bg.is_none() {
            colors.button_bg =
                resolve(&["theme_button_background_normal", "theme_unfocused_bg_color"]);
        }
        if colors.tooltip_bg.is_none() {
            colors.tooltip_bg = resolve(&["tooltip_bg_color"]);
        }
        if colors.tooltip_fg.is_none() {
            colors.tooltip_fg = resolve(&["tooltip_fg_color"]);
        }

        // Derive accent from selection color if not already set
        if colors.accent.is_none()
            && let Some(sel) = resolve(&["theme_selected_bg_color"])
        {
            colors.accent = Some(sel);
        }
    }

    /// Load GTK CSS for a theme. Tries gtk-4.0 first, falls back to gtk-3.0.
    /// Also checks user override directories.
    fn load_gtk_css(theme_name: &str) -> String {
        let candidates = [
            // User themes
            format!(
                "{}/.themes/{}/gtk-4.0/gtk.css",
                std::env::var("HOME").unwrap_or_default(),
                theme_name
            ),
            format!(
                "{}/.themes/{}/gtk-3.0/gtk.css",
                std::env::var("HOME").unwrap_or_default(),
                theme_name
            ),
            // System themes
            format!("/usr/share/themes/{}/gtk-4.0/gtk.css", theme_name),
            format!("/usr/share/themes/{}/gtk-3.0/gtk.css", theme_name),
            // Flatpak/snap locations
            format!("/usr/local/share/themes/{}/gtk-4.0/gtk.css", theme_name),
        ];

        for path in &candidates {
            if let Ok(content) = std::fs::read_to_string(path) {
                // Skip placeholder files (e.g., Adwaita's "this file is no longer used")
                if content.contains("@define-color") {
                    return content;
                }
            }
        }

        String::new()
    }

    /// Parse all `@define-color name value;` declarations from GTK CSS.
    /// Returns a map of name → raw value string.
    fn parse_define_colors(css: &str) -> std::collections::HashMap<String, String> {
        let mut map = std::collections::HashMap::new();
        for line in css.lines() {
            let trimmed = line.trim();
            if let Some(rest) = trimmed.strip_prefix("@define-color") {
                let rest = rest.trim();
                if let Some((name, value)) = rest.split_once(char::is_whitespace) {
                    let value = value.trim().trim_end_matches(';').trim();
                    map.insert(name.to_string(), value.to_string());
                }
            }
        }
        map
    }

    /// Resolve a GTK CSS color name to a `Color`, following `@name` references.
    fn resolve_color(
        defined: &std::collections::HashMap<String, String>,
        name: &str,
    ) -> Option<Color> {
        resolve_color_depth(defined, name, 0)
    }

    fn resolve_color_depth(
        defined: &std::collections::HashMap<String, String>,
        name: &str,
        depth: u32,
    ) -> Option<Color> {
        if depth > 10 {
            return None; // prevent infinite loops
        }

        let value = defined.get(name)?;

        // Reference to another color: @other_name
        if let Some(ref_name) = value.strip_prefix('@') {
            return resolve_color_depth(defined, ref_name.trim(), depth + 1);
        }

        parse_css_color(value)
    }

    /// Parse a CSS color value: #hex, rgb(...), rgba(...), or named color.
    fn parse_css_color(value: &str) -> Option<Color> {
        let value = value.trim();

        // #rrggbb or #rrggbbaa
        if value.starts_with('#') {
            return Some(Color::from_hex(value));
        }

        // rgba(r, g, b, a) — values are 0-255 integers or percentages
        if let Some(inner) = value
            .strip_prefix("rgba(")
            .and_then(|s| s.strip_suffix(')'))
        {
            let parts: Vec<&str> = inner.split(',').collect();
            if parts.len() == 4 {
                let r = parse_css_component(parts[0])?;
                let g = parse_css_component(parts[1])?;
                let b = parse_css_component(parts[2])?;
                let a = parts[3].trim().parse::<f32>().ok()?;
                return Some(Color::from_rgba(r, g, b, a));
            }
        }

        // rgb(r, g, b)
        if let Some(inner) = value.strip_prefix("rgb(").and_then(|s| s.strip_suffix(')')) {
            let parts: Vec<&str> = inner.split(',').collect();
            if parts.len() == 3 {
                let r = parse_css_component(parts[0])?;
                let g = parse_css_component(parts[1])?;
                let b = parse_css_component(parts[2])?;
                return Some(Color::from_rgb(r, g, b));
            }
        }

        // Named colors
        match value.to_lowercase().as_str() {
            "white" => Some(Color::WHITE),
            "black" => Some(Color::BLACK),
            "transparent" => Some(Color::TRANSPARENT),
            _ => None,
        }
    }

    /// Parse a CSS color component: integer (0-255) or float (already 0.0-1.0).
    /// GTK CSS uses integer 0-255 for rgb/rgba channels.
    fn parse_css_component(s: &str) -> Option<f32> {
        let s = s.trim();
        if s.contains('.') {
            // Fractional value — treat as 0.0-1.0 range
            s.parse::<f32>().ok()
        } else if let Ok(i) = s.parse::<u16>() {
            // Integer — treat as 0-255 range
            Some(i as f32 / 255.0)
        } else {
            None
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn parse_define_colors_basic() {
            let css = r#"
@define-color theme_bg_color #eff0f1;
@define-color theme_fg_color #232629;
@define-color theme_selected_bg_color @accent_color;
@define-color accent_color #3584e4;
"#;
            let defined = parse_define_colors(css);
            assert_eq!(defined.get("theme_bg_color").unwrap(), "#eff0f1");
            assert_eq!(defined.get("theme_fg_color").unwrap(), "#232629");

            let c = resolve_color(&defined, "theme_selected_bg_color").unwrap();
            assert!((c.r() - 0x35 as f32 / 255.0).abs() < 0.01);
        }

        #[test]
        fn parse_css_color_hex() {
            let c = parse_css_color("#3584e4").unwrap();
            assert!((c.r() - 0x35 as f32 / 255.0).abs() < 0.01);
            assert!((c.g() - 0x84 as f32 / 255.0).abs() < 0.01);
        }

        #[test]
        fn parse_css_color_rgba() {
            let c = parse_css_color("rgba(61, 174, 233, 0.5)").unwrap();
            assert!((c.r() - 61.0 / 255.0).abs() < 0.01);
            assert!((c.a() - 0.5).abs() < 0.01);
        }

        #[test]
        fn parse_css_color_named() {
            assert_eq!(parse_css_color("white").unwrap(), Color::WHITE);
            assert_eq!(parse_css_color("black").unwrap(), Color::BLACK);
        }

        #[test]
        fn kde_ini_parsing() {
            let content = r#"
[General]
ColorScheme=Breeze-Dark

[Colors:Window]
BackgroundNormal=49,54,59
ForegroundNormal=239,240,241
DecorationFocus=61,174,233
"#;
            let bg = ini_color(content, "Colors:Window", "BackgroundNormal").unwrap();
            assert!((bg.r() - 49.0 / 255.0).abs() < 0.01);

            let fg = ini_color(content, "Colors:Window", "ForegroundNormal").unwrap();
            assert!((fg.r() - 239.0 / 255.0).abs() < 0.01);

            let scheme = ini_value(content, "General", "ColorScheme").unwrap();
            assert!(scheme.contains("Dark"));
        }

        #[test]
        fn gnome_named_accent_mapping() {
            assert!(gnome_named_accent("blue").is_some());
            assert!(gnome_named_accent("teal").is_some());
            assert!(gnome_named_accent("nonexistent").is_none());
        }

        #[test]
        fn mint_y_accent_mapping() {
            assert!(mint_y_accent("Mint-Y-Dark-Aqua").is_some());
            assert!(mint_y_accent("Mint-Y-Blue").is_some());
            assert!(mint_y_accent("Mint-Y").is_some());
            assert!(mint_y_accent("Adwaita").is_none());
        }
    }
}

// ── macOS ───────────────────────────────────────────────────────────────────
#[cfg(target_os = "macos")]
mod platform {
    use super::*;

    pub(super) fn query_color_scheme() -> ColorSchemePreference {
        // Windowless color-scheme detection isn't implemented on macOS yet; the
        // reliable source is winit's `window.theme()` at/after window creation
        // (the app layer should drive `ThemeMode::Native` from that). Warn once
        // in debug so this fallback isn't silent.
        // TODO: use NSAppearance.current.name via objc2 for windowless detection.
        #[cfg(debug_assertions)]
        {
            use std::sync::Once;
            static WARNED: Once = Once::new();
            WARNED.call_once(|| {
                eprintln!(
                    "teksilo-platform: ThemeMode::Native windowless color-scheme \
                     detection is unimplemented on macOS — falling back to \
                     NoPreference; drive it from winit's window.theme() instead."
                );
            });
        }
        ColorSchemePreference::NoPreference
    }

    pub(super) fn query_os_theme_colors() -> OsThemeColors {
        OsThemeColors {
            color_scheme: query_color_scheme(),
            ..Default::default()
        }
    }
}

// ── Windows ─────────────────────────────────────────────────────────────────
#[cfg(target_os = "windows")]
mod platform {
    use super::*;

    pub(super) fn query_color_scheme() -> ColorSchemePreference {
        // Windowless color-scheme detection isn't implemented on Windows yet
        // (UI_ViewManagement::UISettings or the AppsUseLightTheme registry key
        // would do it); winit's `window.theme()` is the reliable path. Warn
        // once in debug so this fallback isn't silent.
        // TODO: read HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme
        // 0 = dark, 1 = light
        #[cfg(debug_assertions)]
        {
            use std::sync::Once;
            static WARNED: Once = Once::new();
            WARNED.call_once(|| {
                eprintln!(
                    "teksilo-platform: ThemeMode::Native windowless color-scheme \
                     detection is unimplemented on Windows — falling back to \
                     NoPreference; drive it from winit's window.theme() instead."
                );
            });
        }
        ColorSchemePreference::NoPreference
    }

    pub(super) fn query_os_theme_colors() -> OsThemeColors {
        OsThemeColors {
            color_scheme: query_color_scheme(),
            ..Default::default()
        }
    }
}

// ── Fallback ────────────────────────────────────────────────────────────────
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
mod platform {
    use super::*;

    pub(super) fn query_color_scheme() -> ColorSchemePreference {
        ColorSchemePreference::NoPreference
    }

    pub(super) fn query_os_theme_colors() -> OsThemeColors {
        OsThemeColors::default()
    }
}