animate/legacy/
align_constraint.rs

1use crate::{Actor, ActorMeta, AlignAxis, 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 AlignConstraint(Object<ffi::ClutterAlignConstraint, ffi::ClutterAlignConstraintClass, AlignConstraintClass>) @extends Constraint, ActorMeta, gobject::InitiallyUnowned;
13
14    match fn {
15        get_type => || ffi::clutter_align_constraint_get_type(),
16    }
17}
18
19impl AlignConstraint {
20    /// Creates a new constraint, aligning a `Actor`'s position with
21    /// regards of the size of the actor to `source`, with the given
22    /// alignment `factor`
23    /// ## `source`
24    /// the `Actor` to use as the source of the
25    ///  alignment, or `None`
26    /// ## `axis`
27    /// the axis to be used to compute the alignment
28    /// ## `factor`
29    /// the alignment factor, between 0.0 and 1.0
30    ///
31    /// # Returns
32    ///
33    /// the newly created `AlignConstraint`
34    pub fn new<P: IsA<Actor>>(source: Option<&P>, axis: AlignAxis, factor: f32) -> AlignConstraint {
35        unsafe {
36            Constraint::from_glib_none(ffi::clutter_align_constraint_new(
37                source.map(|p| p.as_ref()).to_glib_none().0,
38                axis.to_glib(),
39                factor,
40            ))
41            .unsafe_cast()
42        }
43    }
44
45    /// Retrieves the value set using `AlignConstraint::set_align_axis`
46    ///
47    /// # Returns
48    ///
49    /// the alignment axis
50    pub fn get_align_axis(&self) -> AlignAxis {
51        unsafe {
52            from_glib(ffi::clutter_align_constraint_get_align_axis(
53                self.to_glib_none().0,
54            ))
55        }
56    }
57
58    /// Retrieves the factor set using `AlignConstraint::set_factor`
59    ///
60    /// # Returns
61    ///
62    /// the alignment factor
63    pub fn get_factor(&self) -> f32 {
64        unsafe { ffi::clutter_align_constraint_get_factor(self.to_glib_none().0) }
65    }
66
67    /// Retrieves the source of the alignment
68    ///
69    /// # Returns
70    ///
71    /// the `Actor` used as the source
72    ///  of the alignment
73    pub fn get_source(&self) -> Option<Actor> {
74        unsafe {
75            from_glib_none(ffi::clutter_align_constraint_get_source(
76                self.to_glib_none().0,
77            ))
78        }
79    }
80
81    /// Sets the axis to which the alignment refers to
82    /// ## `axis`
83    /// the axis to which the alignment refers to
84    pub fn set_align_axis(&self, axis: AlignAxis) {
85        unsafe {
86            ffi::clutter_align_constraint_set_align_axis(self.to_glib_none().0, axis.to_glib());
87        }
88    }
89
90    /// Sets the alignment factor of the constraint
91    ///
92    /// The factor depends on the `AlignConstraint:align-axis` property
93    /// and it is a value between 0.0 (meaning left, when
94    /// `AlignConstraint:align-axis` is set to `AlignAxis::XAxis`; or
95    /// meaning top, when `AlignConstraint:align-axis` is set to
96    /// `AlignAxis::YAxis`) and 1.0 (meaning right, when
97    /// `AlignConstraint:align-axis` is set to `AlignAxis::XAxis`; or
98    /// meaning bottom, when `AlignConstraint:align-axis` is set to
99    /// `AlignAxis::YAxis`). A value of 0.5 aligns in the middle in either
100    /// cases
101    /// ## `factor`
102    /// the alignment factor, between 0.0 and 1.0
103    pub fn set_factor(&self, factor: f32) {
104        unsafe {
105            ffi::clutter_align_constraint_set_factor(self.to_glib_none().0, factor);
106        }
107    }
108
109    /// Sets the source of the alignment constraint
110    /// ## `source`
111    /// a `Actor`, or `None` to unset the source
112    pub fn set_source<P: IsA<Actor>>(&self, source: Option<&P>) {
113        unsafe {
114            ffi::clutter_align_constraint_set_source(
115                self.to_glib_none().0,
116                source.map(|p| p.as_ref()).to_glib_none().0,
117            );
118        }
119    }
120
121    pub fn connect_property_align_axis_notify<F: Fn(&AlignConstraint) + 'static>(
122        &self,
123        f: F,
124    ) -> SignalHandlerId {
125        unsafe extern "C" fn notify_align_axis_trampoline<F: Fn(&AlignConstraint) + 'static>(
126            this: *mut ffi::ClutterAlignConstraint,
127            _param_spec: glib_sys::gpointer,
128            f: glib_sys::gpointer,
129        ) {
130            let f: &F = &*(f as *const F);
131            f(&from_glib_borrow(this))
132        }
133        unsafe {
134            let f: Box_<F> = Box_::new(f);
135            connect_raw(
136                self.as_ptr() as *mut _,
137                b"notify::align-axis\0".as_ptr() as *const _,
138                Some(transmute::<_, unsafe extern "C" fn()>(
139                    notify_align_axis_trampoline::<F> as *const (),
140                )),
141                Box_::into_raw(f),
142            )
143        }
144    }
145
146    pub fn connect_property_factor_notify<F: Fn(&AlignConstraint) + 'static>(
147        &self,
148        f: F,
149    ) -> SignalHandlerId {
150        unsafe extern "C" fn notify_factor_trampoline<F: Fn(&AlignConstraint) + 'static>(
151            this: *mut ffi::ClutterAlignConstraint,
152            _param_spec: glib_sys::gpointer,
153            f: glib_sys::gpointer,
154        ) {
155            let f: &F = &*(f as *const F);
156            f(&from_glib_borrow(this))
157        }
158        unsafe {
159            let f: Box_<F> = Box_::new(f);
160            connect_raw(
161                self.as_ptr() as *mut _,
162                b"notify::factor\0".as_ptr() as *const _,
163                Some(transmute::<_, unsafe extern "C" fn()>(
164                    notify_factor_trampoline::<F> as *const (),
165                )),
166                Box_::into_raw(f),
167            )
168        }
169    }
170
171    pub fn connect_property_source_notify<F: Fn(&AlignConstraint) + 'static>(
172        &self,
173        f: F,
174    ) -> SignalHandlerId {
175        unsafe extern "C" fn notify_source_trampoline<F: Fn(&AlignConstraint) + 'static>(
176            this: *mut ffi::ClutterAlignConstraint,
177            _param_spec: glib_sys::gpointer,
178            f: glib_sys::gpointer,
179        ) {
180            let f: &F = &*(f as *const F);
181            f(&from_glib_borrow(this))
182        }
183        unsafe {
184            let f: Box_<F> = Box_::new(f);
185            connect_raw(
186                self.as_ptr() as *mut _,
187                b"notify::source\0".as_ptr() as *const _,
188                Some(transmute::<_, unsafe extern "C" fn()>(
189                    notify_source_trampoline::<F> as *const (),
190                )),
191                Box_::into_raw(f),
192            )
193        }
194    }
195}
196
197impl fmt::Display for AlignConstraint {
198    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199        write!(f, "AlignConstraint")
200    }
201}