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