Skip to main content

embedded_gui/widgets/
controls.rs

1use crate::{
2    geometry::Rect,
3    style::Style,
4    widget::{PropertyError, PropertyKey, PropertyValue, Widget},
5};
6
7/// Interactive slider widget.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct SliderWidget {
10    pub value: f32,
11    pub min: f32,
12    pub max: f32,
13}
14
15impl SliderWidget {
16    pub fn new(value: f32, min: f32, max: f32) -> Self {
17        Self {
18            value: value.clamp(min, max),
19            min,
20            max,
21        }
22    }
23}
24
25impl Widget for SliderWidget {
26    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
27
28    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
29        match key {
30            PropertyKey::Value => Some(PropertyValue::Float(self.value)),
31            PropertyKey::Min => Some(PropertyValue::Float(self.min)),
32            PropertyKey::Max => Some(PropertyValue::Float(self.max)),
33            _ => None,
34        }
35    }
36
37    fn set_property<'a>(
38        &mut self,
39        key: PropertyKey,
40        val: PropertyValue<'a>,
41    ) -> Result<(), PropertyError> {
42        match (key, val) {
43            (PropertyKey::Value, PropertyValue::Float(v)) => {
44                self.value = v.clamp(self.min, self.max);
45                Ok(())
46            }
47            _ => Err(PropertyError::NotFound),
48        }
49    }
50}
51
52/// Toggle switch widget.
53#[derive(Clone, Copy, Debug, PartialEq)]
54pub struct ToggleWidget<'a> {
55    pub label: &'a str,
56    pub on: bool,
57}
58
59impl<'a> ToggleWidget<'a> {
60    pub fn new(label: &'a str, on: bool) -> Self {
61        Self { label, on }
62    }
63}
64
65impl<'a> Widget for ToggleWidget<'a> {
66    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
67
68    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
69        match key {
70            PropertyKey::State => Some(PropertyValue::Bool(self.on)),
71            PropertyKey::Text => Some(PropertyValue::Str(self.label)),
72            _ => None,
73        }
74    }
75
76    fn set_property<'b>(
77        &mut self,
78        key: PropertyKey,
79        val: PropertyValue<'b>,
80    ) -> Result<(), PropertyError> {
81        match (key, val) {
82            (PropertyKey::State, PropertyValue::Bool(b)) => {
83                self.on = b;
84                Ok(())
85            }
86            _ => Err(PropertyError::NotFound),
87        }
88    }
89}
90
91/// Checkbox widget.
92#[derive(Clone, Copy, Debug, PartialEq)]
93pub struct CheckboxWidget<'a> {
94    pub label: &'a str,
95    pub checked: bool,
96}
97
98impl<'a> Widget for CheckboxWidget<'a> {
99    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
100
101    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
102        match key {
103            PropertyKey::State => Some(PropertyValue::Bool(self.checked)),
104            PropertyKey::Text => Some(PropertyValue::Str(self.label)),
105            _ => None,
106        }
107    }
108}