Skip to main content

polyhorn_ios_sys/polykit/
view_controller.rs

1use objc::runtime::*;
2use objc::*;
3
4use super::{PLYCallback, PLYView};
5use crate::foundation::NSNumber;
6use crate::Raw;
7
8/// An object that manages a view hierarchy of your UIKit app.
9pub struct PLYViewController {
10    object: *mut Object,
11    view: PLYView,
12}
13
14impl PLYViewController {
15    /// Initializes a newly allocated view controller.
16    pub fn new() -> PLYViewController {
17        unsafe {
18            let mut object: *mut Object = msg_send![class!(PLYViewController), alloc];
19            object = msg_send![object, init];
20            PLYViewController::from_raw(object)
21        }
22    }
23
24    /// The view that the controller manages.
25    pub fn view_mut(&mut self) -> &mut PLYView {
26        &mut self.view
27    }
28
29    /// Presents a view controller modally.
30    pub fn present_view_controller(
31        &mut self,
32        view_controller: &PLYViewController,
33        animated: bool,
34        _completion: Option<()>,
35    ) {
36        unsafe {
37            let _: () = msg_send![self.object, presentViewController: view_controller.as_raw()
38                                                            animated: animated
39                                                          completion: std::ptr::null_mut::<Object>()];
40        }
41    }
42
43    /// Dismisses the view controller that was presented modally by the view
44    /// controller.
45    pub fn dismiss_view_controller(&mut self, animated: bool, _completion: Option<()>) {
46        unsafe {
47            let _: () = msg_send![self.object, dismissViewControllerAnimated: animated
48                                                                  completion: std::ptr::null_mut::<Object>()];
49        }
50    }
51
52    /// Sets a callback that will be invoked when this view controller
53    /// disappears.
54    pub fn set_on_did_disappear(&mut self, callback: &PLYCallback<NSNumber>) {
55        unsafe {
56            let _: () = msg_send![self.object, setOnDidDisappear: callback.as_raw()];
57        }
58    }
59}
60
61impl Raw for PLYViewController {
62    unsafe fn from_raw(object: *mut Object) -> Self {
63        PLYViewController {
64            object,
65            view: PLYView::from_raw_retain(msg_send![object, view]),
66        }
67    }
68
69    unsafe fn as_raw(&self) -> *mut Object {
70        self.object
71    }
72}
73
74impl Clone for PLYViewController {
75    fn clone(&self) -> Self {
76        unsafe { PLYViewController::from_raw_retain(self.as_raw()) }
77    }
78}
79
80impl Drop for PLYViewController {
81    fn drop(&mut self) {
82        unsafe { objc_release(self.object) }
83    }
84}