Skip to main content

polyhorn_ios_sys/polykit/
scroll_view.rs

1use objc::runtime::*;
2use objc::*;
3
4use super::{PLYByEdge, PLYDimension, PLYLayout, PLYView};
5use crate::coregraphics::CGRect;
6use crate::uikit::UIScrollViewIndicatorStyle;
7use crate::Raw;
8
9/// A view that allows the scrolling and zooming of its contained views.
10pub struct PLYScrollView {
11    pub(super) object: *mut Object,
12}
13
14impl PLYScrollView {
15    /// Initializes a newly allocated scroll view.
16    pub fn new() -> PLYScrollView {
17        unsafe {
18            let mut object: *mut Object = msg_send![class!(PLYScrollView), alloc];
19            object = msg_send![object, init];
20            PLYScrollView { object }
21        }
22    }
23
24    /// Upcasts this scroll view to a UIView.
25    pub fn to_view(&self) -> PLYView {
26        unsafe { PLYView::from_raw_retain(self.object) }
27    }
28
29    /// Sets the layout of the content view.
30    pub fn set_content_layout(&mut self, callback: impl FnMut() -> CGRect + 'static) {
31        let layout = PLYLayout::new(callback);
32
33        unsafe {
34            let _: () = msg_send![self.object, setContentLayout: layout];
35        }
36    }
37
38    /// Sets the style of the scroll indicators.
39    pub fn set_indicator_style(&mut self, style: UIScrollViewIndicatorStyle) {
40        unsafe {
41            let _: () = msg_send![self.object, setIndicatorStyle: style];
42        }
43    }
44
45    /// Sets the custom distance that the content view is inset from the safe
46    /// area or scroll view edges.
47    pub fn set_scroll_padding(&mut self, padding: PLYByEdge<PLYDimension>) {
48        unsafe {
49            let _: () = msg_send![self.object, setScrollPadding: padding];
50        }
51    }
52
53    /// Sets the distance the scroll indicators are inset from the edge of the
54    /// scroll view.
55    pub fn set_scrollbar_padding(&mut self, padding: PLYByEdge<PLYDimension>) {
56        unsafe {
57            let _: () = msg_send![self.object, setScrollbarPadding: padding];
58        }
59    }
60}
61
62impl Raw for PLYScrollView {
63    unsafe fn from_raw(object: *mut Object) -> Self {
64        PLYScrollView { object }
65    }
66
67    unsafe fn as_raw(&self) -> *mut Object {
68        self.object
69    }
70}
71
72impl Clone for PLYScrollView {
73    fn clone(&self) -> Self {
74        unsafe { Self::from_raw_retain(self.as_raw()) }
75    }
76}
77
78impl Drop for PLYScrollView {
79    fn drop(&mut self) {
80        unsafe { objc_release(self.object) }
81    }
82}