Skip to main content

polyhorn_ios_sys/polykit/
view.rs

1use 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
14/// An object that manages the content for a rectangular area on the screen.
15pub struct PLYView {
16    pub(super) object: *mut Object,
17}
18
19impl PLYView {
20    /// Initializes a newly allocated view.
21    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    /// The insets that you use to determine the safe area for this view.
30    pub fn safe_area_insets(&self) -> PLYEdgeInsets {
31        unsafe { msg_send![self.object, safeAreaInsets] }
32    }
33
34    /// Sets the view's alpha value.
35    pub fn set_alpha(&mut self, alpha: CGFloat) {
36        unsafe {
37            let _: () = msg_send![self.object, setAlpha: alpha];
38        }
39    }
40
41    /// Sets the view's background color.
42    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    /// The frame rectangle, which describes the view's location and size in its
49    /// superview's coordinate system.
50    pub fn frame(&self) -> CGRect {
51        unsafe { msg_send![self.object, frame] }
52    }
53
54    /// Sets the frame rectangle, which describes the view's location and size
55    /// in its superview's coordinate system.
56    pub fn set_frame(&mut self, frame: CGRect) {
57        unsafe {
58            let _: () = msg_send![self.object, setFrame: frame];
59        }
60    }
61
62    /// The radius to use when drawing rounded corners for the layer's
63    /// background.
64    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    /// The receiver's window object, or `None` if it has none.
71    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    /// Adds a view to the end of the receiver's list of subviews.
83    pub fn add_subview(&mut self, subview: &PLYView) {
84        unsafe {
85            let _: () = msg_send![self.object, addSubview: subview.as_raw()];
86        }
87    }
88
89    /// Unlinks the view from its superview and its window, and removes it from
90    /// the responder chain.
91    pub fn remove_from_superview(&mut self) {
92        unsafe {
93            let _: () = msg_send![self.object, removeFromSuperview];
94        }
95    }
96
97    /// Sets the view's layout.
98    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    /// A Boolean value that determines whether the view is hidden.
107    pub fn set_hidden(&mut self, hidden: bool) {
108        unsafe {
109            let _: () = msg_send![self.object, setHidden: hidden];
110        }
111    }
112
113    /// Specifies the transform applied to the view, relative to the center of
114    /// its bounds.
115    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    /// Invalidates the current layout of the receiver and triggers a layout
123    /// update during the next update cycle.
124    pub fn set_needs_layout(&mut self) {
125        unsafe {
126            let _: () = msg_send![self.object, setNeedsLayout];
127        }
128    }
129
130    /// Sets a callback that is invoked when the user's interaction with this
131    /// view is cancelled.
132    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    /// Sets a callback that is invoked when the user starts interacting with
139    /// this view.
140    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    /// Sets a callback that is invoked when the user ends interacting with this
147    /// view.
148    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    /// Sets a callback that is invoked when the view is layed out.
155    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    /// Adds the specified animation object to the view's render tree.
162    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}