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    /// Compact key-value display
103    KeyValue,
104    /// Compact button for dense UIs
105    ButtonCompact,
106    /// Horizontal layout container
107    Horizontal,
108    /// Inline button without label column (for horizontal layouts)
109    ButtonInline,
110    /// Inline text input without label column (for horizontal layouts)
111    TextInputInline,
112}
113
114#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
115pub struct ElementMeta {
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub min: Option<f64>,
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub max: Option<f64>,
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub step: Option<f64>,
122    /// Accent color for the element
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub accent: Option<AccentColor>,
125    /// Subtitle or secondary text
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub subtitle: Option<String>,
128    /// Number of columns for grid layout
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub cols: Option<usize>,
131    /// Child element IDs for grid layout
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub children: Option<Vec<String>>,
134}
135
136#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
137pub struct ElementDecl {
138    pub id: ElementId,
139    pub kind: ElementKind,
140    pub label: String,
141    pub value: Value,
142    pub meta: ElementMeta,
143    pub window: Arc<str>,
144}
145
146/// Accent colors available for UI elements
147#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
148#[serde(rename_all = "lowercase")]
149pub enum AccentColor {
150    Coral,
151    Teal,
152    Blue,
153    Green,
154    Purple,
155    Orange,
156    Yellow,
157    Red,
158}
159
160impl AccentColor {
161    pub fn as_str(&self) -> &'static str {
162        match self {
163            AccentColor::Coral => "coral",
164            AccentColor::Teal => "teal",
165            AccentColor::Blue => "blue",
166            AccentColor::Green => "green",
167            AccentColor::Purple => "purple",
168            AccentColor::Orange => "orange",
169            AccentColor::Yellow => "yellow",
170            AccentColor::Red => "red",
171        }
172    }
173}