forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiling.js
3475 lines (2931 loc) · 114 KB
/
tiling.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Extension;
if (imports.misc.extensionUtils.extensions) {
Extension = imports.misc.extensionUtils.extensions["paperwm@hedning:matrix.org"];
} else {
Extension = imports.ui.main.extensionManager.lookup("paperwm@hedning:matrix.org");
}
var GLib = imports.gi.GLib;
var Tweener = Extension.imports.utils.tweener;
/** @type {import("@gi-types/meta")} */
var Meta = imports.gi.Meta;
/** @type {import("@gi-types/clutter10")} */
var Clutter = imports.gi.Clutter;
/** @type {import("@gi-types/st")} */
var St = imports.gi.St;
/** @type {import("@gi-types/st")} */
var Main = imports.ui.main;
/** @type {import("@gi-types/shell")} */
var Shell = imports.gi.Shell;
var Gio = imports.gi.Gio;
var Signals = imports.signals;
var utils = Extension.imports.utils;
var debug = utils.debug;
/** @type {import('@gi-types/meta').Stage} */
const stage = global.stage
var Gdk = imports.gi.Gdk;
/**@type {import('@gi-types/meta').WorkspaceManager} */
var workspaceManager = global.workspace_manager;
var display = global.display;
/** @type {Spaces} */
var spaces;
var Minimap = Extension.imports.minimap;
var Scratch = Extension.imports.scratch;
var Gestures = Extension.imports.gestures;
var TopBar = Extension.imports.topbar;
var Navigator = Extension.imports.navigator;
var ClickOverlay = Extension.imports.stackoverlay.ClickOverlay;
var Settings = Extension.imports.settings;
var Me = Extension.imports.tiling;
var prefs = Settings.prefs;
var backgroundSettings = new Gio.Settings({
schema_id: 'org.gnome.desktop.background'
})
var interfaceSettings = new Gio.Settings({
schema_id: "org.gnome.desktop.interface",
});
var borderWidth = 8;
// Mutter prevints windows from being placed further off the screen than 75 pixels.
var stack_margin = 75;
// Some features use this to determine if to sizes is considered equal. ie. `abs(w1 - w2) < sizeSlack`
var sizeSlack = 30;
var PreviewMode = {NONE: 0, STACK: 1, SEQUENTIAL: 2};
var inPreview = PreviewMode.NONE;
var signals, oldSpaces, backgroundGroup, oldMonitors, WindowCloneLayout,
grabSignals;
function init() {
// Symbol to retrieve the focus handler id
signals = new utils.Signals();
grabSignals = new utils.Signals();
oldSpaces = new Map();
oldMonitors = new Map();
backgroundGroup = Main.layoutManager._backgroundGroup;
}
/**
Scrolled and tiled per monitor workspace.
The tiling is composed of an array of columns. A column being an array of
MetaWindows. Ie. the type being [[MetaWindow]].
A Space also contains a visual representation of the tiling. The structure is
currently like this:
A @clip actor which spans the monitor and clips all its contents to the
monitor. The clip lives along side all other space's clips in an actor
spanning the whole global.workspaceManager
An @actor to hold everything that's visible, it contains a @background,
a @label and a @cloneContainer.
The @cloneContainer holds clones of all the tiled windows, it's clipped
by @cloneClip to avoid protruding into neighbouringing monitors.
The @border surrounds the outside of the monitor so is only visible when
using the workspace carousel.
Clones are necessary due to restrictions mutter places on MetaWindowActors.
WindowActors can only live in the `global.window_group` and can't be
moved reliably outside the monitor. We create a Clutter.Clone for every window which
live in @cloneContainer to avoid these problems. Scrolling to a window in
the tiling is then done by simply moving the @cloneContainer.
While eg. animating the cloneContainer WindowActors are all hidden, while the
clones are shown. When animation is done, the MetaWindows are moved to their
correct position and the WindowActors are shown.
The clones are also useful when constructing the workspace stack as it's
easier to scale and move the whole @actor in one go.
# Coordinate system
MetaWindows live in the stage (global) coordinate system. NB: This system
covers all monitors - a window positioned top-left in a monitor might have
non-zero coordinates.
The space (technically the @clip) has it's own coordinate system relative to
its monitor. Ie. 0,0 is the top-left corner of the monitor.
To transform a stage point to space coordinates: `space.actor.transform_stage_point(aX, aY)`
*/
var Space = class Space extends Array {
/** @type {import('@gi-types/clutter10').Actor} */
actor
/** @type {import('@gi-types/meta').BackgroundActor} */
background
constructor (workspace, container, doInit) {
super(0);
this.workspace = workspace;
this.signals = new utils.Signals();
// The windows that should be represented by their WindowActor
this.visible = [];
this._floating = [];
this._populated = false;
let clip = new Clutter.Actor({name: "clip"});
this.clip = clip;
let actor = new Clutter.Actor({name: "space-actor"});
this._visible = true;
this.hide(); // We keep the space actor hidden when inactive due to performance
this.actor = actor;
let cloneClip = new Clutter.Actor({name: "clone-clip"});
this.cloneClip = cloneClip;
let cloneContainer = new St.Widget({name: "clone-container"});
this.cloneContainer = cloneContainer;
// Pick up the same css as the top bar label
let label = new St.Label();
let labelParent = new St.Widget({name: 'panel'});
let labelParent2 = new St.Widget({style_class: 'panel-button'});
for (let p of [labelParent, labelParent2]) {
p.style = `
background-color: transparent;
border-image: none;
background-image: none;
border: none;
`;
}
labelParent.add_actor(labelParent2);
labelParent2.add_actor(label);
this.label = label;
label.hide();
let selection = new St.Widget({name: 'selection',
style_class: 'paperwm-selection tile-preview'});
this.selection = selection;
clip.space = this;
cloneContainer.space = this;
container.add_actor(clip);
clip.add_actor(actor);
actor.add_actor(labelParent);
actor.add_actor(cloneClip);
cloneClip.add_actor(cloneContainer);
this.border = new St.Widget({name: "border"});
this.actor.add_actor(this.border);
this.border.hide();
let monitor = Main.layoutManager.primaryMonitor;
let oldSpace = oldSpaces.get(workspace);
this.targetX = 0;
if (oldSpace && oldSpace.monitor) {
let oldMonitor = Main.layoutManager.monitors[oldSpace.monitor.index];
if (oldMonitor)
monitor = oldMonitor;
}
this.setSettings(Settings.getWorkspaceSettings(this.workspace.index()));
this.setMonitor(monitor, false);
actor.set_pivot_point(0.5, 0);
this.selectedWindow = null;
this.leftStack = 0; // not implemented
this.rightStack = 0; // not implemented
if (doInit)
this.init();
}
init() {
if (this._populated || Main.layoutManager._startingUp)
return;
let workspace = this.workspace;
let oldSpace = oldSpaces.get(workspace);
this.addAll(oldSpace);
oldSpaces.delete(workspace);
this._populated = true;
// FIXME: this prevents bad old values propagating
// Though, targetX shouldn't ideally be able to get into this state.
if (oldSpace && Number.isFinite(oldSpace.targetX)) {
this.targetX = oldSpace.targetX;
}
this.cloneContainer.x = this.targetX;
this.getWindows().forEach(w => {
animateWindow(w);
});
this.layout(false);
let selected = this.selectedWindow;
if (selected) {
ensureViewport(selected, this, true);
}
this.signals.connect(workspace, "window-added",
utils.dynamic_function_ref("add_handler", Me));
this.signals.connect(workspace, "window-removed",
utils.dynamic_function_ref("remove_handler", Me));
this.signals.connect(Main.overview, 'showing',
this.startAnimate.bind(this));
this.signals.connect(Main.overview, 'hidden', this.moveDone.bind(this));
const Convenience = Extension.imports.convenience;
const settings = Convenience.getSettings();
this.signals.connect(
interfaceSettings,
"changed::color-scheme",
this.updateBackground.bind(this)
);
this.signals.connect(settings, 'changed::default-background',
this.updateBackground.bind(this));
this.signals.connect(settings, 'changed::use-default-background',
this.updateBackground.bind(this));
this.signals.connect(backgroundSettings, 'changed::picture-uri',
this.updateBackground.bind(this));
this.signals.connect(
backgroundSettings,
"changed::picture-uri-dark",
this.updateBackground.bind(this)
);
}
show() {
if (this._visible)
return;
this._visible = true;
this.clip.show();
for (let col of this) {
for (let w of col) {
let actor = w.get_compositor_private();
w.clone.cloneActor.source = actor;
}
}
}
hide() {
if (!this._visible)
return;
this._visible = false;
this.clip.hide();
for (let col of this)
for (let w of col)
w.clone.cloneActor.source = null;
}
workArea() {
let workArea = Main.layoutManager.getWorkAreaForMonitor(this.monitor.index);
workArea.x -= this.monitor.x;
workArea.y -= this.monitor.y;
workArea.height -= prefs.vertical_margin + prefs.vertical_margin_bottom;
workArea.y += prefs.vertical_margin;
return workArea;
}
layoutGrabColumn(column, x, y0, targetWidth, availableHeight, time, grabWindow) {
let space = this;
let needRelayout = false;
function mosh(windows, height, y0) {
let targetHeights = fitProportionally(
windows.map(mw => mw.get_frame_rect().height),
height
);
let [w, relayout, y] = space.layoutColumnSimple(windows, x, y0, targetWidth, targetHeights, time);
needRelayout = needRelayout || relayout;
return y;
}
const k = column.indexOf(grabWindow);
if (k < 0) {
throw new Error("Anchor doesn't exist in column " + grabWindow.title);
}
const gap = prefs.window_gap;
const f = grabWindow.get_frame_rect();
let yGrabRel = f.y - this.monitor.y;
targetWidth = f.width;
const H1 = (yGrabRel - y0) - gap - (k-1)*gap;
const H2 = availableHeight - (yGrabRel + f.height - y0) - gap - (column.length-k-2)*gap;
k > 0 && mosh(column.slice(0, k), H1, y0);
let y = mosh(column.slice(k, k+1), f.height, yGrabRel);
k+1 < column.length && mosh(column.slice(k+1), H2, y);
return [targetWidth, needRelayout];
}
layoutColumnSimple(windows, x, y0, targetWidth, targetHeights, time) {
let space = this;
let y = y0;
let widthChanged = false;
let heightChanged = false;
// log("Layout column simple")
for (let i = 0; i < windows.length; i++) {
let mw = windows[i];
let targetHeight = targetHeights[i];
let f = mw.get_frame_rect();
let resizable = !mw.fullscreen &&
mw.get_maximized() !== Meta.MaximizeFlags.BOTH;
if (resizable) {
const hasNewTarget = mw._targetWidth !== targetWidth || mw._targetHeight !== targetHeight;
const targetReached = f.width === targetWidth && f.height === targetHeight;
// Update targets (NB: must happen before resize request)
mw._targetWidth = targetWidth;
mw._targetHeight = targetHeight;
if (!targetReached && hasNewTarget) {
// Explanation for `hasNewTarget` check in commit message
mw.move_resize_frame(true, f.x, f.y, targetWidth, targetHeight);
}
} else {
mw.move_frame(true, space.monitor.x, space.monitor.y);
targetWidth = f.width;
targetHeight = f.height;
}
if (mw.maximized_vertically) {
// NOTE: This should really be f.y - monitor.y, but eg. firefox
// on wayland reports the wrong y coordinates at this point.
y = y - prefs.vertical_margin;
}
// When resize is synchronous, ie. for X11 windows
let nf = mw.get_frame_rect();
if (nf.width !== targetWidth && nf.width !== f.width) {
// log(" Width did not obey", "new", nf.width, "old", f.width, "target", targetWidth, mw.title)
widthChanged = true;
}
if (nf.height !== targetHeight && nf.height !== f.height) {
// log(" Height did not obey", "new", nf.height, "old", f.height, "target", targetHeight, mw.title);
heightChanged = true;
targetHeight = nf.height; // Use actually height for layout
}
let c = mw.clone;
if (c.x !== x || c.targetX !== x ||
c.y !== y || c.targetY !== y) {
// log(" Position window", mw.title, `y: ${c.targetY} -> ${y} x: ${c.targetX} -> ${x}`);
c.targetX = x;
c.targetY = y;
if (time === 0) {
c.x = x;
c.y = y;
} else {
Tweener.addTween(c, {
x, y,
time,
onComplete: this.moveDone.bind(this)
});
}
}
y += targetHeight + prefs.window_gap;
}
return [targetWidth, widthChanged || heightChanged, y];
}
layout(animate = true, options={}) {
// Guard against recursively calling layout
if (!this._populated)
return;
if (this._inLayout)
return;
this._inLayout = true;
this.startAnimate();
let time = animate ? prefs.animation_time : 0;
if (window.instant) {
time = 0;
}
let gap = prefs.window_gap;
let x = 0;
let selectedIndex = this.selectedIndex();
let workArea = this.workArea();
// Happens on monitors-changed
if (workArea.width === 0) {
this._inLayout = false;
return;
}
let availableHeight = workArea.height;
let y0 = workArea.y;
let fixPointAttempCount = 0;
for (let i=0; i<this.length; i++) {
let column = this[i];
// Actorless windows are trouble. Layout could conceivable run while a window is dying or being born.
column = column.filter(mw => mw.get_compositor_private());
if (column.length === 0)
continue;
let selectedInColumn = i === selectedIndex ? this.selectedWindow : null;
let targetWidth;
if (i === selectedIndex) {
targetWidth = selectedInColumn.get_frame_rect().width;
} else {
targetWidth = Math.max(...column.map(w => w.get_frame_rect().width));
}
targetWidth = Math.min(targetWidth, workArea.width - 2*prefs.minimum_margin);
let resultingWidth, relayout;
let allocator = options.customAllocators && options.customAllocators[i];
if (inGrab && column.includes(inGrab.window) && !allocator) {
[resultingWidth, relayout] =
this.layoutGrabColumn(column, x, y0, targetWidth, availableHeight, time,
selectedInColumn);
} else {
allocator = allocator || allocateDefault;
let targetHeights = allocator(column, availableHeight, selectedInColumn);
[resultingWidth, relayout] =
this.layoutColumnSimple(column, x, y0, targetWidth, targetHeights, time);
}
if (relayout) {
if (fixPointAttempCount < 5) {
i--;
fixPointAttempCount++;
continue;
} else {
log("Bail at fixpoint, max tries reached")
}
}
x += resultingWidth + gap;
}
this._inLayout = false;
let oldWidth = this.cloneContainer.width;
let min = workArea.x;
let auto = (this.targetX + oldWidth >= min + workArea.width && this.targetX <= 0)
|| this.targetX === min + Math.round((workArea.width - oldWidth)/2);
// transforms break on width 1
let width = Math.max(1, x - gap);
this.cloneContainer.width = width;
if (auto && animate) {
if (width < workArea.width) {
this.targetX = min + Math.round((workArea.width - width)/2);
} else if (this.targetX + width < min + workArea.width) {
this.targetX = min + workArea.width - width;
} else if (this.targetX > workArea.min ) {
this.targetX = workArea.x;
}
Tweener.addTween(this.cloneContainer,
{ x: this.targetX,
time,
onComplete: this.moveDone.bind(this)
});
}
if (animate) {
ensureViewport(this.selectedWindow, this);
} else {
this.moveDone();
}
this.emit('layout', this);
}
queueLayout() {
if (this._layoutQueued)
return;
this._layoutQueued = true;
Meta.later_add(Meta.LaterType.RESIZE, () => {
this._layoutQueued = false;
this.layout();
});
}
// Space.prototype.isVisible = function
isVisible(metaWindow, margin=0) {
let clone = metaWindow.clone;
let x = clone.x + this.cloneContainer.x;
let workArea = this.workArea();
let min = workArea.x;
if (x - margin + clone.width < min
|| x + margin > min + workArea.width) {
return false;
} else {
return true;
}
}
isFullyVisible(metaWindow) {
let clone = metaWindow.clone;
let x = clone.targetX + this.targetX;
let workArea = this.workArea();
let min = workArea.x;
return min <= x && x + clone.width < min + workArea.width;
}
visibleRatio(metaWindow) {
let clone = metaWindow.clone;
let x = clone.targetX + this.targetX;
let workArea = this.workArea();
let min = workArea.x;
let left = min - x
let right = x + clone.width
return min <= x && x + clone.width < min + workArea.width;
}
isPlaceable(metaWindow) {
let clone = metaWindow.clone;
let x = clone.targetX + this.targetX;
let workArea = Main.layoutManager.getWorkAreaForMonitor(this.monitor.index);
let min = workArea.x - this.monitor.x;
if (x + clone.width < min + stack_margin
|| x > min + workArea.width - stack_margin) {
return false;
} else {
// Fullscreen windows are only placeable on the monitor origin
if ((metaWindow.get_maximized() === Meta.MaximizeFlags.BOTH && x !== min) ||
(metaWindow.fullscreen && x !== 0)) {
return false;
}
return true;
}
}
getWindows() {
return this.reduce((ws, column) => ws.concat(column), []);
}
getWindow(index, row) {
if (row < 0 || index < 0 || index >= this.length)
return false;
let column = this[index];
if (row >= column.length)
return false;
return column[row];
}
isWindowAtPoint(metaWindow, x, y) {
let clone = metaWindow.clone;
let wX = clone.x + this.cloneContainer.x;
return x >= wX && x <= wX + clone.width &&
y >= clone.y && y <= clone.y + clone.height;
}
getWindowAtPoint(x, y) {
for (let column of this) {
for (let w of column) {
if (this.isWindowAtPoint(w, x, y))
return w;
}
}
return null;
}
addWindow(metaWindow, index, row) {
if (!this.selectedWindow)
this.selectedWindow = metaWindow;
if (this.indexOf(metaWindow) !== -1)
return false;
if (row !== undefined && this[index]) {
let column = this[index];
column.splice(row, 0, metaWindow);
} else {
this.splice(index, 0, [metaWindow]);
}
/*
* 3.35+ has a bug where move_frame sometimes triggers another move back to its original position. Make sure tiled windows are always positioned correctly.
*/
this.signals.connect(metaWindow, 'position-changed', (w) => {
if (inGrab)
return
let f = w.get_frame_rect();
let clone = w.clone;
let x = clone.targetX + this.targetX;
let y = this.monitor.y + clone.targetY;
x = Math.min(this.width - stack_margin, Math.max(stack_margin - f.width, x));
x += this.monitor.x;
if (f.x !== x || f.y !== y) {
w.move_frame(true, x, y);
}
});
metaWindow.clone.reparent(this.cloneContainer);
// Make sure the cloneContainer is in a clean state (centered) before layout
if (this.length === 1) {
let workArea = this.workArea();
this.targetX = workArea.x + Math.round((workArea.width - this.cloneContainer.width)/2);
}
this.emit('window-added', metaWindow, index, row);
return true;
}
removeWindow(metaWindow) {
let index = this.indexOf(metaWindow);
if (index === -1)
return this.removeFloating(metaWindow);
this.signals.disconnect(metaWindow);
let selected = this.selectedWindow;
if (selected === metaWindow) {
// Select a new window using the stack ordering;
let windows = this.getWindows();
let i = windows.indexOf(metaWindow);
let neighbours = [windows[i - 1], windows[i + 1]].filter(w => w);
let stack = sortWindows(this, neighbours);
selected = stack[stack.length - 1];
}
let column = this[index];
let row = column.indexOf(metaWindow);
column.splice(row, 1);
if (column.length === 0)
this.splice(index, 1);
this.visible.splice(this.visible.indexOf(metaWindow), 1);
let clone = metaWindow.clone;
this.cloneContainer.remove_actor(clone);
// Don't destroy the selection highlight widget
if (clone.first_child.name === 'selection')
clone.remove_actor(clone.first_child);
let actor = metaWindow.get_compositor_private();
if (actor)
actor.remove_clip();
this.layout();
if (selected) {
ensureViewport(selected, this);
} else {
this.selectedWindow = null;
}
this.emit('window-removed', metaWindow, index, row);
return true;
}
isFloating(metaWindow) {
return this._floating.indexOf(metaWindow) !== -1;
}
addFloating(metaWindow) {
if (this._floating.indexOf(metaWindow) !== -1 ||
metaWindow.is_on_all_workspaces())
return false;
this._floating.push(metaWindow);
let clone = metaWindow.clone;
clone.reparent(this.actor);
return true;
}
removeFloating(metaWindow) {
let i = this._floating.indexOf(metaWindow);
if (i === -1)
return false;
this._floating.splice(i, 1);
this.actor.remove_actor(metaWindow.clone);
return true;
}
swap(direction, metaWindow) {
metaWindow = metaWindow || this.selectedWindow;
let [index, row] = this.positionOf(metaWindow);
let targetIndex = index;
let targetRow = row;
switch (direction) {
case Meta.MotionDirection.LEFT:
targetIndex--;
break;
case Meta.MotionDirection.RIGHT:
targetIndex++;
break;
case Meta.MotionDirection.DOWN:
targetRow++;
break;
case Meta.MotionDirection.UP:
targetRow--;
break;
}
let column = this[index];
if (targetIndex < 0 || targetIndex >= this.length
|| targetRow < 0 || targetRow >= column.length)
return;
utils.swap(this[index], row, targetRow);
utils.swap(this, index, targetIndex);
this.layout();
this.emit('swapped', index, targetIndex, row, targetRow);
ensureViewport(this.selectedWindow, this, true);
}
switchLinear(dir) {
let index = this.selectedIndex();
let column = this[index];
if (!column)
return false;
let row = column.indexOf(this.selectedWindow);
if (utils.in_bounds(column, row + dir) == false) {
index += dir;
if (dir === 1) {
if (index < this.length) row = 0;
} else {
if (index >= 0)
row = this[index].length - 1
}
} else {
row += dir;
}
let metaWindow = this.getWindow(index, row);
ensureViewport(metaWindow, this);
return true;
}
switchLeft() { this.switch(Meta.MotionDirection.LEFT) }
switchRight() { this.switch(Meta.MotionDirection.RIGHT) }
switchUp() { this.switch(Meta.MotionDirection.UP) }
switchDown() { this.switch(Meta.MotionDirection.DOWN) }
switch(direction) {
let space = this;
let index = space.selectedIndex();
if (index === -1) {
return;
}
let row = space[index].indexOf(space.selectedWindow);
switch (direction) {
case Meta.MotionDirection.RIGHT:
index++;
row = -1;
break;;
case Meta.MotionDirection.LEFT:
index--;
row = -1;
}
if (index < 0 || index >= space.length)
return;
let column = space[index];
if (row === -1) {
let selected =
sortWindows(this, column)[column.length - 1];
row = column.indexOf(selected);
}
switch (direction) {
case Meta.MotionDirection.UP:
row--;
break;;
case Meta.MotionDirection.DOWN:
row++;
}
if (row < 0 || row >= column.length)
return;
let metaWindow = space.getWindow(index, row);
ensureViewport(metaWindow, space);
}
positionOf(metaWindow) {
metaWindow = metaWindow || this.selectedWindow;
let index, row;
for (let i=0; i < this.length; i++) {
if (this[i].includes(metaWindow))
return [i, this[i].indexOf(metaWindow)];
}
return false;
}
indexOf(metaWindow) {
for (let i=0; i < this.length; i++) {
if (this[i].includes(metaWindow))
return i;
}
return -1;
}
rowOf(metaWindow) {
let column = this[this.indexOf(metaWindow)];
return column.indexOf(metaWindow);
}
globalToViewport(gx, gy) {
let [ok, vx, vy] = this.actor.transform_stage_point(gx, gy);
return [Math.round(vx), Math.round(vy)];
}
/** Transform global coordinates to scroll cooridinates (cloneContainer relative) */
globalToScroll(gx, gy, {useTarget = false} = {}) {
// Use the smart transform on the actor, as that's the one we scale etc.
// We can then use straight translation on the scroll which makes it possible to use target instead if wanted.
let [vx, vy] = this.globalToViewport(gx, gy);
let sx = vx - (useTarget ? this.targetX : this.cloneContainer.x);
let sy = vy - this.cloneContainer.y;
return [Math.round(sx), Math.round(sy)];
}
viewportToScroll(vx, vy=0) {
return [vx - this.cloneContainer.x, vy - this.cloneContainer.y];
}
moveDone() {
if (this.cloneContainer.x !== this.targetX ||
this.actor.y !== 0 ||
Navigator.navigating || inPreview ||
Main.overview.visible ||
// Block when we're carrying a window in dnd
(inGrab && inGrab.window)
) {
return;
}
this.visible = [];
const monitor = this.monitor;
this.getWindows().forEach(w => {
let actor = w.get_compositor_private();
if (!actor)
return;
let placeable = this.isPlaceable(w);
if (placeable)
this.visible.push(w);
// Guard against races between move_to and layout
// eg. moving can kill ongoing resize on wayland
if (Tweener.isTweening(w.clone))
return;
let unMovable = w.fullscreen ||
w.get_maximized() === Meta.MaximizeFlags.BOTH;
if (unMovable)
return;
let clone = w.clone;
let f = w.get_frame_rect();
let x = clone.targetX + this.targetX;
let y = monitor.y + clone.targetY;
x = Math.max(stack_margin - f.width, x);
x = Math.min(this.width - stack_margin, x);
x += monitor.x;
// let b = w.get_frame_rect();
if ((f.x !== x || f.y !== y)) {
w.move_frame(true, x, y);
}
});
this.visible.forEach(w => {
if (Tweener.isTweening(w.clone))
return;
let actor = w.get_compositor_private();
// The actor's width/height is not correct right after resize
let b = w.get_buffer_rect();
const x = monitor.x - b.x;
const y = monitor.y - b.y;
const cw = monitor.width;
const ch = monitor.height;
actor.set_clip(x, y, cw, ch);
showWindow(w);
});
this._floating.forEach(showWindow);
this.fixOverlays();
if (!Meta.is_wayland_compositor()) {
// See startAnimate
Main.layoutManager.untrackChrome(this.background);
}
this._isAnimating = false;
if (this.selectedWindow && this.selectedWindow === display.focus_window) {
let index = this.indexOf(this.selectedWindow);
this[index].forEach(w => w.lastFrame = w.get_frame_rect());
}
this.emit('move-done');
}
startAnimate(grabWindow) {
if (!this._isAnimating && !Meta.is_wayland_compositor()) {
// Tracking the background fixes issue #80
// It also let us activate window clones clicked during animation
// Untracked in moveDone
Main.layoutManager.trackChrome(this.background);
}
this.visible.forEach(w => {
let actor = w.get_compositor_private();
if (!actor)
return;
actor.remove_clip();
if (inGrab && inGrab.window === w)
return;
animateWindow(w);
});
this._floating.forEach(w => {
let f = w.get_frame_rect();
if (!animateWindow(w))
return;
w.clone.x = f.x - this.monitor.x;
w.clone.y = f.y - this.monitor.y;
});
this._isAnimating = true;
}
fixOverlays(metaWindow) {
metaWindow = metaWindow || this.selectedWindow;
let index = this.indexOf(metaWindow);
let target = this.targetX;
this.monitor.clickOverlay.reset();
for (let overlay = this.monitor.clickOverlay.right,
n=index+1 ; n < this.length; n++) {
let metaWindow = this[n][0];
let clone = metaWindow.clone;
let x = clone.targetX + target;
if (!overlay.target && x + clone.width > this.width) {
overlay.setTarget(this, n);
break;
}
}
for (let overlay = this.monitor.clickOverlay.left,
n=index-1; n >= 0; n--) {
let metaWindow = this[n][0];
let clone = metaWindow.clone;
let x = clone.targetX + target;
if (!overlay.target && x < 0) {
overlay.setTarget(this, n);
break;
}
}
}
setSelectionActive() {
this.selection.opacity = 255;
}