use iced::Color;
pub mod spacing {
pub const XS: f32 = 4.0;
pub const SM: f32 = 8.0;
pub const MD: f32 = 16.0;
pub const LG: f32 = 24.0;
pub const XL: f32 = 32.0;
}
pub const BASE_TEXT_SIZE: f32 = 14.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FontScale(pub f32);
impl Default for FontScale {
fn default() -> Self {
FontScale(1.0)
}
}
impl FontScale {
pub fn apply(&self, base: f32) -> f32 {
base * self.0
}
}
static ACTIVE: std::sync::OnceLock<hyprforge_look::Theme> = std::sync::OnceLock::new();
pub fn init(theme: hyprforge_look::Theme) {
let _ = ACTIVE.set(theme);
}
pub fn active() -> &'static hyprforge_look::Theme {
ACTIVE.get_or_init(hyprforge_look::Theme::default)
}
pub mod surface {
use crate::color::to_iced;
use iced::Color;
pub fn root() -> Color {
to_iced(super::active().surfaces.root)
}
pub fn sidebar() -> Color {
to_iced(super::active().surfaces.sidebar)
}
pub fn card() -> Color {
to_iced(super::active().surfaces.card)
}
pub fn card_border() -> Color {
to_iced(super::active().surfaces.card_border)
}
pub fn row() -> Color {
to_iced(super::active().surfaces.row)
}
}
pub fn text() -> Color {
crate::color::to_iced(active().surfaces.text)
}
pub fn text_dim() -> Color {
crate::color::to_iced(active().surfaces.text_dim)
}
pub fn warning() -> Color {
crate::color::to_iced(active().warning)
}
pub fn info() -> Color {
crate::color::to_iced(active().info)
}
pub fn success() -> Color {
crate::color::to_iced(active().success)
}
pub fn error() -> Color {
crate::color::to_iced(active().error)
}
const ROW_TINT_ALPHA: f32 = 0.22;
pub fn row_tint() -> Color {
Color {
a: ROW_TINT_ALPHA,
..surface::card_border()
}
}
pub fn mono_font() -> iced::Font {
static MONO: std::sync::OnceLock<iced::Font> = std::sync::OnceLock::new();
*MONO.get_or_init(|| mono_font_for(&active().mono_font))
}
fn mono_font_for(family: &str) -> iced::Font {
match family.trim() {
"" => iced::Font::MONOSPACE,
name => iced::Font::with_name(Box::leak(name.to_owned().into_boxed_str())),
}
}
fn palette() -> iced::theme::Palette {
let theme = active();
iced::theme::Palette {
background: crate::color::to_iced(theme.surfaces.root),
text: crate::color::to_iced(theme.surfaces.text),
primary: crate::color::to_iced(theme.accent),
success: crate::color::to_iced(theme.success),
warning: crate::color::to_iced(theme.warning),
danger: crate::color::to_iced(theme.error),
}
}
pub fn app_theme() -> iced::Theme {
iced::Theme::custom("Hyprforge".to_string(), palette())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_font_scale_is_identity() {
assert_eq!(FontScale::default().apply(BASE_TEXT_SIZE), BASE_TEXT_SIZE);
}
#[test]
fn font_scale_multiplies_the_base_size() {
assert_eq!(FontScale(1.5).apply(16.0), 24.0);
assert_eq!(FontScale(0.5).apply(16.0), 8.0);
}
}
#[cfg(test)]
mod palette_tests {
use super::*;
#[test]
fn the_default_theme_reproduces_the_constants_it_replaced() {
let c = |r, g, b| iced::Color::from_rgb8(r, g, b);
assert_eq!(surface::root(), c(0x19, 0x1a, 0x21));
assert_eq!(surface::sidebar(), c(0x12, 0x13, 0x19));
assert_eq!(surface::card(), c(0x25, 0x27, 0x31));
assert_eq!(surface::card_border(), c(0x37, 0x3a, 0x47));
assert_eq!(surface::row(), c(0x1f, 0x21, 0x29));
assert_eq!(text(), c(0xe9, 0xea, 0xef));
assert_eq!(text_dim(), c(0x92, 0x96, 0xa4));
assert_eq!(warning(), c(0xf5, 0xb9, 0x42));
}
#[test]
fn the_row_tint_is_the_border_colour_made_translucent() {
let tint = row_tint();
let border = surface::card_border();
assert_eq!((tint.r, tint.g, tint.b), (border.r, border.g, border.b));
assert!(tint.a > 0.0 && tint.a < 0.5, "a stripe, not a fill: {}", tint.a);
}
#[test]
fn an_unnamed_monospace_family_is_the_toolkits_monospace() {
assert_eq!(mono_font_for(""), iced::Font::MONOSPACE);
assert_eq!(mono_font_for(" "), iced::Font::MONOSPACE);
assert_eq!(mono_font_for("Hack"), iced::Font::with_name("Hack"));
}
#[test]
fn a_process_that_never_installs_a_theme_still_has_one() {
assert_eq!(active().accent, hyprforge_look::Theme::default().accent);
let _ = app_theme();
}
}