beffroi-tools/crates/midra-layout/src/theme.rs

143 lines
4.6 KiB
Rust

//! Application palettes adapted from SOTV's song database; see docs/midra-layout.md.
use super::*;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Theme {
#[default]
Midra,
FoxifiedDark,
CorporateLight,
}
#[derive(Clone, Copy)]
pub struct Palette {
pub canvas: Color32,
pub banner: Color32,
pub grid: Color32,
pub layer: Color32,
pub pattern: [Color32; 2],
pub bg: Color32,
pub panel: Color32,
pub field: Color32,
pub line: Color32,
pub text: Color32,
pub muted: Color32,
pub cyan: Color32,
pub amber: Color32,
pub purple: Color32,
}
impl Theme {
pub const ALL: [Self; 3] = [Self::Midra, Self::FoxifiedDark, Self::CorporateLight];
pub fn name(self) -> &'static str {
match self {
Self::Midra => "Midra Dark",
Self::FoxifiedDark => "Foxified Dark",
Self::CorporateLight => "Corporate Light",
}
}
pub fn current(ctx: &egui::Context) -> Self {
ctx.data(|d| {
d.get_temp(egui::Id::new("application-theme"))
.unwrap_or_default()
})
}
pub fn palette(self) -> Palette {
let rgb = |hex: u32| Color32::from_rgb((hex >> 16) as u8, (hex >> 8) as u8, hex as u8);
match self {
Self::Midra => Palette {
canvas: rgb(0x142d40),
banner: rgb(0x264556),
grid: rgb(0x27475a),
layer: rgb(0x173b50),
pattern: [rgb(0x264b5d), rgb(0x1a3344)],
bg: BG,
panel: PANEL,
field: FIELD,
line: LINE,
text: TEXT,
muted: MUTED,
cyan: CYAN,
amber: AMBER,
purple: PURPLE,
},
Self::FoxifiedDark => Palette {
canvas: rgb(0x281a12),
banner: rgb(0x503322),
grid: rgb(0x62432e),
layer: rgb(0x43291d),
pattern: [rgb(0x503322), rgb(0x362318)],
bg: rgb(0x2e1f16),
panel: rgb(0x3b261b),
field: rgb(0x1e1309),
line: rgb(0xaa6033),
text: rgb(0xffcc99),
muted: rgb(0xffd8b1),
cyan: rgb(0xff6600),
amber: AMBER,
purple: rgb(0x854018),
},
Self::CorporateLight => Palette {
canvas: rgb(0xffffff),
banner: rgb(0xe5e9f0),
grid: rgb(0xd8dee6),
layer: rgb(0xe9eef3),
pattern: [rgb(0xe5e9f0), rgb(0xf5f7fa)],
bg: rgb(0xf4f5f7),
panel: Color32::WHITE,
field: rgb(0xf0f1f3),
line: rgb(0xbbbbbb),
text: rgb(0x222222),
muted: rgb(0x52616f),
cyan: rgb(0x216783),
amber: rgb(0x815600),
purple: rgb(0xc5dce8),
},
}
}
pub fn apply(self, ctx: &egui::Context) {
let p = self.palette();
let mut v = if self == Self::CorporateLight {
egui::Visuals::light()
} else {
egui::Visuals::dark()
};
v.override_text_color = Some(p.text);
v.panel_fill = p.panel;
v.window_fill = p.panel;
v.window_stroke = Stroke::new(1.0_f32, p.line);
v.extreme_bg_color = p.field;
v.faint_bg_color = p.bg;
v.code_bg_color = p.field;
v.hyperlink_color = p.cyan;
v.warn_fg_color = p.amber;
v.error_fg_color = if self == Self::CorporateLight {
Color32::from_rgb(165, 30, 40)
} else {
Color32::LIGHT_RED
};
v.selection.bg_fill = p.purple;
v.selection.stroke = Stroke::new(1.0_f32, p.text);
for (widget, fill, border) in [
(&mut v.widgets.noninteractive, p.panel, p.line),
(&mut v.widgets.inactive, p.field, p.line),
(&mut v.widgets.hovered, p.panel, p.cyan),
(&mut v.widgets.active, p.purple, p.cyan),
(&mut v.widgets.open, p.panel, p.cyan),
] {
widget.bg_fill = fill;
widget.weak_bg_fill = fill;
widget.bg_stroke = Stroke::new(1.0_f32, border);
widget.fg_stroke = Stroke::new(1.0_f32, p.text);
}
ctx.set_visuals(v);
ctx.data_mut(|d| d.insert_temp(egui::Id::new("application-theme"), self));
ctx.request_repaint();
}
}
pub fn palette(ctx: &egui::Context) -> Palette {
Theme::current(ctx).palette()
}