orbit/kit/components/
layout.rs1use crate::component::{Component, ComponentError, ComponentId, Context, Node};
4use std::any::Any;
5
6#[derive(Debug)]
8pub struct Layout {
9 id: ComponentId,
11 pub direction: Direction,
13 pub align: Alignment,
15 pub justify: Justification,
17 pub gap: String,
19 pub padding: String,
21 pub children: Option<String>,
23}
24
25#[derive(Debug, Clone)]
27pub struct LayoutProps {
28 pub direction: Option<Direction>,
30 pub align: Option<Alignment>,
32 pub justify: Option<Justification>,
34 pub gap: Option<String>,
36 pub padding: Option<String>,
38 pub children: Option<String>,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44pub enum Direction {
45 #[default]
47 Row,
48 Column,
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub enum Alignment {
55 #[default]
57 Start,
58 Center,
60 End,
62 Stretch,
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
68pub enum Justification {
69 #[default]
71 Start,
72 Center,
74 End,
76 SpaceBetween,
78 SpaceAround,
80 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 Ok(vec![])
139 }
140}