1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use crate::{Action, Actor, ActorMeta, LongPressState, ModifierType};
use glib::{
    object as gobject,
    object::{Cast, IsA},
    signal::{connect_raw, SignalHandlerId},
    translate::*,
    StaticType, Value,
};
use std::boxed::Box as Box_;
use std::{fmt, mem, mem::transmute};

glib_wrapper! {
    pub struct ClickAction(Object<ffi::ClutterClickAction, ffi::ClutterClickActionClass, ClickActionClass>) @extends Action, ActorMeta, gobject::InitiallyUnowned;

    match fn {
        get_type => || ffi::clutter_click_action_get_type(),
    }
}

impl ClickAction {
    /// Creates a new `ClickAction` instance
    ///
    /// # Returns
    ///
    /// the newly created `ClickAction`
    pub fn new() -> ClickAction {
        unsafe { Action::from_glib_none(ffi::clutter_click_action_new()).unsafe_cast() }
    }
}

impl Default for ClickAction {
    fn default() -> Self {
        Self::new()
    }
}

pub const NONE_CLICK_ACTION: Option<&ClickAction> = None;

/// Trait containing all `ClickAction` methods.
///
/// # Implementors
///
/// [`ClickAction`](struct.ClickAction.html)
pub trait ClickActionExt: 'static {
    /// Retrieves the button that was pressed.
    ///
    /// - 1 - left mouse button in a right-handed configuration, or the right mouse button in a left-handed configuration
    /// - 2 - scroll wheel button
    /// - 3 - right mouse button in a right-handed configuration, or the left mouse button in a left-handed configuration
    /// 
    /// # Returns
    ///
    /// the button value
    fn get_button(&self) -> u32;

    /// Retrieves the screen coordinates of the button press.
    /// ## `press_x`
    /// return location for the X coordinate, or `None`
    /// ## `press_y`
    /// return location for the Y coordinate, or `None`
    fn get_coords(&self) -> (f32, f32);

    /// Retrieves the modifier state of the click action.
    ///
    /// # Returns
    ///
    /// the modifier state parameter, or 0
    fn get_state(&self) -> ModifierType;

    /// Emulates a release of the pointer button, which ungrabs the pointer
    /// and unsets the `ClickAction:pressed` state.
    ///
    /// This function will also cancel the long press gesture if one was
    /// initiated.
    ///
    /// This function is useful to break a grab, for instance after a certain
    /// amount of time has passed.
    fn release(&self);

    /// Whether the clickable actor has the pointer grabbed
    fn get_property_held(&self) -> bool;

    /// The minimum duration of a press for it to be recognized as a long
    /// press gesture, in milliseconds.
    ///
    /// A value of -1 will make the `ClickAction` use the value of
    /// the `Settings:long-press-duration` property.
    fn get_property_long_press_duration(&self) -> i32;

    /// The minimum duration of a press for it to be recognized as a long
    /// press gesture, in milliseconds.
    ///
    /// A value of -1 will make the `ClickAction` use the value of
    /// the `Settings:long-press-duration` property.
    fn set_property_long_press_duration(&self, long_press_duration: i32);

    /// The maximum allowed distance that can be covered (on both axes) before
    /// a long press gesture is cancelled, in pixels.
    ///
    /// A value of -1 will make the `ClickAction` use the value of
    /// the `Settings:dnd-drag-threshold` property.
    fn get_property_long_press_threshold(&self) -> i32;

    /// The maximum allowed distance that can be covered (on both axes) before
    /// a long press gesture is cancelled, in pixels.
    ///
    /// A value of -1 will make the `ClickAction` use the value of
    /// the `Settings:dnd-drag-threshold` property.
    fn set_property_long_press_threshold(&self, long_press_threshold: i32);

    /// Whether the clickable actor should be in "pressed" state
    fn get_property_pressed(&self) -> bool;

    /// The ::clicked signal is emitted when the `Actor` to which
    /// a `ClickAction` has been applied should respond to a
    /// pointer button press and release events
    /// ## `actor`
    /// the `Actor` attached to the `action`
    fn connect_clicked<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;

    /// The ::long-press signal is emitted during the long press gesture
    /// handling.
    ///
    /// This signal can be emitted multiple times with different states.
    ///
    /// The `LongPressState::Query` state will be emitted on button presses,
    /// and its return value will determine whether the long press handling
    /// should be initiated. If the signal handlers will return `true`, the
    /// `LongPressState::Query` state will be followed either by a signal
    /// emission with the `LongPressState::Activate` state if the long press
    /// constraints were respected, or by a signal emission with the
    /// `LongPressState::Cancel` state if the long press was cancelled.
    ///
    /// It is possible to forcibly cancel a long press detection using
    /// `ClickActionExt::release`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    /// ## `state`
    /// the long press state
    ///
    /// # Returns
    ///
    /// Only the `LongPressState::Query` state uses the
    ///  returned value of the handler; other states will ignore it
    fn connect_long_press<F: Fn(&Self, &Actor, LongPressState) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_held_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_long_press_duration_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_long_press_threshold_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_pressed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<ClickAction>> ClickActionExt for O {
    fn get_button(&self) -> u32 {
        unsafe { ffi::clutter_click_action_get_button(self.as_ref().to_glib_none().0) }
    }

    fn get_coords(&self) -> (f32, f32) {
        unsafe {
            let mut press_x = mem::MaybeUninit::uninit();
            let mut press_y = mem::MaybeUninit::uninit();
            ffi::clutter_click_action_get_coords(
                self.as_ref().to_glib_none().0,
                press_x.as_mut_ptr(),
                press_y.as_mut_ptr(),
            );
            let press_x = press_x.assume_init();
            let press_y = press_y.assume_init();
            (press_x, press_y)
        }
    }

    fn get_state(&self) -> ModifierType {
        unsafe {
            from_glib(ffi::clutter_click_action_get_state(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn release(&self) {
        unsafe {
            ffi::clutter_click_action_release(self.as_ref().to_glib_none().0);
        }
    }

    fn get_property_held(&self) -> bool {
        unsafe {
            let mut value = Value::from_type(<bool as StaticType>::static_type());
            gobject_sys::g_object_get_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"held\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `held` getter")
                .unwrap()
        }
    }

    fn get_property_long_press_duration(&self) -> i32 {
        unsafe {
            let mut value = Value::from_type(<i32 as StaticType>::static_type());
            gobject_sys::g_object_get_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"long-press-duration\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `long-press-duration` getter")
                .unwrap()
        }
    }

    fn set_property_long_press_duration(&self, long_press_duration: i32) {
        unsafe {
            gobject_sys::g_object_set_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"long-press-duration\0".as_ptr() as *const _,
                Value::from(&long_press_duration).to_glib_none().0,
            );
        }
    }

    fn get_property_long_press_threshold(&self) -> i32 {
        unsafe {
            let mut value = Value::from_type(<i32 as StaticType>::static_type());
            gobject_sys::g_object_get_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"long-press-threshold\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `long-press-threshold` getter")
                .unwrap()
        }
    }

    fn set_property_long_press_threshold(&self, long_press_threshold: i32) {
        unsafe {
            gobject_sys::g_object_set_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"long-press-threshold\0".as_ptr() as *const _,
                Value::from(&long_press_threshold).to_glib_none().0,
            );
        }
    }

    fn get_property_pressed(&self) -> bool {
        unsafe {
            let mut value = Value::from_type(<bool as StaticType>::static_type());
            gobject_sys::g_object_get_property(
                self.to_glib_none().0 as *mut gobject_sys::GObject,
                b"pressed\0".as_ptr() as *const _,
                value.to_glib_none_mut().0,
            );
            value
                .get()
                .expect("Return Value for property `pressed` getter")
                .unwrap()
        }
    }

    fn connect_clicked<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn clicked_trampoline<P, F: Fn(&P, &Actor) + 'static>(
            this: *mut ffi::ClutterClickAction,
            actor: *mut ffi::ClutterActor,
            f: glib_sys::gpointer,
        ) where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &ClickAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"clicked\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    clicked_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_long_press<F: Fn(&Self, &Actor, LongPressState) -> bool + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn long_press_trampoline<
            P,
            F: Fn(&P, &Actor, LongPressState) -> bool + 'static,
        >(
            this: *mut ffi::ClutterClickAction,
            actor: *mut ffi::ClutterActor,
            state: ffi::ClutterLongPressState,
            f: glib_sys::gpointer,
        ) -> glib_sys::gboolean
        where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &ClickAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
                from_glib(state),
            )
            .to_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"long-press\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    long_press_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_held_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_held_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterClickAction,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::held\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_held_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_long_press_duration_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_long_press_duration_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterClickAction,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::long-press-duration\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_long_press_duration_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_long_press_threshold_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_long_press_threshold_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterClickAction,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::long-press-threshold\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_long_press_threshold_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_pressed_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_pressed_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ffi::ClutterClickAction,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<ClickAction>,
        {
            let f: &F = &*(f as *const F);
            f(&ClickAction::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::pressed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_pressed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for ClickAction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ClickAction")
    }
}