1use egui::{Color32, CornerRadius, Stroke, Visuals};
21use makeover_geometry::{Density, Gap, Step, Surface, TARGET_LEAST, Text};
22
23use crate::Palette;
24
25#[derive(Debug, Clone, Copy, PartialEq)]
35#[non_exhaustive]
36pub struct Metrics {
37 pub radius: CornerRadius,
39 pub focus_stroke: f32,
41 pub target: f32,
43}
44
45impl Default for Metrics {
46 fn default() -> Self {
47 Self {
48 radius: CornerRadius::ZERO,
49 focus_stroke: f32::from(makeover_geometry::Stroke::Mark.px()),
50 target: f32::from(TARGET_LEAST.px()),
51 }
52 }
53}
54
55impl Metrics {
56 #[must_use]
58 pub const fn radius(mut self, radius: CornerRadius) -> Self {
59 self.radius = radius;
60 self
61 }
62
63 #[must_use]
65 pub const fn focus_stroke(mut self, points: f32) -> Self {
66 self.focus_stroke = points;
67 self
68 }
69
70 #[must_use]
72 pub const fn target(mut self, points: f32) -> Self {
73 self.target = points;
74 self
75 }
76}
77
78#[must_use]
86pub fn is_light(palette: &Palette) -> bool {
87 let channel = |c: u8| {
90 let c = f32::from(c) / 255.0;
91 if c <= 0.040_45 {
92 c / 12.92
93 } else {
94 ((c + 0.055) / 1.055).powf(2.4)
95 }
96 };
97 let page = palette.page;
98 let luminance = 0.2126_f32.mul_add(
99 channel(page.r()),
100 0.7152_f32.mul_add(channel(page.g()), 0.0722 * channel(page.b())),
101 );
102 luminance > LIGHT_ABOVE
104}
105
106const LIGHT_ABOVE: f32 = 0.179;
108
109fn mix(from: Color32, to: Color32, at: f32) -> Color32 {
111 let channel = |from: u8, to: u8| {
112 let (from, to) = (f32::from(from), f32::from(to));
113 #[expect(
114 clippy::cast_possible_truncation,
115 clippy::cast_sign_loss,
116 reason = "a mix of two channel values is inside 0..=255 by construction"
117 )]
118 let mixed = (to - from).mul_add(at, from).round() as u8;
119 mixed
120 };
121 Color32::from_rgb(
122 channel(from.r(), to.r()),
123 channel(from.g(), to.g()),
124 channel(from.b(), to.b()),
125 )
126}
127
128#[must_use]
142pub fn visuals(palette: &Palette, metrics: &Metrics) -> Visuals {
143 let radius = metrics.radius;
144 let mut visuals = if is_light(palette) {
145 Visuals::light()
146 } else {
147 Visuals::dark()
148 };
149
150 visuals.panel_fill = palette.overlay;
151 visuals.window_fill = palette.overlay;
152 visuals.extreme_bg_color = palette.well;
156 visuals.faint_bg_color = mix(palette.page, palette.overlay, 0.3);
157
158 visuals.selection.bg_fill = mix(palette.page, palette.content, 0.3);
159 visuals.selection.stroke = Stroke::new(metrics.focus_stroke, palette.content);
160
161 visuals.widgets.noninteractive.bg_fill = palette.overlay;
162 visuals.widgets.inactive.bg_fill = mix(palette.overlay, palette.sunken, 0.3);
163 visuals.widgets.hovered.bg_fill = palette.sunken;
164 visuals.widgets.active.bg_fill = mix(palette.sunken, palette.content, 0.15);
165
166 visuals.widgets.noninteractive.weak_bg_fill = visuals.widgets.noninteractive.bg_fill;
167 visuals.widgets.inactive.weak_bg_fill = visuals.widgets.inactive.bg_fill;
168 visuals.widgets.hovered.weak_bg_fill = visuals.widgets.hovered.bg_fill;
169 visuals.widgets.active.weak_bg_fill = visuals.widgets.active.bg_fill;
170 visuals.widgets.open.weak_bg_fill = visuals.widgets.inactive.bg_fill;
171
172 let hairline = f32::from(makeover_geometry::Stroke::Hairline.px());
173 visuals.widgets.noninteractive.fg_stroke = Stroke::new(hairline, palette.content_secondary);
174 visuals.widgets.inactive.fg_stroke = Stroke::new(hairline, palette.content);
175 visuals.widgets.hovered.fg_stroke = Stroke::new(hairline, palette.content);
176 visuals.widgets.active.fg_stroke = Stroke::new(hairline, palette.content);
177 visuals.widgets.open.fg_stroke = Stroke::new(hairline, palette.content);
178
179 visuals.window_stroke = Stroke::new(hairline, palette.border);
180 visuals.widgets.inactive.bg_stroke =
181 Stroke::new(0.5, mix(palette.border, palette.overlay, 0.3));
182 visuals.widgets.hovered.bg_stroke = Stroke::new(hairline, palette.border);
183 visuals.widgets.active.bg_stroke = Stroke::new(hairline, palette.action);
184 visuals.widgets.noninteractive.bg_stroke =
185 Stroke::new(0.5, mix(palette.border, palette.overlay, 0.4));
186
187 visuals.widgets.noninteractive.corner_radius = radius;
188 visuals.widgets.inactive.corner_radius = radius;
189 visuals.widgets.hovered.corner_radius = radius;
190 visuals.widgets.active.corner_radius = radius;
191 visuals.widgets.open.corner_radius = radius;
192 visuals.window_corner_radius = radius;
193 visuals.menu_corner_radius = radius;
194
195 let shadow = palette.cast();
199 visuals.window_shadow = shadow;
200 visuals.popup_shadow = shadow;
201
202 visuals.widgets.hovered.expansion = 0.0;
203 visuals.widgets.active.expansion = 0.0;
204
205 visuals
206}
207
208#[must_use]
218pub fn spacing(surface: &Surface, density: Density, metrics: &Metrics) -> egui::style::Spacing {
219 let gap = |g: Gap| surface.resolve(g.step_at(density).ratio());
222 let default = egui::style::Spacing::default();
223 egui::style::Spacing {
224 item_spacing: egui::vec2(gap(Gap::Peer), gap(Gap::Bound)),
225 button_padding: egui::vec2(gap(Gap::Peer), surface.resolve(Step::Hair.ratio())),
226 window_margin: egui::vec2(gap(Gap::Group), gap(Gap::Group)).into(),
227 indent: gap(Gap::Pane),
228 interact_size: egui::vec2(
229 default.interact_size.x,
230 metrics.target.max(f32::from(density.target_least_px())),
231 ),
232 ..default
233 }
234}
235
236#[must_use]
242pub fn text_styles(surface: &Surface) -> std::collections::BTreeMap<egui::TextStyle, egui::FontId> {
243 use egui::{FontFamily, FontId, TextStyle};
244 let size = |role: Text| surface.resolve(role.ratio());
245 [
246 (
247 TextStyle::Small,
248 FontId::new(size(Text::Fine), FontFamily::Proportional),
249 ),
250 (
251 TextStyle::Body,
252 FontId::new(size(Text::Body), FontFamily::Proportional),
253 ),
254 (
255 TextStyle::Button,
256 FontId::new(size(Text::Body), FontFamily::Proportional),
257 ),
258 (
259 TextStyle::Monospace,
260 FontId::new(size(Text::Note), FontFamily::Monospace),
261 ),
262 (
263 TextStyle::Heading,
264 FontId::new(size(Text::Head), FontFamily::Proportional),
265 ),
266 ]
267 .into()
268}
269
270#[must_use]
275pub fn style(
276 palette: &Palette,
277 surface: &Surface,
278 density: Density,
279 metrics: &Metrics,
280) -> egui::Style {
281 egui::Style {
282 visuals: visuals(palette, metrics),
283 spacing: spacing(surface, density, metrics),
284 text_styles: text_styles(surface),
285 ..egui::Style::default()
286 }
287}
288
289#[cfg(test)]
290mod tests {
291 use super::*;
292
293 fn palette(page: Color32, content: Color32) -> Palette {
294 Palette {
295 page,
296 content,
297 ..crate::tests::palette()
298 }
299 }
300
301 fn light() -> Palette {
302 palette(Color32::from_rgb(200, 200, 200), Color32::BLACK)
303 }
304
305 fn dark() -> Palette {
306 palette(
307 Color32::from_rgb(26, 27, 38),
308 Color32::from_rgb(192, 202, 245),
309 )
310 }
311
312 #[test]
313 fn a_light_page_starts_from_the_light_preset_and_a_dark_one_does_not() {
314 assert!(is_light(&light()));
318 assert!(!is_light(&dark()));
319 assert!(!visuals(&light(), &Metrics::default()).dark_mode);
320 assert!(visuals(&dark(), &Metrics::default()).dark_mode);
321 }
322
323 #[test]
324 fn a_saturated_page_is_judged_by_luminance_and_not_by_an_average() {
325 let blue = palette(Color32::from_rgb(0, 0, 255), Color32::WHITE);
328 assert!(!is_light(&blue));
329 }
330
331 #[test]
332 fn a_mid_grey_page_reads_as_light() {
333 let grey = palette(Color32::from_rgb(0xA0, 0xA0, 0xA0), Color32::BLACK);
336 assert!(is_light(&grey));
337 let charcoal = palette(Color32::from_rgb(0x40, 0x40, 0x40), Color32::WHITE);
338 assert!(!is_light(&charcoal));
339 }
340
341 #[test]
342 fn every_widget_state_carries_a_weak_fill_as_well_as_a_fill() {
343 let v = visuals(&light(), &Metrics::default());
348 for (name, w) in [
349 ("noninteractive", &v.widgets.noninteractive),
350 ("inactive", &v.widgets.inactive),
351 ("hovered", &v.widgets.hovered),
352 ("active", &v.widgets.active),
353 ] {
354 assert_eq!(w.weak_bg_fill, w.bg_fill, "{name} disagrees with itself");
355 }
356 }
357
358 #[test]
359 fn the_pressed_fill_is_not_the_action_colour() {
360 let p = light();
366 let v = visuals(&p, &Metrics::default());
367 assert_ne!(v.widgets.active.bg_fill, p.action);
368 assert_eq!(v.widgets.active.fg_stroke.color, p.content);
369 assert_eq!(v.selection.stroke.color, p.content);
370 }
371
372 #[test]
373 fn nothing_swells_under_the_cursor() {
374 let v = visuals(&light(), &Metrics::default());
378 assert!(v.widgets.hovered.expansion.abs() < f32::EPSILON);
379 assert!(v.widgets.active.expansion.abs() < f32::EPSILON);
380 }
381
382 #[test]
383 fn a_finger_gets_the_touch_floor() {
384 let s = spacing(&Surface::web(), Density::Touch, &Metrics::default());
385 assert!((s.interact_size.y - 44.0).abs() < f32::EPSILON);
386 let p = spacing(&Surface::web(), Density::Pointer, &Metrics::default());
387 assert!((p.interact_size.y - 24.0).abs() < f32::EPSILON);
388 }
389
390 #[test]
391 fn a_control_clears_the_target_floor_and_text_clears_the_readable_one() {
392 let s = spacing(&Surface::web(), Density::Pointer, &Metrics::default());
395 assert!(
396 s.interact_size.y >= 24.0,
397 "interact_size {} is under the target floor",
398 s.interact_size.y
399 );
400
401 let styles = text_styles(&Surface::web());
402 for (role, font) in &styles {
403 assert!(
404 font.size >= 11.0,
405 "{role:?} is {} points, under the readable floor",
406 font.size
407 );
408 }
409 }
410
411 #[test]
412 fn the_scale_orders_the_text_roles() {
413 let styles = text_styles(&Surface::web());
416 let size = |role: &egui::TextStyle| styles[role].size;
417 assert!(size(&egui::TextStyle::Small) < size(&egui::TextStyle::Body));
418 assert!(size(&egui::TextStyle::Heading) > size(&egui::TextStyle::Body));
419 }
420
421 #[test]
422 fn the_composed_style_is_the_three_pieces() {
423 let p = light();
424 let metrics = Metrics::default().radius(CornerRadius::same(4));
425 let composed = style(&p, &Surface::web(), Density::Pointer, &metrics);
426 assert_eq!(composed.visuals, visuals(&p, &metrics));
427 assert_eq!(
428 composed.spacing,
429 spacing(&Surface::web(), Density::Pointer, &metrics)
430 );
431 assert_eq!(composed.text_styles, text_styles(&Surface::web()));
432 }
433
434 #[test]
435 fn the_focus_ring_and_the_target_match_the_webview_until_the_app_says_otherwise() {
436 let v = visuals(&light(), &Metrics::default());
437 assert!((v.selection.stroke.width - 2.0).abs() < f32::EPSILON);
438 let heavier = visuals(&light(), &Metrics::default().focus_stroke(3.0));
439 assert!((heavier.selection.stroke.width - 3.0).abs() < f32::EPSILON);
440 let roomy = spacing(
441 &Surface::web(),
442 Density::Pointer,
443 &Metrics::default().target(32.0),
444 );
445 assert!((roomy.interact_size.y - 32.0).abs() < f32::EPSILON);
446 }
447
448 #[test]
449 fn repro_spacing_gap_quanta_as_points() {
450 let surface = Surface {
453 base: 16.0,
454 quantum: 2.0,
455 };
456 let s = spacing(&surface, Density::Pointer, &Metrics::default());
457 let size = |gap: Gap| surface.resolve(gap.step_at(Density::Pointer).ratio());
458 assert!((s.item_spacing.x - size(Gap::Peer)).abs() < f32::EPSILON);
459 assert!((s.item_spacing.y - size(Gap::Bound)).abs() < f32::EPSILON);
460 assert!((s.indent - size(Gap::Pane)).abs() < f32::EPSILON);
461 assert!((f32::from(s.window_margin.left) - size(Gap::Group)).abs() < f32::EPSILON);
462 }
463}