1use crate::{ffi, EventController, Gesture, GestureSingle, PropagationLimit, PropagationPhase};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GtkGestureDrag")]
16 pub struct GestureDrag(Object<ffi::GtkGestureDrag, ffi::GtkGestureDragClass>) @extends GestureSingle, Gesture, EventController;
17
18 match fn {
19 type_ => || ffi::gtk_gesture_drag_get_type(),
20 }
21}
22
23impl GestureDrag {
24 pub const NONE: Option<&'static GestureDrag> = None;
25
26 #[doc(alias = "gtk_gesture_drag_new")]
27 pub fn new() -> GestureDrag {
28 assert_initialized_main_thread!();
29 unsafe { Gesture::from_glib_full(ffi::gtk_gesture_drag_new()).unsafe_cast() }
30 }
31
32 pub fn builder() -> GestureDragBuilder {
37 GestureDragBuilder::new()
38 }
39}
40
41impl Default for GestureDrag {
42 fn default() -> Self {
43 Self::new()
44 }
45}
46
47#[must_use = "The builder must be built to be used"]
52pub struct GestureDragBuilder {
53 builder: glib::object::ObjectBuilder<'static, GestureDrag>,
54}
55
56impl GestureDragBuilder {
57 fn new() -> Self {
58 Self {
59 builder: glib::object::Object::builder(),
60 }
61 }
62
63 pub fn button(self, button: u32) -> Self {
64 Self {
65 builder: self.builder.property("button", button),
66 }
67 }
68
69 pub fn exclusive(self, exclusive: bool) -> Self {
70 Self {
71 builder: self.builder.property("exclusive", exclusive),
72 }
73 }
74
75 pub fn touch_only(self, touch_only: bool) -> Self {
76 Self {
77 builder: self.builder.property("touch-only", touch_only),
78 }
79 }
80
81 pub fn n_points(self, n_points: u32) -> Self {
82 Self {
83 builder: self.builder.property("n-points", n_points),
84 }
85 }
86
87 pub fn name(self, name: impl Into<glib::GString>) -> Self {
88 Self {
89 builder: self.builder.property("name", name.into()),
90 }
91 }
92
93 pub fn propagation_limit(self, propagation_limit: PropagationLimit) -> Self {
94 Self {
95 builder: self
96 .builder
97 .property("propagation-limit", propagation_limit),
98 }
99 }
100
101 pub fn propagation_phase(self, propagation_phase: PropagationPhase) -> Self {
102 Self {
103 builder: self
104 .builder
105 .property("propagation-phase", propagation_phase),
106 }
107 }
108
109 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
112 pub fn build(self) -> GestureDrag {
113 assert_initialized_main_thread!();
114 self.builder.build()
115 }
116}
117
118mod sealed {
119 pub trait Sealed {}
120 impl<T: super::IsA<super::GestureDrag>> Sealed for T {}
121}
122
123pub trait GestureDragExt: IsA<GestureDrag> + sealed::Sealed + 'static {
124 #[doc(alias = "gtk_gesture_drag_get_offset")]
125 #[doc(alias = "get_offset")]
126 fn offset(&self) -> Option<(f64, f64)> {
127 unsafe {
128 let mut x = std::mem::MaybeUninit::uninit();
129 let mut y = std::mem::MaybeUninit::uninit();
130 let ret = from_glib(ffi::gtk_gesture_drag_get_offset(
131 self.as_ref().to_glib_none().0,
132 x.as_mut_ptr(),
133 y.as_mut_ptr(),
134 ));
135 if ret {
136 Some((x.assume_init(), y.assume_init()))
137 } else {
138 None
139 }
140 }
141 }
142
143 #[doc(alias = "gtk_gesture_drag_get_start_point")]
144 #[doc(alias = "get_start_point")]
145 fn start_point(&self) -> Option<(f64, f64)> {
146 unsafe {
147 let mut x = std::mem::MaybeUninit::uninit();
148 let mut y = std::mem::MaybeUninit::uninit();
149 let ret = from_glib(ffi::gtk_gesture_drag_get_start_point(
150 self.as_ref().to_glib_none().0,
151 x.as_mut_ptr(),
152 y.as_mut_ptr(),
153 ));
154 if ret {
155 Some((x.assume_init(), y.assume_init()))
156 } else {
157 None
158 }
159 }
160 }
161
162 #[doc(alias = "drag-begin")]
163 fn connect_drag_begin<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
164 unsafe extern "C" fn drag_begin_trampoline<
165 P: IsA<GestureDrag>,
166 F: Fn(&P, f64, f64) + 'static,
167 >(
168 this: *mut ffi::GtkGestureDrag,
169 start_x: std::ffi::c_double,
170 start_y: std::ffi::c_double,
171 f: glib::ffi::gpointer,
172 ) {
173 let f: &F = &*(f as *const F);
174 f(
175 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
176 start_x,
177 start_y,
178 )
179 }
180 unsafe {
181 let f: Box_<F> = Box_::new(f);
182 connect_raw(
183 self.as_ptr() as *mut _,
184 b"drag-begin\0".as_ptr() as *const _,
185 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
186 drag_begin_trampoline::<Self, F> as *const (),
187 )),
188 Box_::into_raw(f),
189 )
190 }
191 }
192
193 #[doc(alias = "drag-end")]
194 fn connect_drag_end<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
195 unsafe extern "C" fn drag_end_trampoline<
196 P: IsA<GestureDrag>,
197 F: Fn(&P, f64, f64) + 'static,
198 >(
199 this: *mut ffi::GtkGestureDrag,
200 offset_x: std::ffi::c_double,
201 offset_y: std::ffi::c_double,
202 f: glib::ffi::gpointer,
203 ) {
204 let f: &F = &*(f as *const F);
205 f(
206 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
207 offset_x,
208 offset_y,
209 )
210 }
211 unsafe {
212 let f: Box_<F> = Box_::new(f);
213 connect_raw(
214 self.as_ptr() as *mut _,
215 b"drag-end\0".as_ptr() as *const _,
216 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
217 drag_end_trampoline::<Self, F> as *const (),
218 )),
219 Box_::into_raw(f),
220 )
221 }
222 }
223
224 #[doc(alias = "drag-update")]
225 fn connect_drag_update<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
226 unsafe extern "C" fn drag_update_trampoline<
227 P: IsA<GestureDrag>,
228 F: Fn(&P, f64, f64) + 'static,
229 >(
230 this: *mut ffi::GtkGestureDrag,
231 offset_x: std::ffi::c_double,
232 offset_y: std::ffi::c_double,
233 f: glib::ffi::gpointer,
234 ) {
235 let f: &F = &*(f as *const F);
236 f(
237 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
238 offset_x,
239 offset_y,
240 )
241 }
242 unsafe {
243 let f: Box_<F> = Box_::new(f);
244 connect_raw(
245 self.as_ptr() as *mut _,
246 b"drag-update\0".as_ptr() as *const _,
247 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
248 drag_update_trampoline::<Self, F> as *const (),
249 )),
250 Box_::into_raw(f),
251 )
252 }
253 }
254}
255
256impl<O: IsA<GestureDrag>> GestureDragExt for O {}