Skip to main content

polyhorn_ios_sys/polykit/
keyboard_avoiding_view.rs

1use objc::runtime::*;
2use objc::*;
3
4use super::{PLYCallback, PLYView};
5use crate::foundation::NSNumber;
6use crate::Raw;
7
8/// A view that invoke a callback when the keyboard appears, changes its
9/// appearance or disappears.
10pub struct PLYKeyboardAvoidingView {
11    pub(super) object: *mut Object,
12}
13
14impl PLYKeyboardAvoidingView {
15    /// Initializes a newly allocated keyboard avoiding view.
16    pub fn new() -> PLYKeyboardAvoidingView {
17        unsafe {
18            let mut object: *mut Object = msg_send![class!(PLYKeyboardAvoidingView), alloc];
19            object = msg_send![object, init];
20            PLYKeyboardAvoidingView { object }
21        }
22    }
23
24    /// Upcasts this keyboard avoiding view to a UIView.
25    pub fn to_view(&self) -> PLYView {
26        unsafe { PLYView::from_raw_retain(self.object) }
27    }
28
29    /// Sets a callback that is invoked whenever the keyboard appears, changes
30    /// its appearance or disappears.
31    pub fn set_on_keyboard(&mut self, callback: PLYCallback<NSNumber>) {
32        unsafe {
33            let _: () = msg_send![self.object, setOnKeyboard: callback.as_raw()];
34        }
35    }
36}
37
38impl Raw for PLYKeyboardAvoidingView {
39    unsafe fn from_raw(object: *mut Object) -> Self {
40        PLYKeyboardAvoidingView { object }
41    }
42
43    unsafe fn as_raw(&self) -> *mut Object {
44        self.object
45    }
46}
47
48impl Clone for PLYKeyboardAvoidingView {
49    fn clone(&self) -> Self {
50        unsafe { Self::from_raw_retain(self.as_raw()) }
51    }
52}
53
54impl Drop for PLYKeyboardAvoidingView {
55    fn drop(&mut self) {
56        unsafe { objc_release(self.object) }
57    }
58}