use eframe::egui::{self, Color32};
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Mode {
Dark,
Light,
}
impl Mode {
pub fn of(dark: bool) -> Self {
if dark { Mode::Dark } else { Mode::Light }
}
pub fn label(self) -> &'static str {
match self {
Mode::Dark => "Dark",
Mode::Light => "Light",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Density {
Compact,
Comfortable,
}
impl Density {
pub fn label(self) -> &'static str {
match self {
Density::Compact => "Compact",
Density::Comfortable => "Comfortable",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Accent {
pub key: &'static str,
pub label: &'static str,
pub color: Color32,
}
pub const ACCENTS: &[Accent] = &[
Accent { key: "blue", label: "Blue #3d8bfd (proposed)", color: Color32::from_rgb(0x3d, 0x8b, 0xfd) },
Accent { key: "brand", label: "Help-site blue #0b6bcb", color: Color32::from_rgb(0x0b, 0x6b, 0xcb) },
Accent { key: "teal", label: "Teal #14b8a6", color: Color32::from_rgb(0x14, 0xb8, 0xa6) },
Accent { key: "violet", label: "Violet #8b5cf6", color: Color32::from_rgb(0x8b, 0x5c, 0xf6) },
Accent { key: "orange", label: "Orange #f08c2e", color: Color32::from_rgb(0xf0, 0x8c, 0x2e) },
Accent { key: "green", label: "Green #2fb170", color: Color32::from_rgb(0x2f, 0xb1, 0x70) },
];
pub fn accent(key: &str) -> Option<Accent> {
ACCENTS.iter().copied().find(|a| a.key == key)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Palette {
pub bg_app: Color32,
pub bg_panel: Color32,
pub bg_raised: Color32,
pub bg_input: Color32,
pub hover: Color32,
pub pressed: Color32,
pub border_subtle: Color32,
pub border_strong: Color32,
pub accent: Color32,
pub accent_fill: Color32,
pub accent_text: Color32,
pub accent_ring: Color32,
pub accent_muted: Color32,
pub on_accent: Color32,
pub text_primary: Color32,
pub text_secondary: Color32,
pub text_disabled: Color32,
pub danger: Color32,
pub danger_fill: Color32,
pub warning: Color32,
pub success: Color32,
pub shadow: Color32,
}
const fn rgb(hex: u32) -> Color32 {
Color32::from_rgb((hex >> 16) as u8, (hex >> 8) as u8, hex as u8)
}
pub fn luminance(c: Color32) -> f32 {
let f = |v: u8| {
let v = f32::from(v) / 255.0;
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
};
0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b())
}
pub fn contrast(a: Color32, b: Color32) -> f32 {
let (la, lb) = (luminance(a), luminance(b));
(la.max(lb) + 0.05) / (la.min(lb) + 0.05)
}
pub fn over(base: Color32, top: Color32) -> Color32 {
let [r, g, b, a] = top.to_srgba_unmultiplied();
mix(base, Color32::from_rgb(r, g, b), f32::from(a) / 255.0)
}
pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
let ch = |x: u8, y: u8| (f32::from(x) * (1.0 - t) + f32::from(y) * t).round() as u8;
Color32::from_rgb(ch(a.r(), b.r()), ch(a.g(), b.g()), ch(a.b(), b.b()))
}
fn alpha(c: Color32, a: f32) -> Color32 {
Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), (a * 255.0).round() as u8)
}
fn moved_until(color: Color32, toward: Color32, ok: impl Fn(Color32) -> bool) -> Color32 {
(0..=50).map(|step| mix(color, toward, step as f32 * 0.02)).find(|c| ok(*c)).unwrap_or(toward)
}
impl Palette {
pub fn new(mode: Mode, accent: Color32) -> Self {
let white = Color32::WHITE;
let black = Color32::BLACK;
let mut p = match mode {
Mode::Dark => Palette {
bg_app: rgb(0x0f151c),
bg_panel: rgb(0x151d26),
bg_raised: rgb(0x1b2531),
bg_input: rgb(0x0f151c),
hover: alpha(white, 0.06),
pressed: alpha(white, 0.10),
border_subtle: rgb(0x263241),
border_strong: rgb(0x334255),
accent,
accent_fill: accent,
accent_text: accent,
accent_ring: accent,
accent_muted: alpha(accent, 0.18),
on_accent: white,
text_primary: rgb(0xe6edf3),
text_secondary: rgb(0x9fb0c0),
text_disabled: rgb(0x5c6b7a),
danger: rgb(0xf26464),
danger_fill: rgb(0xf05252),
warning: rgb(0xf5a524),
success: rgb(0x3fb950),
shadow: alpha(black, 0.45),
},
Mode::Light => Palette {
bg_app: rgb(0xe9edf2),
bg_panel: rgb(0xf4f6f9),
bg_raised: rgb(0xffffff),
bg_input: rgb(0xffffff),
hover: alpha(black, 0.05),
pressed: alpha(black, 0.09),
border_subtle: rgb(0xd3dae3),
border_strong: rgb(0xaeb9c6),
accent,
accent_fill: accent,
accent_text: accent,
accent_ring: accent,
accent_muted: alpha(accent, 0.18),
on_accent: white,
text_primary: rgb(0x17212b),
text_secondary: rgb(0x4a5868),
text_disabled: rgb(0x8d99a6),
danger: rgb(0xc62828),
danger_fill: rgb(0xc62828),
warning: rgb(0x8f5d00),
success: rgb(0x177332),
shadow: alpha(black, 0.18),
},
};
let surfaces = [p.bg_app, p.bg_panel, p.bg_raised, p.bg_input];
let away = if mode == Mode::Dark { white } else { black };
let (hover, pressed, muted) = (p.hover, p.pressed, p.accent_muted);
let carries_white = |fill: Color32| contrast(white, fill) >= 4.5;
p.accent_fill = moved_until(accent, black, carries_white);
p.danger_fill = moved_until(p.danger_fill, black, carries_white);
p.accent_ring = moved_until(accent, away, |c| surfaces.iter().all(|bg| contrast(c, *bg) >= 3.0));
let states: Vec<Color32> = surfaces
.iter()
.flat_map(|s| [*s, over(*s, hover), over(*s, pressed), over(*s, muted), over(over(*s, muted), hover)])
.collect();
p.accent_text = moved_until(accent, away, |c| states.iter().all(|bg| contrast(c, *bg) >= 4.5));
p
}
pub fn fill_hover(fill: Color32) -> Color32 {
mix(fill, Color32::BLACK, 0.08)
}
pub fn fill_pressed(fill: Color32) -> Color32 {
mix(fill, Color32::BLACK, 0.16)
}
}
pub mod space {
pub const XXS: f32 = 2.0;
pub const XS: f32 = 4.0;
pub const SM: f32 = 6.0;
pub const MD: f32 = 8.0;
pub const LG: f32 = 12.0;
pub const XL: f32 = 16.0;
pub const XXL: f32 = 24.0;
pub const XXXL: f32 = 32.0;
}
pub mod radius {
pub const INPUT: f32 = 4.0;
pub const BUTTON: f32 = 6.0;
pub const CARD: f32 = 8.0;
}
pub const BORDER: f32 = 1.0;
pub const RING: f32 = 2.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Text {
Caption,
Body,
Label,
Section,
Title,
Code,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Families {
pub text: egui::FontFamily,
pub strong: egui::FontFamily,
pub mono: egui::FontFamily,
}
impl Default for Families {
fn default() -> Self {
Self {
text: egui::FontFamily::Proportional,
strong: egui::FontFamily::Proportional,
mono: egui::FontFamily::Monospace,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Theme {
pub mode: Mode,
pub density: Density,
pub colors: Palette,
pub families: Families,
pub reduce_motion: bool,
}
impl Default for Theme {
fn default() -> Self {
Theme::new(Mode::Dark, Density::Compact, ACCENTS[0].color)
}
}
impl Theme {
pub fn new(mode: Mode, density: Density, accent: Color32) -> Self {
Theme { mode, density, colors: Palette::new(mode, accent), families: Families::default(), reduce_motion: false }
}
pub fn with_families(mut self, families: Families) -> Self {
self.families = families;
self
}
pub fn with_reduce_motion(mut self, reduce: bool) -> Self {
self.reduce_motion = reduce;
self
}
pub fn control_height(&self) -> f32 {
match self.density {
Density::Compact => 24.0,
Density::Comfortable => 28.0,
}
}
pub fn control_height_sm(&self) -> f32 {
24.0
}
pub fn row_height(&self) -> f32 {
match self.density {
Density::Compact => 24.0,
Density::Comfortable => 28.0,
}
}
pub fn control_padding(&self) -> f32 {
match self.density {
Density::Compact => space::MD,
Density::Comfortable => space::LG,
}
}
pub fn font(&self, role: Text) -> egui::FontId {
let (size, family) = match role {
Text::Caption => (11.0, &self.families.text),
Text::Body => (13.0, &self.families.text),
Text::Label => (12.0, &self.families.strong),
Text::Section => (14.0, &self.families.strong),
Text::Title => (16.0, &self.families.strong),
Text::Code => (12.0, &self.families.mono),
};
egui::FontId::new(size, family.clone())
}
fn seconds(&self, s: f32) -> f32 {
if self.reduce_motion { 0.0 } else { s }
}
pub fn motion_fast(&self) -> f32 {
self.seconds(0.08)
}
pub fn motion_base(&self) -> f32 {
self.seconds(0.12)
}
pub fn motion_slow(&self) -> f32 {
self.seconds(0.16)
}
pub fn shadow_small(&self) -> egui::Shadow {
egui::Shadow { offset: [0, 2], blur: 8, spread: 0, color: self.colors.shadow }
}
pub fn shadow_medium(&self) -> egui::Shadow {
egui::Shadow { offset: [0, 6], blur: 20, spread: 0, color: self.colors.shadow }
}
pub fn raised_frame(&self) -> egui::Frame {
egui::Frame::new()
.fill(self.colors.bg_raised)
.stroke(egui::Stroke::new(BORDER, self.colors.border_subtle))
.corner_radius(radius::CARD)
}
pub fn apply(&self, style: &mut egui::Style) {
let c = &self.colors;
style.text_styles = [
(egui::TextStyle::Small, self.font(Text::Caption)),
(egui::TextStyle::Body, self.font(Text::Body)),
(egui::TextStyle::Button, self.font(Text::Body)),
(egui::TextStyle::Heading, self.font(Text::Title)),
(egui::TextStyle::Monospace, self.font(Text::Code)),
]
.into();
style.animation_time = self.motion_base();
let s = &mut style.spacing;
let compact = self.density == Density::Compact;
s.item_spacing = if compact { egui::vec2(space::SM, space::XS) } else { egui::vec2(space::MD, space::SM) };
s.button_padding = egui::vec2(self.control_padding(), if compact { 3.0 } else { 5.0 });
s.interact_size = egui::vec2(40.0, self.control_height());
s.window_margin = egui::Margin::same(space::LG as i8);
s.menu_margin = egui::Margin::same(space::SM as i8);
s.indent = space::XL;
s.icon_width = 16.0;
s.icon_width_inner = 10.0;
s.icon_spacing = space::SM;
s.slider_rail_height = 4.0;
s.combo_height = 28.0 * 12.0;
s.scroll.bar_width = 6.0;
s.scroll.handle_min_length = 24.0;
let v = &mut style.visuals;
*v = if self.mode == Mode::Dark { egui::Visuals::dark() } else { egui::Visuals::light() };
v.panel_fill = c.bg_panel;
v.window_fill = c.bg_raised;
v.extreme_bg_color = c.bg_input;
v.text_edit_bg_color = Some(c.bg_input);
v.faint_bg_color = over(c.bg_panel, c.hover);
v.code_bg_color = c.bg_input;
v.hyperlink_color = c.accent_text;
v.warn_fg_color = c.warning;
v.error_fg_color = c.danger;
v.weak_text_color = Some(c.text_secondary);
v.window_corner_radius = egui::CornerRadius::same(radius::CARD as u8);
v.menu_corner_radius = egui::CornerRadius::same(radius::CARD as u8);
v.window_stroke = egui::Stroke::new(BORDER, c.border_subtle);
v.window_shadow = self.shadow_medium();
v.popup_shadow = self.shadow_small();
v.selection.bg_fill = over(c.bg_panel, alpha(c.accent, 0.35));
v.selection.stroke = egui::Stroke::new(BORDER, c.accent_ring);
v.text_cursor.stroke = egui::Stroke::new(RING, c.accent_ring);
v.slider_trailing_fill = true;
v.handle_shape = egui::style::HandleShape::Circle;
v.striped = false;
let button = egui::CornerRadius::same(radius::BUTTON as u8);
let w = &mut v.widgets;
w.noninteractive.bg_fill = c.bg_panel;
w.noninteractive.weak_bg_fill = c.bg_panel;
w.noninteractive.bg_stroke = egui::Stroke::new(BORDER, c.border_subtle);
w.noninteractive.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
w.noninteractive.corner_radius = button;
w.inactive.bg_fill = c.bg_input;
w.inactive.weak_bg_fill = c.bg_raised;
w.inactive.bg_stroke = egui::Stroke::new(BORDER, c.border_strong);
w.inactive.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
w.inactive.corner_radius = button;
w.inactive.expansion = 0.0;
w.hovered.bg_fill = over(c.bg_input, c.hover);
w.hovered.weak_bg_fill = over(c.bg_raised, c.hover);
w.hovered.bg_stroke = egui::Stroke::new(BORDER, c.border_strong);
w.hovered.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
w.hovered.corner_radius = button;
w.hovered.expansion = 0.0;
w.active.bg_fill = over(c.bg_input, c.pressed);
w.active.weak_bg_fill = over(c.bg_raised, c.pressed);
w.active.bg_stroke = egui::Stroke::new(BORDER, c.accent_ring);
w.active.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
w.active.corner_radius = button;
w.active.expansion = 0.0;
w.open.bg_fill = over(c.bg_raised, c.pressed);
w.open.weak_bg_fill = over(c.bg_raised, c.pressed);
w.open.bg_stroke = egui::Stroke::new(BORDER, c.accent_ring);
w.open.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
w.open.corner_radius = button;
w.open.expansion = 0.0;
}
pub fn style(&self, base: &egui::Style) -> egui::Style {
let mut style = base.clone();
self.apply(&mut style);
style
}
pub fn of(ui: &egui::Ui) -> Arc<Theme> {
current(ui.ctx())
}
}
fn slot() -> egui::Id {
egui::Id::new("brep-theme")
}
pub fn set(ctx: &egui::Context, theme: Arc<Theme>) {
ctx.data_mut(|d| d.insert_temp(slot(), theme));
}
pub fn clear(ctx: &egui::Context) {
ctx.data_mut(|d| d.remove::<Arc<Theme>>(slot()));
}
pub fn stored(ctx: &egui::Context) -> Option<Arc<Theme>> {
ctx.data(|d| d.get_temp::<Arc<Theme>>(slot()))
}
pub fn current(ctx: &egui::Context) -> Arc<Theme> {
stored(ctx).unwrap_or_default()
}