1use crate::style::Style;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
10pub enum ComponentType {
11 Button,
13 TextBox,
15 Label,
17 Container,
19 Panel,
21 List,
23 Dropdown,
25 Checkbox,
27 Radio,
29 Slider,
31 Custom(String),
33}
34
35#[derive(Debug)]
37pub struct Component {
38 component_type: ComponentType,
40 id: String,
42 style: Style,
44 properties: ComponentProperties,
46 children: Vec<Component>,
48}
49
50#[derive(Debug, Deserialize, Serialize)]
52pub struct ComponentProperties {
53 pub text: Option<String>,
55 pub enabled: bool,
57 pub visible: bool,
59 pub checked: bool,
61 pub min: Option<f32>,
63 pub max: Option<f32>,
65 pub value: Option<serde_json::Value>,
67 pub options: Option<Vec<(String, String)>>,
69 pub custom: serde_json::Value,
71}
72
73impl ComponentProperties {
74 pub fn default() -> Self {
79 Self {
80 text: None,
81 enabled: true,
82 visible: true,
83 checked: false,
84 min: None,
85 max: None,
86 value: None,
87 options: None,
88 custom: serde_json::Value::Object(serde_json::Map::new()),
89 }
90 }
91}
92
93impl Component {
94 pub fn new(component_type: ComponentType, id: &str, style: Style, properties: ComponentProperties) -> Self {
105 Self { component_type, id: id.to_string(), style, properties, children: Vec::new() }
106 }
107
108 pub fn add_child(&mut self, child: Component) {
113 self.children.push(child);
114 }
115
116 pub fn get_child(&self, id: &str) -> Option<&Component> {
124 self.children.iter().find(|child| child.id == id)
125 }
126
127 pub fn get_child_mut(&mut self, id: &str) -> Option<&mut Component> {
135 self.children.iter_mut().find(|child| child.id == id)
136 }
137
138 pub fn set_text(&mut self, text: &str) {
143 self.properties.text = Some(text.to_string());
144 }
145
146 pub fn set_enabled(&mut self, enabled: bool) {
151 self.properties.enabled = enabled;
152 }
153
154 pub fn set_visible(&mut self, visible: bool) {
159 self.properties.visible = visible;
160 }
161
162 pub fn set_checked(&mut self, checked: bool) {
167 self.properties.checked = checked;
168 }
169
170 pub fn set_value(&mut self, value: serde_json::Value) {
175 self.properties.value = Some(value);
176 }
177
178 pub fn component_type(&self) -> &ComponentType {
183 &self.component_type
184 }
185
186 pub fn id(&self) -> &str {
191 &self.id
192 }
193
194 pub fn style(&self) -> &Style {
199 &self.style
200 }
201
202 pub fn style_mut(&mut self) -> &mut Style {
207 &mut self.style
208 }
209
210 pub fn properties(&self) -> &ComponentProperties {
215 &self.properties
216 }
217
218 pub fn properties_mut(&mut self) -> &mut ComponentProperties {
223 &mut self.properties
224 }
225
226 pub fn children(&self) -> &Vec<Component> {
231 &self.children
232 }
233
234 pub fn children_mut(&mut self) -> &mut Vec<Component> {
239 &mut self.children
240 }
241}
242
243pub fn create_button(id: &str, text: &str, style: Style) -> Component {
253 let mut properties = ComponentProperties::default();
254 properties.text = Some(text.to_string());
255 Component::new(ComponentType::Button, id, style, properties)
256}
257
258pub fn create_label(id: &str, text: &str, style: Style) -> Component {
268 let mut properties = ComponentProperties::default();
269 properties.text = Some(text.to_string());
270 Component::new(ComponentType::Label, id, style, properties)
271}
272
273pub fn create_text_box(id: &str, value: &str, style: Style) -> Component {
283 let mut properties = ComponentProperties::default();
284 properties.value = Some(serde_json::Value::String(value.to_string()));
285 Component::new(ComponentType::TextBox, id, style, properties)
286}
287
288pub fn create_container(id: &str, style: Style) -> Component {
297 let properties = ComponentProperties::default();
298 Component::new(ComponentType::Container, id, style, properties)
299}