Skip to main content

embedded_gui/widgets/
basic.rs

1use crate::{
2    geometry::Rect,
3    style::Style,
4    widget::{PropertyError, PropertyKey, PropertyValue, Widget},
5};
6
7/// Basic Panel widget container.
8#[derive(Clone, Copy, Debug, Default, PartialEq)]
9pub struct PanelWidget;
10
11impl Widget for PanelWidget {
12    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
13}
14
15/// Text label widget.
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub struct LabelWidget<'a> {
18    pub text: &'a str,
19}
20
21impl<'a> LabelWidget<'a> {
22    pub fn new(text: &'a str) -> Self {
23        Self { text }
24    }
25}
26
27impl<'a> Widget for LabelWidget<'a> {
28    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
29
30    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
31        match key {
32            PropertyKey::Text => Some(PropertyValue::Str(self.text)),
33            _ => None,
34        }
35    }
36
37    fn set_property<'b>(
38        &mut self,
39        key: PropertyKey,
40        val: PropertyValue<'b>,
41    ) -> Result<(), PropertyError> {
42        match (key, val) {
43            (PropertyKey::Text, PropertyValue::Str(_s)) => Ok(()),
44            _ => Err(PropertyError::NotFound),
45        }
46    }
47}
48
49/// Interactive button widget.
50#[derive(Clone, Copy, Debug, PartialEq)]
51pub struct ButtonWidget<'a> {
52    pub text: &'a str,
53}
54
55impl<'a> ButtonWidget<'a> {
56    pub fn new(text: &'a str) -> Self {
57        Self { text }
58    }
59}
60
61impl<'a> Widget for ButtonWidget<'a> {
62    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
63
64    fn get_property(&self, key: PropertyKey) -> Option<PropertyValue<'_>> {
65        match key {
66            PropertyKey::Text => Some(PropertyValue::Str(self.text)),
67            _ => None,
68        }
69    }
70}
71
72/// Spacer widget for layout alignment.
73#[derive(Clone, Copy, Debug, Default, PartialEq)]
74pub struct SpacerWidget;
75
76impl Widget for SpacerWidget {
77    fn render_widget_bounds(&self, _bounds: Rect, _style: &Style) {}
78}