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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::NavigationDirection;
use crate::SwipeTracker;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem;
use std::mem::transmute;

glib::wrapper! {
    pub struct Swipeable(Interface<ffi::HdySwipeable>) @requires gtk::Widget, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;

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

pub const NONE_SWIPEABLE: Option<&Swipeable> = None;

/// Trait containing all `Swipeable` methods.
///
/// # Implementors
///
/// [`Carousel`](struct.Carousel.html), [`Flap`](struct.Flap.html), [`Leaflet`](struct.Leaflet.html), [`Swipeable`](struct.Swipeable.html)
pub trait SwipeableExt: 'static {
    /// Emits HdySwipeable::child-switched signal. This should be called when the
    /// widget switches visible child widget.
    ///
    /// `duration` can be 0 if the child is switched without animation.
    /// ## `index`
    /// the index of the child to switch to
    /// ## `duration`
    /// Animation duration in milliseconds
    #[doc(alias = "hdy_swipeable_emit_child_switched")]
    fn emit_child_switched(&self, index: u32, duration: i64);

    /// Gets the progress `self` will snap back to after the gesture is canceled.
    ///
    /// # Returns
    ///
    /// the cancel progress, unitless
    #[doc(alias = "hdy_swipeable_get_cancel_progress")]
    fn get_cancel_progress(&self) -> f64;

    /// Gets the swipe distance of `self`. This corresponds to how many pixels
    /// 1 unit represents.
    ///
    /// # Returns
    ///
    /// the swipe distance in pixels
    #[doc(alias = "hdy_swipeable_get_distance")]
    fn get_distance(&self) -> f64;

    /// Gets the current progress of `self`
    ///
    /// # Returns
    ///
    /// the current progress, unitless
    #[doc(alias = "hdy_swipeable_get_progress")]
    fn get_progress(&self) -> f64;

    /// Gets the snap points of `self`. Each snap point represents a progress value
    /// that is considered acceptable to end the swipe on.
    /// ## `n_snap_points`
    /// location to return the number of the snap points
    ///
    /// # Returns
    ///
    /// the snap points of
    ///  `self`. The array must be freed with `g_free`.
    #[doc(alias = "hdy_swipeable_get_snap_points")]
    fn get_snap_points(&self) -> Vec<f64>;

    /// Gets the area `self` can start a swipe from for the given direction and
    /// gesture type.
    /// This can be used to restrict swipes to only be possible from a certain area,
    /// for example, to only allow edge swipes, or to have a draggable element and
    /// ignore swipes elsewhere.
    ///
    /// Swipe area is only considered for direct swipes (as in, not initiated by
    /// `SwipeGroup`).
    ///
    /// If not implemented, the default implementation returns the allocation of
    /// `self`, allowing swipes from anywhere.
    /// ## `navigation_direction`
    /// the direction of the swipe
    /// ## `is_drag`
    /// whether the swipe is caused by a dragging gesture
    /// ## `rect`
    /// a pointer to a `gdk::Rectangle` to store the swipe area
    #[doc(alias = "hdy_swipeable_get_swipe_area")]
    fn get_swipe_area(
        &self,
        navigation_direction: NavigationDirection,
        is_drag: bool,
    ) -> gdk::Rectangle;

    /// Gets the `SwipeTracker` used by this swipeable widget.
    ///
    /// # Returns
    ///
    /// the swipe tracker
    #[doc(alias = "hdy_swipeable_get_swipe_tracker")]
    fn get_swipe_tracker(&self) -> Option<SwipeTracker>;

    /// See HdySwipeable::child-switched.
    /// ## `index`
    /// the index of the child to switch to
    /// ## `duration`
    /// Animation duration in milliseconds
    #[doc(alias = "hdy_swipeable_switch_child")]
    fn switch_child(&self, index: u32, duration: i64);

    /// This signal should be emitted when the widget's visible child is changed.
    ///
    /// `duration` can be 0 if the child is switched without animation.
    ///
    /// This is used by `SwipeGroup`, applications should not connect to it.
    /// ## `index`
    /// the index of the child to switch to
    /// ## `duration`
    /// Animation duration in milliseconds
    fn connect_child_switched<F: Fn(&Self, u32, i64) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Swipeable>> SwipeableExt for O {
    fn emit_child_switched(&self, index: u32, duration: i64) {
        unsafe {
            ffi::hdy_swipeable_emit_child_switched(self.as_ref().to_glib_none().0, index, duration);
        }
    }

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

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

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

    fn get_snap_points(&self) -> Vec<f64> {
        unsafe {
            let mut n_snap_points = mem::MaybeUninit::uninit();
            let ret = FromGlibContainer::from_glib_full_num(
                ffi::hdy_swipeable_get_snap_points(
                    self.as_ref().to_glib_none().0,
                    n_snap_points.as_mut_ptr(),
                ),
                n_snap_points.assume_init() as usize,
            );
            ret
        }
    }

    fn get_swipe_area(
        &self,
        navigation_direction: NavigationDirection,
        is_drag: bool,
    ) -> gdk::Rectangle {
        unsafe {
            let mut rect = gdk::Rectangle::uninitialized();
            ffi::hdy_swipeable_get_swipe_area(
                self.as_ref().to_glib_none().0,
                navigation_direction.to_glib(),
                is_drag.to_glib(),
                rect.to_glib_none_mut().0,
            );
            rect
        }
    }

    fn get_swipe_tracker(&self) -> Option<SwipeTracker> {
        unsafe {
            from_glib_none(ffi::hdy_swipeable_get_swipe_tracker(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn switch_child(&self, index: u32, duration: i64) {
        unsafe {
            ffi::hdy_swipeable_switch_child(self.as_ref().to_glib_none().0, index, duration);
        }
    }

    fn connect_child_switched<F: Fn(&Self, u32, i64) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn child_switched_trampoline<P, F: Fn(&P, u32, i64) + 'static>(
            this: *mut ffi::HdySwipeable,
            index: libc::c_uint,
            duration: i64,
            f: glib::ffi::gpointer,
        ) where
            P: IsA<Swipeable>,
        {
            let f: &F = &*(f as *const F);
            f(
                &Swipeable::from_glib_borrow(this).unsafe_cast_ref(),
                index,
                duration,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"child-switched\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    child_switched_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Swipeable {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Swipeable")
    }
}