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
use crate::{composite::over, steps::steps, Component, Theme};
use palette::{rgb::Rgba, Darken, IntoColor, Lighten, Srgba};
use std::{
    fs::{self, File},
    io::Write,
    num::NonZeroUsize,
};

use super::{to_rgba, OutputError};

impl Theme {
    #[must_use]
    /// turn the theme into css
    pub fn as_gtk4(&self) -> String {
        let Self {
            background,
            primary,
            secondary,
            accent,
            destructive,
            warning,
            success,
            palette,
            ..
        } = self;

        let window_bg = to_rgba(background.base);
        let window_fg = to_rgba(background.on);

        let view_bg = to_rgba(primary.base);
        let view_fg = to_rgba(primary.on);

        let headerbar_bg = to_rgba(background.base);
        let headerbar_fg = to_rgba(background.on);
        let headerbar_border_color = to_rgba(background.divider);

        let sidebar_bg = to_rgba(primary.base);
        let sidebar_fg = to_rgba(primary.on);
        let sidebar_shade = to_rgba(if self.is_dark {
            Rgba::new(0.0, 0.0, 0.0, 0.08)
        } else {
            Rgba::new(0.0, 0.0, 0.0, 0.32)
        });
        let backdrop_overlay = Srgba::new(1.0, 1.0, 1.0, if self.is_dark { 0.08 } else { 0.32 });
        let sidebar_backdrop = to_rgba(over(backdrop_overlay, primary.base));

        let secondary_sidebar_bg = to_rgba(secondary.base);
        let secondary_sidebar_fg = to_rgba(secondary.on);
        let secondary_sidebar_shade = to_rgba(if self.is_dark {
            Rgba::new(0.0, 0.0, 0.0, 0.08)
        } else {
            Rgba::new(0.0, 0.0, 0.0, 0.32)
        });
        let secondary_sidebar_backdrop = to_rgba(over(backdrop_overlay, secondary.base));

        let headerbar_backdrop = to_rgba(background.base);

        let card_bg = to_rgba(background.component.base);
        let card_fg = to_rgba(background.component.on);

        let thumbnail_bg = to_rgba(background.component.base);
        let thumbnail_fg = to_rgba(background.component.on);

        let dialog_bg = to_rgba(primary.base);
        let dialog_fg = to_rgba(primary.on);

        let popover_bg = to_rgba(background.component.base);
        let popover_fg = to_rgba(background.component.on);

        let shade = to_rgba(if self.is_dark {
            Rgba::new(0.0, 0.0, 0.0, 0.32)
        } else {
            Rgba::new(0.0, 0.0, 0.0, 0.08)
        });

        let mut inverted_bg_divider = background.base;
        inverted_bg_divider.alpha = 0.5;
        let scrollbar_outline = to_rgba(inverted_bg_divider);

        let mut css = format! {r#"
@define-color window_bg_color {window_bg};
@define-color window_fg_color {window_fg};

@define-color view_bg_color {view_bg};
@define-color view_fg_color {view_fg};

@define-color headerbar_bg_color {headerbar_bg};
@define-color headerbar_fg_color {headerbar_fg};
@define-color headerbar_border_color_color {headerbar_border_color};
@define-color headerbar_backdrop_color {headerbar_backdrop};

@define-color sidebar_bg_color {sidebar_bg};
@define-color sidebar_fg_color {sidebar_fg};
@define-color sidebar_shade_color {sidebar_shade};
@define-color sidebar_backdrop_color {sidebar_backdrop};

@define-color secondary_sidebar_bg_color {secondary_sidebar_bg};
@define-color secondary_sidebar_fg_color {secondary_sidebar_fg};
@define-color secondary_sidebar_shade_color {secondary_sidebar_shade};
@define-color secondary_sidebar_backdrop_color {secondary_sidebar_backdrop};

@define-color card_bg_color {card_bg};
@define-color card_fg_color {card_fg};

@define-color thumbnail_bg_color {thumbnail_bg};
@define-color thumbnail_fg_color {thumbnail_fg};

@define-color dialog_bg_color {dialog_bg};
@define-color dialog_fg_color {dialog_fg};

@define-color popover_bg_color {popover_bg};
@define-color popover_fg_color {popover_fg};

@define-color shade_color {shade};
@define-color scrollbar_outline_color {scrollbar_outline};
"#};

        css.push_str(&component_gtk4_css("accent", accent));
        css.push_str(&component_gtk4_css("destructive", destructive));
        css.push_str(&component_gtk4_css("warning", warning));
        css.push_str(&component_gtk4_css("success", success));
        css.push_str(&component_gtk4_css("accent", accent));
        css.push_str(&component_gtk4_css("error", destructive));

        css.push_str(&color_css("blue", palette.accent_blue));
        css.push_str(&color_css("green", palette.accent_green));
        css.push_str(&color_css("yellow", palette.accent_yellow));
        css.push_str(&color_css("red", palette.accent_red));
        css.push_str(&color_css("orange", palette.ext_orange));
        css.push_str(&color_css("purple", palette.ext_purple));
        let neutral_steps = steps(palette.neutral_5, NonZeroUsize::new(10).unwrap());
        for (i, c) in neutral_steps[..5].iter().enumerate() {
            css.push_str(&format!("@define-color light_{i} {};\n", to_rgba(*c)));
        }
        for (i, c) in neutral_steps[5..].iter().enumerate() {
            css.push_str(&format!("@define-color dark_{i} {};\n", to_rgba(*c)));
        }
        css
    }

    /// write the CSS to the appropriate directory
    /// Should be written in the XDG config directory for gtk-4.0
    ///
    /// # Errors
    ///
    /// Returns an `OutputError` if there is an error writing the CSS file.
    pub fn write_gtk4(&self) -> Result<(), OutputError> {
        let css_str = self.as_gtk4();
        let Some(config_dir) = dirs::config_dir() else {
            return Err(OutputError::MissingConfigDir);
        };

        let name = if self.is_dark {
            "dark.css"
        } else {
            "light.css"
        };

        let config_dir = config_dir.join("gtk-4.0").join("cosmic");
        if !config_dir.exists() {
            std::fs::create_dir_all(&config_dir).map_err(OutputError::Io)?;
        }

        let mut file = File::create(config_dir.join(name)).map_err(OutputError::Io)?;
        file.write_all(css_str.as_bytes())
            .map_err(OutputError::Io)?;

        Ok(())
    }

    /// Apply gtk color variable settings
    pub fn apply_gtk(is_dark: bool) -> Result<(), OutputError> {
        let Some(config_dir) = dirs::config_dir() else {
            return Err(OutputError::MissingConfigDir);
        };

        let gtk4 = config_dir.join("gtk-4.0");
        let gtk3 = config_dir.join("gtk-3.0");

        fs::create_dir_all(&gtk4).map_err(OutputError::Io)?;
        fs::create_dir_all(&gtk3).map_err(OutputError::Io)?;

        let cosmic_css = gtk4
            .join("cosmic")
            .join(if is_dark { "dark.css" } else { "light.css" });

        let gtk4_dest = gtk4.join("gtk.css");
        let gtk3_dest = gtk3.join("gtk.css");

        #[cfg(target_family = "unix")]
        for gtk_dest in [&gtk4_dest, &gtk3_dest] {
            use std::fs::metadata;
            use std::os::unix::fs::symlink;

            let mut gtk_dest_bak = gtk_dest.clone();
            gtk_dest_bak.set_extension("css.bak");

            if gtk_dest.exists() {
                if metadata(&gtk_dest)
                    .map_err(OutputError::Io)?
                    .file_type()
                    .is_symlink()
                {
                    fs::remove_file(&gtk_dest).map_err(OutputError::Io)?;
                } else {
                    fs::rename(&gtk_dest, gtk_dest_bak).map_err(OutputError::Io)?;
                }
            }

            symlink(&cosmic_css, gtk_dest).map_err(OutputError::Io)?;
        }
        Ok(())
    }

    /// Reset the applied gtk css
    pub fn reset_gtk() -> Result<(), OutputError> {
        let Some(config_dir) = dirs::config_dir() else {
            return Err(OutputError::MissingConfigDir);
        };

        let gtk4 = config_dir.join("gtk-4.0");
        let gtk3 = config_dir.join("gtk-3.0");
        let gtk4_dest = gtk4.join("gtk.css");
        let gtk3_dest = gtk3.join("gtk.css");

        let res = fs::remove_file(gtk3_dest);
        fs::remove_file(gtk4_dest).map_err(OutputError::Io)?;
        Ok(res.map_err(OutputError::Io)?)
    }
}

fn component_gtk4_css(prefix: &str, c: &Component) -> String {
    format!(
        r#"
@define-color {prefix}_color {};
@define-color {prefix}_bg_color {};
@define-color {prefix}_fg_color {};
"#,
        to_rgba(c.base),
        to_rgba(c.base),
        to_rgba(c.on),
    )
}

fn color_css(prefix: &str, c_3: Srgba) -> String {
    let oklch: palette::Oklch = c_3.into_color();
    let c_2: Srgba = oklch.lighten(0.1).into_color();
    let c_1: Srgba = oklch.lighten(0.2).into_color();
    let c_4: Srgba = oklch.darken(0.1).into_color();
    let c_5: Srgba = oklch.darken(0.2).into_color();
    let c_1 = to_rgba(c_1);
    let c_2 = to_rgba(c_2);
    let c_3 = to_rgba(c_3);
    let c_4 = to_rgba(c_4);
    let c_5 = to_rgba(c_5);

    format! {r#"
@define-color {prefix}_1 {c_1};
@define-color {prefix}_2 {c_2};
@define-color {prefix}_3 {c_3};
@define-color {prefix}_4 {c_4};
@define-color {prefix}_5 {c_5};
"#}
}