rust_macios/appkit/
ns_status_item.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    core_graphics::CGFloat,
5    object,
6    objective_c_runtime::{
7        nil,
8        traits::{FromId, PNSObject},
9    },
10    utils::to_bool,
11};
12
13use super::{interface_impl, NSMenu, NSStatusBar, NSStatusBarButton, NSStatusItemBehavior};
14
15object! {
16    /// An individual element displayed in the system menu bar.
17    unsafe pub struct NSStatusItem;
18}
19
20impl NSStatusItem {
21    /// A status item length that is equal to the status bar’s thickness.
22    pub const NSSQUARE_STATUS_ITEM_LENGTH: CGFloat = -2.0;
23
24    /// A status item length that dynamically adjusts to the width of its contents.
25    pub const NSVARIABLE_STATUS_ITEM_LENGTH: CGFloat = -1.0;
26
27    /// Creates a new status item.
28    pub fn new() -> Self {
29        unsafe { Self::from_id(msg_send![NSStatusItem::m_class(), alloc]) }
30    }
31}
32
33impl Default for NSStatusItem {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39#[interface_impl(NSObject)]
40impl NSStatusItem {
41    /* Getting the Item’s Status Bar
42     */
43
44    /// The status bar that displays the status item.
45    #[property]
46    pub fn status_bar(&self) -> NSStatusBar {
47        unsafe { NSStatusBar::from_id(msg_send![self.m_self(), statusBar]) }
48    }
49
50    /* Managing the Status Item’s Behavior
51     */
52
53    /// The set of allowed behaviors for the status item.
54    #[property]
55    pub fn behavior(&self) -> NSStatusItemBehavior {
56        unsafe { msg_send![self.m_self(), behavior] }
57    }
58
59    /// The button displayed in the status bar.
60    #[property]
61    pub fn button(&self) -> Option<NSStatusBarButton> {
62        unsafe {
63            let id = msg_send![self.m_self(), button];
64            if id == nil {
65                None
66            } else {
67                Some(NSStatusBarButton::from_id(id))
68            }
69        }
70    }
71
72    /// The pull-down menu displayed when the user clicks the status item.
73    #[property]
74    pub fn menu(&self) -> NSMenu {
75        unsafe { NSMenu::from_id(msg_send![self.m_self(), menu]) }
76    }
77
78    /* Configuring the Status Item’s Appearance
79     */
80
81    /// A Boolean value indicating if the menu bar currently displays the status item.
82    #[property]
83    pub fn visible(&self) -> bool {
84        unsafe { to_bool(msg_send![self.m_self(), isVisible]) }
85    }
86
87    /// The amount of space in the status bar that should be allocated to the status item.
88    #[property]
89    pub fn length(&self) -> CGFloat {
90        unsafe { msg_send![self.m_self(), length] }
91    }
92}