clay_layout/layout/
mod.rs

1use alignment::{Alignment, LayoutAlignmentX, LayoutAlignmentY};
2use padding::Padding;
3use sizing::Sizing;
4
5use crate::{bindings::*, elements::ElementConfigType, mem::zeroed_init, TypedConfig};
6
7pub mod alignment;
8pub mod padding;
9pub mod sizing;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[repr(u8)]
13pub enum LayoutDirection {
14    LeftToRight = Clay_LayoutDirection_CLAY_LEFT_TO_RIGHT,
15    TopToBottom = Clay_LayoutDirection_CLAY_TOP_TO_BOTTOM,
16}
17
18#[derive(Debug, Copy, Clone)]
19pub struct Layout {
20    pub width: Sizing,
21    pub height: Sizing,
22    pub padding: Padding,
23    pub child_gap: u16,
24    pub child_alignment: Alignment,
25    pub direction: LayoutDirection,
26}
27
28impl Layout {
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    pub fn width(&mut self, width: Sizing) -> &mut Self {
34        self.width = width;
35        self
36    }
37
38    pub fn height(&mut self, height: Sizing) -> &mut Self {
39        self.height = height;
40        self
41    }
42
43    pub fn padding(&mut self, padding: Padding) -> &mut Self {
44        self.padding = padding;
45        self
46    }
47
48    pub fn child_gap(&mut self, child_gap: u16) -> &mut Self {
49        self.child_gap = child_gap;
50        self
51    }
52
53    pub fn child_alignment(&mut self, child_alignment: Alignment) -> &mut Self {
54        self.child_alignment = child_alignment;
55        self
56    }
57
58    pub fn direction(&mut self, direction: LayoutDirection) -> &mut Self {
59        self.direction = direction;
60        self
61    }
62
63    pub fn end(&self) -> TypedConfig {
64        let memory = unsafe { Clay__StoreLayoutConfig((*self).into()) };
65
66        TypedConfig {
67            config_memory: memory as _,
68            id: zeroed_init(),
69            config_type: ElementConfigType::Layout as _,
70        }
71    }
72}
73
74impl Default for Layout {
75    fn default() -> Self {
76        Self {
77            width: Sizing::Fit(0., f32::MAX),
78            height: Sizing::Fit(0., f32::MAX),
79            padding: Padding::default(),
80            child_gap: 0,
81            child_alignment: Alignment::new(LayoutAlignmentX::Left, LayoutAlignmentY::Top),
82            direction: LayoutDirection::LeftToRight,
83        }
84    }
85}
86
87impl From<Clay_LayoutConfig> for Layout {
88    fn from(value: Clay_LayoutConfig) -> Self {
89        Self {
90            width: value.sizing.width.into(),
91            height: value.sizing.height.into(),
92            padding: value.padding.into(),
93            child_gap: value.childGap,
94            child_alignment: value.childAlignment.into(),
95            direction: unsafe {
96                core::mem::transmute::<u8, LayoutDirection>(value.layoutDirection)
97            },
98        }
99    }
100}
101impl From<Layout> for Clay_LayoutConfig {
102    fn from(value: Layout) -> Self {
103        Self {
104            sizing: Clay_Sizing {
105                width: value.width.into(),
106                height: value.height.into(),
107            },
108            padding: value.padding.into(),
109            childGap: value.child_gap,
110            childAlignment: value.child_alignment.into(),
111            layoutDirection: value.direction as _,
112        }
113    }
114}