use serde_json::Value;
pub fn parse_iced_settings(settings: &Value) -> iced::Settings {
let antialiasing = settings
.get("antialiasing")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let vsync = settings
.get("vsync")
.and_then(|v| v.as_bool())
.unwrap_or(true);
let default_text_size = settings
.get("default_text_size")
.and_then(|v| v.as_f64())
.map(|s| iced::Pixels(s as f32));
let default_font = settings.get("default_font").map(|v| {
let family = v.get("family").and_then(|f| f.as_str());
if family == Some("monospace") {
iced::Font::MONOSPACE
} else {
iced::Font::DEFAULT
}
});
let mut iced_settings = iced::Settings {
antialiasing,
vsync,
..Default::default()
};
if let Some(size) = default_text_size {
iced_settings.default_text_size = size;
}
if let Some(font) = default_font {
iced_settings.default_font = font;
}
iced_settings
}
pub fn apply_validate_props(settings: &Value) {
if settings
.get("validate_props")
.and_then(|v| v.as_bool())
.unwrap_or(false)
{
plushie_ext::widgets::set_validate_props(true);
log::info!("prop validation enabled via settings");
}
}
pub fn decode_font_data(value: &Value) -> Option<Vec<u8>> {
match value {
Value::String(s) => {
use base64::Engine;
base64::engine::general_purpose::STANDARD.decode(s).ok()
}
Value::Array(arr) => {
let bytes: Vec<u8> = arr
.iter()
.filter_map(|v| v.as_u64().and_then(|n| u8::try_from(n).ok()))
.collect();
if bytes.len() == arr.len() {
Some(bytes)
} else {
None
}
}
_ => None,
}
}
pub fn parse_inline_fonts(settings: &Value) -> Vec<Vec<u8>> {
let Some(fonts) = settings.get("fonts").and_then(|v| v.as_array()) else {
return Vec::new();
};
let mut result = Vec::new();
for font_val in fonts {
if let Some(obj) = font_val.as_object()
&& let Some(data_val) = obj.get("data")
{
match decode_font_data(data_val) {
Some(bytes) if bytes.is_empty() => {
log::warn!("fonts: empty inline font data, skipping");
}
Some(bytes) => {
log::info!("loaded inline font ({} bytes)", bytes.len());
result.push(bytes);
}
None => {
log::warn!("fonts: failed to decode inline font data");
}
}
}
}
result
}