Skip to main content

w_gui/
element.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4
5pub type ElementId = String;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8#[serde(tag = "type", content = "data")]
9pub enum Value {
10    Float(f64),
11    Bool(bool),
12    Color3([f32; 3]),
13    Color4([f32; 4]),
14    Vec2([f32; 2]),
15    Vec3([f32; 3]),
16    Int(i64),
17    String(String),
18    Enum {
19        selected: usize,
20        options: Vec<String>,
21    },
22    /// Transient: true for one frame when clicked
23    Button(bool),
24    /// Progress value (0.0 to 1.0)
25    Progress(f64),
26    /// Stat card value with optional subvalue
27    StatValue {
28        value: String,
29        subvalue: Option<String>,
30    },
31    /// Status indicator state
32    StatusValue {
33        active: bool,
34        #[serde(skip_serializing_if = "Option::is_none")]
35        active_text: Option<String>,
36        #[serde(skip_serializing_if = "Option::is_none")]
37        inactive_text: Option<String>,
38        #[serde(skip_serializing_if = "Option::is_none")]
39        active_color: Option<String>,
40        #[serde(skip_serializing_if = "Option::is_none")]
41        inactive_color: Option<String>,
42    },
43    /// Mini chart data
44    ChartValue {
45        values: Vec<f32>,
46        #[serde(skip_serializing_if = "Option::is_none")]
47        current: Option<f32>,
48        #[serde(skip_serializing_if = "Option::is_none")]
49        unit: Option<String>,
50    },
51    /// Grid container data
52    GridValue {
53        cols: usize,
54        children: Vec<String>,
55    },
56    /// Plot data for larger charts
57    PlotValue {
58        series: Vec<PlotSeries>,
59        #[serde(skip_serializing_if = "Option::is_none")]
60        x_label: Option<String>,
61        #[serde(skip_serializing_if = "Option::is_none")]
62        y_label: Option<String>,
63    },
64    /// Null value for container elements
65    Null,
66}
67
68/// A data series for plotting
69#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
70pub struct PlotSeries {
71    pub name: String,
72    pub values: Vec<f32>,
73    pub color: String,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
77#[serde(tag = "type", content = "data")]
78pub enum ElementKind {
79    Slider,
80    Checkbox,
81    ColorPicker3,
82    ColorPicker4,
83    TextInput,
84    Dropdown,
85    Button,
86    Label,
87    Separator,
88    /// Section header for grouping
89    Section,
90    /// Progress bar with percentage
91    ProgressBar,
92    /// Stat card display
93    Stat,
94    /// Status indicator with colored dot
95    Status,
96    /// Mini sparkline chart
97    MiniChart,
98    /// Grid layout container
99    Grid,
100    /// Larger plot/chart for data visualization
101    Plot,
102}
103
104#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
105pub struct ElementMeta {
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub min: Option<f64>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub max: Option<f64>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub step: Option<f64>,
112    /// Accent color for the element (coral, teal, blue, green, purple, orange, yellow, red)
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub accent: Option<String>,
115    /// Subtitle or secondary text
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub subtitle: Option<String>,
118    /// Number of columns for grid layout
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub cols: Option<usize>,
121    /// Child element IDs for grid layout
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub children: Option<Vec<String>>,
124}
125
126#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
127pub struct ElementDecl {
128    pub id: ElementId,
129    pub kind: ElementKind,
130    pub label: String,
131    pub value: Value,
132    pub meta: ElementMeta,
133    pub window: Arc<str>,
134}
135
136/// Accent colors available for UI elements
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub enum AccentColor {
139    Coral,
140    Teal,
141    Blue,
142    Green,
143    Purple,
144    Orange,
145    Yellow,
146    Red,
147}
148
149impl AccentColor {
150    pub fn as_str(&self) -> &'static str {
151        match self {
152            AccentColor::Coral => "coral",
153            AccentColor::Teal => "teal",
154            AccentColor::Blue => "blue",
155            AccentColor::Green => "green",
156            AccentColor::Purple => "purple",
157            AccentColor::Orange => "orange",
158            AccentColor::Yellow => "yellow",
159            AccentColor::Red => "red",
160        }
161    }
162}