1use crate::style::Style;
6use serde::{Deserialize, Serialize};
7use oak_json::{JsonValueNode, ast::{JsonObject, JsonString}};
8
9#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
11pub enum ComponentType {
12 Button,
14 TextBox,
16 Label,
18 Container,
20 Panel,
22 List,
24 Dropdown,
26 Checkbox,
28 Radio,
30 Slider,
32 Custom(String),
34}
35
36#[derive(Debug)]
38pub struct Component {
39 component_type: ComponentType,
41 id: String,
43 style: Style,
45 properties: ComponentProperties,
47 children: Vec<Component>,
49}
50
51#[derive(Debug, Deserialize, Serialize)]
53pub struct ComponentProperties {
54 pub text: Option<String>,
56 pub enabled: bool,
58 pub visible: bool,
60 pub checked: bool,
62 pub min: Option<f32>,
64 pub max: Option<f32>,
66 pub value: Option<JsonValueNode>,
68 pub options: Option<Vec<(String, String)>>,
70 pub custom: JsonValueNode,
72}
73
74impl ComponentProperties {
75 pub fn default() -> Self {
80 Self {
81 text: None,
82 enabled: true,
83 visible: true,
84 checked: false,
85 min: None,
86 max: None,
87 value: None,
88 options: None,
89 custom: JsonValueNode::Object(JsonObject { fields: Vec::new(), span: (0..0).into() }),
90 }
91 }
92}
93
94impl Component {
95 pub fn new(component_type: ComponentType, id: &str, style: Style, properties: ComponentProperties) -> Self {
106 Self { component_type, id: id.to_string(), style, properties, children: Vec::new() }
107 }
108
109 pub fn add_child(&mut self, child: Component) {
114 self.children.push(child);
115 }
116
117 pub fn get_child(&self, id: &str) -> Option<&Component> {
125 self.children.iter().find(|child| child.id == id)
126 }
127
128 pub fn get_child_mut(&mut self, id: &str) -> Option<&mut Component> {
136 self.children.iter_mut().find(|child| child.id == id)
137 }
138
139 pub fn set_text(&mut self, text: &str) {
144 self.properties.text = Some(text.to_string());
145 }
146
147 pub fn set_enabled(&mut self, enabled: bool) {
152 self.properties.enabled = enabled;
153 }
154
155 pub fn set_visible(&mut self, visible: bool) {
160 self.properties.visible = visible;
161 }
162
163 pub fn set_checked(&mut self, checked: bool) {
168 self.properties.checked = checked;
169 }
170
171 pub fn set_value(&mut self, value: JsonValueNode) {
176 self.properties.value = Some(value);
177 }
178
179 pub fn component_type(&self) -> &ComponentType {
184 &self.component_type
185 }
186
187 pub fn id(&self) -> &str {
192 &self.id
193 }
194
195 pub fn style(&self) -> &Style {
200 &self.style
201 }
202
203 pub fn style_mut(&mut self) -> &mut Style {
208 &mut self.style
209 }
210
211 pub fn properties(&self) -> &ComponentProperties {
216 &self.properties
217 }
218
219 pub fn properties_mut(&mut self) -> &mut ComponentProperties {
224 &mut self.properties
225 }
226
227 pub fn children(&self) -> &Vec<Component> {
232 &self.children
233 }
234
235 pub fn children_mut(&mut self) -> &mut Vec<Component> {
240 &mut self.children
241 }
242}
243
244pub fn create_button(id: &str, text: &str, style: Style) -> Component {
254 let mut properties = ComponentProperties::default();
255 properties.text = Some(text.to_string());
256 Component::new(ComponentType::Button, id, style, properties)
257}
258
259pub fn create_label(id: &str, text: &str, style: Style) -> Component {
269 let mut properties = ComponentProperties::default();
270 properties.text = Some(text.to_string());
271 Component::new(ComponentType::Label, id, style, properties)
272}
273
274pub fn create_text_box(id: &str, value: &str, style: Style) -> Component {
284 let mut properties = ComponentProperties::default();
285 properties.value = Some(JsonValueNode::String(JsonString { value: value.to_string(), span: (0..0).into() }));
286 Component::new(ComponentType::TextBox, id, style, properties)
287}
288
289pub fn create_container(id: &str, style: Style) -> Component {
298 let properties = ComponentProperties::default();
299 Component::new(ComponentType::Container, id, style, properties)
300}