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
use crate::{prelude::*, NavigationDirection, Swipeable};
use glib::translate::*;
use gtk::subclass::prelude::*;

pub trait SwipeableImpl: WidgetImpl {
    fn cancel_progress(&self) -> f64 {
        self.parent_cancel_progress()
    }

    fn distance(&self) -> f64 {
        self.parent_distance()
    }

    fn progress(&self) -> f64 {
        self.parent_progress()
    }

    fn snap_points(&self) -> Vec<f64> {
        self.parent_snap_points()
    }

    fn swipe_area(
        &self,
        navigation_direction: NavigationDirection,
        is_drag: bool,
    ) -> gdk::Rectangle {
        self.parent_swipe_area(navigation_direction, is_drag)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::SwipeableImplExt> Sealed for T {}
}

pub trait SwipeableImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_cancel_progress(&self) -> f64 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Swipeable>()
                as *const ffi::AdwSwipeableInterface;

            let func = (*parent_iface)
                .get_cancel_progress
                .expect("no parent \"get_cancel_progress\" implementation");

            func(self.obj().unsafe_cast_ref::<Swipeable>().to_glib_none().0)
        }
    }

    fn parent_distance(&self) -> f64 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Swipeable>()
                as *const ffi::AdwSwipeableInterface;

            let func = (*parent_iface)
                .get_distance
                .expect("no parent \"get_distance\" implementation");

            func(self.obj().unsafe_cast_ref::<Swipeable>().to_glib_none().0)
        }
    }

    fn parent_progress(&self) -> f64 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Swipeable>()
                as *const ffi::AdwSwipeableInterface;

            let func = (*parent_iface)
                .get_progress
                .expect("no parent \"get_progress\" implementation");

            func(self.obj().unsafe_cast_ref::<Swipeable>().to_glib_none().0)
        }
    }

    fn parent_snap_points(&self) -> Vec<f64> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Swipeable>()
                as *const ffi::AdwSwipeableInterface;

            let func = (*parent_iface)
                .get_snap_points
                .expect("no parent \"get_snap_points\" implementation");

            let mut n_points = std::mem::MaybeUninit::uninit();

            let points = func(
                self.obj().unsafe_cast_ref::<Swipeable>().to_glib_none().0,
                n_points.as_mut_ptr(),
            );

            let size = n_points.assume_init() as usize;
            Vec::from_raw_parts(points, size, size)
        }
    }

    fn parent_swipe_area(
        &self,
        navigation_direction: NavigationDirection,
        is_drag: bool,
    ) -> gdk::Rectangle {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Swipeable>()
                as *const ffi::AdwSwipeableInterface;

            let func = (*parent_iface)
                .get_swipe_area
                .expect("no parent \"get_swipe_area\" implementation");

            let mut rect = gdk::Rectangle::uninitialized();
            func(
                self.obj().unsafe_cast_ref::<Swipeable>().to_glib_none().0,
                navigation_direction.into_glib(),
                is_drag.into_glib(),
                rect.to_glib_none_mut().0,
            );

            rect
        }
    }
}

impl<T: SwipeableImpl> SwipeableImplExt for T {}

unsafe impl<T: SwipeableImpl> IsImplementable<T> for Swipeable {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.get_cancel_progress = Some(swipeable_get_cancel_progress::<T>);
        iface.get_distance = Some(swipeable_get_distance::<T>);
        iface.get_progress = Some(swipeable_get_progress::<T>);
        iface.get_snap_points = Some(swipeable_get_snap_points::<T>);
        iface.get_swipe_area = Some(swipeable_get_swipe_area::<T>);
    }
}

unsafe extern "C" fn swipeable_get_cancel_progress<T: SwipeableImpl>(
    swipeable: *mut ffi::AdwSwipeable,
) -> f64 {
    let instance = &*(swipeable as *mut T::Instance);
    let imp = instance.imp();

    imp.cancel_progress()
}

unsafe extern "C" fn swipeable_get_distance<T: SwipeableImpl>(
    swipeable: *mut ffi::AdwSwipeable,
) -> f64 {
    let instance = &*(swipeable as *mut T::Instance);
    let imp = instance.imp();

    imp.distance()
}

unsafe extern "C" fn swipeable_get_progress<T: SwipeableImpl>(
    swipeable: *mut ffi::AdwSwipeable,
) -> f64 {
    let instance = &*(swipeable as *mut T::Instance);
    let imp = instance.imp();

    imp.progress()
}

unsafe extern "C" fn swipeable_get_snap_points<T: SwipeableImpl>(
    swipeable: *mut ffi::AdwSwipeable,
    n_pointsptr: *mut libc::c_int,
) -> *mut f64 {
    let instance = &*(swipeable as *mut T::Instance);
    let imp = instance.imp();

    let points = imp.snap_points();

    n_pointsptr.write(points.len() as libc::c_int);
    ToGlibContainerFromSlice::to_glib_full_from_slice(points.as_slice())
}

unsafe extern "C" fn swipeable_get_swipe_area<T: SwipeableImpl>(
    swipeable: *mut ffi::AdwSwipeable,
    navigation_direction: ffi::AdwNavigationDirection,
    is_drag: i32,
    area: *mut gdk::ffi::GdkRectangle,
) {
    let instance = &*(swipeable as *mut T::Instance);
    let imp = instance.imp();

    let swipe_area = imp.swipe_area(from_glib(navigation_direction), from_glib(is_drag));

    *area = *swipe_area.to_glib_full();
}