Skip to main content

tauri_plugin_widgets/models/
data.rs

1//! Data payloads carried by elements: chart points, list rows, and the
2//! declarative canvas draw-command union.
3
4use serde::{Deserialize, Serialize};
5
6#[cfg(feature = "schema")]
7use schemars::JsonSchema;
8
9use super::style::ColorValue;
10
11/// A single labeled data point for [`crate::models::ChartElement`].
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[cfg_attr(feature = "schema", derive(JsonSchema))]
14#[serde(rename_all = "camelCase")]
15pub struct ChartDataPoint {
16    /// Category / x-axis label.
17    pub label: String,
18    /// Value plotted for this point.
19    pub value: f64,
20    /// Optional per-point color override.
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub color: Option<ColorValue>,
23    /// Series name: points with the same `series` form one line / bar group.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub series: Option<String>,
26}
27
28/// A single row for [`crate::models::ListElement`].
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[cfg_attr(feature = "schema", derive(JsonSchema))]
31#[serde(rename_all = "camelCase")]
32pub struct ListItem {
33    /// Row text.
34    pub text: String,
35    /// Optional checked/checkbox marker.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub checked: Option<bool>,
38    /// Action identifier — emits a `widget-action` event when tapped.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub action: Option<String>,
41    /// Opaque payload forwarded alongside the action event.
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub payload: Option<String>,
44}
45
46/// A single declarative draw command for [`crate::models::CanvasElement`].
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[cfg_attr(feature = "schema", derive(JsonSchema))]
49#[serde(tag = "draw", rename_all = "camelCase")]
50pub enum CanvasDrawCommand {
51    /// Filled / stroked circle.
52    #[serde(rename = "circle")]
53    Circle {
54        /// Center x-coordinate.
55        cx: f64,
56        /// Center y-coordinate.
57        cy: f64,
58        /// Radius.
59        r: f64,
60        /// Fill color.
61        #[serde(default, skip_serializing_if = "Option::is_none")]
62        fill: Option<ColorValue>,
63        /// Stroke color.
64        #[serde(default, skip_serializing_if = "Option::is_none")]
65        stroke: Option<ColorValue>,
66        /// Stroke width in points.
67        #[serde(
68            rename = "strokeWidth",
69            default,
70            skip_serializing_if = "Option::is_none"
71        )]
72        stroke_width: Option<f64>,
73    },
74    /// Straight line segment.
75    #[serde(rename = "line")]
76    Line {
77        /// Start x-coordinate.
78        x1: f64,
79        /// Start y-coordinate.
80        y1: f64,
81        /// End x-coordinate.
82        x2: f64,
83        /// End y-coordinate.
84        y2: f64,
85        /// Stroke color.
86        #[serde(default, skip_serializing_if = "Option::is_none")]
87        stroke: Option<ColorValue>,
88        /// Stroke width in points.
89        #[serde(
90            rename = "strokeWidth",
91            default,
92            skip_serializing_if = "Option::is_none"
93        )]
94        stroke_width: Option<f64>,
95        /// Line cap style (`"butt"`, `"round"`, or `"square"`).
96        #[serde(rename = "lineCap", default, skip_serializing_if = "Option::is_none")]
97        line_cap: Option<String>,
98    },
99    /// Filled / stroked rectangle, optionally rounded.
100    #[serde(rename = "rect")]
101    Rect {
102        /// Top-left x-coordinate.
103        x: f64,
104        /// Top-left y-coordinate.
105        y: f64,
106        /// Rectangle width.
107        width: f64,
108        /// Rectangle height.
109        height: f64,
110        /// Fill color.
111        #[serde(default, skip_serializing_if = "Option::is_none")]
112        fill: Option<ColorValue>,
113        /// Stroke color.
114        #[serde(default, skip_serializing_if = "Option::is_none")]
115        stroke: Option<ColorValue>,
116        /// Stroke width in points.
117        #[serde(
118            rename = "strokeWidth",
119            default,
120            skip_serializing_if = "Option::is_none"
121        )]
122        stroke_width: Option<f64>,
123        /// Corner radius in points.
124        #[serde(
125            rename = "cornerRadius",
126            default,
127            skip_serializing_if = "Option::is_none"
128        )]
129        corner_radius: Option<f64>,
130    },
131    /// Filled / stroked arc.
132    #[serde(rename = "arc")]
133    Arc {
134        /// Center x-coordinate.
135        cx: f64,
136        /// Center y-coordinate.
137        cy: f64,
138        /// Radius.
139        r: f64,
140        /// Start angle in degrees.
141        #[serde(rename = "startAngle")]
142        start_angle: f64,
143        /// End angle in degrees.
144        #[serde(rename = "endAngle")]
145        end_angle: f64,
146        /// Fill color.
147        #[serde(default, skip_serializing_if = "Option::is_none")]
148        fill: Option<ColorValue>,
149        /// Stroke color.
150        #[serde(default, skip_serializing_if = "Option::is_none")]
151        stroke: Option<ColorValue>,
152        /// Stroke width in points.
153        #[serde(
154            rename = "strokeWidth",
155            default,
156            skip_serializing_if = "Option::is_none"
157        )]
158        stroke_width: Option<f64>,
159    },
160    /// Text label drawn at a point.
161    #[serde(rename = "text")]
162    Text {
163        /// x-coordinate of the anchor point.
164        x: f64,
165        /// y-coordinate of the anchor point.
166        y: f64,
167        /// Text to draw.
168        content: String,
169        /// Font size in points.
170        #[serde(rename = "fontSize", default, skip_serializing_if = "Option::is_none")]
171        font_size: Option<f64>,
172        /// Text color.
173        #[serde(default, skip_serializing_if = "Option::is_none")]
174        color: Option<ColorValue>,
175        /// Text anchor (`"start"`, `"middle"`, or `"end"`).
176        #[serde(default, skip_serializing_if = "Option::is_none")]
177        anchor: Option<String>,
178    },
179    /// Arbitrary vector path.
180    #[serde(rename = "path")]
181    Path {
182        /// SVG path data (e.g. `"M10 10 L90 90"`)
183        d: String,
184        /// Fill color.
185        #[serde(default, skip_serializing_if = "Option::is_none")]
186        fill: Option<ColorValue>,
187        /// Stroke color.
188        #[serde(default, skip_serializing_if = "Option::is_none")]
189        stroke: Option<ColorValue>,
190        /// Stroke width in points.
191        #[serde(
192            rename = "strokeWidth",
193            default,
194            skip_serializing_if = "Option::is_none"
195        )]
196        stroke_width: Option<f64>,
197    },
198}