Skip to main content

winio/widgets/
label.rs

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