use crate::ui::UIWidget; use maud::{Markup, Render, html}; use super::{ flex::Either, space::{Fraction, ScreenValue}, }; #[allow(non_snake_case)] pub fn Width>>( size: S, inner: T, ) -> WidthWidget { WidthWidget(Box::new(inner), size.into(), 0) } #[allow(non_snake_case)] pub fn MinWidth>>( size: S, inner: T, ) -> WidthWidget { WidthWidget(Box::new(inner), size.into(), 1) } #[allow(non_snake_case)] pub fn MaxWidth>>( size: S, inner: T, ) -> WidthWidget { WidthWidget(Box::new(inner), size.into(), 2) } pub struct WidthWidget(Box, Either, u8); impl Render for WidthWidget { fn render(&self) -> Markup { self.render_with_class("") } } impl UIWidget for WidthWidget { fn can_inherit(&self) -> bool { true } fn base_class(&self) -> Vec { match self.2 { 1 => { return vec![format!( "min-w-{}", self.1 .map(|x| x.to_value().to_string(), |x| x.to_value().to_string()) )]; } 2 => { return vec![format!( "max-w-{}", self.1 .map(|x| x.to_value().to_string(), |x| x.to_value().to_string()) )]; } _ => {} } vec![format!( "w-{}", self.1 .map(|x| x.to_value().to_string(), |x| x.to_value().to_string()) )] } fn extended_class(&self) -> Vec { let mut c = self.base_class(); c.extend_from_slice(&self.0.extended_class()); c } fn render_with_class(&self, class: &str) -> Markup { if self.0.as_ref().can_inherit() { self.0 .as_ref() .render_with_class(&format!("{} {class}", self.base_class().join(" "))) } else { html! { div class=(format!("{} {class}", self.base_class().join(" "))) { (self.0.as_ref()) } } } } }