clutter/auto/
scroll_actor.rs

1// Scriptable
2use crate::{Actor, Animatable, Container, Point, Rect, ScrollMode};
3use glib::{
4    object as gobject,
5    object::{Cast, IsA},
6    signal::{connect_raw, SignalHandlerId},
7    translate::*,
8};
9use std::boxed::Box as Box_;
10use std::{fmt, mem::transmute};
11
12// TODO: implements atk::ImplementorIface, Scriptable
13glib_wrapper! {
14    pub struct ScrollActor(Object<ffi::ClutterScrollActor, ffi::ClutterScrollActorClass, ScrollActorClass>) @extends Actor, gobject::InitiallyUnowned, @implements Animatable, Container;
15
16    match fn {
17        get_type => || ffi::clutter_scroll_actor_get_type(),
18    }
19}
20
21impl ScrollActor {
22    /// Creates a new `ScrollActor`.
23    ///
24    /// # Returns
25    ///
26    /// The newly created `ScrollActor`
27    ///  instance.
28    pub fn new() -> ScrollActor {
29        unsafe { Actor::from_glib_none(ffi::clutter_scroll_actor_new()).unsafe_cast() }
30    }
31}
32
33impl Default for ScrollActor {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39pub const NONE_SCROLL_ACTOR: Option<&ScrollActor> = None;
40
41/// Trait containing all `ScrollActor` methods.
42///
43/// # Implementors
44///
45/// [`ScrollActor`](struct.ScrollActor.html)
46pub trait ScrollActorExt: 'static {
47    /// Retrieves the `ScrollActor:scroll-mode` property
48    ///
49    /// # Returns
50    ///
51    /// the scrolling mode
52    fn get_scroll_mode(&self) -> ScrollMode;
53
54    /// Scrolls the contents of `self` so that `point` is the new origin
55    /// of the visible area.
56    ///
57    /// The coordinates of `point` must be relative to the `self`.
58    ///
59    /// This function will use the currently set easing state of the `self`
60    /// to transition from the current scroll origin to the new one.
61    /// ## `point`
62    /// a `Point`
63    fn scroll_to_point(&self, point: &Point);
64
65    /// Scrolls `self` so that `rect` is in the visible portion.
66    /// ## `rect`
67    /// a `Rect`
68    fn scroll_to_rect(&self, rect: &Rect);
69
70    /// Sets the `ScrollActor:scroll-mode` property.
71    /// ## `mode`
72    /// a `ScrollMode`
73    fn set_scroll_mode(&self, mode: ScrollMode);
74
75    fn connect_property_scroll_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
76}
77
78impl<O: IsA<ScrollActor>> ScrollActorExt for O {
79    fn get_scroll_mode(&self) -> ScrollMode {
80        unsafe {
81            from_glib(ffi::clutter_scroll_actor_get_scroll_mode(
82                self.as_ref().to_glib_none().0,
83            ))
84        }
85    }
86
87    fn scroll_to_point(&self, point: &Point) {
88        unsafe {
89            ffi::clutter_scroll_actor_scroll_to_point(
90                self.as_ref().to_glib_none().0,
91                point.to_glib_none().0,
92            );
93        }
94    }
95
96    fn scroll_to_rect(&self, rect: &Rect) {
97        unsafe {
98            ffi::clutter_scroll_actor_scroll_to_rect(
99                self.as_ref().to_glib_none().0,
100                rect.to_glib_none().0,
101            );
102        }
103    }
104
105    fn set_scroll_mode(&self, mode: ScrollMode) {
106        unsafe {
107            ffi::clutter_scroll_actor_set_scroll_mode(
108                self.as_ref().to_glib_none().0,
109                mode.to_glib(),
110            );
111        }
112    }
113
114    fn connect_property_scroll_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
115        unsafe extern "C" fn notify_scroll_mode_trampoline<P, F: Fn(&P) + 'static>(
116            this: *mut ffi::ClutterScrollActor,
117            _param_spec: glib_sys::gpointer,
118            f: glib_sys::gpointer,
119        ) where
120            P: IsA<ScrollActor>,
121        {
122            let f: &F = &*(f as *const F);
123            f(&ScrollActor::from_glib_borrow(this).unsafe_cast_ref())
124        }
125        unsafe {
126            let f: Box_<F> = Box_::new(f);
127            connect_raw(
128                self.as_ptr() as *mut _,
129                b"notify::scroll-mode\0".as_ptr() as *const _,
130                Some(transmute::<_, unsafe extern "C" fn()>(
131                    notify_scroll_mode_trampoline::<Self, F> as *const (),
132                )),
133                Box_::into_raw(f),
134            )
135        }
136    }
137}
138
139impl fmt::Display for ScrollActor {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        write!(f, "ScrollActor")
142    }
143}