winio_ui_app_kit/widgets/
label.rs1use inherit_methods_macro::inherit_methods;
2use objc2::{MainThreadOnly, rc::Retained};
3use objc2_app_kit::{NSTextAlignment, NSTextField};
4use winio_handle::AsContainer;
5use winio_primitive::{HAlign, Point, Size};
6
7use crate::{Result, catch, widgets::Widget};
8
9#[derive(Debug)]
10pub struct Label {
11 handle: Widget,
12 view: Retained<NSTextField>,
13}
14
15#[inherit_methods(from = "self.handle")]
16impl Label {
17 pub fn new(parent: impl AsContainer) -> Result<Self> {
18 let parent = parent.as_container();
19
20 catch(|| unsafe {
21 let view = NSTextField::new(parent.as_app_kit().mtm());
22 view.setBezeled(false);
23 view.setDrawsBackground(false);
24 view.setEditable(false);
25 view.setSelectable(false);
26 let handle = Widget::from_nsview(parent, Retained::cast_unchecked(view.clone()))?;
27
28 Ok(Self { handle, view })
29 })
30 .flatten()
31 }
32
33 pub fn is_visible(&self) -> Result<bool>;
34
35 pub fn set_visible(&mut self, v: bool) -> Result<()>;
36
37 pub fn is_enabled(&self) -> Result<bool>;
38
39 pub fn set_enabled(&mut self, v: bool) -> Result<()>;
40
41 pub fn preferred_size(&self) -> Result<Size> {
42 let mut size = self.handle.preferred_size()?;
43 size.width += 8.0;
44 Ok(size)
45 }
46
47 pub fn loc(&self) -> Result<Point>;
48
49 pub fn set_loc(&mut self, p: Point) -> Result<()>;
50
51 pub fn size(&self) -> Result<Size>;
52
53 pub fn set_size(&mut self, v: Size) -> Result<()>;
54
55 pub fn tooltip(&self) -> Result<String>;
56
57 pub fn set_tooltip(&mut self, s: impl AsRef<str>) -> Result<()>;
58
59 pub fn text(&self) -> Result<String>;
60
61 pub fn set_text(&mut self, s: impl AsRef<str>) -> Result<()>;
62
63 pub fn halign(&self) -> Result<HAlign> {
64 let align = catch(|| self.view.alignment())?;
65 let align = match align {
66 NSTextAlignment::Right => HAlign::Right,
67 NSTextAlignment::Center => HAlign::Center,
68 NSTextAlignment::Justified => HAlign::Stretch,
69 _ => HAlign::Left,
70 };
71 Ok(align)
72 }
73
74 pub fn set_halign(&mut self, align: HAlign) -> Result<()> {
75 let align = match align {
76 HAlign::Left => NSTextAlignment::Left,
77 HAlign::Center => NSTextAlignment::Center,
78 HAlign::Right => NSTextAlignment::Right,
79 HAlign::Stretch => NSTextAlignment::Justified,
80 };
81 catch(|| self.view.setAlignment(align))
82 }
83}
84
85winio_handle::impl_as_widget!(Label, handle);