Debian Codegraph @

gdk/macos/gdkmacosdrag.c

raw symbols SCIP
1 /*
2 * Copyright © 2020 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * SPDX-License-Identifier: LGPL-2.1-or-later
18 */
19
20 #include "config.h"
21
22 #include "gdkmacosdrag-private.h"
23
24 #include "gdkmacosdevice-private.h"
25 #include "gdkmacoscursor-private.h"
26 #include "gdkmacosdisplay-private.h"
27 #include "gdkmacosdragsurface-private.h"
28 #include "gdkmacospasteboard-private.h"
29 #include "gdkmacosutils-private.h"
30
31 #include "gdk/gdkdeviceprivate.h"
32 #include "gdk/gdkeventsprivate.h"
33 #include <glib/gi18n-lib.h>
34 #include "gdk/gdkseatprivate.h"
35 #include "gdk/gdkprivate.h"
36
37 #define BIG_STEP 20
38 #define SMALL_STEP 1
39 #define ANIM_TIME 500000 /* .5 seconds */
40
41 typedef struct
42 {
43 GdkMacosDrag *drag;
44 GdkFrameClock *frame_clock;
45 gint64 start_time;
46 } GdkMacosZoomback;
47
48 G_DEFINE_TYPE (GdkMacosDrag, gdk_macos_drag, GDK_TYPE_DRAG)
49
50 enum {
51 PROP_0,
52 PROP_DRAG_SURFACE,
53 N_PROPS
54 };
55
56 static GParamSpec *properties [N_PROPS];
57
58 static double
59 ease_out_cubic (double t)
60 {
61 double p = t - 1;
62 return p * p * p + 1;
63 }
64
65 static void
66 gdk_macos_zoomback_destroy (GdkMacosZoomback *zb)
67 {
68 gdk_surface_hide (GDK_SURFACE (zb->drag->drag_surface));
69 g_clear_object (&zb->drag);
70 g_free (zb);
71 }
72
73 static gboolean
74 gdk_macos_zoomback_timeout (gpointer data)
75 {
76 GdkMacosZoomback *zb = data;
77 GdkFrameClock *frame_clock;
78 GdkMacosDrag *drag;
79 gint64 current_time;
80 double f;
81 double t;
82
83 g_assert (zb != NULL);
84 g_assert (GDK_IS_MACOS_DRAG (zb->drag));
85
86 drag = zb->drag;
87 frame_clock = zb->frame_clock;
88
89 if (!frame_clock)
90 return G_SOURCE_REMOVE;
91
92 current_time = gdk_frame_clock_get_frame_time (frame_clock);
93 f = (current_time - zb->start_time) / (double) ANIM_TIME;
94 if (f >= 1.0)
95 return G_SOURCE_REMOVE;
96
97 t = ease_out_cubic (f);
98
99 _gdk_macos_surface_move (GDK_MACOS_SURFACE (drag->drag_surface),
100 (drag->last_x - drag->hot_x) +
101 (drag->start_x - drag->last_x) * t,
102 (drag->last_y - drag->hot_y) +
103 (drag->start_y - drag->last_y) * t);
104 _gdk_macos_surface_set_opacity (GDK_MACOS_SURFACE (drag->drag_surface), 1.0 - f);
105
106 /* Make sure we're topmost */
107 _gdk_macos_surface_show (GDK_MACOS_SURFACE (drag->drag_surface));
108
109 return G_SOURCE_CONTINUE;
110 }
111
112 static GdkSurface *
113 gdk_macos_drag_get_drag_surface (GdkDrag *drag)
114 {
115 return GDK_SURFACE (GDK_MACOS_DRAG (drag)->drag_surface);
116 }
117
118 static void
119 gdk_macos_drag_set_hotspot (GdkDrag *drag,
120 int hot_x,
121 int hot_y)
122 {
123 GdkMacosDrag *self = (GdkMacosDrag *)drag;
124 int change_x;
125 int change_y;
126
127 g_assert (GDK_IS_MACOS_DRAG (self));
128
129 change_x = hot_x - self->hot_x;
130 change_y = hot_y - self->hot_y;
131
132 self->hot_x = hot_x;
133 self->hot_y = hot_y;
134
135 if (change_x || change_y)
136 _gdk_macos_surface_move (GDK_MACOS_SURFACE (self->drag_surface),
137 GDK_SURFACE (self->drag_surface)->x + change_x,
138 GDK_SURFACE (self->drag_surface)->y + change_y);
139 }
140
141 static void
142 gdk_macos_drag_drop_done (GdkDrag *drag,
143 gboolean success)
144 {
145 GdkMacosDrag *self = (GdkMacosDrag *)drag;
146 GdkMacosZoomback *zb;
147 guint id;
148
149 g_assert (GDK_IS_MACOS_DRAG (self));
150
151 if (success)
152 {
153 gdk_surface_hide (GDK_SURFACE (self->drag_surface));
154 g_object_unref (drag);
155 return;
156 }
157
158 /* Apple HIG suggests doing a "zoomback" animation of the surface back
159 * towards the original position.
160 */
161 zb = g_new0 (GdkMacosZoomback, 1);
162 zb->drag = g_object_ref (self);
163 zb->frame_clock = gdk_surface_get_frame_clock (GDK_SURFACE (self->drag_surface));
164 zb->start_time = gdk_frame_clock_get_frame_time (zb->frame_clock);
165
166 id = g_timeout_add_full (G_PRIORITY_DEFAULT, 17,
167 gdk_macos_zoomback_timeout,
168 zb,
169 (GDestroyNotify) gdk_macos_zoomback_destroy);
170 gdk_source_set_static_name_by_id (id, "[gtk] gdk_macos_zoomback_timeout");
171 g_object_unref (drag);
172 }
173
174 void
175 gdk_macos_drag_set_cursor (GdkMacosDrag *self,
176 GdkCursor *cursor)
177 {
178 NSCursor *nscursor;
179
180 g_assert (GDK_IS_MACOS_DRAG (self));
181 g_assert (!cursor || GDK_IS_CURSOR (cursor));
182
183 g_set_object (&self->cursor, cursor);
184
185 nscursor = _gdk_macos_cursor_get_ns_cursor (cursor);
186
187 if (nscursor != NULL)
188 [nscursor set];
189 }
190
191 static void
192 gdk_macos_drag_update_cursor (GdkDrag *drag)
193 {
194 GdkMacosDrag *self = (GdkMacosDrag *)drag;
195 GdkCursor *cursor;
196
197 cursor = gdk_drag_get_cursor (drag, gdk_drag_get_selected_action (drag));
198 gdk_macos_drag_set_cursor (self, cursor);
199 }
200
201 static void
202 gdk_macos_drag_cancel (GdkDrag *drag,
203 GdkDragCancelReason reason)
204 {
205 GdkMacosDrag *self = (GdkMacosDrag *)drag;
206
207 g_assert (GDK_IS_MACOS_DRAG (self));
208
209 if (self->cancelled)
210 return;
211
212 self->cancelled = TRUE;
213 gdk_drag_drop_done (drag, FALSE);
214 }
215
216 static void
217 gdk_macos_drag_drop_performed (GdkDrag *drag,
218 guint32 time)
219 {
220 GdkMacosDrag *self = (GdkMacosDrag *)drag;
221
222 g_assert (GDK_IS_MACOS_DRAG (self));
223
224 g_object_ref (self);
225 g_signal_emit_by_name (drag, "dnd-finished");
226 gdk_drag_drop_done (drag, TRUE);
227 g_object_unref (self);
228 }
229
230 static void
231 gdk_drag_get_current_actions (GdkModifierType state,
232 int button,
233 GdkDragAction actions,
234 GdkDragAction *suggested_action,
235 GdkDragAction *possible_actions)
236 {
237 *suggested_action = 0;
238 *possible_actions = 0;
239
240 if ((button == GDK_BUTTON_MIDDLE || button == GDK_BUTTON_SECONDARY) && (actions & GDK_ACTION_ASK))
241 {
242 *suggested_action = GDK_ACTION_ASK;
243 *possible_actions = actions;
244 }
245 else if (state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))
246 {
247 if ((state & GDK_SHIFT_MASK) && (state & GDK_CONTROL_MASK))
248 {
249 if (actions & GDK_ACTION_LINK)
250 {
251 *suggested_action = GDK_ACTION_LINK;
252 *possible_actions = GDK_ACTION_LINK;
253 }
254 }
255 else if (state & GDK_CONTROL_MASK)
256 {
257 if (actions & GDK_ACTION_COPY)
258 {
259 *suggested_action = GDK_ACTION_COPY;
260 *possible_actions = GDK_ACTION_COPY;
261 }
262 }
263 else
264 {
265 if (actions & GDK_ACTION_MOVE)
266 {
267 *suggested_action = GDK_ACTION_MOVE;
268 *possible_actions = GDK_ACTION_MOVE;
269 }
270 }
271 }
272 else
273 {
274 *possible_actions = actions;
275
276 if ((state & (GDK_ALT_MASK)) && (actions & GDK_ACTION_ASK))
277 *suggested_action = GDK_ACTION_ASK;
278 else if (actions & GDK_ACTION_COPY)
279 *suggested_action = GDK_ACTION_COPY;
280 else if (actions & GDK_ACTION_MOVE)
281 *suggested_action = GDK_ACTION_MOVE;
282 else if (actions & GDK_ACTION_LINK)
283 *suggested_action = GDK_ACTION_LINK;
284 }
285 }
286
287 static void
288 gdk_macos_drag_finalize (GObject *object)
289 {
290 GdkMacosDrag *self = (GdkMacosDrag *)object;
291 GdkMacosDragSurface *drag_surface = g_steal_pointer (&self->drag_surface);
292
293 g_clear_object (&self->cursor);
294
295 G_OBJECT_CLASS (gdk_macos_drag_parent_class)->finalize (object);
296
297 if (drag_surface)
298 gdk_surface_destroy (GDK_SURFACE (drag_surface));
299 }
300
301 static void
302 gdk_macos_drag_get_property (GObject *object,
303 guint prop_id,
304 GValue *value,
305 GParamSpec *pspec)
306 {
307 GdkMacosDrag *self = GDK_MACOS_DRAG (object);
308
309 switch (prop_id)
310 {
311 case PROP_DRAG_SURFACE:
312 g_value_set_object (value, self->drag_surface);
313 break;
314
315 default:
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 }
318 }
319
320 static void
321 gdk_macos_drag_set_property (GObject *object,
322 guint prop_id,
323 const GValue *value,
324 GParamSpec *pspec)
325 {
326 GdkMacosDrag *self = GDK_MACOS_DRAG (object);
327
328 switch (prop_id)
329 {
330 case PROP_DRAG_SURFACE:
331 self->drag_surface = g_value_dup_object (value);
332 break;
333
334 default:
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 }
337 }
338
339 static void
340 gdk_macos_drag_class_init (GdkMacosDragClass *klass)
341 {
342 GObjectClass *object_class = G_OBJECT_CLASS (klass);
343 GdkDragClass *drag_class = GDK_DRAG_CLASS (klass);
344
345 object_class->finalize = gdk_macos_drag_finalize;
346 object_class->get_property = gdk_macos_drag_get_property;
347 object_class->set_property = gdk_macos_drag_set_property;
348
349 drag_class->get_drag_surface = gdk_macos_drag_get_drag_surface;
350 drag_class->set_hotspot = gdk_macos_drag_set_hotspot;
351 drag_class->drop_done = gdk_macos_drag_drop_done;
352 drag_class->update_cursor = gdk_macos_drag_update_cursor;
353 drag_class->cancel = gdk_macos_drag_cancel;
354 drag_class->drop_performed = gdk_macos_drag_drop_performed;
355
356 properties [PROP_DRAG_SURFACE] =
357 g_param_spec_object ("drag-surface", NULL, NULL,
358 GDK_TYPE_MACOS_DRAG_SURFACE,
359 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
360
361 g_object_class_install_properties (object_class, N_PROPS, properties);
362 }
363
364 static void
365 gdk_macos_drag_init (GdkMacosDrag *self)
366 {
367 }
368
369 gboolean
370 _gdk_macos_drag_begin (GdkMacosDrag *self,
371 GdkContentProvider *content,
372 GdkMacosWindow *window)
373 {
374 NSArray<NSDraggingItem *> *items;
375 NSDraggingSession *session;
376 NSPasteboardItem *item;
377 NSEvent *nsevent;
378
379 g_return_val_if_fail (GDK_IS_MACOS_DRAG (self), FALSE);
380 g_return_val_if_fail (GDK_IS_MACOS_WINDOW (window), FALSE);
381
382 GDK_BEGIN_MACOS_ALLOC_POOL;
383
384 item = [[GdkMacosPasteboardItem alloc] initForDrag:GDK_DRAG (self) withContentProvider:content];
385 items = [NSArray arrayWithObject:item];
386 nsevent = _gdk_macos_display_get_last_nsevent ();
387
388 session = [[window contentView] beginDraggingSessionWithItems:items
389 event:nsevent
390 source:window];
391
392 GDK_END_MACOS_ALLOC_POOL;
393
394 _gdk_macos_display_set_drag (GDK_MACOS_DISPLAY (gdk_drag_get_display (GDK_DRAG (self))),
395 [session draggingSequenceNumber],
396 GDK_DRAG (self));
397
398 return TRUE;
399 }
400
401 NSDragOperation
402 _gdk_macos_drag_operation (GdkMacosDrag *self)
403 {
404 NSDragOperation operation = NSDragOperationNone;
405 GdkDragAction actions;
406
407 g_return_val_if_fail (GDK_IS_MACOS_DRAG (self), NSDragOperationNone);
408
409 actions = gdk_drag_get_actions (GDK_DRAG (self));
410
411 if (actions & GDK_ACTION_LINK)
412 operation |= NSDragOperationLink;
413
414 if (actions & GDK_ACTION_MOVE)
415 operation |= NSDragOperationMove;
416
417 if (actions & GDK_ACTION_COPY)
418 operation |= NSDragOperationCopy;
419
420 return operation;
421 }
422
423 GdkDragAction
424 _gdk_macos_drag_ns_operation_to_action (NSDragOperation operation)
425 {
426 if (operation & NSDragOperationCopy)
427 return GDK_ACTION_COPY;
428 if (operation & NSDragOperationMove)
429 return GDK_ACTION_MOVE;
430 if (operation & NSDragOperationLink)
431 return GDK_ACTION_LINK;
432 return GDK_ACTION_NONE;
433 }
434
435 void
436 _gdk_macos_drag_surface_move (GdkMacosDrag *self,
437 int x_root,
438 int y_root)
439 {
440 g_return_if_fail (GDK_IS_MACOS_DRAG (self));
441
442 self->last_x = x_root;
443 self->last_y = y_root;
444
445 if (GDK_IS_MACOS_SURFACE (self->drag_surface))
446 _gdk_macos_surface_move (GDK_MACOS_SURFACE (self->drag_surface),
447 x_root - self->hot_x,
448 y_root - self->hot_y);
449 }
450
451 void
452 _gdk_macos_drag_set_start_position (GdkMacosDrag *self,
453 int start_x,
454 int start_y)
455 {
456 g_return_if_fail (GDK_IS_MACOS_DRAG (self));
457
458 self->start_x = start_x;
459 self->start_y = start_y;
460 }
461
462 void
463 _gdk_macos_drag_set_actions (GdkMacosDrag *self,
464 GdkModifierType mods)
465 {
466 GdkDragAction suggested_action;
467 GdkDragAction possible_actions;
468
469 g_assert (GDK_IS_MACOS_DRAG (self));
470
471 gdk_drag_get_current_actions (mods,
472 GDK_BUTTON_PRIMARY,
473 gdk_drag_get_actions (GDK_DRAG (self)),
474 &suggested_action,
475 &possible_actions);
476
477 gdk_drag_set_selected_action (GDK_DRAG (self), suggested_action);
478 gdk_drag_set_actions (GDK_DRAG (self), possible_actions);
479 }