forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab.js
588 lines (504 loc) · 20.3 KB
/
grab.js
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import Clutter from 'gi://Clutter';
import Graphene from 'gi://Graphene';
import Meta from 'gi://Meta';
import St from 'gi://St';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Settings, Utils, Tiling, Navigator, Scratch } from './imports.js';
import { Easer } from './utils.js';
/**
* Returns a virtual pointer (i.e. mouse) device that can be used to
* "clickout" of a drag operation when `grab_end_op` is unavailable
* (i.e. as of Gnome 44 where `grab_end_op` was removed).
* @returns Clutter.VirtualInputDevice
*/
let virtualPointer;
export function getVirtualPointer() {
if (!virtualPointer) {
virtualPointer = Clutter.get_default_backend()
.get_default_seat()
.create_virtual_device(Clutter.InputDeviceType.POINTER_DEVICE);
}
return virtualPointer;
}
export class MoveGrab {
constructor(metaWindow, type, space) {
this.window = metaWindow;
this.type = type;
this.signals = new Utils.Signals();
this.grabbed = false;
this.initialSpace = space || Tiling.spaces.spaceOfWindow(metaWindow);
this.zoneActors = new Set();
// save whether this was tiled window at start of grab
this.wasTiled = !(this.initialSpace.isFloating(metaWindow) ||
Scratch.isScratchWindow(metaWindow));
this.dndTargets = [];
}
begin({ center } = {}) {
console.debug("#grab", "begin");
this.center = center;
if (this.grabbed)
return;
this.grabbed = true;
global.display.end_grab_op?.(global.get_current_time());
global.display.set_cursor(Meta.Cursor.MOVE_OR_RESIZE_WINDOW);
this.dispatcher = new Navigator.getActionDispatcher(Clutter.GrabState.POINTER);
this.actor = this.dispatcher.actor;
let metaWindow = this.window;
let actor = metaWindow.get_compositor_private();
let clone = metaWindow.clone;
let space = this.initialSpace;
let frame = metaWindow.get_frame_rect();
this.initialY = clone.targetY;
Easer.removeEase(clone);
let [gx, gy, $] = Utils.getPointerCoords();
let px = (gx - actor.x) / actor.width;
let py = (gy - actor.y) / actor.height;
actor.set_pivot_point(px, py);
let [x, y] = space.globalToScroll(gx, gy);
if (clone.get_parent() === this.initialSpace.cloneContainer) {
this.pointerOffset = [x - clone.x, y - clone.y];
px = (x - clone.x) / clone.width;
py = (y - clone.y) / clone.height;
} else {
this.pointerOffset = [gx - frame.x, gy - frame.y];
clone.x = frame.x;
clone.y = frame.y;
px = (gx - clone.x) / clone.width;
py = (gy - clone.y) / clone.height;
}
!center && clone.set_pivot_point(px, py);
center && clone.set_pivot_point(0, 0);
this.signals.connect(this.actor, "button-release-event", this.end.bind(this));
this.signals.connect(this.actor, "touch-event", (act, evt) => {
if (evt.type() === Clutter.EventType.TOUCH_END) {
this.end();
}
else {
this.motion(act, evt);
}
});
this.signals.connect(this.actor, "motion-event", this.motion.bind(this));
this.signals.connect(global.display, "window-entered-monitor",
this.beginDnD.bind(this)
);
this.scrollAnchor = x;
space.startAnimate();
// Make sure the window actor is visible
Navigator.getNavigator();
Tiling.animateWindow(metaWindow);
Easer.removeEase(space.cloneContainer);
}
beginDnD({ center } = {}) {
if (this.dnd) {
return;
}
this.center = center;
this.dnd = true;
console.debug("#grab", "begin DnD");
Navigator.getNavigator().minimaps.forEach(m => typeof m === 'number'
? Utils.timeout_remove(m) : m.hide());
global.display.set_cursor(Meta.Cursor.MOVE_OR_RESIZE_WINDOW);
let metaWindow = this.window;
let clone = metaWindow.clone;
let space = this.initialSpace;
let [gx, gy, $] = global.get_pointer();
let point = {};
if (center) {
point = space.cloneContainer.apply_relative_transform_to_point(
global.stage, new Graphene.Point3D({ x: Math.round(clone.x), y: Math.round(clone.y) }));
} else {
// For some reason the above isn't smooth when DnD is triggered from dragging
let [dx, dy] = this.pointerOffset;
point.x = gx - dx;
point.y = gy - dy;
}
let i = space.indexOf(metaWindow);
let single = i !== -1 && space[i].length === 1;
space.removeWindow(metaWindow);
Utils.actor_reparent(clone, Main.uiGroup);
clone.x = Math.round(point.x);
clone.y = Math.round(point.y);
let newScale = clone.scale_x * space.actor.scale_x;
clone.set_scale(newScale, newScale);
let params = { time: Settings.prefs.animation_time, scale_x: 0.5, scale_y: 0.5, opacity: 240 };
if (center) {
this.pointerOffset = [0, 0];
clone.set_pivot_point(0, 0);
params.x = gx;
params.y = gy;
}
clone.__oldOpacity = clone.opacity;
Easer.addEase(clone, params);
this.signals.connect(global.stage, "button-press-event", this.end.bind(this));
let monitor = Utils.monitorAtPoint(gx, gy);
let onSame = monitor === space.monitor;
let [x, y] = space.globalToViewport(gx, gy);
if (!this.center && onSame && single && space[i]) {
Tiling.move_to(space, space[i][0], { x: x + Settings.prefs.window_gap / 2 });
} else if (!this.center && onSame && single && space[i - 1]) {
Tiling.move_to(space, space[i - 1][0], { x: x - space[i - 1][0].clone.width - Settings.prefs.window_gap / 2 });
} else if (!this.center && onSame && space.length === 0) {
space.targetX = x;
space.cloneContainer.x = x;
}
let [sx, sy] = space.globalToScroll(gx, gy, { useTarget: true });
for (let [workspace, space] of Tiling.spaces) {
this.signals.connect(space.background, "motion-event", this.spaceMotion.bind(this, space));
}
this.selectDndZone(space, sx, sy, single && onSame);
}
spaceMotion(space, background, event) {
let [gx, gy, $] = global.get_pointer();
let [sx, sy] = space.globalToScroll(gx, gy, { useTarget: true });
this.selectDndZone(space, sx, sy);
}
/** x,y in scroll cooridinates */
selectDndZone(space, x, y, initial = false) {
const gap = Settings.prefs.window_gap;
const halfGap = gap / 2;
const columnZoneMarginViz = 100 + halfGap;
const columnZoneMargin = space.length > 0 ? columnZoneMarginViz : Math.round(space.width / 4);
const rowZoneMargin = 250 + halfGap;
let target = null;
const tilingHeight = space.height - Main.layoutManager.panelBox.height;
let fakeClone = {
targetX: null,
targetY: 0,
width: columnZoneMargin,
height: tilingHeight,
};
if (space.length > 0) {
const lastClone = space[space.length - 1][0].clone;
fakeClone.targetX = lastClone.x + lastClone.width + gap;
} else {
let [sx, sy] = space.viewportToScroll(Math.round(space.width / 2), 0);
fakeClone.targetX = sx + halfGap;
}
const columns = [...space, [{ clone: fakeClone }]];
for (let j = 0; j < columns.length; j++) {
const column = columns[j];
const metaWindow = column[0];
const clone = metaWindow.clone;
// FIXME: Non-uniform column width
const colX = clone.targetX;
const colW = clone.width;
// Fast forward if pointer is not inside the column or the column zone
if (x < colX - gap - columnZoneMargin) {
continue;
}
if (colX + colW < x) {
continue;
}
const cx = colX - halfGap;
const l = cx - columnZoneMargin;
const r = cx + columnZoneMargin;
if (l <= x && x <= r) {
target = {
position: [j],
center: cx,
originProp: "x",
sizeProp: "width",
marginA: columnZoneMarginViz,
marginB: columnZoneMarginViz,
space,
actorParams: {
y: Main.layoutManager.panelBox.height,
height: tilingHeight,
},
};
break;
}
// Must be strictly within the column to tile vertically
if (x < colX)
continue;
// vertically tiled
for (let i = 0; i < column.length + 1; i++) {
let clone;
if (i < column.length) {
clone = column[i].clone;
} else {
let lastClone = column[i - 1].clone;
clone = {
targetX: lastClone.targetX,
targetY: lastClone.targetY + lastClone.height + gap,
width: lastClone.width,
height: 0,
};
}
const isFirst = i === 0;
const isLast = i === column.length;
const cy = clone.targetY - halfGap;
const t = cy - rowZoneMargin;
const b = cy + rowZoneMargin;
if (t <= y && y <= b) {
target = {
position: [j, i],
center: cy,
originProp: "y",
sizeProp: "height",
marginA: isFirst ? 0 : rowZoneMargin,
marginB: isLast ? 0 : rowZoneMargin,
space,
actorParams: {
x: clone.targetX,
width: clone.width,
},
};
break;
}
}
}
const sameTarget = (a, b) => {
if (a === b)
return true;
if (!a || !b)
return false;
return a.space === b.space && a.position[0] === b.position[0] && a.position[1] === b.position[1];
};
if (!sameTarget(target, this.dndTarget)) {
// has a new zone target
if (target) {
this.dndTargets.push(target);
}
this.dndTarget = null;
this.activateDndTarget(target, initial);
}
}
motion(actor, event) {
let metaWindow = this.window;
// let [gx, gy] = event.get_coords();
let [gx, gy, $] = global.get_pointer();
if (event.type() === Clutter.EventType.TOUCH_UPDATE) {
[gx, gy] = event.get_coords();
// We update global pointer to match touch event
Utils.warpPointer(gx, gy, false);
}
let [dx, dy] = this.pointerOffset;
let clone = metaWindow.clone;
let tx = clone.get_transition('x');
let ty = clone.get_transition('y');
if (this.dnd) {
if (tx) {
tx.set_to(gx - dx);
ty.set_to(gy - dy);
} else {
clone.x = gx - dx;
clone.y = gy - dy;
}
} else {
let monitor = Utils.monitorAtPoint(gx, gy);
if (monitor !== this.initialSpace.monitor) {
this.beginDnD();
return;
}
if (event.get_state() & Clutter.ModifierType.CONTROL_MASK) {
// NB: only works in wayland
this.beginDnD();
return;
}
let space = this.initialSpace;
let clone = metaWindow.clone;
let [x, y] = space.globalToViewport(gx, gy);
space.targetX = x - this.scrollAnchor;
space.cloneContainer.x = space.targetX;
clone.y = y - dy;
const threshold = 300;
dy = Math.min(threshold, Math.abs(clone.y - this.initialY));
let s = 1 - Math.pow(dy / 500, 3);
let actor = metaWindow.get_compositor_private();
actor.set_scale(s, s);
clone.set_scale(s, s);
if (dy >= threshold) {
this.beginDnD();
}
}
}
end() {
console.debug("#grab", "end");
this.signals.destroy();
this.signals = null;
let metaWindow = this.window;
let actor = metaWindow.get_compositor_private();
let clone = metaWindow.clone;
let [gx, gy, $] = global.get_pointer();
this.zoneActors.forEach(actor => actor.destroy());
let params = {
time: Settings.prefs.animation_time,
scale_x: 1,
scale_y: 1,
opacity: clone?.__oldOpacity ?? 255,
};
if (this.dnd) {
let dndTarget = this.dndTarget;
if (dndTarget) {
let space = dndTarget.space;
space.showSelection();
if (Scratch.isScratchWindow(metaWindow))
Scratch.unmakeScratch(metaWindow);
// Remember the global coordinates of the clone
let [x, y] = clone.get_position();
space.addWindow(metaWindow, ...dndTarget.position);
let [sx, sy] = space.globalToScroll(gx, gy);
let [dx, dy] = this.pointerOffset;
clone.x = sx - dx;
clone.y = sy - dy;
let newScale = clone.scale_x / space.actor.scale_x;
clone.set_scale(newScale, newScale);
actor.set_scale(1, 1);
actor.set_pivot_point(0, 0);
// Tiling.animateWindow(metaWindow);
params.onStopped = () => {
space.moveDone();
clone.set_pivot_point(0, 0);
};
Easer.addEase(clone, params);
space.targetX = space.cloneContainer.x;
space.selectedWindow = metaWindow;
if (dndTarget.position) {
space.layout(true, { customAllocators: { [dndTarget.position[0]]: Tiling.allocateEqualHeight } });
} else {
space.layout();
}
Tiling.move_to(space, metaWindow, { x: x - space.monitor.x });
Tiling.ensureViewport(metaWindow, space);
Utils.actor_raise(clone);
}
else {
metaWindow.move_frame(true, clone.x, clone.y);
Scratch.makeScratch(metaWindow);
this.initialSpace.moveDone();
this.initialSpace.showSelection();
actor.set_scale(clone.scale_x, clone.scale_y);
actor.opacity = clone.opacity;
clone.opacity = clone.__oldOpacity || 255;
clone.set_scale(1, 1);
clone.set_pivot_point(0, 0);
const halftime = 0.5 * Settings.prefs.animation_time;
params.time = halftime;
params.onComplete = () => {
Easer.addEase(actor, {
time: halftime,
onComplete: () => {
Scratch.unmakeScratch(metaWindow);
},
});
};
Easer.addEase(actor, params);
}
Navigator.getNavigator().accept();
}
else if (this.initialSpace.indexOf(metaWindow) !== -1) {
let space = this.initialSpace;
space.targetX = space.cloneContainer.x;
actor.set_scale(1, 1);
actor.set_pivot_point(0, 0);
Tiling.animateWindow(metaWindow);
params.onStopped = () => {
space.moveDone();
clone.set_pivot_point(0, 0);
};
Easer.addEase(clone, params);
Tiling.ensureViewport(metaWindow, space);
Navigator.getNavigator().accept();
}
// NOTE: we reset window here so `window-added` will handle the window,
// and layout will work correctly etc.
this.window = null;
this.initialSpace.layout();
// ensure window is properly activated after layout/ensureViewport tweens
Utils.later_add(Meta.LaterType.IDLE, () => {
Main.activateWindow(metaWindow);
});
// // Make sure the window is on the correct workspace.
// // If the window is transient this will take care of its parent too.
Tiling.setInGrab(false);
if (this.dispatcher) {
Navigator.dismissDispatcher(Clutter.GrabState.POINTER);
}
global.display.set_cursor(Meta.Cursor.DEFAULT);
/**
* Gnome 44 removed the ability to manually end_grab_op.
* Previously we would end the grab_op before doing
* PaperWM grabs. In 44, we can't do this so the grab op
* may still be in progress, which is okay, but won't be ended
* until we "click out". We do this here if needed.
*/
Utils.later_add(Meta.LaterType.IDLE, () => {
if (!global.display.end_grab_op && this.wasTiled) {
// move to current cursor position
let [x, y, _mods] = global.get_pointer();
getVirtualPointer().notify_absolute_motion(
Clutter.get_current_event_time(),
x, y);
getVirtualPointer().notify_button(Clutter.get_current_event_time(),
Clutter.BUTTON_PRIMARY, Clutter.ButtonState.PRESSED);
getVirtualPointer().notify_button(Clutter.get_current_event_time(),
Clutter.BUTTON_PRIMARY, Clutter.ButtonState.RELEASED);
}
});
}
activateDndTarget(zone, first) {
if (!zone) {
return;
}
const mkZoneActor = props => {
let actor = new St.Widget({ style_class: "tile-preview" });
actor.x = props.x ?? 0;
actor.y = props.y ?? 0;
actor.width = props.width ?? 0;
actor.height = props.height ?? 0;
return actor;
};
zone.actor = mkZoneActor({ ...zone.actorParams });
// deactivate previous target
this.dndTargets.filter(t => t !== zone).forEach(t => this.deactivateDndTarget(t));
this.dndTargets = [zone];
this.dndTarget = zone;
this.zoneActors.add(zone.actor);
const raise = () => Utils.actor_raise(zone.actor);
let params = {
time: Settings.prefs.animation_time,
[zone.originProp]: zone.center - zone.marginA,
[zone.sizeProp]: zone.marginA + zone.marginB,
onComplete: raise,
};
if (first) {
params.height = zone.actor.height;
params.y = zone.actor.y;
let clone = this.window.clone;
let space = zone.space;
let [x, y] = space.globalToScroll(...clone.get_transformed_position());
zone.actor.set_position(x, y);
zone.actor.set_size(...clone.get_transformed_size());
} else {
zone.actor[zone.sizeProp] = 0;
zone.actor[zone.originProp] = zone.center;
}
zone.space.cloneContainer.add_child(zone.actor);
zone.space.hideSelection();
zone.actor.show();
raise();
Easer.addEase(zone.actor, params);
}
deactivateDndTarget(zone) {
if (zone) {
zone.space.showSelection();
Easer.addEase(zone.actor, {
time: Settings.prefs.animation_time,
[zone.originProp]: zone.center,
[zone.sizeProp]: 0,
onComplete: () => {
zone.actor.destroy();
this.zoneActors.delete(zone.actor);
},
});
}
}
}
/**
* Resize grab class currently used to identify window grab type.
*/
export class ResizeGrab {
end() {
}
}