Skip to main content

winio_ui_win32/widgets/
button.rs

1use inherit_methods_macro::inherit_methods;
2use windows_sys::Win32::UI::{
3    Controls::WC_BUTTONW,
4    WindowsAndMessaging::{
5        BN_CLICKED, BS_PUSHBUTTON, WM_COMMAND, WS_CHILD, WS_TABSTOP, WS_VISIBLE,
6    },
7};
8use winio_handle::{AsContainer, AsWidget};
9use winio_primitive::{Point, Size};
10
11use crate::{Result, runtime::WindowMessageCommand, widgets::Widget};
12
13#[derive(Debug)]
14pub struct Button {
15    handle: Widget,
16}
17
18#[inherit_methods(from = "self.handle")]
19impl Button {
20    pub fn new(parent: impl AsContainer) -> Result<Self> {
21        let handle = Widget::new(
22            WC_BUTTONW,
23            WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON as u32,
24            0,
25            parent.as_container().as_win32(),
26        )?;
27        Ok(Self { handle })
28    }
29
30    pub fn is_visible(&self) -> Result<bool>;
31
32    pub fn set_visible(&mut self, v: bool) -> Result<()>;
33
34    pub fn is_enabled(&self) -> Result<bool>;
35
36    pub fn set_enabled(&mut self, v: bool) -> Result<()>;
37
38    pub fn preferred_size(&self) -> Result<Size> {
39        let s = self.handle.measure_text()?;
40        Ok(Size::new(s.width + 4.0, s.height + 4.0))
41    }
42
43    pub fn loc(&self) -> Result<Point>;
44
45    pub fn set_loc(&mut self, p: Point) -> Result<()>;
46
47    pub fn size(&self) -> Result<Size>;
48
49    pub fn set_size(&mut self, v: Size) -> Result<()>;
50
51    pub fn tooltip(&self) -> Result<String>;
52
53    pub fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
54
55    pub fn text(&self) -> Result<String>;
56
57    pub fn set_text(&mut self, s: impl AsRef<str>) -> Result<()>;
58
59    pub async fn wait_click(&self) {
60        loop {
61            let WindowMessageCommand {
62                message, handle, ..
63            } = self.handle.wait_parent(WM_COMMAND).await.command();
64            if std::ptr::eq(handle, self.handle.as_widget().as_win32()) && (message == BN_CLICKED) {
65                break;
66            }
67        }
68    }
69}
70
71winio_handle::impl_as_widget!(Button, handle);