forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiling.js
4499 lines (3846 loc) · 145 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
import Clutter from 'gi://Clutter';
import GDesktopEnums from 'gi://GDesktopEnums';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
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, Lib, Gestures, Navigator, Grab, Topbar, Scratch, Stackoverlay, Background
} from './imports.js';
import { Easer } from './utils.js';
import { ClickOverlay } from './stackoverlay.js';
import { WorkspaceSettings } from './workspace.js';
const { signals: Signals } = imports;
const workspaceManager = global.workspace_manager;
const display = global.display;
/** @type {Spaces} */
export let spaces;
let borderWidth = 8;
// Mutter prevints windows from being placed further off the screen than 75 pixels.
export const stack_margin = 75;
// Some features use this to determine if to sizes is considered equal. ie. `abs(w1 - w2) < sizeSlack`
let sizeSlack = 30;
export const PreviewMode = { NONE: 0, STACK: 1, SEQUENTIAL: 2 }; // export
export let inPreview = PreviewMode.NONE; // export
// DEFAULT mode is normal/original PaperWM window focus behaviour
export const FocusModes = { DEFAULT: 0, CENTER: 1 }; // export
export const CycleWindowSizesDirection = { FORWARD: 0, BACKWARDS: 1 };
/**
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)`
*/
let signals, backgroundGroup, grabSignals;
let gsettings, backgroundSettings, interfaceSettings;
let displayConfig;
let saveState;
let startupTimeoutId, timerId;
let workspaceSettings;
export let inGrab;
export function enable(extension) {
inGrab = false;
displayConfig = new Utils.DisplayConfig();
saveState = saveState ?? new SaveState();
gsettings = extension.getSettings();
backgroundSettings = new Gio.Settings({
schema_id: 'org.gnome.desktop.background',
});
interfaceSettings = new Gio.Settings({
schema_id: "org.gnome.desktop.interface",
});
signals = new Utils.Signals();
grabSignals = new Utils.Signals();
let setVerticalMargin = () => {
let vMargin = gsettings.get_int('vertical-margin');
let gap = gsettings.get_int('window-gap');
Settings.prefs.vertical_margin = Math.max(Math.round(gap / 2), vMargin);
};
setVerticalMargin();
// setup actions on gap changes
let onWindowGapChanged = () => {
setVerticalMargin();
Utils.timeout_remove(timerId);
timerId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500, () => {
spaces.mru().forEach(space => {
space.layout();
});
timerId = null;
return false; // on return false destroys timeout
});
};
gsettings.connect('changed::vertical-margin', onWindowGapChanged);
gsettings.connect('changed::vertical-margin-bottom', onWindowGapChanged);
gsettings.connect('changed::window-gap', onWindowGapChanged);
backgroundGroup = Main.layoutManager._backgroundGroup;
workspaceSettings = new WorkspaceSettings(extension);
spaces = new Spaces();
let initWorkspaces = () => {
try {
spaces.init();
} catch (e) {
console.error(e);
}
// Fix the stack overlay
spaces.mru().reverse().forEach(s => {
// if s.selectedWindow exists and is in view, then use option moveto: false
if (s.selectedWindow) {
let options = s.isFullyVisible(s.selectedWindow) ? { moveto: false } : { force: true };
ensureViewport(s.selectedWindow, s, options);
}
s.monitor.clickOverlay.show();
});
Topbar.fixTopBar();
// on idle update space topbar elements and name
Utils.later_add(Meta.LaterType.IDLE, () => {
spaces.forEach(s => {
s.setSpaceTopbarElementsVisible();
s.updateName();
});
});
};
if (Main.layoutManager._startingUp) {
// Defer workspace initialization until existing windows are accessible.
// Otherwise we're unable to restore the tiling-order. (when restarting
// gnome-shell)
signals.connectOneShot(Main.layoutManager, 'startup-complete',
() => displayConfig.upgradeGnomeMonitors(initWorkspaces));
} else {
/**
* Upgrade gnome monitor info objects by add "connector" information, and
* when done (async) callback to initworkspaces.
*/
// NOTE: this should happen after Patches.enable() have run, so we do
// it in a timeout
startupTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 0, () => {
displayConfig.upgradeGnomeMonitors(initWorkspaces);
startupTimeoutId = null;
return false; // on return false destroys timeout
});
}
}
export function disable () {
Utils.timeout_remove(startupTimeoutId);
startupTimeoutId = null;
Utils.timeout_remove(timerId);
timerId = null;
grabSignals.destroy();
grabSignals = null;
signals.destroy();
signals = null;
saveState.prepare();
displayConfig.downgradeGnomeMonitors();
displayConfig = null;
spaces.destroy();
inGrab = null;
gsettings = null;
backgroundGroup = null;
backgroundSettings = null;
interfaceSettings = null;
workspaceSettings = null;
}
/**
* Exported inGrab is read-only from other modules.
* This method allows other modules to change inGrab.
*/
export function setInGrab(value) {
inGrab = value;
}
export 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();
// windows that should be represented by their WindowActor
this.visible = [];
this._floating = [];
this._populated = false;
// default focusMode (can be overriden by saved user pref in Space.init method)
this.focusMode = FocusModes.DEFAULT;
this.focusModeIcon = new Topbar.FocusIcon({
name: 'panel',
style_class: 'space-focus-mode-icon',
})
.setClickFunction(() => {
switchToNextFocusMode(this);
})
.setVisible(false); // hide by default
this.showFocusModeIcon();
this.unfocusXPosition = null; // init
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;
let workspaceIndicator = new St.Widget({
reactive: true,
name: 'panel',
style_class: 'space-workspace-indicator',
});
workspaceIndicator.connect('button-press-event', () => Main.overview.toggle());
this.workspaceIndicator = workspaceIndicator;
let workspaceLabel = new St.Label();
workspaceIndicator.add_actor(workspaceLabel);
this.workspaceLabel = workspaceLabel;
workspaceLabel.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(workspaceIndicator);
actor.add_child(this.focusModeIcon);
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 prevSpace = saveState.prevSpaces.get(workspace);
this.targetX = 0;
if (prevSpace && prevSpace.monitor) {
let prevMonitor = Main.layoutManager.monitors[prevSpace.monitor.index];
if (prevMonitor)
monitor = prevMonitor;
}
this.setSettings(workspaceSettings.getWorkspaceSettings(this.index));
actor.set_pivot_point(0.5, 0);
this.selectedWindow = null;
this.leftStack = 0; // not implemented
this.rightStack = 0; // not implemented
this.windowPositionBarBackdrop = new St.Widget({
name: 'windowPositionBarBackdrop',
style_class: 'paperwm-window-position-bar-backdrop',
});
this.windowPositionBar = new St.Widget({
name: 'windowPositionBar',
style_class: 'paperwm-window-position-bar tile-preview',
});
this.windowPositionBar.hide(); // default on empty space
Utils.actor_raise(this.windowPositionBar);
if (Settings.prefs.show_window_position_bar) {
this.enableWindowPositionBar();
}
// now set monitor for this space
this.setMonitor(monitor);
if (doInit)
this.init();
}
init() {
if (this._populated || Main.layoutManager._startingUp)
return;
let workspace = this.workspace;
let prevSpace = saveState.getPrevSpaceByUUID(this.uuid);
console.info(`restore by uuid: ${this.uuid}, prevSpace name: ${prevSpace?.name}`);
this.addAll(prevSpace);
saveState.prevSpaces.delete(workspace);
this._populated = true;
// init window position bar and space topbar elements
this.windowPositionBarBackdrop.height = Topbar.panelBox.height;
this.setSpaceTopbarElementsVisible();
// apply default focus mode
setFocusMode(getDefaultFocusMode(), this);
this.getWindows().forEach(w => {
animateWindow(w);
});
this.layout(false);
this.signals.connect(workspace, "window-added", (ws, metawindow) => add_handler(ws, metawindow));
this.signals.connect(workspace, "window-removed", (ws, metawindow) => remove_handler(ws, metawindow));
this.signals.connect(Main.overview, 'showing', this.startAnimate.bind(this));
this.signals.connect(Main.overview, 'hidden', () => {
if (!spaces.isActiveSpace(this)) {
return;
}
// warp pointer to monitor that is active
Utils.warpPointerToMonitor(this.monitor);
Utils.later_add(Meta.LaterType.IDLE, () => {
this.moveDone(() => {
ensureViewport(display.focus_window, this, {
moveto: true,
force: true,
ensureAnimation: Settings.prefs.overview_ensure_viewport_animation,
});
});
});
});
this.signals.connect(gsettings, 'changed::default-focus-mode', () => {
setFocusMode(getDefaultFocusMode(), this);
});
// update space elements when in/out of fullscreen
this.signals.connect(global.display, 'in-fullscreen-changed', () => {
this.setSpaceTopbarElementsVisible(true);
});
this.signals.connect(interfaceSettings, "changed::color-scheme", this.updateBackground.bind(this));
this.signals.connect(gsettings, 'changed::default-background', this.updateBackground.bind(this));
this.signals.connect(gsettings, '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));
}
/**
* Returns the space index (which is equivalent to the workspace index).
*/
get index() {
return this.workspace.index();
}
/**
* Activates this space. Safer alternative to space.workspace.activate. Also allows
* setting animation on workspaceSwitch.
* @param {Boolean} animate
*/
activate(defaultAnimation = true, paperwmAnimation = false) {
spaces.space_defaultAnimation = defaultAnimation;
spaces.space_paperwmAnimation = paperwmAnimation;
this.workspace.activate(global.get_current_time());
spaces.space_defaultAnimation = true;
spaces.space_paperwmAnimation = false; // switch to default
}
/**
* Activates this space. Safer alternative to space.workspace.activate_with_focus. Also allows
* setting animation on workspaceSwitch.
* @param {Boolean} animate
*/
activateWithFocus(metaWindow, defaultAnimation = true, paperwmAnimation = false) {
spaces.space_defaultAnimation = defaultAnimation;
spaces.space_paperwmAnimation = paperwmAnimation;
if (metaWindow) {
this.workspace.activate_with_focus(metaWindow, global.get_current_time());
}
else {
this.workspace.activate(global.get_current_time());
}
spaces.space_defaultAnimation = true;
spaces.space_paperwmAnimation = false; // switch to default
}
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 -= Settings.prefs.vertical_margin + Settings.prefs.vertical_margin_bottom;
workArea.y += Settings.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 = Settings.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;
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 (mw.preferredWidth) {
let prop = mw.preferredWidth;
if (prop.value <= 0) {
console.warn("invalid preferredWidth value");
}
else if (prop.unit === 'px') {
targetWidth = prop.value;
}
else if (prop.unit === '%') {
let availableWidth = space.workArea().width - Settings.prefs.horizontal_margin * 2 - Settings.prefs.window_gap;
targetWidth = Math.floor(availableWidth * Math.min(prop.value / 100.0, 1.0));
}
else {
console.warn("invalid preferredWidth unit:", `'${prop.unit}'`, "(should be 'px' or '%')");
}
}
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 -= Settings.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) {
widthChanged = true;
}
if (nf.height !== targetHeight && nf.height !== f.height) {
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) {
// console.debug(" 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 {
Easer.addEase(c, {
x, y,
time,
onComplete: this.moveDone.bind(this),
});
}
}
y += targetHeight + Settings.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;
// option properties
let ensure = options?.ensure ?? true;
let allocators = options?.customAllocators;
this._inLayout = true;
this.startAnimate();
let time = Settings.prefs.animation_time;
let gap = Settings.prefs.window_gap;
let x = gap; // init (ensures autostart apps in particular start properly gapped)
let selectedIndex = this.selectedIndex();
let workArea = this.workArea();
// Happens on monitors-changed
if (workArea.width === 0) {
this._inLayout = false;
return;
}
// compensate to keep window position bar on all monitors
if (Settings.prefs.show_window_position_bar) {
const panelBoxHeight = Topbar.panelBox.height;
const monitor = Main.layoutManager.primaryMonitor;
if (monitor !== this.monitor) {
workArea.y += panelBoxHeight;
workArea.height -= panelBoxHeight;
}
}
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 * Settings.prefs.minimum_margin);
let resultingWidth, relayout;
let allocator = allocators && allocators[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 {
console.warn("Bail at fixpoint, max tries reached");
}
}
x += resultingWidth + gap;
}
// final gap add - required to resolve https://github.com/paperwm/PaperWM/issues/684
x += 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;
}
Easer.addEase(this.cloneContainer,
{
x: this.targetX,
time,
onComplete: this.moveDone.bind(this),
});
}
if (animate && ensure) {
ensureViewport(this.selectedWindow, this);
} else {
this.moveDone();
}
this.emit('layout', this);
}
queueLayout(animate = true) {
if (this._layoutQueued)
return;
this._layoutQueued = true;
Utils.later_add(Meta.LaterType.RESIZE, () => {
this._layoutQueued = false;
this.layout(animate);
});
}
// 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 = this.visibleX(metaWindow);
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 = this.visibleX(metaWindow);
let workArea = this.workArea();
let min = workArea.x;
return min <= x && x + clone.width < min + workArea.width;
}
isPlaceable(metaWindow) {
let clone = metaWindow.clone;
let x = this.visibleX(metaWindow);
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]);
}
/*
* Fix (still needed is 44) for 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 = this.visibleX(w);
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) {
try {
w.move_frame(true, x, y);
}
catch (ex) {
}
}
});
Utils.actor_reparent(metaWindow.clone, 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);
if (this.selectedWindow === 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);
this.selectedWindow = 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 (this.selectedWindow) {
ensureViewport(this.selectedWindow, this);
} else {
// can also be undefined here, will set to null explicitly
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;
Utils.actor_reparent(clone, 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;
}
/**
* Returns true iff this space has a currently fullscreened window.
*/
hasFullScreenWindow() {
return this.getWindows().some(el => el.fullscreen);
}
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;
Lib.swap(this[index], row, targetRow);
Lib.swap(this, index, targetIndex);
this.layout();
this.emit('swapped', index, targetIndex, row, targetRow);
ensureViewport(this.selectedWindow, this, { force: true });
}
switchLinear(dir) {
let index = this.selectedIndex();
let column = this[index];
if (!column)
return false;
let row = column.indexOf(this.selectedWindow);
if (Lib.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) {