rust_macios/appkit/
ns_view.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    foundation::{NSArray, NSCoder, NSRect},
5    object,
6    objective_c_runtime::{id, traits::FromId},
7};
8
9use super::{
10    interface_impl, INSResponder, NSLayoutXAxisAnchor, NSLayoutYAxisAnchor, NSMenuItem, NSWindow,
11};
12
13object! {
14    /// The infrastructure for drawing, printing, and handling events in an app.
15    unsafe pub struct NSView;
16}
17
18impl INSResponder for NSView {}
19
20#[interface_impl(NSResponder)]
21impl NSView {
22    /// Initializes and returns a newly allocated NSView object with a specified frame rectangle.
23    #[method]
24    pub fn init_with_frame(frame: NSRect) -> Self
25    where
26        Self: Sized + FromId,
27    {
28        unsafe {
29            let obj: id = msg_send![Self::m_class(), alloc];
30            Self::from_id(msg_send![obj, initWithFrame: frame])
31        }
32    }
33
34    /// Initializes a view using from data in the specified coder object.
35    #[method]
36    pub fn init_with_coder(coder: NSCoder) -> Self
37    where
38        Self: Sized + FromId,
39    {
40        unsafe {
41            let obj: id = msg_send![Self::m_class(), alloc];
42            Self::from_id(msg_send![obj, initWithCoder: coder])
43        }
44    }
45
46    /// Restores the view to an initial state so that it can be reused.
47    #[method]
48    pub fn prepare_for_reuse(&self) {
49        unsafe { msg_send![self.m_self(), prepareForReuse] }
50    }
51
52    /* Configuring the view
53     */
54
55    /// The view that is the parent of the current view.
56    #[property]
57    pub fn superview(&self) -> NSView {
58        unsafe { NSView::from_id(msg_send![self.m_self(), superview]) }
59    }
60
61    /// The array of views embedded in the current view.
62    #[property]
63    pub fn subviews<V>(&self) -> NSArray<V>
64    where
65        V: INSView,
66    {
67        unsafe { NSArray::from_id(msg_send![self.m_self(), subviews]) }
68    }
69
70    /// The view’s window object, if it is installed in a window.
71    #[property]
72    pub fn window(&self) -> NSWindow {
73        unsafe { NSWindow::from_id(msg_send![self.m_self(), window]) }
74    }
75
76    /// The view’s closest opaque ancestor, which might be the view itself.
77    #[property]
78    pub fn opaque_ancestor(&self) -> Self
79    where
80        Self: Sized + FromId,
81    {
82        unsafe { Self::from_id(msg_send![self.m_self(), opaqueAncestor]) }
83    }
84
85    /// Returns a Boolean value that indicates whether the view is a subview of the specified view.
86    #[property]
87    pub fn is_descendant_of(&self, view: NSView) -> bool {
88        unsafe { msg_send![self.m_self(), isDescendantOfView: view] }
89    }
90
91    /// Returns the closest ancestor shared by the view and another specified view.
92    #[property]
93    pub fn ancestor_shared_with_view(&self, view: NSView) -> NSView {
94        unsafe { NSView::from_id(msg_send![self.m_self(), ancestorSharedWithView: view]) }
95    }
96
97    /// The menu item containing the view or any of its superviews in the view hierarchy.
98    #[property]
99    pub fn enclosing_menu_item(&self) -> NSMenuItem {
100        unsafe { NSMenuItem::from_id(msg_send![self.m_self(), enclosingMenuItem]) }
101    }
102
103    /* Adding and Removing Subviews
104     */
105
106    /// Adds a view to the view’s subviews so it’s displayed above its siblings.
107    #[property]
108    pub fn add_subview<V>(&self, view: V)
109    where
110        V: INSView,
111    {
112        unsafe { msg_send![self.m_self(), addSubview: view] }
113    }
114
115    /* Modifying the Bounds Rectangle
116     */
117
118    /// The view’s bounds rectangle, which expresses its location and size in its own coordinate system.
119    #[property]
120    pub fn bounds(&self) -> NSRect {
121        unsafe { msg_send![self.m_self(), bounds] }
122    }
123
124    /// Sets the view’s bounds rectangle, which expresses its location and size in its own coordinate system.
125    ///
126    /// # Arguments
127    ///
128    /// * `bounds` - The new bounds rectangle.
129    #[property]
130    pub fn set_bounds(&self, bounds: NSRect) {
131        unsafe { msg_send![self.m_self(), setBounds: bounds] }
132    }
133
134    /* Opting In to Auto Layout
135     */
136
137    /// Returns a Boolean value indicating whether the view depends on the constraint-based layout system.
138    #[property]
139    pub fn requires_constraint_based_layout() -> bool {
140        unsafe { msg_send![Self::m_class(), requiresConstraintBasedLayout] }
141    }
142
143    /// A Boolean value indicating whether the view’s autoresizing mask is translated into constraints for the constraint-based layout system.
144    #[property]
145    pub fn translates_autoresizing_mask_to_constraints(&self) -> bool {
146        unsafe { msg_send![self.m_self(), translatesAutoresizingMaskIntoConstraints] }
147    }
148
149    /// Sets a Boolean value indicating whether the view’s autoresizing mask is translated into constraints for the constraint-based layout system.
150    ///
151    /// # Arguments
152    ///
153    /// * `flag` - The new value.
154    #[property]
155    pub fn set_translates_autoresizing_mask_to_constraints(&self, flag: bool) {
156        unsafe {
157            msg_send![
158                self.m_self(),
159                setTranslatesAutoresizingMaskIntoConstraints: flag
160            ]
161        }
162    }
163
164    /* Creating Constraints Using Layout Anchors
165     */
166
167    /// A layout anchor representing the bottom edge of the view’s frame.
168    #[property]
169    pub fn bottom_anchor(&self) -> NSLayoutYAxisAnchor {
170        unsafe { NSLayoutYAxisAnchor::from_id(msg_send![self.m_self(), bottomAnchor]) }
171    }
172
173    /// A layout anchor representing the horizontal center of the view’s frame.
174    #[property]
175    pub fn center_x_anchor(&self) -> NSLayoutXAxisAnchor {
176        unsafe { NSLayoutXAxisAnchor::from_id(msg_send![self.m_self(), centerXAnchor]) }
177    }
178
179    /// A layout anchor representing the vertical center of the view’s frame.
180    #[property]
181    pub fn center_y_anchor(&self) -> NSLayoutYAxisAnchor {
182        unsafe { NSLayoutYAxisAnchor::from_id(msg_send![self.m_self(), centerYAnchor]) }
183    }
184}