use crate::color::Color;
use crate::geometry::Edges;
use crate::widget::Role;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum State {
#[default]
Normal,
Hovered,
Pressed,
Focused,
Disabled,
Selected,
}
impl State {
pub const COUNT: usize = 5;
pub const fn table_index(self) -> Option<usize> {
match self {
State::Normal => Some(0),
State::Hovered => Some(1),
State::Pressed => Some(2),
State::Focused => Some(3),
State::Disabled => Some(4),
State::Selected => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Style {
pub fg: Color,
pub bg: Color,
pub border: Color,
pub radius: f32,
pub padding: Edges,
pub selection: Color,
pub caret: Color,
}
impl Style {
pub const fn new(fg: Color, bg: Color) -> Self {
Self {
fg,
bg,
border: Color::TRANSPARENT,
radius: 0.0,
padding: Edges::ZERO,
selection: Color::rgba(0.24, 0.45, 0.75, 0.55),
caret: Color::rgba(0.85, 0.90, 1.0, 1.0),
}
}
pub fn blend_toward(self, other: Style, t: f32) -> Style {
let t = t.clamp(0.0, 1.0);
let mix = |a: Color, b: Color| {
Color::rgba(
a.r + (b.r - a.r) * t,
a.g + (b.g - a.g) * t,
a.b + (b.b - a.b) * t,
a.a + (b.a - a.a) * t,
)
};
Style {
bg: mix(self.bg, other.bg),
border: mix(self.border, other.border),
..self
}
}
pub const fn with_padding(mut self, padding: Edges) -> Self {
self.padding = padding;
self
}
pub const fn with_radius(mut self, radius: f32) -> Self {
self.radius = radius;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Theme {
slots: [&'static str; Theme::SLOTS],
table: [[Style; State::COUNT]; Theme::SLOTS],
}
impl Theme {
pub const SLOTS: usize = 8;
pub fn muted_text_color(&self) -> Color {
let fg = self.style(Role::TEXT, State::Normal).fg;
let bg = self.style(Role::PANEL, State::Normal).bg;
let t = 0.35;
let l = |a: f32, b: f32| a + (b - a) * t;
let floor = if self.is_dark() { 0.35 } else { 0.30 };
let c = |a: f32, b: f32| l(a, b).max(floor).min(1.0);
Color::rgba(c(fg.r, bg.r), c(fg.g, bg.g), c(fg.b, bg.b), 1.0)
}
pub fn divider_color(&self) -> Color {
let fg = self.style(Role::TEXT, State::Normal).fg;
let bg = self.style(Role::PANEL, State::Normal).bg;
let t = 0.12;
let l = |a: f32, b: f32| a + (b - a) * t;
Color::rgba(l(bg.r, fg.r), l(bg.g, fg.g), l(bg.b, fg.b), 1.0)
}
pub fn is_dark(&self) -> bool {
let bg = self.style(Role::PANEL, State::Normal).bg;
0.299 * bg.r + 0.587 * bg.g + 0.114 * bg.b < 0.5
}
pub fn info_color(&self) -> Color {
if self.is_dark() {
Color::rgba(0.36, 0.62, 0.96, 1.0)
} else {
Color::rgba(0.16, 0.42, 0.76, 1.0)
}
}
pub fn success_color(&self) -> Color {
if self.is_dark() {
Color::rgba(0.36, 0.80, 0.52, 1.0)
} else {
Color::rgba(0.18, 0.58, 0.32, 1.0)
}
}
pub fn warning_color(&self) -> Color {
if self.is_dark() {
Color::rgba(0.95, 0.78, 0.32, 1.0)
} else {
Color::rgba(0.72, 0.56, 0.12, 1.0)
}
}
pub fn error_color(&self) -> Color {
if self.is_dark() {
Color::rgba(0.95, 0.38, 0.38, 1.0)
} else {
Color::rgba(0.76, 0.18, 0.18, 1.0)
}
}
pub const fn slot(role: Role) -> usize {
let name = role.0;
let mut i = 0;
while i < Theme::SLOTS {
if str_eq(name, Theme::slot_name(i)) {
return i;
}
i += 1;
}
0
}
pub const fn slot_name(i: usize) -> &'static str {
match i {
0 => "panel",
1 => "button",
2 => "text_input",
3 => "text",
4 => "list",
5 => "scrollbar",
6 => "list_item",
_ => "window",
}
}
pub fn style(&self, role: Role, state: State) -> Style {
let slot = Theme::slot(role);
match state.table_index() {
Some(index) => self.table[slot][index],
None => {
let normal = self.table[slot][State::Normal.table_index().unwrap_or(0)];
let pressed = self.table[slot][State::Pressed.table_index().unwrap_or(2)];
normal.blend_toward(pressed, 0.55)
}
}
}
pub const DARK: Theme = Theme {
slots: [
"panel",
"button",
"text_input",
"text",
"list",
"scrollbar",
"list_item",
"window",
],
table: [
[
Style::new(
Color::rgba(0.92, 0.94, 0.98, 1.0),
Color::rgba(0.07, 0.08, 0.10, 1.0),
),
Style::new(
Color::rgba(0.92, 0.94, 0.98, 1.0),
Color::rgba(0.10, 0.12, 0.15, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.10, 0.10, 0.13, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.07, 0.07, 0.09, 1.0),
),
Style::new(
Color::rgba(0.50, 0.52, 0.58, 1.0),
Color::rgba(0.05, 0.05, 0.07, 1.0),
),
],
[
Style::new(
Color::rgba(0.96, 0.97, 1.0, 1.0),
Color::rgba(0.18, 0.21, 0.28, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(0.99, 1.0, 1.0, 1.0),
Color::rgba(0.24, 0.29, 0.38, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.26, 0.44, 0.72, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.20, 0.33, 0.55, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(0.55, 0.56, 0.60, 1.0),
Color::rgba(0.12, 0.13, 0.16, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
],
[
Style {
border: Color::rgba(0.27, 0.27, 0.27, 1.0),
..Style::new(
Color::rgba(0.92, 0.94, 0.98, 1.0),
Color::rgba(0.11, 0.12, 0.16, 1.0),
)
}
.with_padding(Edges::symmetric(8.0, 4.0))
.with_radius(4.0),
Style {
border: Color::rgba(0.40, 0.40, 0.40, 1.0),
..Style::new(
Color::rgba(0.94, 0.96, 1.0, 1.0),
Color::rgba(0.13, 0.15, 0.19, 1.0),
)
}
.with_padding(Edges::symmetric(8.0, 4.0))
.with_radius(4.0),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.13, 0.14, 0.18, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.96, 0.98, 1.0, 1.0),
Color::rgba(0.10, 0.11, 0.14, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.55, 0.56, 0.60, 1.0),
Color::rgba(0.08, 0.08, 0.10, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
],
[
Style::new(Color::rgba(0.88, 0.90, 0.94, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.88, 0.90, 0.94, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.88, 0.90, 0.94, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.96, 0.98, 1.0, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.50, 0.52, 0.58, 1.0), Color::TRANSPARENT),
],
[
Style::new(
Color::rgba(0.86, 0.88, 0.92, 1.0),
Color::rgba(0.06, 0.06, 0.08, 1.0),
),
Style::new(
Color::rgba(0.92, 0.95, 1.0, 1.0),
Color::rgba(0.16, 0.19, 0.26, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.20, 0.26, 0.36, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.10, 0.12, 0.16, 1.0),
),
Style::new(
Color::rgba(0.50, 0.52, 0.58, 1.0),
Color::rgba(0.06, 0.06, 0.08, 1.0),
),
],
[
Style::new(Color::TRANSPARENT, Color::rgba(0.30, 0.33, 0.42, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.38, 0.42, 0.53, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.48, 0.54, 0.68, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.30, 0.33, 0.42, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.18, 0.20, 0.26, 1.0)),
],
[
Style::new(
Color::rgba(0.86, 0.88, 0.92, 1.0),
Color::rgba(0.06, 0.06, 0.08, 1.0),
),
Style::new(
Color::rgba(0.92, 0.95, 1.0, 1.0),
Color::rgba(0.16, 0.19, 0.26, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.20, 0.26, 0.36, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.10, 0.12, 0.16, 1.0),
),
Style::new(
Color::rgba(0.50, 0.52, 0.58, 1.0),
Color::rgba(0.06, 0.06, 0.08, 1.0),
),
],
[
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.03, 0.03, 0.04, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.03, 0.03, 0.04, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.03, 0.03, 0.04, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.03, 0.03, 0.04, 1.0),
),
Style::new(
Color::rgba(0.90, 0.92, 0.96, 1.0),
Color::rgba(0.03, 0.03, 0.04, 1.0),
),
],
],
};
}
pub const LIGHT: Theme = Theme {
slots: [
"panel",
"button",
"text_input",
"text",
"list",
"scrollbar",
"list_item",
"window",
],
table: [
[
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.96, 0.96, 0.97, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.94, 0.94, 0.96, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.92, 0.92, 0.94, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.95, 0.95, 0.96, 1.0),
),
Style::new(
Color::rgba(0.45, 0.45, 0.48, 1.0),
Color::rgba(0.96, 0.96, 0.97, 1.0),
),
],
[
Style::new(
Color::rgba(0.12, 0.12, 0.14, 1.0),
Color::rgba(0.88, 0.89, 0.92, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(0.12, 0.12, 0.14, 1.0),
Color::rgba(0.82, 0.85, 0.92, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.30, 0.45, 0.75, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.35, 0.50, 0.80, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
Style::new(
Color::rgba(0.55, 0.55, 0.58, 1.0),
Color::rgba(0.90, 0.90, 0.92, 1.0),
)
.with_padding(Edges::symmetric(12.0, 6.0))
.with_radius(4.0),
],
[
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 1.0, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.97, 0.97, 0.99, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.95, 0.96, 0.99, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.05, 0.05, 0.08, 1.0),
Color::rgba(0.99, 0.99, 1.0, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
Style::new(
Color::rgba(0.60, 0.60, 0.62, 1.0),
Color::rgba(0.94, 0.94, 0.95, 1.0),
)
.with_padding(Edges::symmetric(8.0, 4.0)),
],
[
Style::new(Color::rgba(0.14, 0.14, 0.16, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.14, 0.14, 0.16, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.14, 0.14, 0.16, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.05, 0.05, 0.08, 1.0), Color::TRANSPARENT),
Style::new(Color::rgba(0.60, 0.60, 0.62, 1.0), Color::TRANSPARENT),
],
[
Style::new(
Color::rgba(0.16, 0.16, 0.18, 1.0),
Color::rgba(0.95, 0.95, 0.96, 1.0),
),
Style::new(
Color::rgba(0.16, 0.16, 0.18, 1.0),
Color::rgba(0.88, 0.90, 0.95, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.72, 0.80, 0.95, 1.0),
),
Style::new(
Color::rgba(0.05, 0.05, 0.08, 1.0),
Color::rgba(0.97, 0.97, 0.99, 1.0),
),
Style::new(
Color::rgba(0.60, 0.60, 0.62, 1.0),
Color::rgba(0.95, 0.95, 0.96, 1.0),
),
],
[
Style::new(Color::TRANSPARENT, Color::rgba(0.80, 0.81, 0.85, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.72, 0.74, 0.80, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.60, 0.64, 0.75, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.80, 0.81, 0.85, 1.0)),
Style::new(Color::TRANSPARENT, Color::rgba(0.88, 0.88, 0.90, 1.0)),
],
[
Style::new(
Color::rgba(0.16, 0.16, 0.18, 1.0),
Color::rgba(0.95, 0.95, 0.96, 1.0),
),
Style::new(
Color::rgba(0.16, 0.16, 0.18, 1.0),
Color::rgba(0.88, 0.90, 0.95, 1.0),
),
Style::new(
Color::rgba(1.0, 1.0, 1.0, 1.0),
Color::rgba(0.72, 0.80, 0.95, 1.0),
),
Style::new(
Color::rgba(0.05, 0.05, 0.08, 1.0),
Color::rgba(0.97, 0.97, 0.99, 1.0),
),
Style::new(
Color::rgba(0.60, 0.60, 0.62, 1.0),
Color::rgba(0.95, 0.95, 0.96, 1.0),
),
],
[
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 0.995, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 0.995, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 0.995, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 0.995, 1.0),
),
Style::new(
Color::rgba(0.10, 0.10, 0.12, 1.0),
Color::rgba(0.99, 0.99, 0.995, 1.0),
),
],
],
};
const fn str_eq(a: &str, b: &str) -> bool {
let (a, b) = (a.as_bytes(), b.as_bytes());
if a.len() != b.len() {
return false;
}
let mut i = 0;
while i < a.len() {
if a[i] != b[i] {
return false;
}
i += 1;
}
true
}
pub static DARK_THEME: Theme = Theme::DARK;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dark_and_light_differ() {
let dark = Theme::DARK.style(Role::BUTTON, State::Normal);
let light = LIGHT.style(Role::BUTTON, State::Normal);
assert_ne!(dark.bg, light.bg, "换主题必须真的换颜色");
}
#[test]
fn unknown_role_falls_back_to_panel() {
let custom = Role("terminal");
let a = Theme::DARK.style(custom, State::Normal);
let b = Theme::DARK.style(Role::PANEL, State::Normal);
assert_eq!(a, b);
}
#[test]
fn list_item_and_window_have_own_slots() {
assert_eq!(Theme::slot(Role::LIST_ITEM), 6, "LIST_ITEM 应命中槽位 6");
assert_eq!(Theme::slot(Role::WINDOW), 7, "WINDOW 应命中槽位 7");
assert_eq!(Theme::slot(Role("timeline")), 0, "未知角色仍回退 panel");
assert_eq!(Theme::slot(Role::SCROLLBAR), 5, "scrollbar 应仍在槽位 5");
}
#[test]
fn list_item_differs_from_panel_on_hover() {
let item = Theme::DARK.style(Role::LIST_ITEM, State::Hovered).bg;
let panel = Theme::DARK.style(Role::PANEL, State::Hovered).bg;
assert_ne!(
item, panel,
"列表项 hover 必须与面板 hover 不同,否则菜单 hover 看不见"
);
}
#[test]
fn window_bg_differs_from_panel_in_dark() {
let w = Theme::DARK.style(Role::WINDOW, State::Normal).bg;
let p = Theme::DARK.style(Role::PANEL, State::Normal).bg;
assert_ne!(w, p, "窗口底色应与面板不同,否则层次感丢失");
}
#[test]
fn states_have_distinct_backgrounds() {
let normal = Theme::DARK.style(Role::BUTTON, State::Normal).bg;
let hovered = Theme::DARK.style(Role::BUTTON, State::Hovered).bg;
let pressed = Theme::DARK.style(Role::BUTTON, State::Pressed).bg;
assert_ne!(normal, hovered);
assert_ne!(hovered, pressed);
}
}