Skip to main content

winio_ui_winui/widgets/
check_box.rs

1use std::rc::Rc;
2
3use inherit_methods_macro::inherit_methods;
4use send_wrapper::SendWrapper;
5use windows::core::{HSTRING, Interface};
6use winio_callback::Callback;
7use winio_handle::AsContainer;
8use winio_primitive::{Point, Size};
9use winui3::Microsoft::UI::Xaml::{Controls as MUXC, RoutedEventHandler};
10
11use crate::{GlobalRuntime, Result, Widget, widgets::ToIReference};
12
13#[derive(Debug)]
14pub struct CheckBox {
15    on_click: SendWrapper<Rc<Callback>>,
16    handle: Widget,
17    button: MUXC::CheckBox,
18    text: MUXC::TextBlock,
19}
20
21#[inherit_methods(from = "self.handle")]
22impl CheckBox {
23    pub fn new(parent: impl AsContainer) -> Result<Self> {
24        let button = MUXC::CheckBox::new()?;
25        let on_click = SendWrapper::new(Rc::new(Callback::new()));
26        {
27            let on_click = on_click.clone();
28            button.Click(&RoutedEventHandler::new(move |_, _| {
29                on_click.signal::<GlobalRuntime>(());
30                Ok(())
31            }))?;
32        }
33        let text = MUXC::TextBlock::new()?;
34        button.SetContent(&text)?;
35        Ok(Self {
36            on_click,
37            handle: Widget::new(parent, button.cast()?)?,
38            button,
39            text,
40        })
41    }
42
43    pub fn is_visible(&self) -> Result<bool>;
44
45    pub fn set_visible(&mut self, v: bool) -> Result<()>;
46
47    pub fn is_enabled(&self) -> Result<bool>;
48
49    pub fn set_enabled(&mut self, v: bool) -> Result<()>;
50
51    pub fn preferred_size(&self) -> Result<Size>;
52
53    pub fn loc(&self) -> Result<Point>;
54
55    pub fn set_loc(&mut self, p: Point) -> Result<()>;
56
57    pub fn size(&self) -> Result<Size>;
58
59    pub fn set_size(&mut self, v: Size) -> Result<()>;
60
61    pub fn tooltip(&self) -> Result<String>;
62
63    pub fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
64
65    pub fn text(&self) -> Result<String> {
66        Ok(self.text.Text()?.to_string_lossy())
67    }
68
69    pub fn set_text(&mut self, s: impl AsRef<str>) -> Result<()> {
70        self.text.SetText(&HSTRING::from(s.as_ref()))?;
71        Ok(())
72    }
73
74    pub fn is_checked(&self) -> Result<bool> {
75        self.button.IsChecked()?.GetBoolean()
76    }
77
78    pub fn set_checked(&mut self, v: bool) -> Result<()> {
79        self.button.SetIsChecked(&v.to_reference()?)?;
80        Ok(())
81    }
82
83    pub async fn wait_click(&self) {
84        self.on_click.wait().await
85    }
86}
87
88winio_handle::impl_as_widget!(CheckBox, handle);