rust_macios/appkit/
ns_popover.rs

1use objc::{class, msg_send, sel, sel_impl};
2
3use crate::{
4    core_graphics::CGRectEdge,
5    foundation::{NSCoder, NSRect, NSSize},
6    object,
7    objective_c_runtime::{id, traits::FromId},
8    utils::to_bool,
9};
10
11use super::{
12    interface_impl, ns_appearance::NSAppearance, INSResponder, INSView, INSViewController,
13    NSPopoverBehavior, NSViewController,
14};
15
16object! {
17    /// A means to display additional content related to existing content on the screen.
18    unsafe pub struct NSPopover;
19}
20
21impl NSPopover {
22    /// Creates a new popover.
23    pub fn new() -> Self {
24        unsafe { Self::from_id(msg_send![class!(NSPopover), new]) }
25    }
26}
27
28impl Default for NSPopover {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl INSResponder for NSPopover {}
35
36#[interface_impl(NSResponder)]
37impl NSPopover {
38    /* Accessing a Popover’s Content View Controller
39     */
40
41    /// The view controller that manages the content of the popover.
42    #[property]
43    pub fn content_view_controller(&self) -> NSViewController {
44        unsafe { NSViewController::from_id(msg_send![self.m_self(), contentViewController]) }
45    }
46
47    /// Sets the view controller that manages the content of the popover.
48    ///
49    /// # Arguments
50    ///
51    /// * `view_controller` - The view controller that manages the content of the popover.
52    #[property]
53    pub fn set_content_view_controller<Controller>(&self, view_controller: Controller)
54    where
55        Controller: INSViewController,
56    {
57        unsafe { msg_send![self.m_self(), setContentViewController: view_controller] }
58    }
59
60    /*  Managing a Popover's Position and Size */
61
62    /// Specifies the behavior of the popover.
63    #[property]
64    pub fn behavior(&self) -> NSPopoverBehavior {
65        unsafe { msg_send![self.m_self(), behavior] }
66    }
67
68    /// Sets the behavior of the popover.
69    ///
70    /// # Arguments
71    ///
72    /// * `behavior` - The behavior of the popover.
73    #[property]
74    pub fn set_behavior(&self, behavior: NSPopoverBehavior) {
75        unsafe { msg_send![self.m_self(), setBehavior: behavior] }
76    }
77
78    /// Shows the popover anchored to the specified view.
79    #[method]
80    pub fn show_relative_to_rect_of_view_preferred_edge<V>(
81        &self,
82        rect: NSRect,
83        view: V,
84        edge: CGRectEdge,
85    ) where
86        V: INSView,
87    {
88        unsafe {
89            msg_send![self.m_self(), showRelativeToRect: rect ofView: view preferredEdge: edge]
90        }
91    }
92
93    /// The rectangle within the positioning view relative to which the popover should be positioned.
94    #[property]
95    pub fn positioning_rect(&self) -> NSRect {
96        unsafe { msg_send![self.m_self(), positioningRect] }
97    }
98
99    /* Managing a Popover's Appearance
100     */
101
102    /// The appearance of the popover.
103    #[property]
104    pub fn appearance(&self) -> NSAppearance {
105        unsafe { NSAppearance::from_id(msg_send![self.m_self(), appearance]) }
106    }
107
108    /// Sets the appearance of the popover.
109    ///
110    /// # Arguments
111    ///
112    /// * `appearance` - The appearance to use.
113    #[method]
114    pub fn set_appearance(&self, appearance: NSAppearance) {
115        unsafe { msg_send![self.m_self(), setAppearance: appearance] }
116    }
117
118    /// The appearance that will be used when the popover is displayed onscreen.
119    #[property]
120    pub fn effective_appearance(&self) -> NSAppearance {
121        unsafe { NSAppearance::from_id(msg_send![self.m_self(), effectiveAppearance]) }
122    }
123
124    /// Specifies if the popover is to be animated.
125    #[property]
126    pub fn animates(&self) -> bool {
127        unsafe { to_bool(msg_send![self.m_self(), animates]) }
128    }
129
130    /// The content size of the popover.
131    #[property]
132    pub fn content_size(&self) -> NSSize {
133        unsafe { msg_send![self.m_self(), contentSize] }
134    }
135
136    /// Sets the content size of the popover.
137    ///
138    /// # Arguments
139    ///
140    /// * `size` - The size to use.
141    #[property]
142    pub fn set_content_size(&self, size: NSSize) {
143        unsafe { msg_send![self.m_self(), setContentSize: size] }
144    }
145
146    /// The display state of the popover.
147    #[property]
148    pub fn shown(&self) -> bool {
149        unsafe { to_bool(msg_send![self.m_self(), isShown]) }
150    }
151
152    /// A Boolean value that indicates whether the window created by a popover's detachment is automatically created.
153    #[property]
154    pub fn detached(&self) -> bool {
155        unsafe { to_bool(msg_send![self.m_self(), isDetached]) }
156    }
157
158    /* Closing a Popover
159     */
160
161    /// Attempts to close the popover.
162    #[method]
163    pub fn perform_close(&self, sender: id) {
164        unsafe { msg_send![self.m_self(), performClose: sender] }
165    }
166
167    /// Forces the popover to close without consulting its delegate.
168    #[method]
169    pub fn close(&self) {
170        unsafe { msg_send![self.m_self(), close] }
171    }
172
173    /// The delegate of the popover.
174    #[property]
175    pub fn delegate(&self) -> id {
176        unsafe { msg_send![self.m_self(), delegate] }
177    }
178
179    /// Sets the delegate of the popover.
180    ///
181    /// # Arguments
182    ///
183    /// * `delegate` - The delegate to use.
184    #[method]
185    pub fn set_delegate(&self, delegate: id) {
186        unsafe { msg_send![self.m_self(), setDelegate: delegate] }
187    }
188
189    /* Initializers
190     */
191
192    /// Creates a new popover.
193    #[method]
194    pub fn init(&self) -> NSPopover {
195        unsafe { msg_send![self.m_self(), init] }
196    }
197
198    /// Creates a new popover with `NSCoder`
199    #[method]
200    pub fn init_with_coder(&self, coder: NSCoder) -> NSPopover {
201        unsafe { msg_send![self.m_self(), initWithCoder: coder] }
202    }
203}