Skip to main content

embedded_gui/widgets/
gauges.rs

1use crate::{
2    geometry::Rect,
3    style::Style,
4    widget::{PropertyError, PropertyKey, PropertyValue, Widget},
5};
6
7/// Progress bar widget.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct ProgressBarWidget {
10    pub value: f32,
11}
12
13impl ProgressBarWidget {
14    pub fn new(value: f32) -> Self {
15        Self {
16            value: value.clamp(0.0, 1.0),
17        }
18    }
19}
20
21impl Widget for ProgressBarWidget {
22    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
23
24    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
25        match key {
26            PropertyKey::Value | PropertyKey::Progress => Some(PropertyValue::Float(self.value)),
27            _ => None,
28        }
29    }
30
31    fn set_property<'a>(
32        &mut self,
33        key: PropertyKey,
34        val: PropertyValue<'a>,
35    ) -> Result<(), PropertyError> {
36        match (key, val) {
37            (PropertyKey::Value | PropertyKey::Progress, PropertyValue::Float(v)) => {
38                self.value = v.clamp(0.0, 1.0);
39                Ok(())
40            }
41            _ => Err(PropertyError::NotFound),
42        }
43    }
44}