based/src/ui/background.rs
2025-01-14 22:56:07 +01:00

66 lines
1.3 KiB
Rust

use super::UIWidget;
use maud::{Markup, Render, html};
pub trait UIColor {
fn color_class(&self) -> &str;
}
pub enum Blue {
_500,
}
impl UIColor for Blue {
fn color_class(&self) -> &str {
match self {
Blue::_500 => "blue-500",
}
}
}
pub enum Gray {
_800,
}
impl UIColor for Gray {
fn color_class(&self) -> &str {
match self {
Gray::_800 => "gray-800",
}
}
}
#[allow(non_snake_case)]
pub fn Background<T: UIWidget + 'static, C: UIColor + 'static>(
color: C,
inner: T,
) -> BackgroundWidget {
BackgroundWidget(Box::new(inner), Box::new(color))
}
pub struct BackgroundWidget(Box<dyn UIWidget>, Box<dyn UIColor>);
impl Render for BackgroundWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for BackgroundWidget {
fn can_inherit(&self) -> bool {
true
}
fn render_with_class(&self, class: &str) -> Markup {
if self.0.as_ref().can_inherit() {
self.0
.as_ref()
.render_with_class(&format!("bg-{} {class}", self.1.color_class()))
} else {
html! {
div class=(format!("bg-{} {class}", self.1.color_class())) {
(self.0.as_ref())
}
}
}
}
}