Skip to main content

winio/widgets/
check_box.rs

1use inherit_methods_macro::inherit_methods;
2use winio_elm::{Component, ComponentSender};
3use winio_handle::BorrowedContainer;
4use winio_primitive::{Enable, Failable, Layoutable, Point, Size, TextWidget, ToolTip, Visible};
5
6use crate::{
7    sys,
8    sys::{Error, Result},
9};
10
11/// A simple check box.
12#[derive(Debug)]
13pub struct CheckBox {
14    widget: sys::CheckBox,
15}
16
17impl Failable for CheckBox {
18    type Error = Error;
19}
20
21#[inherit_methods(from = "self.widget")]
22impl ToolTip for CheckBox {
23    fn tooltip(&self) -> Result<String>;
24
25    fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
26}
27
28#[inherit_methods(from = "self.widget")]
29impl TextWidget for CheckBox {
30    fn text(&self) -> Result<String>;
31
32    fn set_text(&mut self, s: impl AsRef<str>) -> Result<()>;
33}
34
35#[inherit_methods(from = "self.widget")]
36impl CheckBox {
37    /// If the box is checked.
38    pub fn is_checked(&self) -> Result<bool>;
39
40    /// Set the checked state.
41    pub fn set_checked(&mut self, v: bool) -> Result<()>;
42}
43
44#[inherit_methods(from = "self.widget")]
45impl Visible for CheckBox {
46    fn is_visible(&self) -> Result<bool>;
47
48    fn set_visible(&mut self, v: bool) -> Result<()>;
49}
50
51#[inherit_methods(from = "self.widget")]
52impl Enable for CheckBox {
53    fn is_enabled(&self) -> Result<bool>;
54
55    fn set_enabled(&mut self, v: bool) -> Result<()>;
56}
57
58#[inherit_methods(from = "self.widget")]
59impl Layoutable for CheckBox {
60    fn loc(&self) -> Result<Point>;
61
62    fn set_loc(&mut self, p: Point) -> Result<()>;
63
64    fn size(&self) -> Result<Size>;
65
66    fn set_size(&mut self, v: Size) -> Result<()>;
67
68    fn preferred_size(&self) -> Result<Size>;
69}
70
71/// Events of [`CheckBox`].
72#[derive(Debug)]
73#[non_exhaustive]
74pub enum CheckBoxEvent {
75    /// The check box has been clicked.
76    Click,
77}
78
79/// Messages of [`CheckBox`].
80#[derive(Debug)]
81#[non_exhaustive]
82pub enum CheckBoxMessage {}
83
84impl Component for CheckBox {
85    type Error = Error;
86    type Event = CheckBoxEvent;
87    type Init<'a> = BorrowedContainer<'a>;
88    type Message = CheckBoxMessage;
89
90    async fn init(init: Self::Init<'_>, _sender: &ComponentSender<Self>) -> Result<Self> {
91        let widget = sys::CheckBox::new(init)?;
92        Ok(Self { widget })
93    }
94
95    async fn start(&mut self, sender: &ComponentSender<Self>) -> ! {
96        loop {
97            self.widget.wait_click().await;
98            sender.output(CheckBoxEvent::Click);
99        }
100    }
101}
102
103winio_handle::impl_as_widget!(CheckBox, widget);