1use eframe::egui::{self, Color32};
14use std::sync::Arc;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
19pub enum Mode {
20 Dark,
21 Light,
22}
23
24impl Mode {
25 pub fn of(dark: bool) -> Self {
26 if dark { Mode::Dark } else { Mode::Light }
27 }
28 pub fn label(self) -> &'static str {
29 match self {
30 Mode::Dark => "Dark",
31 Mode::Light => "Light",
32 }
33 }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum Density {
39 Compact,
40 Comfortable,
41}
42
43impl Density {
44 pub fn label(self) -> &'static str {
45 match self {
46 Density::Compact => "Compact",
47 Density::Comfortable => "Comfortable",
48 }
49 }
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub struct Accent {
55 pub key: &'static str,
57 pub label: &'static str,
58 pub color: Color32,
59}
60
61pub const ACCENTS: &[Accent] = &[
63 Accent { key: "blue", label: "Blue #3d8bfd (proposed)", color: Color32::from_rgb(0x3d, 0x8b, 0xfd) },
64 Accent { key: "brand", label: "Help-site blue #0b6bcb", color: Color32::from_rgb(0x0b, 0x6b, 0xcb) },
65 Accent { key: "teal", label: "Teal #14b8a6", color: Color32::from_rgb(0x14, 0xb8, 0xa6) },
66 Accent { key: "violet", label: "Violet #8b5cf6", color: Color32::from_rgb(0x8b, 0x5c, 0xf6) },
67 Accent { key: "orange", label: "Orange #f08c2e", color: Color32::from_rgb(0xf0, 0x8c, 0x2e) },
68 Accent { key: "green", label: "Green #2fb170", color: Color32::from_rgb(0x2f, 0xb1, 0x70) },
69];
70
71pub fn accent(key: &str) -> Option<Accent> {
73 ACCENTS.iter().copied().find(|a| a.key == key)
74}
75
76#[derive(Debug, Clone, Copy, PartialEq)]
79pub struct Palette {
80 pub bg_app: Color32,
82 pub bg_panel: Color32,
84 pub bg_raised: Color32,
86 pub bg_input: Color32,
88 pub hover: Color32,
90 pub pressed: Color32,
92 pub border_subtle: Color32,
94 pub border_strong: Color32,
96 pub accent: Color32,
98 pub accent_fill: Color32,
101 pub accent_text: Color32,
104 pub accent_ring: Color32,
106 pub accent_muted: Color32,
109 pub on_accent: Color32,
111 pub text_primary: Color32,
112 pub text_secondary: Color32,
113 pub text_disabled: Color32,
115 pub danger: Color32,
116 pub danger_fill: Color32,
118 pub warning: Color32,
119 pub success: Color32,
120 pub info: Color32,
123 pub shadow: Color32,
125}
126
127const fn rgb(hex: u32) -> Color32 {
128 Color32::from_rgb((hex >> 16) as u8, (hex >> 8) as u8, hex as u8)
129}
130
131pub fn luminance(c: Color32) -> f32 {
133 let f = |v: u8| {
134 let v = f32::from(v) / 255.0;
135 if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
136 };
137 0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b())
138}
139
140pub fn contrast(a: Color32, b: Color32) -> f32 {
142 let (la, lb) = (luminance(a), luminance(b));
143 (la.max(lb) + 0.05) / (la.min(lb) + 0.05)
144}
145
146pub fn over(base: Color32, top: Color32) -> Color32 {
148 let [r, g, b, a] = top.to_srgba_unmultiplied();
149 mix(base, Color32::from_rgb(r, g, b), f32::from(a) / 255.0)
150}
151
152pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
154 let ch = |x: u8, y: u8| (f32::from(x) * (1.0 - t) + f32::from(y) * t).round() as u8;
155 Color32::from_rgb(ch(a.r(), b.r()), ch(a.g(), b.g()), ch(a.b(), b.b()))
156}
157
158fn alpha(c: Color32, a: f32) -> Color32 {
159 Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), (a * 255.0).round() as u8)
160}
161
162fn moved_until(color: Color32, toward: Color32, ok: impl Fn(Color32) -> bool) -> Color32 {
165 (0..=50).map(|step| mix(color, toward, step as f32 * 0.02)).find(|c| ok(*c)).unwrap_or(toward)
166}
167
168impl Palette {
169 pub fn new(mode: Mode, accent: Color32) -> Self {
170 let white = Color32::WHITE;
171 let black = Color32::BLACK;
172 let mut p = match mode {
173 Mode::Dark => Palette {
174 bg_app: rgb(0x0f151c),
175 bg_panel: rgb(0x151d26),
176 bg_raised: rgb(0x1b2531),
177 bg_input: rgb(0x0f151c),
178 hover: alpha(white, 0.06),
179 pressed: alpha(white, 0.10),
180 border_subtle: rgb(0x263241),
181 border_strong: rgb(0x334255),
182 accent,
183 accent_fill: accent,
184 accent_text: accent,
185 accent_ring: accent,
186 accent_muted: alpha(accent, 0.18),
187 on_accent: white,
188 text_primary: rgb(0xe6edf3),
189 text_secondary: rgb(0x9fb0c0),
190 text_disabled: rgb(0x5c6b7a),
191 danger: rgb(0xf26464),
193 danger_fill: rgb(0xf05252),
194 warning: rgb(0xf5a524),
195 success: rgb(0x3fb950),
196 info: ACCENTS[0].color,
197 shadow: alpha(black, 0.45),
198 },
199 Mode::Light => Palette {
200 bg_app: rgb(0xe9edf2),
201 bg_panel: rgb(0xf4f6f9),
202 bg_raised: rgb(0xffffff),
203 bg_input: rgb(0xffffff),
204 hover: alpha(black, 0.05),
205 pressed: alpha(black, 0.09),
206 border_subtle: rgb(0xd3dae3),
207 border_strong: rgb(0xaeb9c6),
208 accent,
209 accent_fill: accent,
210 accent_text: accent,
211 accent_ring: accent,
212 accent_muted: alpha(accent, 0.18),
213 on_accent: white,
214 text_primary: rgb(0x17212b),
215 text_secondary: rgb(0x4a5868),
216 text_disabled: rgb(0x8d99a6),
217 danger: rgb(0xc62828),
218 danger_fill: rgb(0xc62828),
219 warning: rgb(0x8f5d00),
220 success: rgb(0x177332),
221 info: ACCENTS[0].color,
222 shadow: alpha(black, 0.18),
223 },
224 };
225 let surfaces = [p.bg_app, p.bg_panel, p.bg_raised, p.bg_input];
228 let away = if mode == Mode::Dark { white } else { black };
229 let (hover, pressed, muted) = (p.hover, p.pressed, p.accent_muted);
230 let carries_white = |fill: Color32| contrast(white, fill) >= 4.5;
233 p.accent_fill = moved_until(accent, black, carries_white);
234 p.danger_fill = moved_until(p.danger_fill, black, carries_white);
235 p.accent_ring = moved_until(accent, away, |c| surfaces.iter().all(|bg| contrast(c, *bg) >= 3.0));
236 let states: Vec<Color32> = surfaces
238 .iter()
239 .flat_map(|s| [*s, over(*s, hover), over(*s, pressed), over(*s, muted), over(over(*s, muted), hover)])
240 .collect();
241 p.accent_text = moved_until(accent, away, |c| states.iter().all(|bg| contrast(c, *bg) >= 4.5));
242 p.info = moved_until(ACCENTS[0].color, away, |c| states.iter().all(|bg| contrast(c, *bg) >= 4.5));
243 p
244 }
245
246 pub fn fill_hover(fill: Color32) -> Color32 {
249 mix(fill, Color32::BLACK, 0.08)
250 }
251
252 pub fn fill_pressed(fill: Color32) -> Color32 {
254 mix(fill, Color32::BLACK, 0.16)
255 }
256}
257
258pub mod space {
260 pub const XXS: f32 = 2.0;
261 pub const XS: f32 = 4.0;
262 pub const SM: f32 = 6.0;
263 pub const MD: f32 = 8.0;
264 pub const LG: f32 = 12.0;
265 pub const XL: f32 = 16.0;
266 pub const XXL: f32 = 24.0;
267 pub const XXXL: f32 = 32.0;
268}
269
270pub mod radius {
272 pub const INPUT: f32 = 4.0;
274 pub const BUTTON: f32 = 6.0;
276 pub const CARD: f32 = 8.0;
278}
279
280pub const BORDER: f32 = 1.0;
282pub const RING: f32 = 2.0;
283
284#[derive(Debug, Clone, Copy, PartialEq, Eq)]
286pub enum Text {
287 Caption,
289 Body,
291 Label,
293 Section,
295 Title,
297 Code,
299 Data,
303}
304
305#[derive(Debug, Clone, PartialEq, Eq)]
309pub struct Families {
310 pub text: egui::FontFamily,
311 pub strong: egui::FontFamily,
313 pub mono: egui::FontFamily,
314}
315
316impl Default for Families {
317 fn default() -> Self {
318 Self {
319 text: egui::FontFamily::Proportional,
320 strong: egui::FontFamily::Proportional,
321 mono: egui::FontFamily::Monospace,
322 }
323 }
324}
325
326#[derive(Debug, Clone, PartialEq)]
328pub struct Theme {
329 pub mode: Mode,
330 pub density: Density,
331 pub colors: Palette,
332 pub families: Families,
333 pub reduce_motion: bool,
335 pub min_target: f32,
339}
340
341impl Default for Theme {
342 fn default() -> Self {
343 Theme::new(Mode::Dark, Density::Compact, ACCENTS[0].color)
344 }
345}
346
347impl Theme {
348 pub fn new(mode: Mode, density: Density, accent: Color32) -> Self {
349 Theme { mode, density, colors: Palette::new(mode, accent), families: Families::default(), reduce_motion: false, min_target: 0.0 }
350 }
351
352 pub fn with_families(mut self, families: Families) -> Self {
353 self.families = families;
354 self
355 }
356
357 pub fn with_min_target(mut self, min_target: f32) -> Self {
358 self.min_target = min_target;
359 self
360 }
361
362 pub fn with_reduce_motion(mut self, reduce: bool) -> Self {
363 self.reduce_motion = reduce;
364 self
365 }
366
367 pub fn control_height(&self) -> f32 {
369 let height: f32 = match self.density {
370 Density::Compact => 24.0,
371 Density::Comfortable => 28.0,
372 };
373 height.max(self.min_target)
374 }
375
376 pub fn control_height_sm(&self) -> f32 {
378 24.0_f32.max(self.min_target)
379 }
380
381 pub fn row_height(&self) -> f32 {
383 let height: f32 = match self.density {
384 Density::Compact => 24.0,
385 Density::Comfortable => 28.0,
386 };
387 height.max(self.min_target)
388 }
389
390 pub fn control_padding(&self) -> f32 {
392 match self.density {
393 Density::Compact => space::MD,
394 Density::Comfortable => space::LG,
395 }
396 }
397
398 pub fn font(&self, role: Text) -> egui::FontId {
399 let (size, family) = match role {
400 Text::Caption => (11.0, &self.families.text),
401 Text::Body => (13.0, &self.families.text),
402 Text::Label => (12.0, &self.families.strong),
403 Text::Section => (14.0, &self.families.strong),
404 Text::Title => (16.0, &self.families.strong),
405 Text::Code | Text::Data => (12.0, &self.families.mono),
406 };
407 egui::FontId::new(size, family.clone())
408 }
409
410 fn seconds(&self, s: f32) -> f32 {
411 if self.reduce_motion { 0.0 } else { s }
412 }
413 pub fn motion_fast(&self) -> f32 {
415 self.seconds(0.08)
416 }
417 pub fn motion_base(&self) -> f32 {
419 self.seconds(0.12)
420 }
421 pub fn motion_slow(&self) -> f32 {
423 self.seconds(0.16)
424 }
425
426 pub fn shadow_small(&self) -> egui::Shadow {
428 egui::Shadow { offset: [0, 2], blur: 8, spread: 0, color: self.colors.shadow }
429 }
430 pub fn shadow_medium(&self) -> egui::Shadow {
432 egui::Shadow { offset: [0, 6], blur: 20, spread: 0, color: self.colors.shadow }
433 }
434
435 pub fn raised_frame(&self) -> egui::Frame {
437 egui::Frame::new()
438 .fill(self.colors.bg_raised)
439 .stroke(egui::Stroke::new(BORDER, self.colors.border_subtle))
440 .corner_radius(radius::CARD)
441 }
442
443 pub fn apply(&self, style: &mut egui::Style) {
447 let c = &self.colors;
448 style.text_styles = [
449 (egui::TextStyle::Small, self.font(Text::Caption)),
450 (egui::TextStyle::Body, self.font(Text::Body)),
451 (egui::TextStyle::Button, self.font(Text::Body)),
452 (egui::TextStyle::Heading, self.font(Text::Title)),
453 (egui::TextStyle::Monospace, self.font(Text::Code)),
454 ]
455 .into();
456 style.animation_time = self.motion_base();
457
458 let s = &mut style.spacing;
459 let compact = self.density == Density::Compact;
460 s.item_spacing = if compact { egui::vec2(space::SM, space::XS) } else { egui::vec2(space::MD, space::SM) };
461 s.button_padding = egui::vec2(self.control_padding(), if compact { 3.0 } else { 5.0 });
462 s.interact_size = egui::vec2(40.0, self.control_height());
463 s.window_margin = egui::Margin::same(space::LG as i8);
464 s.menu_margin = egui::Margin::same(space::SM as i8);
465 s.indent = space::XL;
466 s.icon_width = 16.0;
467 s.icon_width_inner = 10.0;
468 s.icon_spacing = space::SM;
469 s.slider_rail_height = 4.0;
470 s.combo_height = 28.0 * 12.0;
471 s.scroll.bar_width = 6.0;
474 s.scroll.handle_min_length = 24.0;
475
476 let v = &mut style.visuals;
477 *v = if self.mode == Mode::Dark { egui::Visuals::dark() } else { egui::Visuals::light() };
478 v.panel_fill = c.bg_panel;
479 v.window_fill = c.bg_raised;
480 v.extreme_bg_color = c.bg_input;
481 v.text_edit_bg_color = Some(c.bg_input);
482 v.faint_bg_color = over(c.bg_panel, c.hover);
483 v.code_bg_color = c.bg_input;
484 v.hyperlink_color = c.accent_text;
485 v.warn_fg_color = c.warning;
486 v.error_fg_color = c.danger;
487 v.weak_text_color = Some(c.text_secondary);
488 v.window_corner_radius = egui::CornerRadius::same(radius::CARD as u8);
489 v.menu_corner_radius = egui::CornerRadius::same(radius::CARD as u8);
490 v.window_stroke = egui::Stroke::new(BORDER, c.border_subtle);
491 v.window_shadow = self.shadow_medium();
492 v.popup_shadow = self.shadow_small();
493 v.selection.bg_fill = over(c.bg_panel, c.accent_muted);
496 v.selection.stroke = egui::Stroke::new(BORDER, c.accent_text);
497 v.text_cursor.stroke = egui::Stroke::new(RING, c.accent_ring);
498 v.slider_trailing_fill = true;
499 v.handle_shape = egui::style::HandleShape::Circle;
500 v.striped = false;
501
502 let button = egui::CornerRadius::same(radius::BUTTON as u8);
503 let w = &mut v.widgets;
504 w.noninteractive.bg_fill = c.bg_panel;
506 w.noninteractive.weak_bg_fill = c.bg_panel;
507 w.noninteractive.bg_stroke = egui::Stroke::new(BORDER, c.border_subtle);
508 w.noninteractive.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
509 w.noninteractive.corner_radius = button;
510 w.inactive.bg_fill = c.bg_input;
514 w.inactive.weak_bg_fill = c.bg_raised;
515 w.inactive.bg_stroke = egui::Stroke::new(BORDER, c.border_strong);
516 w.inactive.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
517 w.inactive.corner_radius = button;
518 w.inactive.expansion = 0.0;
519 w.hovered.bg_fill = over(c.bg_input, c.hover);
520 w.hovered.weak_bg_fill = over(c.bg_raised, c.hover);
521 w.hovered.bg_stroke = egui::Stroke::new(BORDER, c.border_strong);
522 w.hovered.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
523 w.hovered.corner_radius = button;
524 w.hovered.expansion = 0.0;
525 w.active.bg_fill = over(c.bg_input, c.pressed);
526 w.active.weak_bg_fill = over(c.bg_raised, c.pressed);
527 w.active.bg_stroke = egui::Stroke::new(BORDER, c.accent_ring);
528 w.active.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
529 w.active.corner_radius = button;
530 w.active.expansion = 0.0;
531 w.open.bg_fill = over(c.bg_raised, c.pressed);
532 w.open.weak_bg_fill = over(c.bg_raised, c.pressed);
533 w.open.bg_stroke = egui::Stroke::new(BORDER, c.accent_ring);
534 w.open.fg_stroke = egui::Stroke::new(BORDER, c.text_primary);
535 w.open.corner_radius = button;
536 w.open.expansion = 0.0;
537 }
538
539 pub fn style(&self, base: &egui::Style) -> egui::Style {
541 let mut style = base.clone();
542 self.apply(&mut style);
543 style
544 }
545
546 pub fn of(ui: &egui::Ui) -> Arc<Theme> {
549 current(ui.ctx())
550 }
551}
552
553fn slot() -> egui::Id {
554 egui::Id::new("brep-theme")
555}
556
557pub fn set(ctx: &egui::Context, theme: Arc<Theme>) {
559 ctx.data_mut(|d| d.insert_temp(slot(), theme));
560}
561
562pub fn clear(ctx: &egui::Context) {
564 ctx.data_mut(|d| d.remove::<Arc<Theme>>(slot()));
565}
566
567pub fn stored(ctx: &egui::Context) -> Option<Arc<Theme>> {
569 ctx.data(|d| d.get_temp::<Arc<Theme>>(slot()))
570}
571
572pub fn current(ctx: &egui::Context) -> Arc<Theme> {
574 stored(ctx).unwrap_or_default()
575}
576
577#[derive(Debug, Clone, Copy, PartialEq)]
580struct Installed {
581 accent: Color32,
582 density: Density,
583 reduce_motion: bool,
584 min_target: f32,
585}
586
587#[derive(Debug, Clone)]
596struct ClassicMetrics {
597 spacing: egui::style::Spacing,
598 text_sizes: Vec<(egui::TextStyle, f32)>,
599 visuals: egui::Visuals,
601 animation_time: f32,
605}
606
607impl ClassicMetrics {
608 fn restore(&self, style: &mut egui::Style) {
609 style.spacing = self.spacing.clone();
610 for (text_style, size) in &self.text_sizes {
611 if let Some(font) = style.text_styles.get_mut(text_style) {
612 font.size = *size;
613 }
614 }
615 let (v, classic) = (&mut style.visuals, &self.visuals);
616 v.window_stroke.width = classic.window_stroke.width;
617 v.selection.stroke.width = classic.selection.stroke.width;
618 v.text_cursor.stroke.width = classic.text_cursor.stroke.width;
619 v.handle_shape = classic.handle_shape;
620 let states = |w: &mut egui::style::Widgets, c: &egui::style::Widgets| {
621 for (state, classic) in [(&mut w.noninteractive, &c.noninteractive), (&mut w.inactive, &c.inactive), (&mut w.hovered, &c.hovered), (&mut w.active, &c.active), (&mut w.open, &c.open)] {
622 state.bg_stroke.width = classic.bg_stroke.width;
623 state.fg_stroke.width = classic.fg_stroke.width;
624 state.expansion = classic.expansion;
625 }
626 };
627 states(&mut v.widgets, &classic.widgets);
628 }
629}
630
631pub fn install(ctx: &egui::Context, settings: &brep_render::style::RenderSettings) {
637 let want = Installed {
638 accent: accent(settings.ui_accent).unwrap_or(ACCENTS[0]).color,
639 density: match settings.ui_density {
640 brep_render::style::UiDensity::Compact => Density::Compact,
641 brep_render::style::UiDensity::Comfortable => Density::Comfortable,
642 },
643 reduce_motion: settings.reduce_motion,
644 min_target: crate::viewport::touch::target_size(ctx, egui::Vec2::ZERO).y,
645 };
646 let key = egui::Id::new("brep-theme-installed");
647 let mode = if ctx.theme() == egui::Theme::Dark { Mode::Dark } else { Mode::Light };
648 let build = |mode| Theme::new(mode, want.density, want.accent).with_reduce_motion(want.reduce_motion).with_min_target(want.min_target);
649 if ctx.data(|d| d.get_temp::<Installed>(key)) != Some(want) {
650 for (egui_theme, mode) in [(egui::Theme::Dark, Mode::Dark), (egui::Theme::Light, Mode::Light)] {
651 let theme = build(mode);
652 let classic_key = egui::Id::new(("brep-theme-classic-metrics", mode == Mode::Dark));
654 let classic = ctx.data(|d| d.get_temp::<ClassicMetrics>(classic_key)).unwrap_or_else(|| {
655 let style = ctx.style_of(egui_theme);
656 let classic = ClassicMetrics { spacing: style.spacing.clone(), text_sizes: style.text_styles.iter().map(|(k, font)| (k.clone(), font.size)).collect(), visuals: style.visuals.clone(), animation_time: style.animation_time };
657 ctx.data_mut(|d| d.insert_temp(classic_key, classic.clone()));
658 classic
659 });
660 ctx.style_mut_of(egui_theme, |style| {
661 theme.apply(style);
662 if want.density == Density::Compact {
663 classic.restore(style);
664 }
665 if !want.reduce_motion {
666 style.animation_time = classic.animation_time;
667 }
668 });
669 }
670 ctx.data_mut(|d| {
671 d.insert_temp(key, want);
672 for egui_theme in [egui::Theme::Dark, egui::Theme::Light] {
675 d.remove::<egui::style::Spacing>(egui::Id::new(("brep-touch-desktop-spacing", format!("{egui_theme:?}"))));
676 }
677 });
678 set(ctx, Arc::new(build(mode)));
679 } else if stored(ctx).is_none_or(|theme| theme.mode != mode) {
680 set(ctx, Arc::new(build(mode)));
681 }
682}
683
684pub fn classic_bar_metrics(ctx: &egui::Context, style: &mut egui::Style) {
691 if crate::viewport::touch::enabled(ctx) {
692 return;
693 }
694 let dark = ctx.theme() == egui::Theme::Dark;
695 if let Some(classic) = ctx.data(|d| d.get_temp::<ClassicMetrics>(egui::Id::new(("brep-theme-classic-metrics", dark)))) {
696 classic.restore(style);
697 }
698}
699