polyhorn_ios_sys/polykit/
view.rs1use objc::runtime::*;
2use objc::*;
3
4use super::{
5 PLYAnimationHandle, PLYCallback, PLYCornerRadii, PLYEdgeInsets, PLYKeyframeAnimation,
6 PLYLayout, PLYLayoutEvent, PLYWindow,
7};
8use crate::coregraphics::{CGFloat, CGRect};
9use crate::foundation::NSString;
10use crate::quartzcore::CATransform3D;
11use crate::uikit::UIColor;
12use crate::Raw;
13
14pub struct PLYView {
16 pub(super) object: *mut Object,
17}
18
19impl PLYView {
20 pub fn new() -> PLYView {
22 unsafe {
23 let mut object: *mut Object = msg_send![class!(PLYView), alloc];
24 object = msg_send![object, init];
25 PLYView { object }
26 }
27 }
28
29 pub fn safe_area_insets(&self) -> PLYEdgeInsets {
31 unsafe { msg_send![self.object, safeAreaInsets] }
32 }
33
34 pub fn set_alpha(&mut self, alpha: CGFloat) {
36 unsafe {
37 let _: () = msg_send![self.object, setAlpha: alpha];
38 }
39 }
40
41 pub fn set_background_color(&mut self, color: UIColor) {
43 unsafe {
44 let _: () = msg_send![self.object, setOpaqueBackgroundColor: color.as_raw() ];
45 }
46 }
47
48 pub fn frame(&self) -> CGRect {
51 unsafe { msg_send![self.object, frame] }
52 }
53
54 pub fn set_frame(&mut self, frame: CGRect) {
57 unsafe {
58 let _: () = msg_send![self.object, setFrame: frame];
59 }
60 }
61
62 pub fn set_corner_radii(&mut self, corner_radii: PLYCornerRadii) {
65 unsafe {
66 let _: () = msg_send![self.object, setCornerRadii: corner_radii];
67 }
68 }
69
70 pub fn window(&self) -> Option<PLYWindow> {
72 unsafe {
73 let object: *mut Object = msg_send![self.object, window];
74
75 match object.is_null() {
76 false => Some(PLYWindow::from_raw_retain(object)),
77 true => None,
78 }
79 }
80 }
81
82 pub fn add_subview(&mut self, subview: &PLYView) {
84 unsafe {
85 let _: () = msg_send![self.object, addSubview: subview.as_raw()];
86 }
87 }
88
89 pub fn remove_from_superview(&mut self) {
92 unsafe {
93 let _: () = msg_send![self.object, removeFromSuperview];
94 }
95 }
96
97 pub fn set_layout(&mut self, callback: impl FnMut() -> CGRect + 'static) {
99 let layout = PLYLayout::new(callback);
100
101 unsafe {
102 let _: () = msg_send![self.object, setLayout: layout.as_raw()];
103 }
104 }
105
106 pub fn set_hidden(&mut self, hidden: bool) {
108 unsafe {
109 let _: () = msg_send![self.object, setHidden: hidden];
110 }
111 }
112
113 pub fn set_transform(&mut self, transform: CATransform3D) {
116 unsafe {
117 let object: *mut Object = msg_send![self.object, layer];
118 let _: () = msg_send![object, setTransform: transform];
119 }
120 }
121
122 pub fn set_needs_layout(&mut self) {
125 unsafe {
126 let _: () = msg_send![self.object, setNeedsLayout];
127 }
128 }
129
130 pub fn set_on_pointer_cancel(&mut self, callback: PLYCallback<NSString>) {
133 unsafe {
134 let _: () = msg_send![self.object, setOnPointerCancel: callback.as_raw()];
135 }
136 }
137
138 pub fn set_on_pointer_down(&mut self, callback: PLYCallback<NSString>) {
141 unsafe {
142 let _: () = msg_send![self.object, setOnPointerDown: callback.as_raw()];
143 }
144 }
145
146 pub fn set_on_pointer_up(&mut self, callback: PLYCallback<NSString>) {
149 unsafe {
150 let _: () = msg_send![self.object, setOnPointerUp: callback.as_raw()];
151 }
152 }
153
154 pub fn set_on_layout(&mut self, callback: PLYCallback<PLYLayoutEvent>) {
156 unsafe {
157 let _: () = msg_send![self.object, setOnLayout: callback.as_raw()];
158 }
159 }
160
161 pub fn add_animation(
163 &mut self,
164 animation: PLYKeyframeAnimation,
165 key_path: &str,
166 ) -> PLYAnimationHandle {
167 let key_path = NSString::from(key_path);
168 unsafe {
169 PLYAnimationHandle::from_raw_retain(
170 msg_send![self.object, addKeyframeAnimation: animation.as_raw()
171 forKeyPath: key_path.as_raw()],
172 )
173 }
174 }
175}
176
177impl Raw for PLYView {
178 unsafe fn from_raw(object: *mut Object) -> Self {
179 PLYView { object }
180 }
181
182 unsafe fn as_raw(&self) -> *mut Object {
183 self.object
184 }
185}
186
187impl Clone for PLYView {
188 fn clone(&self) -> Self {
189 unsafe { Self::from_raw_retain(self.as_raw()) }
190 }
191}
192
193impl Drop for PLYView {
194 fn drop(&mut self) {
195 unsafe { objc_release(self.object) }
196 }
197}