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
use crate::{Action, Actor, ActorMeta, GestureAction, PanAxis};
use glib::{
    object as gobject,
    object::{Cast, IsA},
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;
use std::{fmt, mem, mem::transmute};

glib_wrapper! {
    pub struct PanAction(Object<ffi::ClutterPanAction, ffi::ClutterPanActionClass, PanActionClass>) @extends GestureAction, Action, ActorMeta, gobject::InitiallyUnowned;

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

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

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

/// Trait containing all `PanAction` methods.
///
/// # Implementors
///
/// [`PanAction`](struct.PanAction.html)
pub trait PanActionExt: 'static {
    /// Retrieves the initial acceleration factor for interpolated ::pan events.
    ///
    /// # Returns
    ///
    /// The initial acceleration factor for interpolated events.
    fn get_acceleration_factor(&self) -> f64;

    /// Retrieves the delta, in stage space, dependent on the current state
    /// of the `PanAction`, and respecting the constraint specified by the
    /// `PanAction:pan-axis` property.
    /// ## `point`
    /// the touch point index, with 0 being the first touch
    ///  point received by the action
    /// ## `delta_x`
    /// return location for the X delta
    /// ## `delta_y`
    /// return location for the Y delta
    ///
    /// # Returns
    ///
    /// the distance since last motion event
    fn get_constrained_motion_delta(&self, point: u32) -> (f32, f32, f32);

    /// Retrieves the deceleration rate of interpolated ::pan events.
    ///
    /// # Returns
    ///
    /// The deceleration rate of the interpolated events.
    fn get_deceleration(&self) -> f64;

    /// Checks if the action should emit ::pan events even after releasing
    /// the pointer during a panning gesture, to emulate some kind of
    /// kinetic inertia.
    ///
    /// # Returns
    ///
    /// `true` if interpolated events emission is active.
    fn get_interpolate(&self) -> bool;

    /// Retrieves the coordinates, in stage space, of the latest interpolated
    /// event, analogous to `GestureActionExt::get_motion_coords`.
    /// ## `interpolated_x`
    /// return location for the latest
    ///  interpolated event's X coordinate
    /// ## `interpolated_y`
    /// return location for the latest
    ///  interpolated event's Y coordinate
    fn get_interpolated_coords(&self) -> (f32, f32);

    /// Retrieves the delta, in stage space, since the latest interpolated
    /// event, analogous to `GestureActionExt::get_motion_delta`.
    /// ## `delta_x`
    /// return location for the X delta since
    ///  the latest interpolated event
    /// ## `delta_y`
    /// return location for the Y delta since
    ///  the latest interpolated event
    ///
    /// # Returns
    ///
    /// the distance since the latest interpolated event
    fn get_interpolated_delta(&self) -> (f32, f32, f32);

    /// Retrieves the axis constraint set by `PanActionExt::set_pan_axis`
    ///
    /// # Returns
    ///
    /// the axis constraint
    fn get_pan_axis(&self) -> PanAxis;

    /// Factor applied to the momentum velocity at the time of releasing the
    /// pointer when generating interpolated ::pan events.
    /// ## `factor`
    /// The acceleration factor
    fn set_acceleration_factor(&self, factor: f64);

    /// Sets the deceleration rate of the interpolated ::pan events generated
    /// after a pan gesture. This is approximately the value that the momentum
    /// at the time of releasing the pointer is divided by every 60th of a second.
    /// ## `rate`
    /// The deceleration rate
    fn set_deceleration(&self, rate: f64);

    /// Sets whether the action should emit interpolated ::pan events
    /// after the drag has ended, to emulate the gesture kinetic inertia.
    /// ## `should_interpolate`
    /// whether to enable interpolated pan events
    fn set_interpolate(&self, should_interpolate: bool);

    /// Restricts the panning action to a specific axis
    /// ## `axis`
    /// the axis to constraint the panning to
    fn set_pan_axis(&self, axis: PanAxis);

    /// The ::pan signal is emitted to keep track of the motion during
    /// a pan gesture. `is_interpolated` is set to `true` during the
    /// interpolation phase of the pan, after the drag has ended and
    /// the :interpolate property was set to `true`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    /// ## `is_interpolated`
    /// if the event is the result of interpolating
    ///  the motion velocity at the end of the drag
    ///
    /// # Returns
    ///
    /// `true` if the pan should continue, and `false` if
    ///  the pan should be cancelled.
    fn connect_pan<F: Fn(&Self, &Actor, bool) -> bool + 'static>(&self, f: F) -> SignalHandlerId;

    /// The ::pan-stopped signal is emitted at the end of the interpolation
    /// phase of the pan action, only when :interpolate is set to `true`.
    /// ## `actor`
    /// the `Actor` attached to the `action`
    fn connect_pan_stopped<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId;

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

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

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

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

impl<O: IsA<PanAction>> PanActionExt for O {
    fn get_acceleration_factor(&self) -> f64 {
        unsafe { ffi::clutter_pan_action_get_acceleration_factor(self.as_ref().to_glib_none().0) }
    }

    fn get_constrained_motion_delta(&self, point: u32) -> (f32, f32, f32) {
        unsafe {
            let mut delta_x = mem::MaybeUninit::uninit();
            let mut delta_y = mem::MaybeUninit::uninit();
            let ret = ffi::clutter_pan_action_get_constrained_motion_delta(
                self.as_ref().to_glib_none().0,
                point,
                delta_x.as_mut_ptr(),
                delta_y.as_mut_ptr(),
            );
            let delta_x = delta_x.assume_init();
            let delta_y = delta_y.assume_init();
            (ret, delta_x, delta_y)
        }
    }

    fn get_deceleration(&self) -> f64 {
        unsafe { ffi::clutter_pan_action_get_deceleration(self.as_ref().to_glib_none().0) }
    }

    fn get_interpolate(&self) -> bool {
        unsafe {
            from_glib(ffi::clutter_pan_action_get_interpolate(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_interpolated_coords(&self) -> (f32, f32) {
        unsafe {
            let mut interpolated_x = mem::MaybeUninit::uninit();
            let mut interpolated_y = mem::MaybeUninit::uninit();
            ffi::clutter_pan_action_get_interpolated_coords(
                self.as_ref().to_glib_none().0,
                interpolated_x.as_mut_ptr(),
                interpolated_y.as_mut_ptr(),
            );
            let interpolated_x = interpolated_x.assume_init();
            let interpolated_y = interpolated_y.assume_init();
            (interpolated_x, interpolated_y)
        }
    }

    fn get_interpolated_delta(&self) -> (f32, f32, f32) {
        unsafe {
            let mut delta_x = mem::MaybeUninit::uninit();
            let mut delta_y = mem::MaybeUninit::uninit();
            let ret = ffi::clutter_pan_action_get_interpolated_delta(
                self.as_ref().to_glib_none().0,
                delta_x.as_mut_ptr(),
                delta_y.as_mut_ptr(),
            );
            let delta_x = delta_x.assume_init();
            let delta_y = delta_y.assume_init();
            (ret, delta_x, delta_y)
        }
    }

    fn get_pan_axis(&self) -> PanAxis {
        unsafe {
            from_glib(ffi::clutter_pan_action_get_pan_axis(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_acceleration_factor(&self, factor: f64) {
        unsafe {
            ffi::clutter_pan_action_set_acceleration_factor(self.as_ref().to_glib_none().0, factor);
        }
    }

    fn set_deceleration(&self, rate: f64) {
        unsafe {
            ffi::clutter_pan_action_set_deceleration(self.as_ref().to_glib_none().0, rate);
        }
    }

    fn set_interpolate(&self, should_interpolate: bool) {
        unsafe {
            ffi::clutter_pan_action_set_interpolate(
                self.as_ref().to_glib_none().0,
                should_interpolate.to_glib(),
            );
        }
    }

    fn set_pan_axis(&self, axis: PanAxis) {
        unsafe {
            ffi::clutter_pan_action_set_pan_axis(self.as_ref().to_glib_none().0, axis.to_glib());
        }
    }

    fn connect_pan<F: Fn(&Self, &Actor, bool) -> bool + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn pan_trampoline<P, F: Fn(&P, &Actor, bool) -> bool + 'static>(
            this: *mut ffi::ClutterPanAction,
            actor: *mut ffi::ClutterActor,
            is_interpolated: glib_sys::gboolean,
            f: glib_sys::gpointer,
        ) -> glib_sys::gboolean
        where
            P: IsA<PanAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &PanAction::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(actor),
                from_glib(is_interpolated),
            )
            .to_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"pan\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    pan_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_pan_stopped<F: Fn(&Self, &Actor) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn pan_stopped_trampoline<P, F: Fn(&P, &Actor) + 'static>(
            this: *mut ffi::ClutterPanAction,
            actor: *mut ffi::ClutterActor,
            f: glib_sys::gpointer,
        ) where
            P: IsA<PanAction>,
        {
            let f: &F = &*(f as *const F);
            f(
                &PanAction::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"pan-stopped\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    pan_stopped_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

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

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

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

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

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