Skip to main content

winio_ui_winui/widgets/
label.rs

1use inherit_methods_macro::inherit_methods;
2use windows::core::{HSTRING, Interface};
3use winio_handle::AsContainer;
4use winio_primitive::{HAlign, Point, Size};
5use winui3::Microsoft::UI::Xaml::{Controls as MUXC, TextWrapping};
6
7use crate::{Result, Widget, widgets::Convertible};
8
9#[derive(Debug)]
10pub struct Label {
11    handle: Widget,
12    label: MUXC::TextBlock,
13}
14
15#[inherit_methods(from = "self.handle")]
16impl Label {
17    pub fn new(parent: impl AsContainer) -> Result<Self> {
18        let label = MUXC::TextBlock::new()?;
19        label.SetTextWrapping(TextWrapping::Wrap)?;
20        Ok(Self {
21            handle: Widget::new(parent, label.cast()?)?,
22            label,
23        })
24    }
25
26    pub fn is_visible(&self) -> Result<bool>;
27
28    pub fn set_visible(&mut self, v: bool) -> Result<()>;
29
30    pub fn is_enabled(&self) -> Result<bool>;
31
32    pub fn set_enabled(&mut self, v: bool) -> Result<()>;
33
34    pub fn preferred_size(&self) -> Result<Size>;
35
36    pub fn loc(&self) -> Result<Point>;
37
38    pub fn set_loc(&mut self, p: Point) -> Result<()>;
39
40    pub fn size(&self) -> Result<Size>;
41
42    pub fn set_size(&mut self, v: Size) -> Result<()>;
43
44    pub fn tooltip(&self) -> Result<String>;
45
46    pub fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
47
48    pub fn text(&self) -> Result<String> {
49        Ok(self.label.Text()?.to_string_lossy())
50    }
51
52    pub fn set_text(&mut self, s: impl AsRef<str>) -> Result<()> {
53        self.label.SetText(&HSTRING::from(s.as_ref()))?;
54        Ok(())
55    }
56
57    pub fn halign(&self) -> Result<HAlign> {
58        Ok(HAlign::from_native(self.label.TextAlignment()?))
59    }
60
61    pub fn set_halign(&mut self, align: HAlign) -> Result<()> {
62        self.label.SetTextAlignment(align.to_native())?;
63        Ok(())
64    }
65}
66
67winio_handle::impl_as_widget!(Label, handle);