1use super::{
4 BoolValue, IntValue, IntoBoolValue, IntoIntValue, IntoTextValue, MountContext, TextValue, View,
5};
6use crate::bridge as ffi;
7use crate::{Button, CheckBox, ComboBox, Label, LineEdit, ProgressBar, Slider, SpinBox};
8use yse_model::{Signal, Var};
9
10pub struct LabelView {
12 text: TextValue,
13}
14
15pub fn label(text: impl IntoTextValue) -> LabelView {
17 LabelView {
18 text: text.into_text_value(),
19 }
20}
21
22impl View for LabelView {
23 type Output = Label;
24
25 fn mount_in(self, context: &MountContext) -> Label {
26 let initial = match &self.text {
27 TextValue::Static(text) => text.clone(),
28 TextValue::Reactive(signal) => signal.value().as_ref().clone(),
29 };
30 let inner = context.child(unsafe { ffi::widget_new_label(&initial, context.parent.raw()) });
31 let label = Label { inner };
32 if let TextValue::Reactive(signal) = self.text {
33 label.bind_text(&signal);
34 }
35 label
36 }
37}
38
39type ClickObserver = Box<dyn FnMut(&())>;
40type ValueObserver = Box<dyn FnMut(&i32)>;
41
42pub struct ButtonView {
44 text: String,
45 enabled: Option<BoolValue>,
46 clicks: Vec<ClickObserver>,
47}
48
49pub fn button(text: impl Into<String>) -> ButtonView {
51 ButtonView {
52 text: text.into(),
53 enabled: None,
54 clicks: Vec::new(),
55 }
56}
57
58impl ButtonView {
59 pub fn enabled(mut self, enabled: impl IntoBoolValue) -> Self {
61 self.enabled = Some(enabled.into_bool_value());
62 self
63 }
64
65 pub fn on_click(mut self, observer: impl FnMut(&()) + 'static) -> Self {
67 self.clicks.push(Box::new(observer));
68 self
69 }
70}
71
72impl View for ButtonView {
73 type Output = Button;
74
75 fn mount_in(self, context: &MountContext) -> Button {
76 let inner =
77 context.child(unsafe { ffi::widget_new_button(&self.text, context.parent.raw()) });
78 let button = Button { inner };
79 if let Some(enabled) = self.enabled {
80 match enabled {
81 BoolValue::Static(enabled) => button.set_enabled(enabled),
82 BoolValue::Reactive(signal) => button.bind_enabled(&signal),
83 }
84 }
85 for observer in self.clicks {
86 button.on_click(observer);
87 }
88 button
89 }
90}
91
92enum LineEditValue {
93 Static(String),
94 Reactive(Signal<String>),
95 Controlled(Var<String>),
96}
97
98pub struct LineEditView {
100 value: LineEditValue,
101}
102
103pub fn line_edit(text: impl Into<String>) -> LineEditView {
105 LineEditView {
106 value: LineEditValue::Static(text.into()),
107 }
108}
109
110impl LineEditView {
111 pub fn value(mut self, signal: Signal<String>) -> Self {
113 self.value = LineEditValue::Reactive(signal);
114 self
115 }
116
117 pub fn controlled(mut self, value: Var<String>) -> Self {
120 self.value = LineEditValue::Controlled(value);
121 self
122 }
123}
124
125impl View for LineEditView {
126 type Output = LineEdit;
127
128 fn mount_in(self, context: &MountContext) -> LineEdit {
129 let initial = match &self.value {
130 LineEditValue::Static(text) => text.clone(),
131 LineEditValue::Reactive(signal) => signal.value().as_ref().clone(),
132 LineEditValue::Controlled(var) => var.value().as_ref().clone(),
133 };
134 let inner =
135 context.child(unsafe { ffi::widget_new_line_edit(&initial, context.parent.raw()) });
136 let input = LineEdit { inner };
137 match self.value {
138 LineEditValue::Static(_) => {}
139 LineEditValue::Reactive(signal) => input.bind_text(&signal),
140 LineEditValue::Controlled(var) => input.bind_text_two_way(&var),
141 }
142 input
143 }
144}
145
146pub struct CheckBoxView {
148 text: String,
149 checked: Option<BoolValue>,
150}
151
152pub fn checkbox(text: impl Into<String>) -> CheckBoxView {
154 CheckBoxView {
155 text: text.into(),
156 checked: None,
157 }
158}
159
160impl CheckBoxView {
161 pub fn checked(mut self, checked: impl IntoBoolValue) -> Self {
163 self.checked = Some(checked.into_bool_value());
164 self
165 }
166}
167
168impl View for CheckBoxView {
169 type Output = CheckBox;
170
171 fn mount_in(self, context: &MountContext) -> CheckBox {
172 let inner =
173 context.child(unsafe { ffi::widget_new_checkbox(&self.text, context.parent.raw()) });
174 let checkbox = CheckBox { inner };
175 if let Some(checked) = self.checked {
176 match checked {
177 BoolValue::Static(checked) => checkbox.set_checked(checked),
178 BoolValue::Reactive(signal) => checkbox.bind_checked(&signal),
179 }
180 }
181 checkbox
182 }
183}
184
185pub struct ComboBoxView {
187 items: Vec<String>,
188 selected: Option<IntValue>,
189 changes: Vec<ValueObserver>,
190}
191
192pub fn combo_box<I, S>(items: I) -> ComboBoxView
194where
195 I: IntoIterator<Item = S>,
196 S: Into<String>,
197{
198 ComboBoxView {
199 items: items.into_iter().map(Into::into).collect(),
200 selected: None,
201 changes: Vec::new(),
202 }
203}
204
205impl ComboBoxView {
206 pub fn value(mut self, value: impl IntoIntValue) -> Self {
208 self.selected = Some(value.into_int_value());
209 self
210 }
211
212 pub fn on_value_change(mut self, observer: impl FnMut(&i32) + 'static) -> Self {
214 self.changes.push(Box::new(observer));
215 self
216 }
217}
218
219impl View for ComboBoxView {
220 type Output = ComboBox;
221
222 fn mount_in(self, context: &MountContext) -> ComboBox {
223 let inner =
224 context.child(unsafe { ffi::widget_new_combo(self.items, context.parent.raw()) });
225 let combo = ComboBox { inner };
226 if let Some(selected) = self.selected {
227 match selected {
228 IntValue::Static(index) => combo.set_current_index(index),
229 IntValue::Reactive(signal) => combo.bind_value(&signal),
230 IntValue::Controlled(var) => combo.bind_value_two_way(&var),
231 }
232 }
233 for observer in self.changes {
234 combo.on_value_change(observer);
235 }
236 combo
237 }
238}
239
240pub struct SpinBoxView {
242 value: IntValue,
243 range: (i32, i32),
244 changes: Vec<ValueObserver>,
245}
246
247pub fn spin_box(value: impl IntoIntValue) -> SpinBoxView {
249 SpinBoxView {
250 value: value.into_int_value(),
251 range: (0, 100),
252 changes: Vec::new(),
253 }
254}
255
256impl SpinBoxView {
257 pub fn range(mut self, min: i32, max: i32) -> Self {
259 self.range = (min, max);
260 self
261 }
262
263 pub fn on_value_change(mut self, observer: impl FnMut(&i32) + 'static) -> Self {
265 self.changes.push(Box::new(observer));
266 self
267 }
268}
269
270impl View for SpinBoxView {
271 type Output = SpinBox;
272
273 fn mount_in(self, context: &MountContext) -> SpinBox {
274 let initial = match &self.value {
275 IntValue::Static(value) => *value,
276 IntValue::Reactive(signal) => *signal.value(),
277 IntValue::Controlled(var) => *var.value(),
278 };
279 let inner = context.child(unsafe {
280 ffi::widget_new_spin_box(self.range.0, self.range.1, initial, context.parent.raw())
281 });
282 let spin = SpinBox { inner };
283 match self.value {
284 IntValue::Static(_) => {}
285 IntValue::Reactive(signal) => spin.bind_value(&signal),
286 IntValue::Controlled(var) => spin.bind_value_two_way(&var),
287 }
288 for observer in self.changes {
289 spin.on_value_change(observer);
290 }
291 spin
292 }
293}
294
295pub struct SliderView {
297 value: IntValue,
298 range: (i32, i32),
299 changes: Vec<ValueObserver>,
300}
301
302pub fn slider(value: impl IntoIntValue) -> SliderView {
304 SliderView {
305 value: value.into_int_value(),
306 range: (0, 100),
307 changes: Vec::new(),
308 }
309}
310
311impl SliderView {
312 pub fn range(mut self, min: i32, max: i32) -> Self {
314 self.range = (min, max);
315 self
316 }
317
318 pub fn on_value_change(mut self, observer: impl FnMut(&i32) + 'static) -> Self {
320 self.changes.push(Box::new(observer));
321 self
322 }
323}
324
325impl View for SliderView {
326 type Output = Slider;
327
328 fn mount_in(self, context: &MountContext) -> Slider {
329 let initial = match &self.value {
330 IntValue::Static(value) => *value,
331 IntValue::Reactive(signal) => *signal.value(),
332 IntValue::Controlled(var) => *var.value(),
333 };
334 let inner = context.child(unsafe {
335 ffi::widget_new_slider(self.range.0, self.range.1, initial, context.parent.raw())
336 });
337 let slider = Slider { inner };
338 match self.value {
339 IntValue::Static(_) => {}
340 IntValue::Reactive(signal) => slider.bind_value(&signal),
341 IntValue::Controlled(var) => slider.bind_value_two_way(&var),
342 }
343 for observer in self.changes {
344 slider.on_value_change(observer);
345 }
346 slider
347 }
348}
349
350pub struct ProgressBarView {
352 value: IntValue,
353 range: (i32, i32),
354}
355
356pub fn progress_bar(value: impl IntoIntValue) -> ProgressBarView {
358 ProgressBarView {
359 value: value.into_int_value(),
360 range: (0, 100),
361 }
362}
363
364impl ProgressBarView {
365 pub fn range(mut self, min: i32, max: i32) -> Self {
367 self.range = (min, max);
368 self
369 }
370}
371
372impl View for ProgressBarView {
373 type Output = ProgressBar;
374
375 fn mount_in(self, context: &MountContext) -> ProgressBar {
376 let initial = match &self.value {
377 IntValue::Static(value) => *value,
378 IntValue::Reactive(signal) => *signal.value(),
379 IntValue::Controlled(var) => *var.value(),
380 };
381 let inner = context.child(unsafe {
382 ffi::widget_new_progress_bar(self.range.0, self.range.1, initial, context.parent.raw())
383 });
384 let bar = ProgressBar { inner };
385 match self.value {
386 IntValue::Static(_) => {}
387 IntValue::Reactive(signal) => bar.bind_value(&signal),
388 IntValue::Controlled(var) => bar.bind_value(&var.signal()),
389 }
390 bar
391 }
392}