Skip to main content

nemo_plugin/
components.rs

1//! Component builders for common UI elements
2
3use crate::builder::{Builder, LayoutBuilder};
4use nemo_plugin_api::PluginValue;
5
6/// Builder for a Label component
7pub struct Label(LayoutBuilder);
8
9impl Label {
10    /// Create a new label with text
11    pub fn new(text: impl Into<String>) -> Self {
12        Self(LayoutBuilder::new("label").attr("text", PluginValue::String(text.into())))
13    }
14
15    /// Set the label text
16    pub fn text(mut self, text: impl Into<String>) -> Self {
17        self.0 = self.0.attr("text", PluginValue::String(text.into()));
18        self
19    }
20
21    /// Set the label size (xs, sm, md, lg, xl)
22    pub fn size(mut self, size: impl Into<String>) -> Self {
23        self.0 = self.0.attr("size", PluginValue::String(size.into()));
24        self
25    }
26
27    /// Set the label color
28    pub fn color(mut self, color: impl Into<String>) -> Self {
29        self.0 = self.0.attr("color", PluginValue::String(color.into()));
30        self
31    }
32
33    /// Set the font weight
34    pub fn weight(mut self, weight: impl Into<String>) -> Self {
35        self.0 = self.0.attr("weight", PluginValue::String(weight.into()));
36        self
37    }
38
39    /// Bind the text to a data path
40    pub fn bind_text(mut self, path: impl Into<String>) -> Self {
41        self.0 = self.0.bind("text", path);
42        self
43    }
44
45    /// Set width
46    pub fn width(mut self, width: i64) -> Self {
47        self.0 = self.0.width(width);
48        self
49    }
50
51    /// Set height
52    pub fn height(mut self, height: i64) -> Self {
53        self.0 = self.0.height(height);
54        self
55    }
56}
57
58impl Builder for Label {
59    fn build(self) -> PluginValue {
60        self.0.build()
61    }
62}
63
64/// Builder for an Input component
65pub struct Input(LayoutBuilder);
66
67impl Input {
68    /// Create a new input
69    pub fn new() -> Self {
70        Self(LayoutBuilder::new("input"))
71    }
72
73    /// Set the input value
74    pub fn value(mut self, value: impl Into<String>) -> Self {
75        self.0 = self.0.attr("value", PluginValue::String(value.into()));
76        self
77    }
78
79    /// Set the placeholder text
80    pub fn placeholder(mut self, placeholder: impl Into<String>) -> Self {
81        self.0 = self
82            .0
83            .attr("placeholder", PluginValue::String(placeholder.into()));
84        self
85    }
86
87    /// Set the on_change handler
88    pub fn on_change(mut self, handler: impl Into<String>) -> Self {
89        self.0 = self.0.on("change", handler);
90        self
91    }
92
93    /// Set the on_submit handler
94    pub fn on_submit(mut self, handler: impl Into<String>) -> Self {
95        self.0 = self.0.on("submit", handler);
96        self
97    }
98
99    /// Bind the value to a data path
100    pub fn bind_value(mut self, path: impl Into<String>) -> Self {
101        self.0 = self.0.bind("value", path);
102        self
103    }
104
105    /// Set width
106    pub fn width(mut self, width: i64) -> Self {
107        self.0 = self.0.width(width);
108        self
109    }
110
111    /// Set whether the input is disabled
112    pub fn disabled(mut self, disabled: bool) -> Self {
113        self.0 = self.0.attr("disabled", PluginValue::Bool(disabled));
114        self
115    }
116}
117
118impl Default for Input {
119    fn default() -> Self {
120        Self::new()
121    }
122}
123
124impl Builder for Input {
125    fn build(self) -> PluginValue {
126        self.0.build()
127    }
128}
129
130/// Builder for a Button component
131pub struct Button(LayoutBuilder);
132
133impl Button {
134    /// Create a new button with label
135    pub fn new(label: impl Into<String>) -> Self {
136        Self(LayoutBuilder::new("button").attr("label", PluginValue::String(label.into())))
137    }
138
139    /// Set the button label
140    pub fn label(mut self, label: impl Into<String>) -> Self {
141        self.0 = self.0.attr("label", PluginValue::String(label.into()));
142        self
143    }
144
145    /// Set the button variant (primary, secondary, danger, etc.)
146    pub fn variant(mut self, variant: impl Into<String>) -> Self {
147        self.0 = self.0.attr("variant", PluginValue::String(variant.into()));
148        self
149    }
150
151    /// Set the on_click handler
152    pub fn on_click(mut self, handler: impl Into<String>) -> Self {
153        self.0 = self.0.on("click", handler);
154        self
155    }
156
157    /// Set whether the button is disabled
158    pub fn disabled(mut self, disabled: bool) -> Self {
159        self.0 = self.0.attr("disabled", PluginValue::Bool(disabled));
160        self
161    }
162
163    /// Set width
164    pub fn width(mut self, width: i64) -> Self {
165        self.0 = self.0.width(width);
166        self
167    }
168}
169
170impl Builder for Button {
171    fn build(self) -> PluginValue {
172        self.0.build()
173    }
174}
175
176/// Builder for a Switch/Toggle component
177pub struct Switch(LayoutBuilder);
178
179impl Switch {
180    /// Create a new switch
181    pub fn new() -> Self {
182        Self(LayoutBuilder::new("switch"))
183    }
184
185    /// Set the switch label
186    pub fn label(mut self, label: impl Into<String>) -> Self {
187        self.0 = self.0.attr("label", PluginValue::String(label.into()));
188        self
189    }
190
191    /// Set whether the switch is checked
192    pub fn checked(mut self, checked: bool) -> Self {
193        self.0 = self.0.attr("checked", PluginValue::Bool(checked));
194        self
195    }
196
197    /// Set the on_click handler
198    pub fn on_click(mut self, handler: impl Into<String>) -> Self {
199        self.0 = self.0.on("click", handler);
200        self
201    }
202
203    /// Bind the checked state to a data path
204    pub fn bind_checked(mut self, path: impl Into<String>) -> Self {
205        self.0 = self.0.bind("checked", path);
206        self
207    }
208
209    /// Set whether the switch is disabled
210    pub fn disabled(mut self, disabled: bool) -> Self {
211        self.0 = self.0.attr("disabled", PluginValue::Bool(disabled));
212        self
213    }
214}
215
216impl Default for Switch {
217    fn default() -> Self {
218        Self::new()
219    }
220}
221
222impl Builder for Switch {
223    fn build(self) -> PluginValue {
224        self.0.build()
225    }
226}
227
228/// Builder for a Slider component
229pub struct Slider(LayoutBuilder);
230
231impl Slider {
232    /// Create a new slider
233    pub fn new() -> Self {
234        Self(LayoutBuilder::new("slider"))
235    }
236
237    /// Set the slider value
238    pub fn value(mut self, value: f64) -> Self {
239        self.0 = self.0.attr("value", PluginValue::Float(value));
240        self
241    }
242
243    /// Set the minimum value
244    pub fn min(mut self, min: f64) -> Self {
245        self.0 = self.0.attr("min", PluginValue::Float(min));
246        self
247    }
248
249    /// Set the maximum value
250    pub fn max(mut self, max: f64) -> Self {
251        self.0 = self.0.attr("max", PluginValue::Float(max));
252        self
253    }
254
255    /// Set the step increment
256    pub fn step(mut self, step: f64) -> Self {
257        self.0 = self.0.attr("step", PluginValue::Float(step));
258        self
259    }
260
261    /// Set the on_change handler
262    pub fn on_change(mut self, handler: impl Into<String>) -> Self {
263        self.0 = self.0.on("change", handler);
264        self
265    }
266
267    /// Bind the value to a data path
268    pub fn bind_value(mut self, path: impl Into<String>) -> Self {
269        self.0 = self.0.bind("value", path);
270        self
271    }
272}
273
274impl Default for Slider {
275    fn default() -> Self {
276        Self::new()
277    }
278}
279
280impl Builder for Slider {
281    fn build(self) -> PluginValue {
282        self.0.build()
283    }
284}
285
286/// Builder for an Image component
287pub struct Image(LayoutBuilder);
288
289impl Image {
290    /// Create a new image with source path
291    pub fn new(src: impl Into<String>) -> Self {
292        Self(LayoutBuilder::new("image").attr("src", PluginValue::String(src.into())))
293    }
294
295    /// Set the image source
296    pub fn src(mut self, src: impl Into<String>) -> Self {
297        self.0 = self.0.attr("src", PluginValue::String(src.into()));
298        self
299    }
300
301    /// Set the alt text
302    pub fn alt(mut self, alt: impl Into<String>) -> Self {
303        self.0 = self.0.attr("alt", PluginValue::String(alt.into()));
304        self
305    }
306
307    /// Set width
308    pub fn width(mut self, width: i64) -> Self {
309        self.0 = self.0.width(width);
310        self
311    }
312
313    /// Set height
314    pub fn height(mut self, height: i64) -> Self {
315        self.0 = self.0.height(height);
316        self
317    }
318}
319
320impl Builder for Image {
321    fn build(self) -> PluginValue {
322        self.0.build()
323    }
324}