orbit/kit/components/
layout.rs

1// Layout components for OrbitKit
2
3use crate::component::{Component, ComponentError, ComponentId, Context, Node};
4use std::any::Any;
5
6/// Layout component
7#[derive(Debug)]
8pub struct Layout {
9    /// Component ID for tracking
10    id: ComponentId,
11    /// Layout direction (row, column)
12    pub direction: Direction,
13    /// Layout alignment
14    pub align: Alignment,
15    /// Layout justification
16    pub justify: Justification,
17    /// Layout gap
18    pub gap: String,
19    /// Layout padding
20    pub padding: String,
21    /// Child content
22    pub children: Option<String>,
23}
24
25/// Layout props
26#[derive(Debug, Clone)]
27pub struct LayoutProps {
28    /// Layout direction (row, column)
29    pub direction: Option<Direction>,
30    /// Layout alignment
31    pub align: Option<Alignment>,
32    /// Layout justification
33    pub justify: Option<Justification>,
34    /// Layout gap
35    pub gap: Option<String>,
36    /// Layout padding
37    pub padding: Option<String>,
38    /// Child content
39    pub children: Option<String>,
40}
41
42/// Layout direction
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44pub enum Direction {
45    /// Horizontal layout
46    #[default]
47    Row,
48    /// Vertical layout
49    Column,
50}
51
52/// Layout alignment
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub enum Alignment {
55    /// Align items at the start
56    #[default]
57    Start,
58    /// Align items at the center
59    Center,
60    /// Align items at the end
61    End,
62    /// Stretch items to fill the container
63    Stretch,
64}
65
66/// Layout justification
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
68pub enum Justification {
69    /// Justify items at the start
70    #[default]
71    Start,
72    /// Justify items at the center
73    Center,
74    /// Justify items at the end
75    End,
76    /// Space between items
77    SpaceBetween,
78    /// Space around items
79    SpaceAround,
80    /// Space evenly between items
81    SpaceEvenly,
82}
83
84impl Default for Layout {
85    fn default() -> Self {
86        Self {
87            id: ComponentId::new(),
88            direction: Direction::default(),
89            align: Alignment::default(),
90            justify: Justification::default(),
91            gap: "0px".to_string(),
92            padding: "0px".to_string(),
93            children: None,
94        }
95    }
96}
97
98impl Component for Layout {
99    type Props = LayoutProps;
100
101    fn component_id(&self) -> ComponentId {
102        self.id
103    }
104
105    fn create(props: Self::Props, _context: Context) -> Self {
106        Self {
107            id: ComponentId::new(),
108            direction: props.direction.unwrap_or_default(),
109            align: props.align.unwrap_or_default(),
110            justify: props.justify.unwrap_or_default(),
111            gap: props.gap.unwrap_or_else(|| "0px".to_string()),
112            padding: props.padding.unwrap_or_else(|| "0px".to_string()),
113            children: props.children,
114        }
115    }
116
117    fn update(&mut self, props: Self::Props) -> Result<(), ComponentError> {
118        self.direction = props.direction.unwrap_or(self.direction);
119        self.align = props.align.unwrap_or(self.align);
120        self.justify = props.justify.unwrap_or(self.justify);
121        self.gap = props.gap.unwrap_or_else(|| self.gap.clone());
122        self.padding = props.padding.unwrap_or_else(|| self.padding.clone());
123        self.children = props.children;
124        Ok(())
125    }
126
127    fn as_any(&self) -> &dyn Any {
128        self
129    }
130
131    fn as_any_mut(&mut self) -> &mut dyn Any {
132        self
133    }
134
135    fn render(&self) -> Result<Vec<Node>, ComponentError> {
136        // For now, return an empty Vec since we're not yet using the Node system
137        // In a real implementation, this would return the actual DOM nodes
138        Ok(vec![])
139    }
140}