This commit is contained in:
JMARyA 2025-01-14 22:56:07 +01:00
parent 4a537cd933
commit 8208fa8899
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
20 changed files with 944 additions and 15 deletions

78
src/ui/flex.rs Normal file
View file

@ -0,0 +1,78 @@
use super::UIWidget;
use maud::{Markup, Render, html};
#[allow(non_snake_case)]
pub fn Flex<T: UIWidget + 'static>(inner: T) -> FlexWidget {
FlexWidget(Box::new(inner), vec![], false)
}
pub enum Justify {
Center,
Between,
}
pub struct FlexWidget(Box<dyn UIWidget>, Vec<String>, bool);
impl Render for FlexWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl FlexWidget {
pub fn full_center(mut self) -> Self {
self.1.push("items-center".to_owned());
self.1.push("justify-center".to_owned());
self
}
pub fn group(mut self) -> Self {
self.2 = true;
self
}
pub fn justify(mut self, value: Justify) -> Self {
let class = match value {
Justify::Center => "justify-center".to_owned(),
Justify::Between => "justify-between".to_owned(),
};
self.1.push(class);
self
}
pub fn space_x(mut self, x: u32) -> Self {
self.1.push(format!("space-x-{x}"));
self
}
pub fn items_center(mut self) -> Self {
self.1.push("items-center".to_owned());
self
}
pub fn gap(mut self, amount: u32) -> Self {
self.1.push(format!("gap-{amount}"));
self
}
}
impl UIWidget for FlexWidget {
fn can_inherit(&self) -> bool {
true
}
fn render_with_class(&self, class: &str) -> Markup {
if self.0.as_ref().can_inherit() && !self.2 {
self.0
.as_ref()
.render_with_class(&format!("flex {} {class}", self.1.join(" ")))
} else {
html! {
div class=(format!("flex {} {class}", self.1.join(" "))) {
(self.0.as_ref())
}
}
}
}
}