Skip to main content

animate/legacy/
scroll_actor.rs

1// Scriptable
2use crate::{Actor, Animatable, Container, InternalPoint, InternalRect, 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
39/// Trait containing all `ScrollActor` methods.
40///
41/// # Implementors
42///
43/// [`ScrollActor`](struct.ScrollActor.html)
44pub trait ScrollActorExt: 'static {
45    /// Retrieves the `ScrollActor:scroll-mode` property
46    ///
47    /// # Returns
48    ///
49    /// the scrolling mode
50    fn get_scroll_mode(&self) -> ScrollMode;
51
52    /// Scrolls the contents of `self` so that `point` is the new origin
53    /// of the visible area.
54    ///
55    /// The coordinates of `point` must be relative to the `self`.
56    ///
57    /// This function will use the currently set easing state of the `self`
58    /// to transition from the current scroll origin to the new one.
59    /// ## `point`
60    /// a `Point`
61    fn scroll_to_point(&self, point: &InternalPoint);
62
63    /// Scrolls `self` so that `rect` is in the visible portion.
64    /// ## `rect`
65    /// a `Rect`
66    fn scroll_to_rect(&self, rect: &InternalRect);
67
68    /// Sets the `ScrollActor:scroll-mode` property.
69    /// ## `mode`
70    /// a `ScrollMode`
71    fn set_scroll_mode(&self, mode: ScrollMode);
72
73    fn connect_property_scroll_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
74}
75
76impl<O: IsA<ScrollActor>> ScrollActorExt for O {
77    fn get_scroll_mode(&self) -> ScrollMode {
78        unsafe {
79            from_glib(ffi::clutter_scroll_actor_get_scroll_mode(
80                self.as_ref().to_glib_none().0,
81            ))
82        }
83    }
84
85    fn scroll_to_point(&self, point: &InternalPoint) {
86        unsafe {
87            ffi::clutter_scroll_actor_scroll_to_point(
88                self.as_ref().to_glib_none().0,
89                point.to_glib_none().0,
90            );
91        }
92    }
93
94    fn scroll_to_rect(&self, rect: &InternalRect) {
95        unsafe {
96            ffi::clutter_scroll_actor_scroll_to_rect(
97                self.as_ref().to_glib_none().0,
98                rect.to_glib_none().0,
99            );
100        }
101    }
102
103    fn set_scroll_mode(&self, mode: ScrollMode) {
104        unsafe {
105            ffi::clutter_scroll_actor_set_scroll_mode(
106                self.as_ref().to_glib_none().0,
107                mode.to_glib(),
108            );
109        }
110    }
111
112    fn connect_property_scroll_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
113        unsafe extern "C" fn notify_scroll_mode_trampoline<P, F: Fn(&P) + 'static>(
114            this: *mut ffi::ClutterScrollActor,
115            _param_spec: glib_sys::gpointer,
116            f: glib_sys::gpointer,
117        ) where
118            P: IsA<ScrollActor>,
119        {
120            let f: &F = &*(f as *const F);
121            f(&ScrollActor::from_glib_borrow(this).unsafe_cast_ref())
122        }
123        unsafe {
124            let f: Box_<F> = Box_::new(f);
125            connect_raw(
126                self.as_ptr() as *mut _,
127                b"notify::scroll-mode\0".as_ptr() as *const _,
128                Some(transmute::<_, unsafe extern "C" fn()>(
129                    notify_scroll_mode_trampoline::<Self, F> as *const (),
130                )),
131                Box_::into_raw(f),
132            )
133        }
134    }
135}
136
137impl fmt::Display for ScrollActor {
138    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139        write!(f, "ScrollActor")
140    }
141}