Files
vectorcraft/crates/ui-egui/src/theme.rs
T
BFlat 6f37bd839c VectorCraft on the craft UI framework (#8800)
VectorCraft's UI moves onto the shared craft UI framework in storytold/craft-libs (`next`; the framework side is storytold/craft-libs#2809), as PhotoCraft's did. The app keeps its engine, document model, file formats, commands and automation; its windows, panels, bars, dialogs and canvas are now built and driven by the framework: shell, docking and panels, hosted dialogs, keyboard, focus, IME and pointer routing, and theme tokens.

About 264,000 lines of VectorCraft's own egui UI code are removed and about 154,000 added. craft-libs is pinned at bf62d509f (in `next`), the egui fork at f5025284.

Tests that drove the old UI were rewritten against the port where only their premise changed, with their assertions kept. The UI tests still failing at merge, and their follow-ups, are listed in the pull request.
2026-11-24 08:25:24 +07:00

167 lines
5.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! The user's interface choices (brightness, accent, colour scheme) and how they select the root theme.
use craft::theme::{ModeChoice, Rgba, ThemeId};
use egui::{FontFamily, FontId};
use serde::{Deserialize, Serialize};
use vectorcraft_engine::Prefs;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Brightness {
Dark,
#[default]
MediumDark,
MediumLight,
Light,
}
impl Brightness {
pub const ALL: [Brightness; 4] = [Brightness::Dark, Brightness::MediumDark, Brightness::MediumLight, Brightness::Light];
pub fn label(self) -> &'static str {
match self {
Brightness::Dark => "Dark",
Brightness::MediumDark => "Medium Dark",
Brightness::MediumLight => "Medium Light",
Brightness::Light => "Light",
}
}
pub fn id(self) -> &'static str {
match self {
Brightness::Dark => "dark",
Brightness::MediumDark => "mediumDark",
Brightness::MediumLight => "mediumLight",
Brightness::Light => "light",
}
}
pub fn parse(s: &str) -> Option<Self> {
Self::ALL.into_iter().find(|b| b.id().eq_ignore_ascii_case(s) || b.label().eq_ignore_ascii_case(s))
}
}
/// User Interface › Accent Color: the one colour for what is on, chosen or focused. Coral, the
/// default, is the app icon's.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Accent {
#[default]
Coral,
Blue,
Violet,
Green,
Amber,
Pink,
}
impl Accent {
pub const ALL: [Accent; 6] = [Accent::Coral, Accent::Blue, Accent::Violet, Accent::Green, Accent::Amber, Accent::Pink];
pub fn id(self) -> &'static str {
match self {
Accent::Coral => "coral",
Accent::Blue => "blue",
Accent::Violet => "violet",
Accent::Green => "green",
Accent::Amber => "amber",
Accent::Pink => "pink",
}
}
pub fn parse(s: &str) -> Option<Self> {
Self::ALL.into_iter().find(|a| a.id().eq_ignore_ascii_case(s))
}
/// The accent's seed colour (`0xRRGGBB`) on dark panels and on light ones.
fn seed_hex(self, dark: bool) -> u32 {
match (self, dark) {
(Accent::Coral, true) => 0xf06a52,
(Accent::Coral, false) => 0xd9472f,
(Accent::Blue, true) => 0x5b96ff,
(Accent::Blue, false) => 0x2563eb,
(Accent::Violet, true) => 0xa98bff,
(Accent::Violet, false) => 0x7146dc,
(Accent::Green, true) => 0x3fcf8e,
(Accent::Green, false) => 0x16955a,
(Accent::Amber, true) => 0xf5b544,
(Accent::Amber, false) => 0xb06d06,
(Accent::Pink, true) => 0xf36aa3,
(Accent::Pink, false) => 0xcc3672,
}
}
}
/// User Interface › Color Scheme: Modern (the default: slate greys and the chosen accent) or
/// Classic (the neutral greys and blue accent of earlier versions). Both keep the same layout,
/// cards and corners; only the colours differ.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Scheme {
#[default]
Modern,
Classic,
}
impl Scheme {
pub const ALL: [Scheme; 2] = [Scheme::Modern, Scheme::Classic];
pub fn id(self) -> &'static str {
match self {
Scheme::Modern => "modern",
Scheme::Classic => "classic",
}
}
pub fn parse(s: &str) -> Option<Self> {
Self::ALL.into_iter().find(|c| c.id().eq_ignore_ascii_case(s))
}
}
/// A built-in theme's id; the ids are constants, so the fallback never shows.
fn theme_id(id: &str) -> ThemeId {
ThemeId::parse(id).unwrap_or_else(|_| ThemeId::suite_default())
}
/// The root theme the saved interface preferences select: its id, its fixed mode (the four
/// brightness levels decide light or dark, never the system) and the accent seed. Classic keeps its
/// own blue and Modern's coral is in its theme files, so both return no seed there.
pub fn root_theme(prefs: &Prefs) -> (ThemeId, ModeChoice, Option<Rgba>) {
let brightness = Brightness::parse(&prefs.ui_brightness).unwrap_or_default();
let accent = Accent::parse(&prefs.ui_accent).unwrap_or_default();
let scheme = Scheme::parse(&prefs.ui_color_scheme).unwrap_or_default();
let dark = matches!(brightness, Brightness::Dark | Brightness::MediumDark);
let mode = if dark { ModeChoice::Dark } else { ModeChoice::Light };
match scheme {
Scheme::Classic => {
let id = match brightness {
Brightness::MediumDark => "craft.artcraft",
Brightness::MediumLight => "craft.pro-light-gray",
Brightness::Dark | Brightness::Light => "craft.pro",
};
(theme_id(id), mode, None)
}
Scheme::Modern => {
let id = match brightness {
Brightness::Dark | Brightness::Light => "craft.vectorcraft-modern",
Brightness::MediumDark | Brightness::MediumLight => "craft.vectorcraft-modern-medium",
};
let seed = (accent != Accent::Coral).then(|| {
let h = accent.seed_hex(dark);
Rgba::rgb((h >> 16) as u8, (h >> 8) as u8, h as u8)
});
(theme_id(id), mode, seed)
}
}
}
pub fn semibold(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(craft::ui::fonts::SEMIBOLD.into()))
}
pub fn mono(size: f32) -> FontId {
FontId::monospace(size)
}
#[cfg(test)]
mod tests {
use super::*;
/// The saved strings are what Preferences and old settings files hold.
#[test]
fn brightness_roundtrip() {
for b in Brightness::ALL {
assert_eq!(Brightness::parse(b.id()), Some(b));
assert_eq!(Brightness::parse(b.label()), Some(b));
}
}
}