use crate::layout::style::FlexStyle;
use crate::layout::types::FlexAlign;
#[derive(Debug, Clone, Copy)]
pub struct LayoutAttr {
pub width: Option<f32>,
pub height: Option<f32>,
pub min_width: Option<f32>,
pub min_height: Option<f32>,
pub max_width: Option<f32>,
pub max_height: Option<f32>,
pub flex_grow: f32,
pub flex_shrink: Option<f32>,
pub margin: f32,
pub margin_l: Option<f32>,
pub margin_t: Option<f32>,
pub margin_r: Option<f32>,
pub margin_b: Option<f32>,
pub padding: f32,
pub padding_l: Option<f32>,
pub padding_t: Option<f32>,
pub padding_r: Option<f32>,
pub padding_b: Option<f32>,
pub justify_content: Option<FlexAlign>,
pub align_items: Option<FlexAlign>,
pub align_self: Option<FlexAlign>,
pub gap: f32,
}
impl Default for LayoutAttr {
fn default() -> Self {
Self::new()
}
}
impl LayoutAttr {
pub fn new() -> Self {
Self {
width: None,
height: None,
min_width: None,
min_height: None,
max_width: None,
max_height: None,
flex_grow: 0.0,
flex_shrink: None,
margin: 0.0,
margin_l: None,
margin_t: None,
margin_r: None,
margin_b: None,
padding: 0.0,
padding_l: None,
padding_t: None,
padding_r: None,
padding_b: None,
justify_content: None,
align_items: None,
align_self: None,
gap: 0.0,
}
}
pub fn width(mut self, v: f32) -> Self {
self.width = Some(v);
self
}
pub fn height(mut self, v: f32) -> Self {
self.height = Some(v);
self
}
pub fn min_width(mut self, v: f32) -> Self {
self.min_width = Some(v);
self
}
pub fn min_height(mut self, v: f32) -> Self {
self.min_height = Some(v);
self
}
pub fn max_width(mut self, v: f32) -> Self {
self.max_width = Some(v);
self
}
pub fn max_height(mut self, v: f32) -> Self {
self.max_height = Some(v);
self
}
pub fn expand(mut self, v: bool) -> Self {
self.flex_grow = if v { 1.0 } else { 0.0 };
self
}
pub fn flex_grow(mut self, v: f32) -> Self {
self.flex_grow = v;
self
}
pub fn flex_shrink(mut self, v: f32) -> Self {
self.flex_shrink = Some(v);
self
}
pub fn margin(mut self, v: f32) -> Self {
self.margin = v;
self
}
pub fn margin_left(mut self, v: f32) -> Self {
self.margin_l = Some(v);
self
}
pub fn margin_top(mut self, v: f32) -> Self {
self.margin_t = Some(v);
self
}
pub fn margin_right(mut self, v: f32) -> Self {
self.margin_r = Some(v);
self
}
pub fn margin_bottom(mut self, v: f32) -> Self {
self.margin_b = Some(v);
self
}
pub fn padding(mut self, v: f32) -> Self {
self.padding = v;
self
}
pub fn padding_left(mut self, v: f32) -> Self {
self.padding_l = Some(v);
self
}
pub fn padding_top(mut self, v: f32) -> Self {
self.padding_t = Some(v);
self
}
pub fn padding_right(mut self, v: f32) -> Self {
self.padding_r = Some(v);
self
}
pub fn padding_bottom(mut self, v: f32) -> Self {
self.padding_b = Some(v);
self
}
pub fn justify_content(mut self, a: FlexAlign) -> Self {
self.justify_content = Some(a);
self
}
pub fn align_items(mut self, a: FlexAlign) -> Self {
self.align_items = Some(a);
self
}
pub fn align_self(mut self, a: FlexAlign) -> Self {
self.align_self = Some(a);
self
}
pub fn gap(mut self, v: f32) -> Self {
self.gap = v;
self
}
pub fn apply(self, style: FlexStyle) -> FlexStyle {
let mut s = style;
if let Some(v) = self.width {
s = s.width(v);
}
if let Some(v) = self.height {
s = s.height(v);
}
if let Some(v) = self.min_width {
s = s.min_width(v);
}
if let Some(v) = self.min_height {
s = s.min_height(v);
}
if let Some(v) = self.max_width {
s = s.max_width(v);
}
if let Some(v) = self.max_height {
s = s.max_height(v);
}
if self.flex_grow != 0.0 {
s = s.flex_grow(self.flex_grow);
}
if let Some(v) = self.flex_shrink {
s = s.flex_shrink(v);
}
if self.margin > 0.0 {
s = s.margin_all(self.margin);
}
if let Some(v) = self.margin_l {
s = s.margin_left(v);
}
if let Some(v) = self.margin_t {
s = s.margin_top(v);
}
if let Some(v) = self.margin_r {
s = s.margin_right(v);
}
if let Some(v) = self.margin_b {
s = s.margin_bottom(v);
}
if self.padding > 0.0 {
s = s.padding_all(self.padding);
}
if let Some(v) = self.padding_l {
s = s.padding_left(v);
}
if let Some(v) = self.padding_t {
s = s.padding_top(v);
}
if let Some(v) = self.padding_r {
s = s.padding_right(v);
}
if let Some(v) = self.padding_b {
s = s.padding_bottom(v);
}
if let Some(a) = self.justify_content {
s = s.justify_content(a);
}
if let Some(a) = self.align_items {
s = s.align_items(a);
}
if let Some(a) = self.align_self {
s = s.align_self(a);
}
if self.gap > 0.0 {
s = s.gap(self.gap);
}
s
}
}
#[macro_export]
macro_rules! layout_methods {
(for $ty:ty) => {
pub fn layout(mut self, l: $crate::widget::layout::LayoutAttr) -> Self {
self.layout = l;
self
}
pub fn width(mut self, v: f32) -> Self {
self.layout = self.layout.width(v);
self
}
pub fn height(mut self, v: f32) -> Self {
self.layout = self.layout.height(v);
self
}
pub fn min_width(mut self, v: f32) -> Self {
self.layout = self.layout.min_width(v);
self
}
pub fn min_height(mut self, v: f32) -> Self {
self.layout = self.layout.min_height(v);
self
}
pub fn max_width(mut self, v: f32) -> Self {
self.layout = self.layout.max_width(v);
self
}
pub fn max_height(mut self, v: f32) -> Self {
self.layout = self.layout.max_height(v);
self
}
pub fn expand(mut self, v: bool) -> Self {
self.layout = self.layout.expand(v);
self
}
pub fn flex_grow(mut self, v: f32) -> Self {
self.layout = self.layout.flex_grow(v);
self
}
pub fn flex_shrink(mut self, v: f32) -> Self {
self.layout = self.layout.flex_shrink(v);
self
}
pub fn margin(mut self, v: f32) -> Self {
self.layout = self.layout.margin(v);
self
}
pub fn margin_left(mut self, v: f32) -> Self {
self.layout = self.layout.margin_left(v);
self
}
pub fn margin_top(mut self, v: f32) -> Self {
self.layout = self.layout.margin_top(v);
self
}
pub fn margin_right(mut self, v: f32) -> Self {
self.layout = self.layout.margin_right(v);
self
}
pub fn margin_bottom(mut self, v: f32) -> Self {
self.layout = self.layout.margin_bottom(v);
self
}
pub fn padding(mut self, v: f32) -> Self {
self.layout = self.layout.padding(v);
self
}
pub fn padding_left(mut self, v: f32) -> Self {
self.layout = self.layout.padding_left(v);
self
}
pub fn padding_top(mut self, v: f32) -> Self {
self.layout = self.layout.padding_top(v);
self
}
pub fn padding_right(mut self, v: f32) -> Self {
self.layout = self.layout.padding_right(v);
self
}
pub fn padding_bottom(mut self, v: f32) -> Self {
self.layout = self.layout.padding_bottom(v);
self
}
pub fn justify_content(mut self, a: FlexAlign) -> Self {
self.layout = self.layout.justify_content(a);
self
}
pub fn align_items(mut self, a: FlexAlign) -> Self {
self.layout = self.layout.align_items(a);
self
}
pub fn align_self(mut self, a: FlexAlign) -> Self {
self.layout = self.layout.align_self(a);
self
}
pub fn gap(mut self, v: f32) -> Self {
self.layout = self.layout.gap(v);
self
}
};
}