based/src/ui/primitives/svg.rs
2025-01-20 08:41:20 +01:00

76 lines
1.7 KiB
Rust

use maud::{Markup, Render, html};
use crate::ui::{UIWidget, color::UIColor};
#[allow(non_snake_case)]
pub fn SVG<T: UIWidget + 'static>(inner: T) -> SVGWidget {
SVGWidget(Box::new(inner), None, None, None)
}
pub struct SVGWidget(
Box<dyn UIWidget>,
Option<Box<dyn UIColor>>,
Option<Box<dyn UIColor>>,
Option<u32>,
);
impl SVGWidget {
pub fn fill<C: UIColor + 'static>(mut self, color: C) -> Self {
self.1 = Some(Box::new(color));
self
}
pub fn stroke<C: UIColor + 'static>(mut self, color: C) -> Self {
self.2 = Some(Box::new(color));
self
}
pub fn stroke_width(mut self, width: u32) -> Self {
self.3 = Some(width);
self
}
}
impl Render for SVGWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for SVGWidget {
fn can_inherit(&self) -> bool {
false
}
fn base_class(&self) -> Vec<String> {
let mut ret = vec![];
if let Some(fill) = &self.1 {
ret.push(format!("fill-{}", fill.color_class()));
}
if let Some(stroke) = &self.2 {
ret.push(format!("stroke-{}", stroke.color_class()));
}
if let Some(stroke_width) = &self.3 {
ret.push(format!("stroke-[{stroke_width}px]"));
}
ret
}
fn extended_class(&self) -> Vec<String> {
let mut c = self.base_class();
c.extend_from_slice(&self.0.extended_class());
c
}
fn render_with_class(&self, _: &str) -> Markup {
html! {
svg class=(self.base_class().join(" ")) {
(self.0.as_ref())
}
}
}
}