clutter/auto/
bind_constraint.rs

1use crate::{Actor, ActorMeta, BindCoordinate, Constraint};
2use glib::{
3    object as gobject,
4    object::{Cast, IsA, ObjectType as ObjectType_},
5    signal::{connect_raw, SignalHandlerId},
6    translate::*,
7};
8use std::boxed::Box as Box_;
9use std::{fmt, mem::transmute};
10
11glib_wrapper! {
12    pub struct BindConstraint(Object<ffi::ClutterBindConstraint, ffi::ClutterBindConstraintClass, BindConstraintClass>) @extends Constraint, ActorMeta, gobject::InitiallyUnowned;
13
14    match fn {
15        get_type => || ffi::clutter_bind_constraint_get_type(),
16    }
17}
18
19impl BindConstraint {
20    /// Creates a new constraint, binding a `Actor`'s position to
21    /// the given `coordinate` of the position of `source`
22    /// ## `source`
23    /// the `Actor` to use as the source of
24    ///  the binding, or `None`
25    /// ## `coordinate`
26    /// the coordinate to bind
27    /// ## `offset`
28    /// the offset to apply to the binding, in pixels
29    ///
30    /// # Returns
31    ///
32    /// the newly created `BindConstraint`
33    pub fn new<P: IsA<Actor>>(
34        source: Option<&P>,
35        coordinate: BindCoordinate,
36        offset: f32,
37    ) -> BindConstraint {
38        unsafe {
39            Constraint::from_glib_none(ffi::clutter_bind_constraint_new(
40                source.map(|p| p.as_ref()).to_glib_none().0,
41                coordinate.to_glib(),
42                offset,
43            ))
44            .unsafe_cast()
45        }
46    }
47
48    /// Retrieves the bound coordinate of the constraint
49    ///
50    /// # Returns
51    ///
52    /// the bound coordinate
53    pub fn get_coordinate(&self) -> BindCoordinate {
54        unsafe {
55            from_glib(ffi::clutter_bind_constraint_get_coordinate(
56                self.to_glib_none().0,
57            ))
58        }
59    }
60
61    /// Retrieves the offset set using `BindConstraint::set_offset`
62    ///
63    /// # Returns
64    ///
65    /// the offset, in pixels
66    pub fn get_offset(&self) -> f32 {
67        unsafe { ffi::clutter_bind_constraint_get_offset(self.to_glib_none().0) }
68    }
69
70    /// Retrieves the `Actor` set using `BindConstraint::set_source`
71    ///
72    /// # Returns
73    ///
74    /// a pointer to the source actor
75    pub fn get_source(&self) -> Option<Actor> {
76        unsafe {
77            from_glib_none(ffi::clutter_bind_constraint_get_source(
78                self.to_glib_none().0,
79            ))
80        }
81    }
82
83    /// Sets the coordinate to bind in the constraint
84    /// ## `coordinate`
85    /// the coordinate to bind
86    pub fn set_coordinate(&self, coordinate: BindCoordinate) {
87        unsafe {
88            ffi::clutter_bind_constraint_set_coordinate(
89                self.to_glib_none().0,
90                coordinate.to_glib(),
91            );
92        }
93    }
94
95    /// Sets the offset to be applied to the constraint
96    /// ## `offset`
97    /// the offset to apply, in pixels
98    pub fn set_offset(&self, offset: f32) {
99        unsafe {
100            ffi::clutter_bind_constraint_set_offset(self.to_glib_none().0, offset);
101        }
102    }
103
104    /// Sets the source `Actor` for the constraint
105    /// ## `source`
106    /// a `Actor`, or `None` to unset the source
107    pub fn set_source<P: IsA<Actor>>(&self, source: Option<&P>) {
108        unsafe {
109            ffi::clutter_bind_constraint_set_source(
110                self.to_glib_none().0,
111                source.map(|p| p.as_ref()).to_glib_none().0,
112            );
113        }
114    }
115
116    pub fn connect_property_coordinate_notify<F: Fn(&BindConstraint) + 'static>(
117        &self,
118        f: F,
119    ) -> SignalHandlerId {
120        unsafe extern "C" fn notify_coordinate_trampoline<F: Fn(&BindConstraint) + 'static>(
121            this: *mut ffi::ClutterBindConstraint,
122            _param_spec: glib_sys::gpointer,
123            f: glib_sys::gpointer,
124        ) {
125            let f: &F = &*(f as *const F);
126            f(&from_glib_borrow(this))
127        }
128        unsafe {
129            let f: Box_<F> = Box_::new(f);
130            connect_raw(
131                self.as_ptr() as *mut _,
132                b"notify::coordinate\0".as_ptr() as *const _,
133                Some(transmute::<_, unsafe extern "C" fn()>(
134                    notify_coordinate_trampoline::<F> as *const (),
135                )),
136                Box_::into_raw(f),
137            )
138        }
139    }
140
141    pub fn connect_property_offset_notify<F: Fn(&BindConstraint) + 'static>(
142        &self,
143        f: F,
144    ) -> SignalHandlerId {
145        unsafe extern "C" fn notify_offset_trampoline<F: Fn(&BindConstraint) + 'static>(
146            this: *mut ffi::ClutterBindConstraint,
147            _param_spec: glib_sys::gpointer,
148            f: glib_sys::gpointer,
149        ) {
150            let f: &F = &*(f as *const F);
151            f(&from_glib_borrow(this))
152        }
153        unsafe {
154            let f: Box_<F> = Box_::new(f);
155            connect_raw(
156                self.as_ptr() as *mut _,
157                b"notify::offset\0".as_ptr() as *const _,
158                Some(transmute::<_, unsafe extern "C" fn()>(
159                    notify_offset_trampoline::<F> as *const (),
160                )),
161                Box_::into_raw(f),
162            )
163        }
164    }
165
166    pub fn connect_property_source_notify<F: Fn(&BindConstraint) + 'static>(
167        &self,
168        f: F,
169    ) -> SignalHandlerId {
170        unsafe extern "C" fn notify_source_trampoline<F: Fn(&BindConstraint) + 'static>(
171            this: *mut ffi::ClutterBindConstraint,
172            _param_spec: glib_sys::gpointer,
173            f: glib_sys::gpointer,
174        ) {
175            let f: &F = &*(f as *const F);
176            f(&from_glib_borrow(this))
177        }
178        unsafe {
179            let f: Box_<F> = Box_::new(f);
180            connect_raw(
181                self.as_ptr() as *mut _,
182                b"notify::source\0".as_ptr() as *const _,
183                Some(transmute::<_, unsafe extern "C" fn()>(
184                    notify_source_trampoline::<F> as *const (),
185                )),
186                Box_::into_raw(f),
187            )
188        }
189    }
190}
191
192impl fmt::Display for BindConstraint {
193    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
194        write!(f, "BindConstraint")
195    }
196}