use egui::{Color32, CornerRadius, Stroke, Visuals};
use makeover_geometry::{Density, Gap, Step, Surface, TARGET_LEAST, Text};
use crate::Palette;
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Metrics {
pub radius: CornerRadius,
pub focus_stroke: f32,
pub target: f32,
}
impl Default for Metrics {
fn default() -> Self {
Self {
radius: CornerRadius::ZERO,
focus_stroke: f32::from(makeover_geometry::Stroke::Mark.px()),
target: f32::from(TARGET_LEAST.px()),
}
}
}
impl Metrics {
#[must_use]
pub const fn radius(mut self, radius: CornerRadius) -> Self {
self.radius = radius;
self
}
#[must_use]
pub const fn focus_stroke(mut self, points: f32) -> Self {
self.focus_stroke = points;
self
}
#[must_use]
pub const fn target(mut self, points: f32) -> Self {
self.target = points;
self
}
}
#[must_use]
pub fn is_light(palette: &Palette) -> bool {
let channel = |c: u8| {
let c = f32::from(c) / 255.0;
if c <= 0.040_45 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
};
let page = palette.page;
let luminance = 0.2126_f32.mul_add(
channel(page.r()),
0.7152_f32.mul_add(channel(page.g()), 0.0722 * channel(page.b())),
);
luminance > LIGHT_ABOVE
}
const LIGHT_ABOVE: f32 = 0.179;
fn mix(from: Color32, to: Color32, at: f32) -> Color32 {
let channel = |from: u8, to: u8| {
let (from, to) = (f32::from(from), f32::from(to));
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "a mix of two channel values is inside 0..=255 by construction"
)]
let mixed = (to - from).mul_add(at, from).round() as u8;
mixed
};
Color32::from_rgb(
channel(from.r(), to.r()),
channel(from.g(), to.g()),
channel(from.b(), to.b()),
)
}
#[must_use]
pub fn visuals(palette: &Palette, metrics: &Metrics) -> Visuals {
let radius = metrics.radius;
let mut visuals = if is_light(palette) {
Visuals::light()
} else {
Visuals::dark()
};
visuals.panel_fill = palette.overlay;
visuals.window_fill = palette.overlay;
visuals.extreme_bg_color = palette.well;
visuals.faint_bg_color = mix(palette.page, palette.overlay, 0.3);
visuals.selection.bg_fill = mix(palette.page, palette.content, 0.3);
visuals.selection.stroke = Stroke::new(metrics.focus_stroke, palette.content);
visuals.widgets.noninteractive.bg_fill = palette.overlay;
visuals.widgets.inactive.bg_fill = mix(palette.overlay, palette.sunken, 0.3);
visuals.widgets.hovered.bg_fill = palette.sunken;
visuals.widgets.active.bg_fill = mix(palette.sunken, palette.content, 0.15);
visuals.widgets.noninteractive.weak_bg_fill = visuals.widgets.noninteractive.bg_fill;
visuals.widgets.inactive.weak_bg_fill = visuals.widgets.inactive.bg_fill;
visuals.widgets.hovered.weak_bg_fill = visuals.widgets.hovered.bg_fill;
visuals.widgets.active.weak_bg_fill = visuals.widgets.active.bg_fill;
visuals.widgets.open.weak_bg_fill = visuals.widgets.inactive.bg_fill;
let hairline = f32::from(makeover_geometry::Stroke::Hairline.px());
visuals.widgets.noninteractive.fg_stroke = Stroke::new(hairline, palette.content_secondary);
visuals.widgets.inactive.fg_stroke = Stroke::new(hairline, palette.content);
visuals.widgets.hovered.fg_stroke = Stroke::new(hairline, palette.content);
visuals.widgets.active.fg_stroke = Stroke::new(hairline, palette.content);
visuals.widgets.open.fg_stroke = Stroke::new(hairline, palette.content);
visuals.window_stroke = Stroke::new(hairline, palette.border);
visuals.widgets.inactive.bg_stroke =
Stroke::new(0.5, mix(palette.border, palette.overlay, 0.3));
visuals.widgets.hovered.bg_stroke = Stroke::new(hairline, palette.border);
visuals.widgets.active.bg_stroke = Stroke::new(hairline, palette.action);
visuals.widgets.noninteractive.bg_stroke =
Stroke::new(0.5, mix(palette.border, palette.overlay, 0.4));
visuals.widgets.noninteractive.corner_radius = radius;
visuals.widgets.inactive.corner_radius = radius;
visuals.widgets.hovered.corner_radius = radius;
visuals.widgets.active.corner_radius = radius;
visuals.widgets.open.corner_radius = radius;
visuals.window_corner_radius = radius;
visuals.menu_corner_radius = radius;
let shadow = palette.cast();
visuals.window_shadow = shadow;
visuals.popup_shadow = shadow;
visuals.widgets.hovered.expansion = 0.0;
visuals.widgets.active.expansion = 0.0;
visuals
}
#[must_use]
pub fn spacing(surface: &Surface, density: Density, metrics: &Metrics) -> egui::style::Spacing {
let gap = |g: Gap| surface.resolve(g.step_at(density).ratio());
let default = egui::style::Spacing::default();
egui::style::Spacing {
item_spacing: egui::vec2(gap(Gap::Peer), gap(Gap::Bound)),
button_padding: egui::vec2(gap(Gap::Peer), surface.resolve(Step::Hair.ratio())),
window_margin: egui::vec2(gap(Gap::Group), gap(Gap::Group)).into(),
indent: gap(Gap::Pane),
interact_size: egui::vec2(
default.interact_size.x,
metrics.target.max(f32::from(density.target_least_px())),
),
..default
}
}
#[must_use]
pub fn text_styles(surface: &Surface) -> std::collections::BTreeMap<egui::TextStyle, egui::FontId> {
use egui::{FontFamily, FontId, TextStyle};
let size = |role: Text| surface.resolve(role.ratio());
[
(
TextStyle::Small,
FontId::new(size(Text::Fine), FontFamily::Proportional),
),
(
TextStyle::Body,
FontId::new(size(Text::Body), FontFamily::Proportional),
),
(
TextStyle::Button,
FontId::new(size(Text::Body), FontFamily::Proportional),
),
(
TextStyle::Monospace,
FontId::new(size(Text::Note), FontFamily::Monospace),
),
(
TextStyle::Heading,
FontId::new(size(Text::Head), FontFamily::Proportional),
),
]
.into()
}
#[must_use]
pub fn style(
palette: &Palette,
surface: &Surface,
density: Density,
metrics: &Metrics,
) -> egui::Style {
egui::Style {
visuals: visuals(palette, metrics),
spacing: spacing(surface, density, metrics),
text_styles: text_styles(surface),
..egui::Style::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn palette(page: Color32, content: Color32) -> Palette {
Palette {
page,
content,
..crate::tests::palette()
}
}
fn light() -> Palette {
palette(Color32::from_rgb(200, 200, 200), Color32::BLACK)
}
fn dark() -> Palette {
palette(
Color32::from_rgb(26, 27, 38),
Color32::from_rgb(192, 202, 245),
)
}
#[test]
fn a_light_page_starts_from_the_light_preset_and_a_dark_one_does_not() {
assert!(is_light(&light()));
assert!(!is_light(&dark()));
assert!(!visuals(&light(), &Metrics::default()).dark_mode);
assert!(visuals(&dark(), &Metrics::default()).dark_mode);
}
#[test]
fn a_saturated_page_is_judged_by_luminance_and_not_by_an_average() {
let blue = palette(Color32::from_rgb(0, 0, 255), Color32::WHITE);
assert!(!is_light(&blue));
}
#[test]
fn a_mid_grey_page_reads_as_light() {
let grey = palette(Color32::from_rgb(0xA0, 0xA0, 0xA0), Color32::BLACK);
assert!(is_light(&grey));
let charcoal = palette(Color32::from_rgb(0x40, 0x40, 0x40), Color32::WHITE);
assert!(!is_light(&charcoal));
}
#[test]
fn every_widget_state_carries_a_weak_fill_as_well_as_a_fill() {
let v = visuals(&light(), &Metrics::default());
for (name, w) in [
("noninteractive", &v.widgets.noninteractive),
("inactive", &v.widgets.inactive),
("hovered", &v.widgets.hovered),
("active", &v.widgets.active),
] {
assert_eq!(w.weak_bg_fill, w.bg_fill, "{name} disagrees with itself");
}
}
#[test]
fn the_pressed_fill_is_not_the_action_colour() {
let p = light();
let v = visuals(&p, &Metrics::default());
assert_ne!(v.widgets.active.bg_fill, p.action);
assert_eq!(v.widgets.active.fg_stroke.color, p.content);
assert_eq!(v.selection.stroke.color, p.content);
}
#[test]
fn nothing_swells_under_the_cursor() {
let v = visuals(&light(), &Metrics::default());
assert!(v.widgets.hovered.expansion.abs() < f32::EPSILON);
assert!(v.widgets.active.expansion.abs() < f32::EPSILON);
}
#[test]
fn a_finger_gets_the_touch_floor() {
let s = spacing(&Surface::web(), Density::Touch, &Metrics::default());
assert!((s.interact_size.y - 44.0).abs() < f32::EPSILON);
let p = spacing(&Surface::web(), Density::Pointer, &Metrics::default());
assert!((p.interact_size.y - 24.0).abs() < f32::EPSILON);
}
#[test]
fn a_control_clears_the_target_floor_and_text_clears_the_readable_one() {
let s = spacing(&Surface::web(), Density::Pointer, &Metrics::default());
assert!(
s.interact_size.y >= 24.0,
"interact_size {} is under the target floor",
s.interact_size.y
);
let styles = text_styles(&Surface::web());
for (role, font) in &styles {
assert!(
font.size >= 11.0,
"{role:?} is {} points, under the readable floor",
font.size
);
}
}
#[test]
fn the_scale_orders_the_text_roles() {
let styles = text_styles(&Surface::web());
let size = |role: &egui::TextStyle| styles[role].size;
assert!(size(&egui::TextStyle::Small) < size(&egui::TextStyle::Body));
assert!(size(&egui::TextStyle::Heading) > size(&egui::TextStyle::Body));
}
#[test]
fn the_composed_style_is_the_three_pieces() {
let p = light();
let metrics = Metrics::default().radius(CornerRadius::same(4));
let composed = style(&p, &Surface::web(), Density::Pointer, &metrics);
assert_eq!(composed.visuals, visuals(&p, &metrics));
assert_eq!(
composed.spacing,
spacing(&Surface::web(), Density::Pointer, &metrics)
);
assert_eq!(composed.text_styles, text_styles(&Surface::web()));
}
#[test]
fn the_focus_ring_and_the_target_match_the_webview_until_the_app_says_otherwise() {
let v = visuals(&light(), &Metrics::default());
assert!((v.selection.stroke.width - 2.0).abs() < f32::EPSILON);
let heavier = visuals(&light(), &Metrics::default().focus_stroke(3.0));
assert!((heavier.selection.stroke.width - 3.0).abs() < f32::EPSILON);
let roomy = spacing(
&Surface::web(),
Density::Pointer,
&Metrics::default().target(32.0),
);
assert!((roomy.interact_size.y - 32.0).abs() < f32::EPSILON);
}
#[test]
fn repro_spacing_gap_quanta_as_points() {
let surface = Surface {
base: 16.0,
quantum: 2.0,
};
let s = spacing(&surface, Density::Pointer, &Metrics::default());
let size = |gap: Gap| surface.resolve(gap.step_at(Density::Pointer).ratio());
assert!((s.item_spacing.x - size(Gap::Peer)).abs() < f32::EPSILON);
assert!((s.item_spacing.y - size(Gap::Bound)).abs() < f32::EPSILON);
assert!((s.indent - size(Gap::Pane)).abs() < f32::EPSILON);
assert!((f32::from(s.window_margin.left) - size(Gap::Group)).abs() < f32::EPSILON);
}
}