From fb4a142c9ca1e09466a18fbcbe9a562063f9ca1d Mon Sep 17 00:00:00 2001 From: JMARyA Date: Tue, 21 Jan 2025 17:25:45 +0100 Subject: [PATCH] refactor --- src/ui/mod.rs | 49 +++++++++++--------- src/ui/primitives/animation.rs | 49 ++++++++++---------- src/ui/primitives/aspect.rs | 73 ------------------------------ src/ui/primitives/display.rs | 20 +++++++++ src/ui/primitives/header.rs | 40 ----------------- src/ui/primitives/mod.rs | 3 -- src/ui/primitives/table.rs | 1 + src/ui/primitives/zindex.rs | 82 ---------------------------------- 8 files changed, 75 insertions(+), 242 deletions(-) delete mode 100644 src/ui/primitives/aspect.rs delete mode 100644 src/ui/primitives/header.rs delete mode 100644 src/ui/primitives/zindex.rs diff --git a/src/ui/mod.rs b/src/ui/mod.rs index a314169..477f3a4 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -14,44 +14,54 @@ pub mod components; // Preludes pub mod prelude { pub use super::color::*; - pub use super::primitives::Context; - pub use super::primitives::NoBrowserAppearance; - pub use super::primitives::Nothing; - pub use super::primitives::Side; - pub use super::primitives::Size; pub use super::primitives::animation::{Animated, Animation, Delay, Duration, Scope, Timing}; - pub use super::primitives::aspect::Aspect; - pub use super::primitives::background::Background; + pub use super::primitives::background::{ + Background, BackgroundClip, BackgroundGradient, BackgroundOrigin, BackgroundPosition, + BackgroundRepeat, BackgroundScrollAttachment, BackgroundSize, + }; pub use super::primitives::border::{ Border, BorderSide, BorderSize, BorderStyle, Outline, OutlineStyle, Ring, }; pub use super::primitives::container::Container; pub use super::primitives::cursor::{Action, Cursor, TouchAction}; pub use super::primitives::display::{ - BoxDecorationBreak, BoxSizing, BreakAfter, BreakBefore, BreakInside, BreakInsideValue, - BreakValue, Clear, Display, Float, ObjectFit, Overflow, + AlignSelf, Aspect, BoxDecorationBreak, BoxSizing, BreakAfter, BreakBefore, BreakInside, + BreakInsideValue, BreakValue, Clear, Display, Float, JustifySelf, ObjectFit, Overflow, + PlaceSelf, ZIndex, }; pub use super::primitives::div::Div; pub use super::primitives::filter::{ - Blur, Brightness, Contrast, Grayscale, HueRotate, Invert, Saturate, Sepia, + BackgroundBlendMode, BlendMode, Blur, Brightness, Contrast, Grayscale, HueRotate, Invert, + MixBlendMode, Opacity, Saturate, Sepia, }; pub use super::primitives::flex::{ - Direction, Flex, FlexBasis, FlexGrow, Justify, Order, Strategy, Wrap, + AlignContent, AlignItems, Direction, DivideStyle, DivideWidth, Flex, FlexBasis, FlexGrow, + Justify, JustifyItems, Order, Strategy, Wrap, + }; + pub use super::primitives::grid::{ + Columns, Grid, GridAmount, GridAutoFlow, GridAutoSize, GridElementColumn, GridElementRow, + GridElementValue, }; - pub use super::primitives::header::Header; pub use super::primitives::height::{Height, MaxHeight, MinHeight}; - pub use super::primitives::image::Image; + pub use super::primitives::image::{Image, Source, Video}; + pub use super::primitives::width::{MaxWidth, MinWidth, Width}; + pub use super::primitives::{Context, NoBrowserAppearance, Nothing, Side, Size, script}; + // TODO : + pub use super::primitives::input::*; pub use super::primitives::link::Link; + pub use super::primitives::list::{OrderedList, UnorderedList}; pub use super::primitives::margin::Margin; pub use super::primitives::padding::Padding; - pub use super::primitives::position::{Position, PositionKind, Resize, Resizeable}; + pub use super::primitives::position::{ + ObjectPosition, Position, PositionKind, Resize, Resizeable, + }; pub use super::primitives::rounded::Rounded; - pub use super::primitives::script; pub use super::primitives::scroll::{Overscroll, Scroll, SnapAlign, SnapType}; - pub use super::primitives::shadow::Shadow; + pub use super::primitives::shadow::{DropShadow, Shadow}; pub use super::primitives::sized::Sized; - pub use super::primitives::space::{ScreenValue, SpaceBetween}; + pub use super::primitives::space::{Fraction, ScreenValue, SpaceBetween}; pub use super::primitives::svg::SVG; + pub use super::primitives::table::{Caption, Header, Table, TableData, TableHead, TableRow}; pub use super::primitives::text::{ AccentColor, Code, DecorationKind, DecorationStyle, DecorationThickness, LetterSpacing, LineClamp, LineHeight, ListStyle, NumberStyle, Paragraph, Span, Text, TextAlignment, @@ -60,11 +70,10 @@ pub mod prelude { VerticalTextAlignment, }; pub use super::primitives::transform::{ - RenderTransformCPU, RenderTransformGPU, Rotate, Scale, Skew, Transform, TransformOrigin, + RenderTransformCPU, RenderTransformGPU, Rotate, Scale, Skew, SkewValue, Transform, + TransformOrigin, }; pub use super::primitives::visibility::Visibility; - pub use super::primitives::width::{MaxWidth, MinWidth, Width}; - pub use super::primitives::zindex::ZIndex; pub use super::wrapper::{ _2XLScreen, Hover, LargeScreen, MediumScreen, Screen, SmallScreen, XLScreen, }; diff --git a/src/ui/primitives/animation.rs b/src/ui/primitives/animation.rs index d95dd24..64e668c 100644 --- a/src/ui/primitives/animation.rs +++ b/src/ui/primitives/animation.rs @@ -23,31 +23,32 @@ pub struct AnimatedWidget { animation: Option, } +macro_rules! setter { + ($fnname:ident, $varname:ident, $vartype:ident, $internal_var:ident) => { + #[must_use] + pub fn $fnname(mut self, $varname: $vartype) -> Self { + self.$internal_var = $varname; + self + } + }; +} + +macro_rules! setter_opt { + ($fnname:ident, $varname:ident, $vartype:ident, $internal_var:ident) => { + #[must_use] + pub fn $fnname(mut self, $varname: $vartype) -> Self { + self.$internal_var = Some($varname); + self + } + }; +} + impl AnimatedWidget { - pub fn scope(mut self, scope: Scope) -> Self { - self.scope = scope; - self - } - - pub fn timing(mut self, timing: Timing) -> Self { - self.timing = Some(timing); - self - } - - pub fn delay(mut self, delay: Delay) -> Self { - self.delay = Some(delay); - self - } - - pub fn duration(mut self, duration: Duration) -> Self { - self.duration = Some(duration); - self - } - - pub fn animate(mut self, animation: Animation) -> Self { - self.animation = Some(animation); - self - } + setter!(scope, scope, Scope, scope); + setter_opt!(timing, timing, Timing, timing); + setter_opt!(delay, delay, Delay, delay); + setter_opt!(duration, duration, Duration, duration); + setter_opt!(animate, animation, Animation, animation); } impl Render for AnimatedWidget { diff --git a/src/ui/primitives/aspect.rs b/src/ui/primitives/aspect.rs deleted file mode 100644 index 4723067..0000000 --- a/src/ui/primitives/aspect.rs +++ /dev/null @@ -1,73 +0,0 @@ -use maud::{Markup, Render, html}; - -use crate::ui::UIWidget; - -pub struct Aspect { - kind: u8, - inner: Box, -} - -impl Aspect { - pub fn auto(inner: T) -> Self { - Self { - kind: 0, - inner: Box::new(inner), - } - } - - pub fn square(inner: T) -> Self { - Self { - kind: 1, - inner: Box::new(inner), - } - } - - pub fn video(inner: T) -> Self { - Self { - kind: 2, - inner: Box::new(inner), - } - } -} - -impl Render for Aspect { - fn render(&self) -> Markup { - self.render_with_class("") - } -} - -impl UIWidget for Aspect { - fn can_inherit(&self) -> bool { - true - } - - fn base_class(&self) -> Vec { - let class = match self.kind { - 0 => "aspect-auto", - 1 => "aspect-square", - 2 => "aspect-video", - _ => "", - }; - vec![class.to_string()] - } - - fn extended_class(&self) -> Vec { - let mut c = self.base_class(); - c.extend_from_slice(&self.inner.extended_class()); - c - } - - fn render_with_class(&self, class: &str) -> Markup { - if self.inner.as_ref().can_inherit() { - html! { - (self.inner.as_ref().render_with_class(&format!("{class} {}", self.base_class().join(" ")))) - } - } else { - html! { - div class=(format!("{class} {}", self.base_class().join(" "))) { - (self.inner.as_ref()) - } - } - } - } -} diff --git a/src/ui/primitives/display.rs b/src/ui/primitives/display.rs index 287bd59..d780744 100644 --- a/src/ui/primitives/display.rs +++ b/src/ui/primitives/display.rs @@ -312,3 +312,23 @@ impl AlignSelf { constructor!(Stretch, "self-stretch"); constructor!(Baseline, "self-baseline"); } + +string_class_widget!(Aspect); + +impl Aspect { + constructor!(Auto, "aspect-auto"); + constructor!(Square, "aspect-square"); + constructor!(Video, "aspect-video"); +} + +string_class_widget!(ZIndex); + +impl ZIndex { + constructor!(Auto, "z-auto"); + constructor!(Zero, "z-0"); + constructor!(One, "z-10"); + constructor!(Two, "z-20"); + constructor!(Three, "z-30"); + constructor!(Four, "z-40"); + constructor!(Five, "z-50"); +} diff --git a/src/ui/primitives/header.rs b/src/ui/primitives/header.rs deleted file mode 100644 index 28b5fdc..0000000 --- a/src/ui/primitives/header.rs +++ /dev/null @@ -1,40 +0,0 @@ -use maud::{Markup, Render, html}; - -use crate::ui::UIWidget; - -#[allow(non_snake_case)] -pub fn Header(inner: T) -> HeaderWidget { - HeaderWidget(Box::new(inner)) -} - -pub struct HeaderWidget(Box); - -impl Render for HeaderWidget { - fn render(&self) -> Markup { - self.render_with_class("") - } -} - -impl UIWidget for HeaderWidget { - fn can_inherit(&self) -> bool { - true - } - - fn base_class(&self) -> Vec { - vec![] - } - - 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 { - html! { - header class=(class) { - (self.0.as_ref()) - } - } - } -} diff --git a/src/ui/primitives/mod.rs b/src/ui/primitives/mod.rs index d2d21f5..38816ba 100644 --- a/src/ui/primitives/mod.rs +++ b/src/ui/primitives/mod.rs @@ -2,7 +2,6 @@ use super::UIWidget; use maud::{Markup, PreEscaped, Render, html}; pub mod animation; -pub mod aspect; pub mod background; pub mod border; pub mod container; @@ -12,7 +11,6 @@ pub mod div; pub mod filter; pub mod flex; pub mod grid; -pub mod header; pub mod height; pub mod image; pub mod input; @@ -32,7 +30,6 @@ pub mod text; pub mod transform; pub mod visibility; pub mod width; -pub mod zindex; #[allow(non_snake_case)] #[must_use] diff --git a/src/ui/primitives/table.rs b/src/ui/primitives/table.rs index 60bf8a6..3a82002 100644 --- a/src/ui/primitives/table.rs +++ b/src/ui/primitives/table.rs @@ -202,3 +202,4 @@ macro_rules! element_widget { element_widget!(TableRow, TableRowWidget, tr); element_widget!(TableHead, TableHeadWidget, th); element_widget!(TableData, TableDataWidget, td); +element_widget!(Header, HeaderWidget, header); diff --git a/src/ui/primitives/zindex.rs b/src/ui/primitives/zindex.rs deleted file mode 100644 index 2ddf0b4..0000000 --- a/src/ui/primitives/zindex.rs +++ /dev/null @@ -1,82 +0,0 @@ -use maud::{Markup, Render, html}; - -use crate::ui::UIWidget; - -pub struct ZIndex(Box, u8); - -impl Render for ZIndex { - fn render(&self) -> Markup { - self.render_with_class("") - } -} - -impl ZIndex { - pub fn auto(inner: T) -> Self { - Self(Box::new(inner), 0) - } - - pub fn zero(inner: T) -> Self { - Self(Box::new(inner), 1) - } - - pub fn one(inner: T) -> Self { - Self(Box::new(inner), 2) - } - - pub fn two(inner: T) -> Self { - Self(Box::new(inner), 3) - } - - pub fn three(inner: T) -> Self { - Self(Box::new(inner), 4) - } - - pub fn four(inner: T) -> Self { - Self(Box::new(inner), 5) - } - - pub fn five(inner: T) -> Self { - Self(Box::new(inner), 6) - } -} - -impl UIWidget for ZIndex { - fn can_inherit(&self) -> bool { - true - } - - fn base_class(&self) -> Vec { - let class = match self.1 { - 0 => "z-auto", - 1 => "z-0", - 2 => "z-10", - 3 => "z-20", - 4 => "z-30", - 5 => "z-40", - 6 => "z-50", - _ => "z-auto", - }; - - vec![class.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()) - } - } - } - } -}