animate/legacy/
clone.rs

1// Scriptable
2use crate::{Actor, Animatable, Container};
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 Clone(Object<ffi::ClutterClone, ffi::ClutterCloneClass, CloneClass>) @extends Actor, gobject::InitiallyUnowned, @implements Animatable, Container;
15
16    match fn {
17        get_type => || ffi::clutter_clone_get_type(),
18    }
19}
20
21impl Clone {
22    /// Creates a new `Actor` which clones `source`/
23    /// ## `source`
24    /// a `Actor`, or `None`
25    ///
26    /// # Returns
27    ///
28    /// the newly created `Clone`
29    pub fn new<P: IsA<Actor>>(source: &P) -> Clone {
30        unsafe {
31            Actor::from_glib_none(ffi::clutter_clone_new(source.as_ref().to_glib_none().0))
32                .unsafe_cast()
33        }
34    }
35}
36
37/// Trait containing all `Clone` methods.
38///
39/// # Implementors
40///
41/// [`Clone`](struct.Clone.html)
42pub trait CloneExt: 'static {
43    /// Retrieves the source `Actor` being cloned by `self`.
44    ///
45    /// # Returns
46    ///
47    /// the actor source for the clone
48    fn get_source(&self) -> Option<Actor>;
49
50    /// Sets `source` as the source actor to be cloned by `self`.
51    /// ## `source`
52    /// a `Actor`, or `None`
53    fn set_source<P: IsA<Actor>>(&self, source: Option<&P>);
54
55    fn connect_property_source_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
56}
57
58impl<O: IsA<Clone>> CloneExt for O {
59    fn get_source(&self) -> Option<Actor> {
60        unsafe {
61            from_glib_none(ffi::clutter_clone_get_source(
62                self.as_ref().to_glib_none().0,
63            ))
64        }
65    }
66
67    fn set_source<P: IsA<Actor>>(&self, source: Option<&P>) {
68        unsafe {
69            ffi::clutter_clone_set_source(
70                self.as_ref().to_glib_none().0,
71                source.map(|p| p.as_ref()).to_glib_none().0,
72            );
73        }
74    }
75
76    fn connect_property_source_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
77        unsafe extern "C" fn notify_source_trampoline<P, F: Fn(&P) + 'static>(
78            this: *mut ffi::ClutterClone,
79            _param_spec: glib_sys::gpointer,
80            f: glib_sys::gpointer,
81        ) where
82            P: IsA<Clone>,
83        {
84            let f: &F = &*(f as *const F);
85            f(&Clone::from_glib_borrow(this).unsafe_cast_ref())
86        }
87        unsafe {
88            let f: Box_<F> = Box_::new(f);
89            connect_raw(
90                self.as_ptr() as *mut _,
91                b"notify::source\0".as_ptr() as *const _,
92                Some(transmute::<_, unsafe extern "C" fn()>(
93                    notify_source_trampoline::<Self, F> as *const (),
94                )),
95                Box_::into_raw(f),
96            )
97        }
98    }
99}
100
101impl fmt::Display for Clone {
102    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103        write!(f, "Clone")
104    }
105}