rust_macios/appkit/
ns_view.rs1use 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 unsafe pub struct NSView;
16}
17
18impl INSResponder for NSView {}
19
20#[interface_impl(NSResponder)]
21impl NSView {
22 #[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 #[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 #[method]
48 pub fn prepare_for_reuse(&self) {
49 unsafe { msg_send![self.m_self(), prepareForReuse] }
50 }
51
52 #[property]
57 pub fn superview(&self) -> NSView {
58 unsafe { NSView::from_id(msg_send![self.m_self(), superview]) }
59 }
60
61 #[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 #[property]
72 pub fn window(&self) -> NSWindow {
73 unsafe { NSWindow::from_id(msg_send![self.m_self(), window]) }
74 }
75
76 #[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 #[property]
87 pub fn is_descendant_of(&self, view: NSView) -> bool {
88 unsafe { msg_send![self.m_self(), isDescendantOfView: view] }
89 }
90
91 #[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 #[property]
99 pub fn enclosing_menu_item(&self) -> NSMenuItem {
100 unsafe { NSMenuItem::from_id(msg_send![self.m_self(), enclosingMenuItem]) }
101 }
102
103 #[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 #[property]
120 pub fn bounds(&self) -> NSRect {
121 unsafe { msg_send![self.m_self(), bounds] }
122 }
123
124 #[property]
130 pub fn set_bounds(&self, bounds: NSRect) {
131 unsafe { msg_send![self.m_self(), setBounds: bounds] }
132 }
133
134 #[property]
139 pub fn requires_constraint_based_layout() -> bool {
140 unsafe { msg_send![Self::m_class(), requiresConstraintBasedLayout] }
141 }
142
143 #[property]
145 pub fn translates_autoresizing_mask_to_constraints(&self) -> bool {
146 unsafe { msg_send![self.m_self(), translatesAutoresizingMaskIntoConstraints] }
147 }
148
149 #[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 #[property]
169 pub fn bottom_anchor(&self) -> NSLayoutYAxisAnchor {
170 unsafe { NSLayoutYAxisAnchor::from_id(msg_send![self.m_self(), bottomAnchor]) }
171 }
172
173 #[property]
175 pub fn center_x_anchor(&self) -> NSLayoutXAxisAnchor {
176 unsafe { NSLayoutXAxisAnchor::from_id(msg_send![self.m_self(), centerXAnchor]) }
177 }
178
179 #[property]
181 pub fn center_y_anchor(&self) -> NSLayoutYAxisAnchor {
182 unsafe { NSLayoutYAxisAnchor::from_id(msg_send![self.m_self(), centerYAnchor]) }
183 }
184}