-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
DragDropAction.vala
451 lines (368 loc) · 15.1 KB
/
DragDropAction.vala
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// Copyright (C) 2013 Tom Beckmann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
using Clutter;
namespace Gala {
[Flags]
public enum DragDropActionType {
SOURCE,
DESTINATION
}
public class DragDropAction : Clutter.Action {
private static Gee.HashMap<string,Gee.LinkedList<Actor>>? sources = null;
private static Gee.HashMap<string,Gee.LinkedList<Actor>>? destinations = null;
/**
* A drag has been started. You have to connect to this signal and
* return an actor that is transformed during the drag operation.
*
* @param x The global x coordinate where the action was activated
* @param y The global y coordinate where the action was activated
* @return A ClutterActor that serves as handle
*/
public signal Actor? drag_begin (float x, float y);
/**
* A drag has been canceled. You may want to consider cleaning up
* your handle.
*/
public signal void drag_canceled ();
/**
* A drag action has successfully been finished.
*
* @param actor The actor on which the drag finished
*/
public signal void drag_end (Actor actor);
/**
* The destination has been crossed
*
* @param target the target actor that is crossing the destination
* @param hovered indicates whether the actor is now hovered or not
*/
public signal void crossed (Actor? target, bool hovered);
/**
* Emitted on the source when a destination is crossed.
*
* @param destination The destination actor that has been crossed
* @param hovered Whether the actor is now hovered or has just been left
*/
public signal void destination_crossed (Actor destination, bool hovered);
/**
* The source has been clicked, but the movement was not larger than
* the drag threshold. Useful if the source is also activable.
*
* @param button The button which was pressed
*/
public signal void actor_clicked (uint32 button);
/**
* The type of the action
*/
public DragDropActionType drag_type { get; construct; }
/**
* The unique id given to this drag-drop-group
*/
public string drag_id { get; construct; }
public Actor handle { get; private set; }
/**
* Indicates whether a drag action is currently active
*/
public bool dragging { get; private set; default = false; }
/**
* Allow checking the parents of reactive children if they are valid destinations
* if the child is none
*/
public bool allow_bubbling { get; set; default = true; }
public Actor? hovered { private get; set; default = null; }
private bool clicked = false;
private float last_x;
private float last_y;
private Grab? grab = null;
private static unowned Actor? grabbed_actor = null;
private InputDevice? grabbed_device = null;
private ulong on_event_id = 0;
/**
* Create a new DragDropAction
*
* @param type The type of this actor
* @param id An ID that marks which sources can be dragged on
* which destinations. It has to be the same for all actors that
* should be compatible with each other.
*/
public DragDropAction (DragDropActionType type, string id) {
Object (drag_type : type, drag_id : id);
if (sources == null)
sources = new Gee.HashMap<string,Gee.LinkedList<Actor>> ();
if (destinations == null)
destinations = new Gee.HashMap<string,Gee.LinkedList<Actor>> ();
}
~DragDropAction () {
if (actor != null)
release_actor (actor);
}
public override void set_actor (Actor? new_actor) {
if (actor != null) {
release_actor (actor);
}
if (new_actor != null) {
connect_actor (new_actor);
}
base.set_actor (new_actor);
}
private void release_actor (Actor actor) {
if (DragDropActionType.SOURCE in drag_type) {
var source_list = sources.@get (drag_id);
source_list.remove (actor);
}
if (DragDropActionType.DESTINATION in drag_type) {
var dest_list = destinations[drag_id];
dest_list.remove (actor);
}
}
private void connect_actor (Actor actor) {
if (DragDropActionType.SOURCE in drag_type) {
var source_list = sources.@get (drag_id);
if (source_list == null) {
source_list = new Gee.LinkedList<Actor> ();
sources.@set (drag_id, source_list);
}
source_list.add (actor);
}
if (DragDropActionType.DESTINATION in drag_type) {
var dest_list = destinations[drag_id];
if (dest_list == null) {
dest_list = new Gee.LinkedList<Actor> ();
destinations[drag_id] = dest_list;
}
dest_list.add (actor);
}
}
private void emit_crossed (Actor destination, bool is_hovered) {
get_drag_drop_action (destination).crossed (actor, is_hovered);
destination_crossed (destination, is_hovered);
}
public override bool handle_event (Event event) {
if (!(DragDropActionType.SOURCE in drag_type)) {
return false;
}
switch (event.get_type ()) {
case EventType.BUTTON_PRESS:
if (event.get_button () != 1) {
actor_clicked (event.get_button ());
return false;
}
if (grabbed_actor != null) {
return false;
}
grab_actor (actor, event.get_device ());
clicked = true;
float x, y;
event.get_coords (out x, out y);
last_x = x;
last_y = y;
return true;
case EventType.BUTTON_RELEASE:
if (!dragging) {
float x, y, ex, ey;
event.get_coords (out ex, out ey);
actor.get_transformed_position (out x, out y);
// release has happened within bounds of actor
if (x < ex && x + actor.width > ex && y < ey && y + actor.height > ey) {
actor_clicked (event.get_button ());
}
ungrab_actor ();
clicked = false;
dragging = false;
return true;
} else if (dragging) {
if (hovered != null) {
finish ();
} else {
cancel ();
}
return true;
}
break;
default:
break;
}
return base.handle_event (event);
}
private void grab_actor (Actor actor, InputDevice device) {
if (grabbed_actor != null) {
critical ("Tried to grab an actor with a grab already in progress");
}
grab = actor.get_stage ().grab (actor);
grabbed_actor = actor;
grabbed_device = device;
on_event_id = actor.event.connect (on_event);
}
private void ungrab_actor () {
if (on_event_id == 0 || grabbed_actor == null) {
return;
}
if (grab != null) {
grab.dismiss ();
grab = null;
}
grabbed_device = null;
grabbed_actor.disconnect (on_event_id);
on_event_id = 0;
grabbed_actor = null;
}
private bool on_event (Clutter.Event event) {
var device = event.get_device ();
if (grabbed_device != null &&
device != grabbed_device &&
device.get_device_type () != InputDeviceType.KEYBOARD_DEVICE) {
return false;
}
switch (event.get_type ()) {
case EventType.KEY_PRESS:
if (event.get_key_symbol () == Key.Escape) {
cancel ();
}
break;
case EventType.MOTION:
float x, y;
event.get_coords (out x, out y);
if (!dragging && clicked) {
var drag_threshold = Clutter.Settings.get_default ().dnd_drag_threshold;
if (Math.fabsf (last_x - x) > drag_threshold || Math.fabsf (last_y - y) > drag_threshold) {
handle = drag_begin (x, y);
if (handle == null) {
ungrab_actor ();
critical ("No handle has been returned by the started signal, aborting drag.");
return false;
}
clicked = false;
dragging = true;
ungrab_actor ();
grab_actor (handle, event.get_device ());
handle.reactive = false;
var source_list = sources.@get (drag_id);
if (source_list != null) {
var dest_list = destinations[drag_id];
foreach (var actor in source_list) {
// Do not unset reactivity on destinations
if (dest_list == null || actor in dest_list) {
continue;
}
actor.reactive = false;
}
}
}
return true;
} else if (dragging) {
handle.x -= last_x - x;
handle.y -= last_y - y;
last_x = x;
last_y = y;
var stage = actor.get_stage ();
var actor = stage.get_actor_at_pos (PickMode.REACTIVE, (int) x, (int) y);
DragDropAction action = null;
// if we're allowed to bubble and this actor is not a destination, check its parents
if (actor != null && (action = get_drag_drop_action (actor)) == null && allow_bubbling) {
while ((actor = actor.get_parent ()) != stage) {
if ((action = get_drag_drop_action (actor)) != null)
break;
}
}
// didn't change, no need to do anything
if (actor == hovered)
return true;
if (action == null) {
// apparently we left ours if we had one before
if (hovered != null) {
emit_crossed (hovered, false);
hovered = null;
}
return true;
}
// signal the previous one that we left it
if (hovered != null) {
emit_crossed (hovered, false);
}
// tell the new one that it is hovered
hovered = actor;
emit_crossed (hovered, true);
return true;
}
break;
default:
break;
}
return false;
}
/**
* Looks for a DragDropAction instance if this actor has one or NULL.
* It also checks if it is a DESTINATION and if the id matches
*
* @return the DragDropAction instance on this actor or NULL
*/
private DragDropAction? get_drag_drop_action (Actor actor) {
DragDropAction? drop_action = null;
foreach (var action in actor.get_actions ()) {
drop_action = action as DragDropAction;
if (drop_action == null
|| !(DragDropActionType.DESTINATION in drop_action.drag_type)
|| drop_action.drag_id != drag_id)
continue;
return drop_action;
}
return null;
}
/**
* Abort the drag
*/
public void cancel () {
cleanup ();
drag_canceled ();
}
/**
* Allows you to abort all drags currently running for a given drag-id
*/
public static void cancel_all_by_id (string id) {
var actors = sources.@get (id);
if (actors == null)
return;
foreach (var actor in actors) {
foreach (var action in actor.get_actions ()) {
var drag_action = action as DragDropAction;
if (drag_action != null && drag_action.dragging) {
drag_action.cancel ();
break;
}
}
}
}
private void finish () {
// make sure they reset the style or whatever they changed when hovered
emit_crossed (hovered, false);
cleanup ();
drag_end (hovered);
}
private void cleanup () {
var source_list = sources.@get (drag_id);
if (source_list != null) {
foreach (var actor in source_list) {
actor.reactive = true;
}
}
if (dragging) {
ungrab_actor ();
}
dragging = false;
}
}
}