winio_primitive/
traits.rs

1use crate::{Point, Rect, Size};
2
3/// A trait for types that can fail with an associated error type.
4pub trait Failable {
5    /// The error type associated.
6    type Error;
7}
8
9/// Trait for a widget to set visibility.
10pub trait Visible: Failable {
11    /// If the widget is visible.
12    fn is_visible(&self) -> Result<bool, Self::Error>;
13
14    /// Set the visibility.
15    fn set_visible(&mut self, v: bool) -> Result<(), Self::Error>;
16
17    /// Show the widget.
18    fn show(&mut self) -> Result<(), Self::Error> {
19        self.set_visible(true)
20    }
21
22    /// Hide the widget.
23    fn hide(&mut self) -> Result<(), Self::Error> {
24        self.set_visible(false)
25    }
26}
27
28/// Trait for a widget to enable or disable.
29pub trait Enable: Failable {
30    /// If the widget is enabled.
31    fn is_enabled(&self) -> Result<bool, Self::Error>;
32
33    /// Set if the widget is enabled.
34    fn set_enabled(&mut self, v: bool) -> Result<(), Self::Error>;
35
36    /// Enable the widget.
37    fn enable(&mut self) -> Result<(), Self::Error> {
38        self.set_enabled(true)
39    }
40
41    /// Disable the widget.
42    fn disable(&mut self) -> Result<(), Self::Error> {
43        self.set_enabled(false)
44    }
45}
46
47/// Common trait for widgets that have a tooltip.
48pub trait ToolTip: Failable {
49    /// Get the tooltip text of the widget.
50    fn tooltip(&self) -> Result<String, Self::Error>;
51
52    /// Set the tooltip text of the widget.
53    fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<(), Self::Error>;
54}
55
56/// Common trait for widgets that have text.
57pub trait TextWidget: Failable {
58    /// Get the text of the widget.
59    fn text(&self) -> Result<String, Self::Error>;
60
61    /// Set the text of the widget.
62    ///
63    /// If the widget supports multiline strings, lines are separated with `\n`.
64    /// You don't need to handle CRLF.
65    fn set_text(&mut self, s: impl AsRef<str>) -> Result<(), Self::Error>;
66}
67
68/// Trait for a layoutable widget.
69///
70/// To create a responsive layout, always set location and size together.
71pub trait Layoutable: Failable {
72    /// The left top location.
73    fn loc(&self) -> Result<Point, Self::Error>;
74
75    /// Move the location.
76    fn set_loc(&mut self, p: Point) -> Result<(), Self::Error>;
77
78    /// The size.
79    fn size(&self) -> Result<Size, Self::Error>;
80
81    /// Resize.
82    fn set_size(&mut self, s: Size) -> Result<(), Self::Error>;
83
84    /// The bounding rectangle.
85    fn rect(&self) -> Result<Rect, Self::Error> {
86        Ok(Rect::new(self.loc()?, self.size()?))
87    }
88
89    /// Set the location and size.
90    fn set_rect(&mut self, r: Rect) -> Result<(), Self::Error> {
91        self.set_loc(r.origin)?;
92        self.set_size(r.size)
93    }
94
95    /// The preferred size.
96    fn preferred_size(&self) -> Result<Size, Self::Error> {
97        Ok(Size::zero())
98    }
99
100    /// Min acceptable size.
101    fn min_size(&self) -> Result<Size, Self::Error> {
102        self.preferred_size()
103    }
104}