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