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
118pub trait GestureDragExt: IsA<GestureDrag> + 'static {
119 #[doc(alias = "gtk_gesture_drag_get_offset")]
120 #[doc(alias = "get_offset")]
121 fn offset(&self) -> Option<(f64, f64)> {
122 unsafe {
123 let mut x = std::mem::MaybeUninit::uninit();
124 let mut y = std::mem::MaybeUninit::uninit();
125 let ret = from_glib(ffi::gtk_gesture_drag_get_offset(
126 self.as_ref().to_glib_none().0,
127 x.as_mut_ptr(),
128 y.as_mut_ptr(),
129 ));
130 if ret {
131 Some((x.assume_init(), y.assume_init()))
132 } else {
133 None
134 }
135 }
136 }
137
138 #[doc(alias = "gtk_gesture_drag_get_start_point")]
139 #[doc(alias = "get_start_point")]
140 fn start_point(&self) -> Option<(f64, f64)> {
141 unsafe {
142 let mut x = std::mem::MaybeUninit::uninit();
143 let mut y = std::mem::MaybeUninit::uninit();
144 let ret = from_glib(ffi::gtk_gesture_drag_get_start_point(
145 self.as_ref().to_glib_none().0,
146 x.as_mut_ptr(),
147 y.as_mut_ptr(),
148 ));
149 if ret {
150 Some((x.assume_init(), y.assume_init()))
151 } else {
152 None
153 }
154 }
155 }
156
157 #[doc(alias = "drag-begin")]
158 fn connect_drag_begin<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
159 unsafe extern "C" fn drag_begin_trampoline<
160 P: IsA<GestureDrag>,
161 F: Fn(&P, f64, f64) + 'static,
162 >(
163 this: *mut ffi::GtkGestureDrag,
164 start_x: std::ffi::c_double,
165 start_y: std::ffi::c_double,
166 f: glib::ffi::gpointer,
167 ) {
168 let f: &F = &*(f as *const F);
169 f(
170 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
171 start_x,
172 start_y,
173 )
174 }
175 unsafe {
176 let f: Box_<F> = Box_::new(f);
177 connect_raw(
178 self.as_ptr() as *mut _,
179 c"drag-begin".as_ptr() as *const _,
180 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
181 drag_begin_trampoline::<Self, F> as *const (),
182 )),
183 Box_::into_raw(f),
184 )
185 }
186 }
187
188 #[doc(alias = "drag-end")]
189 fn connect_drag_end<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
190 unsafe extern "C" fn drag_end_trampoline<
191 P: IsA<GestureDrag>,
192 F: Fn(&P, f64, f64) + 'static,
193 >(
194 this: *mut ffi::GtkGestureDrag,
195 offset_x: std::ffi::c_double,
196 offset_y: std::ffi::c_double,
197 f: glib::ffi::gpointer,
198 ) {
199 let f: &F = &*(f as *const F);
200 f(
201 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
202 offset_x,
203 offset_y,
204 )
205 }
206 unsafe {
207 let f: Box_<F> = Box_::new(f);
208 connect_raw(
209 self.as_ptr() as *mut _,
210 c"drag-end".as_ptr() as *const _,
211 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
212 drag_end_trampoline::<Self, F> as *const (),
213 )),
214 Box_::into_raw(f),
215 )
216 }
217 }
218
219 #[doc(alias = "drag-update")]
220 fn connect_drag_update<F: Fn(&Self, f64, f64) + 'static>(&self, f: F) -> SignalHandlerId {
221 unsafe extern "C" fn drag_update_trampoline<
222 P: IsA<GestureDrag>,
223 F: Fn(&P, f64, f64) + 'static,
224 >(
225 this: *mut ffi::GtkGestureDrag,
226 offset_x: std::ffi::c_double,
227 offset_y: std::ffi::c_double,
228 f: glib::ffi::gpointer,
229 ) {
230 let f: &F = &*(f as *const F);
231 f(
232 GestureDrag::from_glib_borrow(this).unsafe_cast_ref(),
233 offset_x,
234 offset_y,
235 )
236 }
237 unsafe {
238 let f: Box_<F> = Box_::new(f);
239 connect_raw(
240 self.as_ptr() as *mut _,
241 c"drag-update".as_ptr() as *const _,
242 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
243 drag_update_trampoline::<Self, F> as *const (),
244 )),
245 Box_::into_raw(f),
246 )
247 }
248 }
249}
250
251impl<O: IsA<GestureDrag>> GestureDragExt for O {}