animate/legacy/
click_action.rs

1use crate::{Action, Actor, ActorMeta, LongPressState, ModifierType};
2use glib::{
3    object as gobject,
4    object::{Cast, IsA},
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 ClickAction(Object<ffi::ClutterClickAction, ffi::ClutterClickActionClass, ClickActionClass>) @extends Action, ActorMeta, gobject::InitiallyUnowned;
14
15    match fn {
16        get_type => || ffi::clutter_click_action_get_type(),
17    }
18}
19
20impl ClickAction {
21    /// Creates a new `ClickAction` instance
22    ///
23    /// # Returns
24    ///
25    /// the newly created `ClickAction`
26    pub fn new() -> ClickAction {
27        unsafe { Action::from_glib_none(ffi::clutter_click_action_new()).unsafe_cast() }
28    }
29}
30
31impl Default for ClickAction {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37/// Trait containing all `ClickAction` methods.
38///
39/// # Implementors
40///
41/// [`ClickAction`](struct.ClickAction.html)
42pub trait ClickActionExt: 'static {
43    /// Retrieves the button that was pressed.
44    ///
45    /// - 1 - left mouse button in a right-handed configuration, or the right mouse button in a left-handed configuration
46    /// - 2 - scroll wheel button
47    /// - 3 - right mouse button in a right-handed configuration, or the left mouse button in a left-handed configuration
48    ///
49    /// # Returns
50    ///
51    /// the button value
52    fn get_button(&self) -> u32;
53
54    /// Retrieves the screen coordinates of the button press.
55    /// ## `press_x`
56    /// return location for the X coordinate, or `None`
57    /// ## `press_y`
58    /// return location for the Y coordinate, or `None`
59    fn get_coords(&self) -> (f32, f32);
60
61    /// Retrieves the modifier state of the click action.
62    ///
63    /// # Returns
64    ///
65    /// the modifier state parameter, or 0
66    fn get_state(&self) -> ModifierType;
67
68    /// Emulates a release of the pointer button, which ungrabs the pointer
69    /// and unsets the `ClickAction:pressed` state.
70    ///
71    /// This function will also cancel the long press gesture if one was
72    /// initiated.
73    ///
74    /// This function is useful to break a grab, for instance after a certain
75    /// amount of time has passed.
76    fn release(&self);
77
78    /// Whether the clickable actor has the pointer grabbed
79    fn get_property_held(&self) -> bool;
80
81    /// The minimum duration of a press for it to be recognized as a long
82    /// press gesture, in milliseconds.
83    ///
84    /// A value of -1 will make the `ClickAction` use the value of
85    /// the `Settings:long-press-duration` property.
86    fn get_property_long_press_duration(&self) -> i32;
87
88    /// The minimum duration of a press for it to be recognized as a long
89    /// press gesture, in milliseconds.
90    ///
91    /// A value of -1 will make the `ClickAction` use the value of
92    /// the `Settings:long-press-duration` property.
93    fn set_property_long_press_duration(&self, long_press_duration: i32);
94
95    /// The maximum allowed distance that can be covered (on both axes) before
96    /// a long press gesture is cancelled, in pixels.
97    ///
98    /// A value of -1 will make the `ClickAction` use the value of
99    /// the `Settings:dnd-drag-threshold` property.
100    fn get_property_long_press_threshold(&self) -> i32;
101
102    /// The maximum allowed distance that can be covered (on both axes) before
103    /// a long press gesture is cancelled, in pixels.
104    ///
105    /// A value of -1 will make the `ClickAction` use the value of
106    /// the `Settings:dnd-drag-threshold` property.
107    fn set_property_long_press_threshold(&self, long_press_threshold: i32);
108
109    /// Whether the clickable actor should be in "pressed" state
110    fn get_property_pressed(&self) -> bool;
111
112    /// The ::clicked signal is emitted when the `Actor` to which
113    /// a `ClickAction` has been applied should respond to a
114    /// pointer button press and release events
115    /// ## `actor`
116    /// the `Actor` attached to the `action`
117    fn connect_clicked<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;
118
119    /// The ::long-press signal is emitted during the long press gesture
120    /// handling.
121    ///
122    /// This signal can be emitted multiple times with different states.
123    ///
124    /// The `LongPressState::Query` state will be emitted on button presses,
125    /// and its return value will determine whether the long press handling
126    /// should be initiated. If the signal handlers will return `true`, the
127    /// `LongPressState::Query` state will be followed either by a signal
128    /// emission with the `LongPressState::Activate` state if the long press
129    /// constraints were respected, or by a signal emission with the
130    /// `LongPressState::Cancel` state if the long press was cancelled.
131    ///
132    /// It is possible to forcibly cancel a long press detection using
133    /// `ClickActionExt::release`.
134    /// ## `actor`
135    /// the `Actor` attached to the `action`
136    /// ## `state`
137    /// the long press state
138    ///
139    /// # Returns
140    ///
141    /// Only the `LongPressState::Query` state uses the
142    ///  returned value of the handler; other states will ignore it
143    fn connect_long_press<F: Fn(&Self, &Actor, LongPressState) -> bool + 'static>(
144        &self,
145        f: F,
146    ) -> SignalHandlerId;
147
148    fn connect_property_held_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
149
150    fn connect_property_long_press_duration_notify<F: Fn(&Self) + 'static>(
151        &self,
152        f: F,
153    ) -> SignalHandlerId;
154
155    fn connect_property_long_press_threshold_notify<F: Fn(&Self) + 'static>(
156        &self,
157        f: F,
158    ) -> SignalHandlerId;
159
160    fn connect_property_pressed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
161}
162
163impl<O: IsA<ClickAction>> ClickActionExt for O {
164    fn get_button(&self) -> u32 {
165        unsafe { ffi::clutter_click_action_get_button(self.as_ref().to_glib_none().0) }
166    }
167
168    fn get_coords(&self) -> (f32, f32) {
169        unsafe {
170            let mut press_x = mem::MaybeUninit::uninit();
171            let mut press_y = mem::MaybeUninit::uninit();
172            ffi::clutter_click_action_get_coords(
173                self.as_ref().to_glib_none().0,
174                press_x.as_mut_ptr(),
175                press_y.as_mut_ptr(),
176            );
177            let press_x = press_x.assume_init();
178            let press_y = press_y.assume_init();
179            (press_x, press_y)
180        }
181    }
182
183    fn get_state(&self) -> ModifierType {
184        unsafe {
185            from_glib(ffi::clutter_click_action_get_state(
186                self.as_ref().to_glib_none().0,
187            ))
188        }
189    }
190
191    fn release(&self) {
192        unsafe {
193            ffi::clutter_click_action_release(self.as_ref().to_glib_none().0);
194        }
195    }
196
197    fn get_property_held(&self) -> bool {
198        unsafe {
199            let mut value = Value::from_type(<bool as StaticType>::static_type());
200            gobject_sys::g_object_get_property(
201                self.to_glib_none().0 as *mut gobject_sys::GObject,
202                b"held\0".as_ptr() as *const _,
203                value.to_glib_none_mut().0,
204            );
205            value
206                .get()
207                .expect("Return Value for property `held` getter")
208                .unwrap()
209        }
210    }
211
212    fn get_property_long_press_duration(&self) -> i32 {
213        unsafe {
214            let mut value = Value::from_type(<i32 as StaticType>::static_type());
215            gobject_sys::g_object_get_property(
216                self.to_glib_none().0 as *mut gobject_sys::GObject,
217                b"long-press-duration\0".as_ptr() as *const _,
218                value.to_glib_none_mut().0,
219            );
220            value
221                .get()
222                .expect("Return Value for property `long-press-duration` getter")
223                .unwrap()
224        }
225    }
226
227    fn set_property_long_press_duration(&self, long_press_duration: i32) {
228        unsafe {
229            gobject_sys::g_object_set_property(
230                self.to_glib_none().0 as *mut gobject_sys::GObject,
231                b"long-press-duration\0".as_ptr() as *const _,
232                Value::from(&long_press_duration).to_glib_none().0,
233            );
234        }
235    }
236
237    fn get_property_long_press_threshold(&self) -> i32 {
238        unsafe {
239            let mut value = Value::from_type(<i32 as StaticType>::static_type());
240            gobject_sys::g_object_get_property(
241                self.to_glib_none().0 as *mut gobject_sys::GObject,
242                b"long-press-threshold\0".as_ptr() as *const _,
243                value.to_glib_none_mut().0,
244            );
245            value
246                .get()
247                .expect("Return Value for property `long-press-threshold` getter")
248                .unwrap()
249        }
250    }
251
252    fn set_property_long_press_threshold(&self, long_press_threshold: i32) {
253        unsafe {
254            gobject_sys::g_object_set_property(
255                self.to_glib_none().0 as *mut gobject_sys::GObject,
256                b"long-press-threshold\0".as_ptr() as *const _,
257                Value::from(&long_press_threshold).to_glib_none().0,
258            );
259        }
260    }
261
262    fn get_property_pressed(&self) -> bool {
263        unsafe {
264            let mut value = Value::from_type(<bool as StaticType>::static_type());
265            gobject_sys::g_object_get_property(
266                self.to_glib_none().0 as *mut gobject_sys::GObject,
267                b"pressed\0".as_ptr() as *const _,
268                value.to_glib_none_mut().0,
269            );
270            value
271                .get()
272                .expect("Return Value for property `pressed` getter")
273                .unwrap()
274        }
275    }
276
277    fn connect_clicked<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
278        unsafe extern "C" fn clicked_trampoline<P, F: Fn(&P, &Actor) + 'static>(
279            this: *mut ffi::ClutterClickAction,
280            actor: *mut ffi::ClutterActor,
281            f: glib_sys::gpointer,
282        ) where
283            P: IsA<ClickAction>,
284        {
285            let f: &F = &*(f as *const F);
286            f(
287                &ClickAction::from_glib_borrow(this).unsafe_cast_ref(),
288                &from_glib_borrow(actor),
289            )
290        }
291        unsafe {
292            let f: Box_<F> = Box_::new(f);
293            connect_raw(
294                self.as_ptr() as *mut _,
295                b"clicked\0".as_ptr() as *const _,
296                Some(transmute::<_, unsafe extern "C" fn()>(
297                    clicked_trampoline::<Self, F> as *const (),
298                )),
299                Box_::into_raw(f),
300            )
301        }
302    }
303
304    fn connect_long_press<F: Fn(&Self, &Actor, LongPressState) -> bool + 'static>(
305        &self,
306        f: F,
307    ) -> SignalHandlerId {
308        unsafe extern "C" fn long_press_trampoline<
309            P,
310            F: Fn(&P, &Actor, LongPressState) -> bool + 'static,
311        >(
312            this: *mut ffi::ClutterClickAction,
313            actor: *mut ffi::ClutterActor,
314            state: ffi::ClutterLongPressState,
315            f: glib_sys::gpointer,
316        ) -> glib_sys::gboolean
317        where
318            P: IsA<ClickAction>,
319        {
320            let f: &F = &*(f as *const F);
321            f(
322                &ClickAction::from_glib_borrow(this).unsafe_cast_ref(),
323                &from_glib_borrow(actor),
324                from_glib(state),
325            )
326            .to_glib()
327        }
328        unsafe {
329            let f: Box_<F> = Box_::new(f);
330            connect_raw(
331                self.as_ptr() as *mut _,
332                b"long-press\0".as_ptr() as *const _,
333                Some(transmute::<_, unsafe extern "C" fn()>(
334                    long_press_trampoline::<Self, F> as *const (),
335                )),
336                Box_::into_raw(f),
337            )
338        }
339    }
340
341    fn connect_property_held_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
342        unsafe extern "C" fn notify_held_trampoline<P, F: Fn(&P) + 'static>(
343            this: *mut ffi::ClutterClickAction,
344            _param_spec: glib_sys::gpointer,
345            f: glib_sys::gpointer,
346        ) where
347            P: IsA<ClickAction>,
348        {
349            let f: &F = &*(f as *const F);
350            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
351        }
352        unsafe {
353            let f: Box_<F> = Box_::new(f);
354            connect_raw(
355                self.as_ptr() as *mut _,
356                b"notify::held\0".as_ptr() as *const _,
357                Some(transmute::<_, unsafe extern "C" fn()>(
358                    notify_held_trampoline::<Self, F> as *const (),
359                )),
360                Box_::into_raw(f),
361            )
362        }
363    }
364
365    fn connect_property_long_press_duration_notify<F: Fn(&Self) + 'static>(
366        &self,
367        f: F,
368    ) -> SignalHandlerId {
369        unsafe extern "C" fn notify_long_press_duration_trampoline<P, F: Fn(&P) + 'static>(
370            this: *mut ffi::ClutterClickAction,
371            _param_spec: glib_sys::gpointer,
372            f: glib_sys::gpointer,
373        ) where
374            P: IsA<ClickAction>,
375        {
376            let f: &F = &*(f as *const F);
377            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
378        }
379        unsafe {
380            let f: Box_<F> = Box_::new(f);
381            connect_raw(
382                self.as_ptr() as *mut _,
383                b"notify::long-press-duration\0".as_ptr() as *const _,
384                Some(transmute::<_, unsafe extern "C" fn()>(
385                    notify_long_press_duration_trampoline::<Self, F> as *const (),
386                )),
387                Box_::into_raw(f),
388            )
389        }
390    }
391
392    fn connect_property_long_press_threshold_notify<F: Fn(&Self) + 'static>(
393        &self,
394        f: F,
395    ) -> SignalHandlerId {
396        unsafe extern "C" fn notify_long_press_threshold_trampoline<P, F: Fn(&P) + 'static>(
397            this: *mut ffi::ClutterClickAction,
398            _param_spec: glib_sys::gpointer,
399            f: glib_sys::gpointer,
400        ) where
401            P: IsA<ClickAction>,
402        {
403            let f: &F = &*(f as *const F);
404            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
405        }
406        unsafe {
407            let f: Box_<F> = Box_::new(f);
408            connect_raw(
409                self.as_ptr() as *mut _,
410                b"notify::long-press-threshold\0".as_ptr() as *const _,
411                Some(transmute::<_, unsafe extern "C" fn()>(
412                    notify_long_press_threshold_trampoline::<Self, F> as *const (),
413                )),
414                Box_::into_raw(f),
415            )
416        }
417    }
418
419    fn connect_property_pressed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
420        unsafe extern "C" fn notify_pressed_trampoline<P, F: Fn(&P) + 'static>(
421            this: *mut ffi::ClutterClickAction,
422            _param_spec: glib_sys::gpointer,
423            f: glib_sys::gpointer,
424        ) where
425            P: IsA<ClickAction>,
426        {
427            let f: &F = &*(f as *const F);
428            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
429        }
430        unsafe {
431            let f: Box_<F> = Box_::new(f);
432            connect_raw(
433                self.as_ptr() as *mut _,
434                b"notify::pressed\0".as_ptr() as *const _,
435                Some(transmute::<_, unsafe extern "C" fn()>(
436                    notify_pressed_trampoline::<Self, F> as *const (),
437                )),
438                Box_::into_raw(f),
439            )
440        }
441    }
442}
443
444impl fmt::Display for ClickAction {
445    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
446        write!(f, "ClickAction")
447    }
448}