sunset theme.xml in favor of globals

This commit is contained in:
galister
2025-12-14 00:32:50 +09:00
parent 5035893fab
commit 17165123b9
11 changed files with 82 additions and 41 deletions

View File

@@ -125,6 +125,25 @@ impl Color {
pub fn debug_ansi_block(&self) -> String {
format!("{}███{}", self.debug_ansi_format(), ANSI_RESET_CODE)
}
#[must_use]
pub fn with_alpha(&self, n: f32) -> Self {
Self {
r: self.r,
g: self.g,
b: self.b,
a: n,
}
}
#[must_use]
pub fn to_hex(&self) -> String {
let r = (self.r.clamp(0.0, 1.0) * 255.0).round() as u8;
let g = (self.g.clamp(0.0, 1.0) * 255.0).round() as u8;
let b = (self.b.clamp(0.0, 1.0) * 255.0).round() as u8;
let a = (self.a.clamp(0.0, 1.0) * 255.0).round() as u8;
format!("#{r:02X}{g:02X}{b:02X}{a:02X}")
}
}
impl Default for Color {

View File

@@ -21,6 +21,8 @@ pub struct Defaults {
pub button_color: drawing::Color,
pub accent_color: drawing::Color,
pub danger_color: drawing::Color,
pub faded_color: drawing::Color,
pub translucent_alpha: f32,
}
impl Default for Defaults {
@@ -31,6 +33,8 @@ impl Default for Defaults {
button_color: drawing::Color::new(1.0, 1.0, 1.0, 0.05),
accent_color: drawing::Color::new(0.0, 0.54, 1.0, 1.0),
danger_color: drawing::Color::new(0.8, 0.0, 0.0, 1.0),
faded_color: drawing::Color::new(0.4, 0.5, 0.6, 1.0),
translucent_alpha: 0.25,
}
}
}

View File

@@ -377,6 +377,30 @@ impl ParserContext<'_> {
log::warn!("duplicate widget ID \"{id}\" in the same layout file!");
}
}
fn populate_theme_variables(&mut self) {
let def = self.doc_params.globals.defaults();
macro_rules! insert_color_vars {
($self:expr, $name:literal, $field:expr, $alpha:expr) => {
$self.insert_var(concat!("color_", $name), &$field.to_hex());
$self.insert_var(
concat!("color_", $name, "_translucent"),
&$field.with_alpha($alpha).to_hex(),
);
$self.insert_var(concat!("color_", $name, "_50"), &$field.mult_rgb(0.50).to_hex());
$self.insert_var(concat!("color_", $name, "_20"), &$field.mult_rgb(0.20).to_hex());
$self.insert_var(concat!("color_", $name, "_10"), &$field.mult_rgb(0.10).to_hex());
$self.insert_var(concat!("color_", $name, "_5"), &$field.mult_rgb(0.05).to_hex());
$self.insert_var(concat!("color_", $name, "_1"), &$field.mult_rgb(0.01).to_hex());
};
}
insert_color_vars!(self, "text", def.text_color, def.translucent_alpha);
insert_color_vars!(self, "accent", def.accent_color, def.translucent_alpha);
insert_color_vars!(self, "danger", def.danger_color, def.translucent_alpha);
insert_color_vars!(self, "faded", def.faded_color, def.translucent_alpha);
}
}
// Parses a color from a HTML hex string
@@ -1030,6 +1054,8 @@ pub fn parse_from_assets(
) -> anyhow::Result<ParserState> {
let parser_data = ParserData::default();
let mut ctx = create_default_context(doc_params, layout, &parser_data);
ctx.populate_theme_variables();
let (file, node_layout) = get_doc_from_asset_path(&ctx, doc_params.path)?;
parse_document_root(&file, &mut ctx, parent_id, node_layout)?;

View File

@@ -6,8 +6,8 @@ use taffy::{
use crate::{
drawing,
parser::{
is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val, print_invalid_attrib,
print_invalid_value, AttribPair,
AttribPair, is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val,
print_invalid_attrib, print_invalid_value,
},
renderer_vk::text::{FontWeight, HorizontalAlign, TextStyle},
widget::util::WLength,