based/src/ui/primitives/shadow.rs
2025-01-20 06:09:56 +01:00

163 lines
4.2 KiB
Rust

use crate::ui::{UIWidget, color::UIColor};
use maud::{Markup, Render, html};
pub struct Shadow(Box<dyn UIWidget>, String, Option<Box<dyn UIColor>>);
impl Shadow {
pub fn small<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "sm".to_owned(), None)
}
pub fn regular<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), String::new(), None)
}
pub fn medium<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "md".to_owned(), None)
}
pub fn large<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "lg".to_owned(), None)
}
pub fn xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "xl".to_owned(), None)
}
pub fn _2xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "2xl".to_owned(), None)
}
pub fn inner<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "inner".to_owned(), None)
}
pub fn none<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "none".to_owned(), None)
}
}
impl Shadow {
pub fn color<C: UIColor + 'static>(mut self, color: C) -> Self {
self.2 = Some(Box::new(color));
self
}
}
impl Render for Shadow {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for Shadow {
fn can_inherit(&self) -> bool {
true
}
fn base_class(&self) -> Vec<String> {
let mut ret = if self.1.is_empty() {
vec!["shadow".to_string()]
} else {
vec![format!("shadow-{}", self.1)]
};
if let Some(color) = &self.2 {
ret.push(format!("shadow-{}", color.color_class()));
}
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, 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())
}
}
}
}
}
pub struct DropShadow(Box<dyn UIWidget>, String);
impl DropShadow {
pub fn small<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "sm".to_owned())
}
pub fn regular<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), String::new())
}
pub fn medium<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "md".to_owned())
}
pub fn large<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "lg".to_owned())
}
pub fn none<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "none".to_owned())
}
pub fn xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "xl".to_owned())
}
pub fn _2xl<T: UIWidget + 'static>(inner: T) -> Self {
Self(Box::new(inner), "2xl".to_owned())
}
}
impl Render for DropShadow {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for DropShadow {
fn can_inherit(&self) -> bool {
true
}
fn base_class(&self) -> Vec<String> {
if self.1.is_empty() {
vec!["drop-shadow".to_string()]
} else {
vec![format!("drop-shadow-{}", self.1)]
}
}
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, 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())
}
}
}
}
}