hyprforge_ui/theme.rs
1use iced::Color;
2
3/// Spacing scale shared by every Hyprforge app, so a sidebar, a settings
4/// row, and a dialog all breathe the same amount no matter which app they're
5/// in (vision pillar #2: one shared visual grammar).
6pub mod spacing {
7 /// Tightest gap: between a label and the control right beside it.
8 pub const XS: f32 = 4.0;
9 /// Between closely related elements within one row or group.
10 pub const SM: f32 = 8.0;
11 /// The default gap between rows and between a card's edge and its
12 /// content — reach for this one unless a specific case argues
13 /// otherwise.
14 pub const MD: f32 = 16.0;
15 /// Between distinct sections of a screen.
16 pub const LG: f32 = 24.0;
17 /// Between top-level regions — sidebar and content, page margins.
18 pub const XL: f32 = 32.0;
19}
20
21/// Base text size in logical pixels before `FontScale` is applied.
22pub const BASE_TEXT_SIZE: f32 = 14.0;
23
24/// A single scale factor threaded through every text/spacing measurement in
25/// the shared widget layer, so accessibility (larger text) is a property of
26/// the theme rather than something bolted onto individual screens later.
27#[derive(Debug, Clone, Copy, PartialEq)]
28pub struct FontScale(pub f32);
29
30impl Default for FontScale {
31 fn default() -> Self {
32 FontScale(1.0)
33 }
34}
35
36impl FontScale {
37 /// Scales a logical-pixel size by this factor. `base` is almost
38 /// always [`BASE_TEXT_SIZE`] or a spacing constant, never a value a
39 /// caller already scaled — applying this twice compounds instead of
40 /// replacing.
41 pub fn apply(&self, base: f32) -> f32 {
42 base * self.0
43 }
44}
45
46/// The theme every widget in this process draws from.
47///
48/// Process-global and read-mostly, set once at startup — the same
49/// lifetime the font scale already had. The alternative, threading a
50/// `&Theme` beside `FontScale` through some four hundred widget call
51/// sites, buys nothing while there is one window per process.
52///
53/// When that stops being true — a second app wanting a per-window theme
54/// — this becomes the fallback rather than the answer, and the widgets
55/// take a `&Theme` argument. Nothing here has to change for that; the
56/// call sites do.
57static ACTIVE: std::sync::OnceLock<hyprforge_look::Theme> = std::sync::OnceLock::new();
58
59/// Installs the resolved theme. Call once, before the first frame.
60///
61/// Later calls are ignored rather than rejected: a second call is a bug
62/// in the caller, not a reason to bring down a window the user is
63/// looking at.
64pub fn init(theme: hyprforge_look::Theme) {
65 let _ = ACTIVE.set(theme);
66}
67
68/// The active theme, or the default if nobody installed one.
69///
70/// Deliberately `get_or_init` rather than `expect`: an app that forgets
71/// to call [`init`] renders exactly what it would have rendered before
72/// any of this was configurable, instead of panicking on its first
73/// frame.
74pub fn active() -> &'static hyprforge_look::Theme {
75 ACTIVE.get_or_init(hyprforge_look::Theme::default)
76}
77
78/// Explicit surface colors, used directly by the shared widget layer
79/// instead of leaning on `Theme::extended_palette()`'s auto-derived
80/// shades. Auto-derivation (`deviate(base, 0.1)` etc.) only shifts
81/// lightness a few percent, which reads as "everything is the same flat
82/// gray" at normal monitor brightness — real elevation (root vs sidebar
83/// vs card) needs a deliberately chosen jump, the way macOS/Windows
84/// layer system chrome vs content vs cards.
85pub mod surface {
86 use crate::color::to_iced;
87 use iced::Color;
88
89 /// Window root — behind everything.
90 pub fn root() -> Color {
91 to_iced(super::active().surfaces.root)
92 }
93 /// Sidebar — recessed a step darker than root.
94 pub fn sidebar() -> Color {
95 to_iced(super::active().surfaces.sidebar)
96 }
97 /// Card/section background — raised a step lighter than root.
98 pub fn card() -> Color {
99 to_iced(super::active().surfaces.card)
100 }
101 /// Card border.
102 pub fn card_border() -> Color {
103 to_iced(super::active().surfaces.card_border)
104 }
105 /// A list row's background on hover/emphasis, between root and card.
106 pub fn row() -> Color {
107 to_iced(super::active().surfaces.row)
108 }
109}
110
111/// Primary text color (near-white, not pure white — easier on the eyes on
112/// a dark surface).
113pub fn text() -> Color {
114 crate::color::to_iced(active().surfaces.text)
115}
116
117/// Secondary/meta text — timestamps, hints, IDs.
118pub fn text_dim() -> Color {
119 crate::color::to_iced(active().surfaces.text_dim)
120}
121
122/// Something the user should look at before saving — a chord already bound,
123/// a combination the compositor will refuse. Not an error: nothing has gone
124/// wrong yet.
125pub fn warning() -> Color {
126 crate::color::to_iced(active().warning)
127}
128
129/// Somewhere that is not this machine — a remote host, a mounted share.
130///
131/// Reach for this only for that meaning. It is a state colour, and the
132/// point of a state colour is that seeing it tells you something; using
133/// it as a second accent because it looks nice would make it stop
134/// meaning anything, which is the failure the whole reserved-colour
135/// scheme exists to prevent.
136pub fn info() -> Color {
137 crate::color::to_iced(active().info)
138}
139
140/// Something went well — a change applied, a device connected, a battery
141/// charging.
142///
143/// A state colour, like [`info`]: it means something only as long as it
144/// is never used for anything else.
145pub fn success() -> Color {
146 crate::color::to_iced(active().success)
147}
148
149/// Danger or failure — a conflicting bind, a destructive button, a
150/// value the compositor refused.
151pub fn error() -> Color {
152 crate::color::to_iced(active().error)
153}
154
155/// How opaque the alternating row fill is, laid over whatever the rows
156/// sit on.
157///
158/// The mockup's 22%. A tint rather than a fifth opaque grey on purpose:
159/// the same stripe then reads correctly on a card and on the window
160/// root, where an opaque shade chosen for one would be a visible step on
161/// the other.
162const ROW_TINT_ALPHA: f32 = 0.22;
163
164/// The fill every other setting row gets, so a long page scans as rows
165/// without a divider between each one.
166///
167/// Derived from [`surface::card_border`] rather than chosen: it is the
168/// lightest step in the ramp, so a stripe of it stays inside the look
169/// whatever the user's theme does to the greys.
170pub fn row_tint() -> Color {
171 Color {
172 a: ROW_TINT_ALPHA,
173 ..surface::card_border()
174 }
175}
176
177/// The monospace font for anything shown as it is written — a config
178/// line, a key, a value.
179///
180/// The theme's family when it names one, and iced's generic monospace
181/// otherwise; `hyprforge_appearance::look` turns the fontconfig alias
182/// `Monospace` into "otherwise" before it gets here.
183///
184/// Interned once for the process, because an iced `Font` holds a
185/// `&'static str` and the theme is fixed for the process's lifetime
186/// anyway (see [`init`]).
187pub fn mono_font() -> iced::Font {
188 static MONO: std::sync::OnceLock<iced::Font> = std::sync::OnceLock::new();
189 *MONO.get_or_init(|| mono_font_for(&active().mono_font))
190}
191
192/// [`mono_font`] for a given family name, without the process-wide
193/// cache — separate so a test can reach it without installing a theme.
194fn mono_font_for(family: &str) -> iced::Font {
195 match family.trim() {
196 "" => iced::Font::MONOSPACE,
197 name => iced::Font::with_name(Box::leak(name.to_owned().into_boxed_str())),
198 }
199}
200
201fn palette() -> iced::theme::Palette {
202 let theme = active();
203 iced::theme::Palette {
204 background: crate::color::to_iced(theme.surfaces.root),
205 text: crate::color::to_iced(theme.surfaces.text),
206 // The accent is read, not chosen. It used to be a hand-picked
207 // violet with a comment saying it "matches this desktop's own
208 // window-border accent" — it was eyeballed, and it was wrong by
209 // a few points. Now it *is* that colour.
210 primary: crate::color::to_iced(theme.accent),
211 success: crate::color::to_iced(theme.success),
212 warning: crate::color::to_iced(theme.warning),
213 danger: crate::color::to_iced(theme.error),
214 }
215}
216
217/// Builds the `iced::Theme` every window in this process is created
218/// with, from the resolved [`active`] palette rather than an iced
219/// built-in — so a window matches the compositor's own accent instead of
220/// iced's stock dark theme.
221pub fn app_theme() -> iced::Theme {
222 iced::Theme::custom("Hyprforge".to_string(), palette())
223}
224
225#[cfg(test)]
226mod tests {
227 use super::*;
228
229 #[test]
230 fn default_font_scale_is_identity() {
231 assert_eq!(FontScale::default().apply(BASE_TEXT_SIZE), BASE_TEXT_SIZE);
232 }
233
234 #[test]
235 fn font_scale_multiplies_the_base_size() {
236 assert_eq!(FontScale(1.5).apply(16.0), 24.0);
237 assert_eq!(FontScale(0.5).apply(16.0), 8.0);
238 }
239}
240
241#[cfg(test)]
242mod palette_tests {
243 use super::*;
244
245 /// The values the Settings app shipped as compile-time constants,
246 /// before the palette became runtime.
247 ///
248 /// Making the look configurable must not quietly restyle anything.
249 /// If these ever need updating, that is a design decision and should
250 /// arrive as its own commit with a reason — not as a side effect of
251 /// plumbing.
252 #[test]
253 fn the_default_theme_reproduces_the_constants_it_replaced() {
254 let c = |r, g, b| iced::Color::from_rgb8(r, g, b);
255 assert_eq!(surface::root(), c(0x19, 0x1a, 0x21));
256 assert_eq!(surface::sidebar(), c(0x12, 0x13, 0x19));
257 assert_eq!(surface::card(), c(0x25, 0x27, 0x31));
258 assert_eq!(surface::card_border(), c(0x37, 0x3a, 0x47));
259 assert_eq!(surface::row(), c(0x1f, 0x21, 0x29));
260 assert_eq!(text(), c(0xe9, 0xea, 0xef));
261 assert_eq!(text_dim(), c(0x92, 0x96, 0xa4));
262 assert_eq!(warning(), c(0xf5, 0xb9, 0x42));
263 }
264
265 /// The stripe is the border colour made translucent — not a new
266 /// grey. If it ever becomes opaque it stops working on both of the
267 /// surfaces rows sit on at once.
268 #[test]
269 fn the_row_tint_is_the_border_colour_made_translucent() {
270 let tint = row_tint();
271 let border = surface::card_border();
272 assert_eq!((tint.r, tint.g, tint.b), (border.r, border.g, border.b));
273 assert!(tint.a > 0.0 && tint.a < 0.5, "a stripe, not a fill: {}", tint.a);
274 }
275
276 /// No family named means iced's own monospace, never the
277 /// proportional default a missing name would fall back to.
278 #[test]
279 fn an_unnamed_monospace_family_is_the_toolkits_monospace() {
280 assert_eq!(mono_font_for(""), iced::Font::MONOSPACE);
281 assert_eq!(mono_font_for(" "), iced::Font::MONOSPACE);
282 assert_eq!(mono_font_for("Hack"), iced::Font::with_name("Hack"));
283 }
284
285 /// An app that never calls `init` must render, not panic. The lock
286 /// screen's whole design rests on the same idea: the path where
287 /// everything else failed still has to put something legible on
288 /// screen.
289 #[test]
290 fn a_process_that_never_installs_a_theme_still_has_one() {
291 assert_eq!(active().accent, hyprforge_look::Theme::default().accent);
292 let _ = app_theme();
293 }
294}