use iced::{Length, Padding};
use crate::theme::resolve::Theme;
use crate::theme::resolve::computed::ComputedStyle;
#[derive(Debug, Clone, Copy)]
pub struct ComponentSize {
pub pad_v: f32,
pub pad_h: f32,
pub font_size: f32,
pub icon_size: f32,
pub min_w: Option<f32>,
pub min_h: Option<f32>,
pub gap: f32,
}
impl ComponentSize {
pub fn from_computed(computed: &ComputedStyle) -> Self {
let font_size = computed.length("font-size").unwrap_or(16.0);
Self {
pad_v: computed.length("padding-v").unwrap_or(8.0),
pad_h: computed.length("padding-h").unwrap_or(16.0),
font_size,
icon_size: computed.length("icon-size").unwrap_or(font_size),
min_w: computed.length("min-width"),
min_h: computed.length("min-height"),
gap: computed.length("gap").unwrap_or(6.0),
}
}
pub fn padding(&self) -> Padding {
Padding {
top: self.pad_v,
right: self.pad_h,
bottom: self.pad_v,
left: self.pad_h,
}
}
pub fn padding_symmetric(&self) -> Padding {
Padding::from([self.pad_v, self.pad_h])
}
pub fn min_width(&self) -> Length {
match self.min_w {
Some(w) => Length::Fixed(w),
None => Length::Shrink,
}
}
pub fn min_height(&self) -> Length {
match self.min_h {
Some(h) => Length::Fixed(h),
None => Length::Shrink,
}
}
}
impl Theme {
pub fn sizing(&self, classes: &[&str]) -> ComponentSize {
let computed = self.resolve(classes, None);
ComponentSize::from_computed(&computed)
}
}