jay_config/
theme.rs

1//! Tools for configuring the look of the compositor.
2
3use serde::{Deserialize, Serialize};
4
5/// A color.
6///
7/// When specifying RGBA values of a color, the RGB values can either be specified
8/// *straight* or *premultiplied*. Premultiplied means that the RGB values have already
9/// been multiplied by the alpha value.
10///
11/// Given a color, to reduce its opacity by half,
12///
13/// - if you're working with premultiplied values, you would multiply each component by `0.5`;
14/// - if you're working with straight values, you would multiply only the alpha component by `0.5`.
15///
16/// When using hexadecimal notation, `#RRGGBBAA`, the RGB values are usually straight.
17// values are stored premultiplied
18#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
19pub struct Color {
20    r: f32,
21    g: f32,
22    b: f32,
23    a: f32,
24}
25
26fn to_f32(c: u8) -> f32 {
27    c as f32 / 255f32
28}
29
30fn to_u8(c: f32) -> u8 {
31    (c * 255f32) as u8
32}
33
34fn validate_f32(f: f32) -> bool {
35    f >= 0.0 && f <= 1.0
36}
37
38fn validate_f32_all(f: [f32; 4]) -> bool {
39    if !f.into_iter().all(validate_f32) {
40        log::warn!(
41            "f32 values {:?} are not in the valid color range. Using solid black instead xyz",
42            f
43        );
44        return false;
45    }
46    true
47}
48
49impl Color {
50    /// Solid black.
51    pub const BLACK: Self = Self {
52        r: 0.0,
53        g: 0.0,
54        b: 0.0,
55        a: 1.0,
56    };
57
58    /// Creates a new color from `u8` RGB values.
59    pub fn new(r: u8, g: u8, b: u8) -> Self {
60        Self {
61            r: to_f32(r),
62            g: to_f32(g),
63            b: to_f32(b),
64            a: 1.0,
65        }
66    }
67
68    /// Creates a new color from straight `u8` RGBA values.
69    pub fn new_straight(r: u8, g: u8, b: u8, a: u8) -> Self {
70        Self::new_f32_straight(to_f32(r), to_f32(g), to_f32(b), to_f32(a))
71    }
72
73    /// Creates a new color from premultiplied `f32` RGBA values.
74    pub fn new_f32_premultiplied(r: f32, g: f32, b: f32, a: f32) -> Self {
75        if !validate_f32_all([r, g, b, a]) {
76            Self::BLACK
77        } else if r > a || g > a || b > a {
78            log::warn!(
79                "f32 values {:?} are not valid valid for a premultiplied color. Using solid black instead.",
80                [r, g, b, a]
81            );
82            Self::BLACK
83        } else {
84            Self { r, g, b, a }
85        }
86    }
87
88    /// Creates a new color from straight `f32` RGBA values.
89    pub fn new_f32_straight(r: f32, g: f32, b: f32, a: f32) -> Self {
90        if !validate_f32_all([r, g, b, a]) {
91            Self::BLACK
92        } else {
93            Self {
94                r: r * a,
95                g: g * a,
96                b: b * a,
97                a,
98            }
99        }
100    }
101
102    /// Creates a new color from `f32` RGB values.
103    pub fn new_f32(r: f32, g: f32, b: f32) -> Self {
104        Self { r, g, b, a: 1.0 }
105    }
106
107    /// Converts the color to its premultiplied `f32` RGBA values.
108    pub fn to_f32_premultiplied(&self) -> [f32; 4] {
109        [self.r, self.g, self.b, self.a]
110    }
111
112    /// Converts the color to its straight `f32` RGBA values.
113    pub fn to_f32_straight(&self) -> [f32; 4] {
114        if self.a == 0.0 {
115            [0.0, 0.0, 0.0, 0.0]
116        } else {
117            let a = self.a;
118            [self.r / a, self.g / a, self.b / a, a]
119        }
120    }
121
122    /// Converts the color to its straight `u8` RGBA values.
123    pub fn to_u8_straight(&self) -> [u8; 4] {
124        let [r, g, b, a] = self.to_f32_straight();
125        [to_u8(r), to_u8(g), to_u8(b), to_u8(a)]
126    }
127}
128
129/// Resets all sizes to their defaults.
130pub fn reset_sizes() {
131    get!().reset_sizes();
132}
133
134/// Resets all colors to their defaults.
135pub fn reset_colors() {
136    get!().reset_colors();
137}
138
139/// Returns the current font.
140pub fn get_font() -> String {
141    get!().get_font()
142}
143
144/// Sets the font.
145///
146/// Default: `monospace 8`.
147///
148/// The font name should be specified in [pango][pango] syntax.
149///
150/// [pango]: https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html
151pub fn set_font(font: &str) {
152    get!().set_font(font)
153}
154
155/// Resets the font to the default.
156///
157/// Currently the default is `monospace 8`.
158pub fn reset_font() {
159    get!().reset_font()
160}
161
162/// Elements of the compositor whose color can be changed.
163pub mod colors {
164    use {
165        crate::theme::Color,
166        serde::{Deserialize, Serialize},
167    };
168
169    /// An element of the GUI whose color can be changed.
170    #[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
171    pub struct Colorable(#[doc(hidden)] pub u32);
172
173    impl Colorable {
174        /// Sets the color to an RGB value.
175        pub fn set(self, r: u8, g: u8, b: u8) {
176            let color = Color::new(r, g, b);
177            get!().set_color(self, color);
178        }
179
180        /// Sets the color to a `Color` that might contain an alpha component.
181        pub fn set_color(self, color: Color) {
182            get!().set_color(self, color);
183        }
184
185        /// Gets the current color.
186        pub fn get(self) -> Color {
187            get!(Color::BLACK).get_color(self)
188        }
189    }
190
191    macro_rules! colors {
192        ($($(#[$attr:meta])* const $n:expr => $name:ident,)*) => {
193            $(
194                $(#[$attr])*
195                pub const $name: Colorable = Colorable($n);
196            )*
197        }
198    }
199
200    colors! {
201        /// The title background color of an unfocused window.
202        ///
203        /// Default: `#222222`.
204        const 01 => UNFOCUSED_TITLE_BACKGROUND_COLOR,
205        /// The title background color of a focused window.
206        ///
207        /// Default: `#285577`.
208        const 02 => FOCUSED_TITLE_BACKGROUND_COLOR,
209        /// The title background color of an unfocused window that was the last focused
210        /// window in its container.
211        ///
212        /// Default: `#5f676a`.
213        const 03 => FOCUSED_INACTIVE_TITLE_BACKGROUND_COLOR,
214        /// The background color of the desktop.
215        ///
216        /// Default: `#001019`.
217        ///
218        /// You can use an application such as [swaybg][swaybg] to further customize the background.
219        ///
220        /// [swaybg]: https://github.com/swaywm/swaybg
221        const 04 => BACKGROUND_COLOR,
222        /// The background color of the bar.
223        ///
224        /// Default: `#000000`.
225        const 05 => BAR_BACKGROUND_COLOR,
226        /// The color of the 1px separator below window titles.
227        ///
228        /// Default: `#333333`.
229        const 06 => SEPARATOR_COLOR,
230        /// The color of the border between windows.
231        ///
232        /// Default: `#3f474a`.
233        const 07 => BORDER_COLOR,
234        /// The title text color of an unfocused window.
235        ///
236        /// Default: `#888888`.
237        const 08 => UNFOCUSED_TITLE_TEXT_COLOR,
238        /// The title text color of a focused window.
239        ///
240        /// Default: `#ffffff`.
241        const 09 => FOCUSED_TITLE_TEXT_COLOR,
242        /// The title text color of an unfocused window that was the last focused
243        /// window in its container.
244        ///
245        /// Default: `#ffffff`.
246        const 10 => FOCUSED_INACTIVE_TITLE_TEXT_COLOR,
247        /// The color of the status text in the bar.
248        ///
249        /// Default: `#ffffff`.
250        const 11 => BAR_STATUS_TEXT_COLOR,
251        /// The title background color of an unfocused window that might be captured.
252        ///
253        /// Default: `#220303`.
254        const 12 => CAPTURED_UNFOCUSED_TITLE_BACKGROUND_COLOR,
255        /// The title background color of a focused window that might be captured.
256        ///
257        /// Default: `#772831`.
258        const 13 => CAPTURED_FOCUSED_TITLE_BACKGROUND_COLOR,
259        /// The title background color of a window that has requested attention.
260        ///
261        /// Default: `#23092c`.
262        const 14 => ATTENTION_REQUESTED_BACKGROUND_COLOR,
263        /// Color used to highlight parts of the UI.
264        ///
265        /// Default: `#9d28c67f`.
266        const 15 => HIGHLIGHT_COLOR,
267    }
268
269    /// Sets the color of GUI element.
270    pub fn set_color(element: Colorable, color: Color) {
271        get!().set_color(element, color);
272    }
273
274    /// Gets the color of GUI element.
275    pub fn get_color(element: Colorable) -> Color {
276        get!(Color::BLACK).get_color(element)
277    }
278}
279
280/// Elements of the compositor whose size can be changed.
281pub mod sized {
282    use serde::{Deserialize, Serialize};
283
284    /// An element of the GUI whose size can be changed.
285    #[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
286    pub struct Resizable(#[doc(hidden)] pub u32);
287
288    impl Resizable {
289        /// Gets the current size.
290        pub fn get(self) -> i32 {
291            get!(0).get_size(self)
292        }
293
294        /// Sets the size.
295        pub fn set(self, size: i32) {
296            get!().set_size(self, size)
297        }
298    }
299
300    macro_rules! sizes {
301        ($($(#[$attr:meta])* const $n:expr => $name:ident,)*) => {
302            $(
303                $(#[$attr])*
304                pub const $name: Resizable = Resizable($n);
305            )*
306        }
307    }
308
309    sizes! {
310        /// The height of window titles.
311        ///
312        /// Default: 17
313        const 01 => TITLE_HEIGHT,
314        /// The width of borders between windows.
315        ///
316        /// Default: 4
317        const 02 => BORDER_WIDTH,
318    }
319}