animate/legacy/
snap_constraint.rs

1use crate::{Actor, ActorMeta, Constraint, SnapEdge};
2use glib::{
3    object as gobject,
4    object::{Cast, IsA, ObjectType as ObjectType_},
5    signal::{connect_raw, SignalHandlerId},
6    translate::*,
7    StaticType, Value,
8};
9use std::boxed::Box as Box_;
10use std::{fmt, mem, mem::transmute};
11
12glib_wrapper! {
13    pub struct SnapConstraint(Object<ffi::ClutterSnapConstraint, ffi::ClutterSnapConstraintClass, SnapConstraintClass>) @extends Constraint, ActorMeta, gobject::InitiallyUnowned;
14
15    match fn {
16        get_type => || ffi::clutter_snap_constraint_get_type(),
17    }
18}
19
20impl SnapConstraint {
21    /// Creates a new `SnapConstraint` that will snap a `Actor`
22    /// to the `edge` of `source`, with the given `offset`.
23    /// ## `source`
24    /// the `Actor` to use as the source of
25    ///  the constraint, or `None`
26    /// ## `from_edge`
27    /// the edge of the actor to use in the constraint
28    /// ## `to_edge`
29    /// the edge of `source` to use in the constraint
30    /// ## `offset`
31    /// the offset to apply to the constraint, in pixels
32    ///
33    /// # Returns
34    ///
35    /// the newly created `SnapConstraint`
36    pub fn new<P: IsA<Actor>>(
37        source: Option<&P>,
38        from_edge: SnapEdge,
39        to_edge: SnapEdge,
40        offset: f32,
41    ) -> SnapConstraint {
42        unsafe {
43            Constraint::from_glib_none(ffi::clutter_snap_constraint_new(
44                source.map(|p| p.as_ref()).to_glib_none().0,
45                from_edge.to_glib(),
46                to_edge.to_glib(),
47                offset,
48            ))
49            .unsafe_cast()
50        }
51    }
52
53    /// Retrieves the edges used by the `self`
54    /// ## `from_edge`
55    /// return location for the actor's edge, or `None`
56    /// ## `to_edge`
57    /// return location for the source's edge, or `None`
58    pub fn get_edges(&self) -> (SnapEdge, SnapEdge) {
59        unsafe {
60            let mut from_edge = mem::MaybeUninit::uninit();
61            let mut to_edge = mem::MaybeUninit::uninit();
62            ffi::clutter_snap_constraint_get_edges(
63                self.to_glib_none().0,
64                from_edge.as_mut_ptr(),
65                to_edge.as_mut_ptr(),
66            );
67            let from_edge = from_edge.assume_init();
68            let to_edge = to_edge.assume_init();
69            (from_glib(from_edge), from_glib(to_edge))
70        }
71    }
72
73    /// Retrieves the offset set using `SnapConstraint::set_offset`
74    ///
75    /// # Returns
76    ///
77    /// the offset, in pixels
78    pub fn get_offset(&self) -> f32 {
79        unsafe { ffi::clutter_snap_constraint_get_offset(self.to_glib_none().0) }
80    }
81
82    /// Retrieves the `Actor` set using `SnapConstraint::set_source`
83    ///
84    /// # Returns
85    ///
86    /// a pointer to the source actor
87    pub fn get_source(&self) -> Option<Actor> {
88        unsafe {
89            from_glib_none(ffi::clutter_snap_constraint_get_source(
90                self.to_glib_none().0,
91            ))
92        }
93    }
94
95    /// Sets the edges to be used by the `self`
96    ///
97    /// The `from_edge` is the edge on the `Actor` to which `self`
98    /// has been added. The `to_edge` is the edge of the `Actor` inside
99    /// the `SnapConstraint:source` property.
100    /// ## `from_edge`
101    /// the edge on the actor
102    /// ## `to_edge`
103    /// the edge on the source
104    pub fn set_edges(&self, from_edge: SnapEdge, to_edge: SnapEdge) {
105        unsafe {
106            ffi::clutter_snap_constraint_set_edges(
107                self.to_glib_none().0,
108                from_edge.to_glib(),
109                to_edge.to_glib(),
110            );
111        }
112    }
113
114    /// Sets the offset to be applied to the constraint
115    /// ## `offset`
116    /// the offset to apply, in pixels
117    pub fn set_offset(&self, offset: f32) {
118        unsafe {
119            ffi::clutter_snap_constraint_set_offset(self.to_glib_none().0, offset);
120        }
121    }
122
123    /// Sets the source `Actor` for the constraint
124    /// ## `source`
125    /// a `Actor`, or `None` to unset the source
126    pub fn set_source<P: IsA<Actor>>(&self, source: Option<&P>) {
127        unsafe {
128            ffi::clutter_snap_constraint_set_source(
129                self.to_glib_none().0,
130                source.map(|p| p.as_ref()).to_glib_none().0,
131            );
132        }
133    }
134
135    /// The edge of the `Actor` that should be snapped
136    pub fn get_property_from_edge(&self) -> SnapEdge {
137        unsafe {
138            let mut value = Value::from_type(<SnapEdge as StaticType>::static_type());
139            gobject_sys::g_object_get_property(
140                self.as_ptr() as *mut gobject_sys::GObject,
141                b"from-edge\0".as_ptr() as *const _,
142                value.to_glib_none_mut().0,
143            );
144            value
145                .get()
146                .expect("Return Value for property `from-edge` getter")
147                .unwrap()
148        }
149    }
150
151    /// The edge of the `Actor` that should be snapped
152    pub fn set_property_from_edge(&self, from_edge: SnapEdge) {
153        unsafe {
154            gobject_sys::g_object_set_property(
155                self.as_ptr() as *mut gobject_sys::GObject,
156                b"from-edge\0".as_ptr() as *const _,
157                Value::from(&from_edge).to_glib_none().0,
158            );
159        }
160    }
161
162    /// The edge of the `SnapConstraint:source` that should be snapped
163    pub fn get_property_to_edge(&self) -> SnapEdge {
164        unsafe {
165            let mut value = Value::from_type(<SnapEdge as StaticType>::static_type());
166            gobject_sys::g_object_get_property(
167                self.as_ptr() as *mut gobject_sys::GObject,
168                b"to-edge\0".as_ptr() as *const _,
169                value.to_glib_none_mut().0,
170            );
171            value
172                .get()
173                .expect("Return Value for property `to-edge` getter")
174                .unwrap()
175        }
176    }
177
178    /// The edge of the `SnapConstraint:source` that should be snapped
179    pub fn set_property_to_edge(&self, to_edge: SnapEdge) {
180        unsafe {
181            gobject_sys::g_object_set_property(
182                self.as_ptr() as *mut gobject_sys::GObject,
183                b"to-edge\0".as_ptr() as *const _,
184                Value::from(&to_edge).to_glib_none().0,
185            );
186        }
187    }
188
189    pub fn connect_property_from_edge_notify<F: Fn(&SnapConstraint) + 'static>(
190        &self,
191        f: F,
192    ) -> SignalHandlerId {
193        unsafe extern "C" fn notify_from_edge_trampoline<F: Fn(&SnapConstraint) + 'static>(
194            this: *mut ffi::ClutterSnapConstraint,
195            _param_spec: glib_sys::gpointer,
196            f: glib_sys::gpointer,
197        ) {
198            let f: &F = &*(f as *const F);
199            f(&from_glib_borrow(this))
200        }
201        unsafe {
202            let f: Box_<F> = Box_::new(f);
203            connect_raw(
204                self.as_ptr() as *mut _,
205                b"notify::from-edge\0".as_ptr() as *const _,
206                Some(transmute::<_, unsafe extern "C" fn()>(
207                    notify_from_edge_trampoline::<F> as *const (),
208                )),
209                Box_::into_raw(f),
210            )
211        }
212    }
213
214    pub fn connect_property_offset_notify<F: Fn(&SnapConstraint) + 'static>(
215        &self,
216        f: F,
217    ) -> SignalHandlerId {
218        unsafe extern "C" fn notify_offset_trampoline<F: Fn(&SnapConstraint) + 'static>(
219            this: *mut ffi::ClutterSnapConstraint,
220            _param_spec: glib_sys::gpointer,
221            f: glib_sys::gpointer,
222        ) {
223            let f: &F = &*(f as *const F);
224            f(&from_glib_borrow(this))
225        }
226        unsafe {
227            let f: Box_<F> = Box_::new(f);
228            connect_raw(
229                self.as_ptr() as *mut _,
230                b"notify::offset\0".as_ptr() as *const _,
231                Some(transmute::<_, unsafe extern "C" fn()>(
232                    notify_offset_trampoline::<F> as *const (),
233                )),
234                Box_::into_raw(f),
235            )
236        }
237    }
238
239    pub fn connect_property_source_notify<F: Fn(&SnapConstraint) + 'static>(
240        &self,
241        f: F,
242    ) -> SignalHandlerId {
243        unsafe extern "C" fn notify_source_trampoline<F: Fn(&SnapConstraint) + 'static>(
244            this: *mut ffi::ClutterSnapConstraint,
245            _param_spec: glib_sys::gpointer,
246            f: glib_sys::gpointer,
247        ) {
248            let f: &F = &*(f as *const F);
249            f(&from_glib_borrow(this))
250        }
251        unsafe {
252            let f: Box_<F> = Box_::new(f);
253            connect_raw(
254                self.as_ptr() as *mut _,
255                b"notify::source\0".as_ptr() as *const _,
256                Some(transmute::<_, unsafe extern "C" fn()>(
257                    notify_source_trampoline::<F> as *const (),
258                )),
259                Box_::into_raw(f),
260            )
261        }
262    }
263
264    pub fn connect_property_to_edge_notify<F: Fn(&SnapConstraint) + 'static>(
265        &self,
266        f: F,
267    ) -> SignalHandlerId {
268        unsafe extern "C" fn notify_to_edge_trampoline<F: Fn(&SnapConstraint) + 'static>(
269            this: *mut ffi::ClutterSnapConstraint,
270            _param_spec: glib_sys::gpointer,
271            f: glib_sys::gpointer,
272        ) {
273            let f: &F = &*(f as *const F);
274            f(&from_glib_borrow(this))
275        }
276        unsafe {
277            let f: Box_<F> = Box_::new(f);
278            connect_raw(
279                self.as_ptr() as *mut _,
280                b"notify::to-edge\0".as_ptr() as *const _,
281                Some(transmute::<_, unsafe extern "C" fn()>(
282                    notify_to_edge_trampoline::<F> as *const (),
283                )),
284                Box_::into_raw(f),
285            )
286        }
287    }
288}
289
290impl fmt::Display for SnapConstraint {
291    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
292        write!(f, "SnapConstraint")
293    }
294}