-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.js
1702 lines (1566 loc) · 53 KB
/
main.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
// -----------------------------------------------------------------
// KWin - Quarter Tiling: A Tiling Script for the KWin Window Manager
// -----------------------------------------------------------------
// Shortcuts for KWin objects
var opt = options;
var ws = workspace;
// Shortcuts for workspace properties
var cAct;
// Array for included clients
var tiles = [];
// KWin client.resourceClasses & client.resourceNames that are not tiled
var ignoredClients = [
"albert",
"kazam",
"krunner",
"ksmserver",
"lattedock",
"pinentry",
"Plasma",
"plasma",
"plasma-desktop",
"plasmashell",
"plugin-container",
"simplescreenrecorder",
"yakuake",
];
// Concats the hardcoded ignoredClients with the ones read from the QML interface
ignoredClients = ignoredClients.concat(
readConfig("ignoredClients", "wine, steam, kate").toString().split(', ')
);
// Easy way to disable all Java-based programs
if (readConfig("ignoreJava", 0) == 1) {
var java = "sun-awt-x11-xframepeer";
ignoredClients.push(java);
}
// KWin client.resourceClasses & client.resourceNames to which tiles are adjusted to
var fixedClients = [
"kcalc",
];
// Concats the hardcoded fixedClients with the ones read from the QML interface
fixedClients = fixedClients.concat(
readConfig("fixedClients", "telegram, telegram-desktop, telegramdesktop").toString().split(', ')
);
// KWin client.captions that are not tiled
var ignoredCaptions = [
"File Upload",
"Move to Trash",
"Quit GIMP",
"Create a New Image",
"QEMU",
];
// Concats the hardcoded ignoredCaptions with the ones read from the QML interface
if (readConfig("ignoredCaptions", "") != "") {
ignoredCaptions = ignoredCaptions.concat(readConfig("ignoredCaptions", "").toString().split(', '));
}
// Activities that will be ignored by the script
var ignoredActivities = [];
function readIgnoredActs() {
ignoredActivities = readConfig("ignoredActivities", "").toString().split(', ');
if (ignoredActivities != "") {
for (var i = 0; i < ignoredActivities.length; i++) {
var act = parseInt(ignoredActivities[i]); // Transfers the entries to integers
if (isNaN(act)) {
ignoredActivities.splice(i, 0); // Removes entries that aren't integers
} else if (ws.activities.length > act - 1) {
ignoredActivities[i] = ws.activities[act - 1].toString();
}
}
} else {
ignoredActivities = [];
}
}
// Virtual desktops that will be ignored by the script
var ignoredDesktops = readConfig("ignoredDesktops", "").toString().split(', ');
if (ignoredDesktops != "") {
for (var i = 0; i < ignoredDesktops.length; i++) {
var desk = parseInt(ignoredDesktops[i]); // Transfers the entries to integers
if (isNaN(desk)) {
ignoredDesktops.splice(i, 0); // Removes entries that aren't integers
} else {
ignoredDesktops[i] = desk; // Enters the transfered entry to the array
}
}
ignoredDesktops.push(-1);
} else {
ignoredDesktops = [-1];
}
// Screens that will be completely ignored
var ignoredScreens = readConfig("ignoredScreens", "").toString().split(', ');
if (ignoredScreens != "") {
for (var i = 0; i < ignoredScreens.length; i++) {
// Removes every non-number entry
var scr = parseInt(ignoredScreens[i]); // Transfers the entries to integers
if (isNaN(scr)) {
ignoredScreens.splice(i, 0); // Removes entries that aren't integers
} else {
ignoredScreens[i] = scr - 1; // -1 for consistency with desktops
}
}
} else {
ignoredScreens = [];
}
var gap = readConfig("gap", 8);
// Negative gaps are not allowed (difficulty recognizing maxed, overlapping x < 0 changes screen)
if (gap < 0) {
gap = 0;
}
var margins = [];
margins[0] = readConfig("mt", 0);
margins[1] = readConfig("ml", 0);
margins[2] = readConfig("mb", 1);
margins[3] = readConfig("mr", 0);
if (gap == 0) {
fixMargins();
}
// Avoids issues with ifMaxed() if gaps are set to zero
function fixMargins() {
for (var i = 0; i < margins.length; i++) {
if (margins[i] > 0) {
return;
}
}
margins[2] = 1; // Bottom is used because it's the least noticable and needed
}
var centerTo = readConfig("centerTo", 1); // 0 == Screen, 1 == Tile
// Starts the script
function init() {
cAct = ws.currentActivity.toString();
registerKeys();
var desks = readConfig("numDesks", 2);
if (desks < 1) {
desks = 1;
}
ws.desktops = desks;
readIgnoredActs();
for (var j = 0; j < ws.activities.length; j++) {
var act = ws.activities[j].toString();
if (ignoredActivities.indexOf(act) === -1) {
tiles[act] = [];
for (var i = 1; i <= desks; i++) {
createDesktop(act, i);
}
}
}
addClients();
connectWorkspace();
}
// Registers all the shortcuts used for the script
function registerKeys() {
registerShortcut(
"Quarter: Float/Tile Desktop",
"Quarter: Float/Tile Desktop",
"Meta+Esc",
function() {
// Floats the whole desktop and aligns windows to the middle
var clients = ws.clientList();
var client;
var desk = ws.currentDesktop;
if (ignoredDesktops.indexOf(desk) > -1) {
ignoredDesktops.splice(i, 1);
for (var i = 0; i < clients.length; i++) {
client = clients[i];
if (client.desktop == desk && client.included !== true) {
addClient(client, true, client.desktop, client.screen);
}
}
} else {
ignoredDesktops.push(desk);
var removedClients = [];
for (var j = 0; j < clients.length; j++) {
client = clients[j];
if (client.desktop === desk && client.included) {
removedClients.push([client, client.geometry]);
}
}
for (j = 0; j < removedClients.length; j++) {
client = removedClients[j][0];
removeClient(client, false, client.desktop, client.screen);
client.geometry = removedClients[j][1];
}
}
});
registerShortcut(
"Quarter: Float On/Off",
"Quarter: Float On/Off",
"Meta+F",
function() {
var client = ws.activeClient;
client.oldGeo = client.geometry;
if (client.included) {
removeClient(client, false, client.desktop, client.screen);
} else {
addClient(client, true, client.desktop, client.screen);
}
});
registerShortcut(
"Quarter: Move Up",
"Quarter: Move Up",
"Meta+K",
function() {
var client = ws.activeClient;
var desk = client.desktop;
var scr = client.screen;
if (cAct !== client.act) {
removeClient(client, false, desk, scr);
}
if (client.included && ignoredScreens.indexOf(client.screen) === -1) {
var i = findClientIndex(client, ws.currentDesktop, scr);
if (i === 2) {
swapClients(i, 1, scr, scr);
} else if (i === 3) {
swapClients(i, 0, scr, scr);
} else {
return;
}
tileClients();
} else {
var tile = screenGeo(scr);
var rect = client.geometry;
rect.x = tile.x + gap + margins[1];
rect.y = tile.y + gap + margins[0];
rect.width = tile.width - gap * 2 - margins[1] - margins[3];
rect.height = tile.height * 0.5 - gap - margins[0];
if (client.geometry.x === rect.x && client.geometry.y === rect.y &&
client.geometry.width === rect.width && client.geometry.height === rect.height) {
client.geometry = screenGeo(scr, true);
} else {
client.geometry = rect;
}
}
});
registerShortcut(
"Quarter: Move Down",
"Quarter: Move Down",
"Meta+J",
function() {
var client = ws.activeClient;
var act = client.act;
var desk = client.desktop;
var scr = client.screen;
if (cAct !== client.act) {
removeClient(client, false, desk, scr);
}
if (client.included && ignoredScreens.indexOf(client.screen) === -1) {
var i = findClientIndex(client, desk, scr);
if (i === 0 && tiles[act][desk][scr].length === 4) {
swapClients(i, 3, scr, scr);
} else if (i === 1 && tiles[act][desk][scr].length >= 3) {
swapClients(i, 2, scr, scr);
} else {
return;
}
tileClients();
} else {
var tile = screenGeo(scr);
var rect = client.geometry;
rect.x = tile.x + gap + margins[1];
rect.y = tile.y + gap + margins[0];
rect.width = tile.width - gap * 2 - margins[1] - margins[3];
rect.height = tile.height * 0.5 - gap - margins[2];
rect.y += rect.height;
if (client.geometry.x === rect.x && client.geometry.y === rect.y &&
client.geometry.width === rect.width && client.geometry.height === rect.height) {
client.geometry = screenGeo(scr, true);
} else {
client.geometry = rect;
}
}
});
registerShortcut(
"Quarter: Move Left",
"Quarter: Move Left",
"Meta+H",
function() {
var client = ws.activeClient;
var act = client.act;
var desk = client.desktop;
var scr = client.screen;
if (cAct !== client.act) {
removeClient(client, false, desk, scr);
}
if (client.included && ignoredScreens.indexOf(client.screen) === -1) {
var i = findClientIndex(client, desk, scr);
if (i === 1) {
swapClients(i, 0, scr, scr);
} else if (i === 2 && tiles[act][desk][scr].length === 4) {
swapClients(i, 3, scr, scr);
} else if (i === 2) {
swapClients(i, 0, scr, scr);
} else {
return;
}
tileClients();
} else {
var tile = screenGeo(scr);
var rect = client.geometry;
rect.x = tile.x + gap + margins[1];
rect.y = tile.y + gap + margins[0];
rect.width = tile.width * 0.5 - gap - margins[1];
rect.height = tile.height - gap * 2 - margins[0] - margins[2];
if (client.geometry.x === rect.x && client.geometry.y === rect.y &&
client.geometry.width === rect.width && client.geometry.height === rect.height) {
client.geometry = screenGeo(scr, true);
} else {
client.geometry = rect;
}
}
});
registerShortcut(
"Quarter: Move Right",
"Quarter: Move Right",
"Meta+L",
function() {
var client = ws.activeClient;
var act = client.act;
var desk = client.desktop;
var scr = client.screen;
if (cAct !== client.act) {
removeClient(client, false, desk, scr);
}
if (client.included && ignoredScreens.indexOf(client.screen) === -1) {
var i = findClientIndex(client, desk, scr);
if (i === 0 && tiles[act][desk][scr].length > 1) {
swapClients(i, 1, scr, scr);
} else if (i === 3) {
swapClients(i, 2, scr, scr);
} else {
return;
}
tileClients();
} else {
var tile = screenGeo(scr);
var rect = client.geometry;
rect.x = tile.x + gap + margins[1];
rect.y = tile.y + gap + margins[0];
rect.width = tile.width * 0.5 - gap - margins[3];
rect.height = tile.height - gap * 2 - margins[0] - margins[2];
rect.x += rect.width;
if (client.geometry.x === rect.x && client.geometry.y === rect.y &&
client.geometry.width === rect.width && client.geometry.height === rect.height) {
client.geometry = screenGeo(scr, true);
} else {
client.geometry = rect;
}
}
});
registerShortcut(
"Quarter: Move to Next Screen",
"Quarter: Move to Next Screen",
"Meta+Right",
function() {
var client = ws.activeClient;
var scr = client.screen + 1;
if (scr >= ws.numScreens) {
scr = 0;
}
throwClient(client, client.desktop, client.screen, client.desktop, scr);
tileClients();
});
registerShortcut(
"Quarter: Move to Previous Screen",
"Quarter: Move to Previous Screen",
"Meta+Left",
function() {
var client = ws.activeClient;
var scr = client.screen - 1;
if (scr < 0) {
scr = ws.numScreens - 1;
}
throwClient(client, client.desktop, client.screen, client.desktop, scr);
tileClients();
});
registerShortcut(
"Quarter: + Window Size",
"Quarter: + Window Size",
"Meta+Y",
function() {
// TODO: Implement for fixed clients
var client = ws.activeClient;
if (client.fixed) {
return;
}
var scr = client.screen;
var x = Math.round(screenWidth(scr) * 0.01);
var y = Math.round(screenHeight(scr) * 0.01);
if (client.included && ignoredScreens.indexOf(scr) === -1) {
resizeClient(client, scr, x, y);
tileClients();
} else {
var rect = client.geometry;
rect.width += x;
rect.height += y;
rect.x -= x * 0.5;
rect.y -= y * 0.5;
client.geometry = rect;
}
});
registerShortcut(
"Quarter: - Window Size",
"Quarter: - Window Size",
"Meta+N",
function() {
// TODO: Implement for fixed clients
var client = ws.activeClient;
if (client.fixed) {
return;
}
var scr = client.screen;
var x = Math.round(screenWidth(scr) * 0.01);
var y = Math.round(screenHeight(scr) * 0.01);
if (client.included && ignoredScreens.indexOf(scr) === -1) {
resizeClient(client, scr, -x, -y);
tileClients();
} else {
var rect = client.geometry;
rect.width -= x;
rect.height -= y;
rect.x += x * 0.5;
rect.y += y * 0.5;
client.geometry = rect;
}
});
registerShortcut(
"Quarter: Reset Layout",
"Quarter: Reset Layout",
"Meta+R",
function() {
tiles[cAct][ws.currentDesktop][ws.activeScreen].layout = newLayout(ws.activeScreen);
tileClients();
});
registerShortcut(
"Quarter: Toggle Gaps On/Off",
"Quarter: Toggle Gaps On/Off",
"Meta+G",
function() {
if (gap <= 1) {
gap = readConfig("gap", 8);
} else {
for (var i = 0; i < margins.length; i++) {
if (margins[i] > 0) {
gap = 0;
tileClients();
return;
}
}
gap = 1; /* If margins are set to zero, gaps can't be zero
or the last window will be recognized as maximized */
}
tileClients();
});
registerShortcut(
"Quarter: + Gap Size",
"Quarter: + Gap Size",
"Meta+PgUp",
function() {
gap += 2;
tileClients();
});
registerShortcut(
"Quarter: - Gap Size",
"Quarter: - Gap Size",
"Meta+Down",
function() {
gap -= 2;
if (gap < 0) {
gap = 0;
}
tileClients();
});
}
// Connects the KWin:Workspace signals to the following functions
function connectWorkspace() {
ws.clientAdded.connect(function(client) {
addClient(client, true, ws.currentDesktop, ws.activeScreen);
});
ws.clientRemoved.connect(function(client) {
removeClient(client, true, client.desktop, client.screen);
});
ws.desktopPresenceChanged.connect(function(client, desk) {
if (client && client.included) {
if (ws.desktops < client.oldDesk) {
removeClient(client, false, client.oldDesk, client.oldScr);
client.closeWindow();
} else if (client.desktop !== client.oldDesk){
throwClient(client, client.oldDesk, client.oldScr, client.desktop, client.screen);
}
} else {
tileClients();
}
});
ws.currentDesktopChanged.connect(function(client, desk) {
if (client && client.included) {
if (ws.desktops < client.oldDesk) {
removeClient(client, false, client.oldDesk, client.oldScr);
client.closeWindow();
} else if (client.desktop !== client.oldDesk){
throwClient(client, client.oldDesk, client.oldScr, client.desktop, client.screen);
}
} else {
tileClients();
}
});
ws.clientMaximizeSet.connect(function(client, h, v) {
if (client.included) {
maximizeClient(client, h, v);
}
});
ws.clientFullScreenSet.connect(function(client, full, user) {
if (client.included) {
fullScreenClient(client, full, user);
}
});
ws.clientMinimized.connect(function(client, full, user) {
if (client.included) {
minimizeClient(client);
}
});
ws.clientUnminimized.connect(function(client) {
if (client.included) {
unminimizeClient(client);
}
});
ws.numberDesktopsChanged.connect(adjustDesktops);
ws.activityAdded.connect(function(act) {
createActivity(act);
});
ws.currentActivityChanged.connect(function(act) {
cAct = curAct();
tileClients();
});
}
// -------------------
// Client Manipulation
// -------------------
// Runs an ignore-check and if it passes, adds a client to tiles[]
function addClient(client, follow, desk, scr) {
if (checkClient(client)) {
print("START: addClient(" + client + ", " + follow + ", " + desk + ", " + scr + ")");
var act = cAct;
// If tiles.length exceeds the maximum amount, moves to another screen or another desktop
if (tiles[act][desk][scr].length === tiles[act][desk][scr].max ||
tiles[act][desk][scr].length === 4 || tiles[act][desk][scr].blocked) {
// Fixes a bug that makes the maximum go over 4 when removing virtual desktops
if (tiles[act][desk][scr].length === 4) {
tiles[act][desk][scr].max = 4;
}
var freeTile = findSpace(); // Looks for a free space on existing desktops
if (freeTile) {
desk = freeTile[0];
scr = freeTile[1];
} else {
ws.desktops += 1;
desk = ws.desktops;
}
} else if (tiles[act][desk][scr].length === 0) {
tiles[act][desk][scr].layout = newLayout(scr);
}
if (follow) {
ws.currentDesktop = desk;
}
client.desktop = desk;
ws.activeClient = client;
connectClient(client, desk, scr);
tiles[act][desk][scr].push(client);
if (client.minimized) {
reserveClient(client, desk, scr);
} else if (client.fullScreen || isMaxed(client)) {
tiles[act][desk][scr].blocked = true;
// Moves the client to the next screen before tiling (to avoid tiling a maxed client)
client.geometry = screenGeo(scr);
reserveClient(client, desk, scr);
} else if (ignoredScreens.indexOf(client.screen) === -1) {
if (client.fixed !== true) {
resetClient(client, 2, scr);
}
fitClient(client, desk, scr, "add");
tileClients();
}
print("END: addClient(" + client + ", " + follow + ", " + desk + ", " + scr + ")");
}
}
// Adds all the clients that existed before the script was executed
function addClients() {
var clients = ws.clientList();
for (var i = 0; i < clients.length; i++) {
addClient(clients[i], false, clients[i].desktop, clients[i].screen);
}
/*
// Sorts clients by their y-coordinate
clients.sort(function(a, b) {
return a.geometry.y - b.geometry.y;
});
// Split the sorted list in half (two rows)
var upperClients = [];
var lowerClients = [];
for (var i = 0; i < Math.max(clients.length * 0.5); i++) {
upperClients[i] = clients[i];
}
var j = 0;
for (i = clients.length - 1; i >= Math.max(clients.length * 0.5); i--) {
lowerClients[j] = clients[i];
j++;
}
// Sort the split lists by x-coordinate
upperClients.sort(function(a, b) {
return a.geometry.x - b.geometry.x;
});
lowerClients.sort(function(a, b) {
return (-1 * a.geometry.x) - (-1 * b.geometry.x);
});
// Add the clients in proper order
for (i = 0; i < upperClients.length; i++) {
addClient(upperClients[i], false, upperClients[i].desktop, upperClients[i].screen);
}
for (i = 0; i < lowerClients.length; i++) {
addClient(lowerClients[i], false, lowerClients[i].desktop, lowerClients[i].screen);
}
*/
}
// Connects the signals of the new KWin:Client to the following functions
function connectClient(client, desk, scr) {
client.clientStartUserMovedResized.connect(startMove);
client.clientFinishUserMovedResized.connect(endMove);
client.activeChanged.connect(tileClients);
client.desktopChanged.connect(function() {
if (ignoredDesktops.indexOf(client.desktop) > -1) {
removeClient(client, false, client.oldDesk, client.oldScr);
} else {
if (client.included !== true) {
addClient(client, true, client.desktop, client.screen);
}
}
});
if (fixedClients.indexOf(client.resourceClass.toString()) > -1 ||
fixedClients.indexOf(client.resourceName.toString()) > -1) {
client.fixed = true;
}
client.included = true;
client.reserved = false;
client.act = cAct;
client.oldDesk = desk;
client.oldScr = scr;
client.oldIndex = -1;
client.oldGeo = client.geometry;
}
// Removes the closed client from tiles[]
function removeClient(client, follow, desk, scr) {
print("START: removeClient(" + client + ", " + follow + ", " + desk + ", " + scr + ")");
if (client.included) {
resetClient(client, 1);
if (typeof follow === "undefined") { follow = false; }
if (typeof desk === "undefined") { desk = client.oldDesk; }
if (typeof scr === "undefined") { scr = client.oldScr; }
var act = client.act;
if (client.reserved) {
tiles[act][desk][scr].max += 1;
if (client.minimized !== true) {
tiles[act][desk][scr].blocked = false;
}
} else {
var i = findClientIndex(client, desk, scr);
tiles[act][desk][scr].splice(i, 1);
for (var j = 0; j < tiles[act][desk].length; j++) {
if (tiles[act][desk][j].length > 0) {
follow = false;
break;
}
} if (follow) {
ws.currentDesktop = findBusy();
} else if (tiles[act][desk][scr].length > i && client.fixed) {
// Fits the client filling removed client's spot
fitClient(tiles[act][desk][scr][i], desk, scr, "remove");
}
}
disconnectClient(client);
print("END: removeClient(" + client + ", " + follow + ", " + desk + ", " + scr + ")");
fitClients(act, desk, scr, "remove");
tileClients();
} else {
print("END: removeClient() " + client + " NOT INCLUDED");
}
}
// Disconnects the signals from removed clients,
// so they will not trigger when a manually floated client is interacted with
function disconnectClient(client) {
client.included = false;
client.clientStartUserMovedResized.disconnect(startMove);
client.clientFinishUserMovedResized.disconnect(endMove);
}
// "Reserves" a spot for client on its current desktop and screen
function reserveClient(client, desk, scr) {
print("START: reserveClient(" + client + ", " + desk + ", " + scr + ")");
var act = client.act;
if (client.included && client.reserved === false && ignoredScreens.indexOf(scr) === -1) {
var i = findClientIndex(client, desk, scr);
tiles[act][desk][scr].max -= 1;
tiles[act][desk][scr].splice(i, 1);
client.oldIndex = i;
client.oldDesk = desk;
client.oldScr = scr;
client.reserved = true;
tileClients();
}
print("END: reserveClient(" + client + ", " + desk + ", " + scr + ")");
}
// "Adds" a client back to the desktop on its reserved tile
function unreserveClient(client) {
print("START: unreserveClient(" + client + ")");
if (client.included && client.reserved && ignoredScreens.indexOf(client.oldScr) === -1) {
var act = cAct;
client.reserved = false;
tiles[act][client.oldDesk][client.oldScr].max += 1;
if (client.oldIndex >= 0) {
tiles[act][client.oldDesk][client.oldScr].splice(client.oldIndex, 0, client);
} else {
tiles[act][client.oldDesk][client.oldScr].push(client);
}
client.desktop = client.oldDesk;
ws.currentDesktop = client.oldDesk;
}
tileClients();
print("END: unreserveClient(" + client + ")");
}
function fitClients(act, desk, scr, action) {
if (ignoredActivities.indexOf(act) > -1) { return; }
var t = tiles[act][desk][scr];
switch (t.length) {
case 2:
fitClient(t[0], desk, scr, action, true);
fitClient(t[1], desk, scr, action, true);
break;
case 3:
fitClient(t[0], desk, scr, action, true);
if (t[2].geometry.height > t[1].geometry.height) {
fitClient(t[2], desk, scr, action, true);
} else {
fitClient(t[1], desk, scr, action, true);
}
break;
case 4:
if (t[3].geometry.height > t[0].geometry.height) {
fitClient(t[3], desk, scr, action, true);
} else {
fitClient(t[0], desk, scr, action, true);
}
if (t[2].geometry.height > t[1].geometry.height) {
fitClient(t[2], desk, scr, action, true);
} else {
fitClient(t[1], desk, scr, action, true);
}
}
}
// Adjusts the layout according to the size of a client
function fitClient(client, desk, scr, action, all) {
print("START: fitClient(" + client + ", " + desk + ", " +
scr + ", " + action + ", " + all + ")");
// Cases where autosizing does not happen
if (client.reserved || ignoredActivities.indexOf(client.act) > -1) {
return;
}
var opposite = oppositeClient(client, scr);
var neighbour = neighbourClient(client, scr);
var act = client.act;
var tile = tiles[act][desk][scr].layout[findClientIndex(client, desk, scr)];
// Vertical adjustment
var y = 0;
if (opposite) {
if (client.fixed && opposite[0].fixed) {
// If both are fixed, center
y = client.geometry.height + gap * 1.5 - tile.height;
y = 0.5 * (y - (opposite[0].geometry.height + gap * 1.5 - tiles[act][desk][scr].layout[opposite[1]].height));
} else if (client.fixed !== true && opposite[0].fixed) {
// If only opposite is fixed, fit accordingly
y = -1 * (opposite[0].geometry.height + gap * 1.5 - tiles[act][desk][scr].layout[opposite[1]].height);
// TODO: This will be an option
if (action == "add") {
if (y < newTile(scr).height - tile.height) {
// New windows can't be larger than a default tile
y = newTile(scr).height - tile.height;
}
}
} else if (client.fixed || action !== "swap") {
y = client.geometry.height + gap * 1.5 - tile.height;
// TODO: This will be an option
if (action == "add") {
if (y > newTile(scr).height - tile.height) {
// New windows can't be larger than a default tile
y = newTile(scr).height - tile.height;
}
}
}
}
resizeClient(client, scr, 0, y);
print("END: fitClient(" + client + ", " + desk + ", " +
scr + ", " + action + ", " + all + ")");
}
// Aligns clients to the layout
function tileClients(desk) {
var act = cAct;
if (ignoredActivities.indexOf(act) > -1) { return; }
if (typeof desk === "undefined") { desk = ws.currentDesktop; }
print("START: tileClients(" + desk + ")");
for (var i = 0; i < ws.numScreens; i++) {
if (ignoredScreens.indexOf(i) > -1) {
// Don't tile ignored screens
} else if (typeof tiles[act][desk][i] !== "undefined") {
var t = tiles[act][desk][i];
var adj = [];
switch (t.length) {
case 1:
adj[0] = {};
adj[0].x = t.layout[0].x + gap;
adj[0].y = t.layout[0].y + gap;
adj[0].width = t.layout[0].width + t.layout[1].width - gap * 2;
adj[0].height = t.layout[0].height + t.layout[3].height - gap * 2;
break;
case 2:
adj[0] = {};
adj[0].x = t.layout[0].x + gap;
adj[0].y = t.layout[0].y + gap;
adj[0].width = t.layout[0].width - gap * 1.5;
adj[0].height = t.layout[0].height + t.layout[3].height - gap * 2;
adj[1] = {};
adj[1].x = t.layout[1].x + gap * 0.5;
adj[1].y = t.layout[1].y + gap;
adj[1].width = t.layout[1].width - gap * 1.5;
adj[1].height = t.layout[1].height + t.layout[2].height - gap * 2;
break;
case 3:
adj[0] = {};
adj[0].x = t.layout[0].x + gap;
adj[0].y = t.layout[0].y + gap;
adj[0].width = t.layout[0].width - gap * 1.5;
adj[0].height = t.layout[0].height + t.layout[3].height - gap * 2;
adj[1] = {};
adj[1].x = t.layout[1].x + gap * 0.5;
adj[1].y = t.layout[1].y + gap;
adj[1].width = t.layout[1].width - gap * 1.5;
adj[1].height = t.layout[1].height - gap * 1.5;
adj[2] = {};
adj[2].x = t.layout[2].x + gap * 0.5;
adj[2].y = t.layout[2].y + gap * 0.5;
adj[2].width = t.layout[2].width - gap * 1.5;
adj[2].height = t.layout[2].height - gap * 1.5;
break;
case 4:
adj[0] = {};
adj[0].x = t.layout[0].x + gap;
adj[0].y = tiles[act][desk][i].layout[0].y + gap;
adj[0].width = t.layout[0].width - gap * 1.5;
adj[0].height = t.layout[0].height - gap * 1.5;
adj[1] = {};
adj[1].x = t.layout[1].x + gap * 0.5;
adj[1].y = t.layout[1].y + gap;
adj[1].width = t.layout[1].width - gap * 1.5;
adj[1].height = t.layout[1].height - gap * 1.5;
adj[2] = {};
adj[2].x = t.layout[2].x + gap * 0.5;
adj[2].y = t.layout[2].y + gap * 0.5;
adj[2].width = t.layout[2].width - gap * 1.5;
adj[2].height = t.layout[2].height - gap * 1.5;
adj[3] = {};
adj[3].x = t.layout[3].x + gap;
adj[3].y = t.layout[3].y + gap * 0.5;
adj[3].width = t.layout[3].width - gap * 1.5;
adj[3].height = t.layout[3].height - gap * 1.5;
break;
}
// Regular clients always have the size of their tile
// Fixed clients get further adjusted to their tile
for (var j = 0; j < adj.length; j++) {
if (tiles[act][desk][i][j].fixed) {
var rect = tiles[act][desk][i][j].geometry;
// Center the clients according to the tile (1) or screen (2)
if (centerTo == 1) {
rect.x = adj[j].x + adj[j].width * 0.5 - rect.width * 0.5;
rect.y = adj[j].y + adj[j].height * 0.5 - rect.height * 0.5;
} else {
switch (j) {
case 0:
if (adj.length === 1) {
rect.x = adj[j].x + adj[j].width * 0.5 - rect.width * 0.5;
rect.y = adj[j].y + adj[j].height * 0.5 - rect.height * 0.5;
} else if (adj.length == 2 || adj.length == 3) {
rect.x = adj[j].x + adj[j].width - rect.width;
rect.y = adj[j].y + adj[j].height * 0.5 - rect.height * 0.5;
} else {
rect.x = adj[j].x + adj[j].width - rect.width;
rect.y = adj[j].y + adj[j].height - rect.height;
}
break;
case 1:
if (adj.length === 2) {
rect.x = adj[j].x;
rect.y = adj[j].y + adj[j].height * 0.5 - rect.height * 0.5;
} else {
rect.x = adj[j].x;
rect.y = adj[j].y + adj[j].height - rect.height;
}
break;
case 2:
rect.x = adj[j].x;
rect.y = adj[j].y;
break;
case 3:
rect.x = adj[j].x + adj[j].width - rect.width;
rect.y = adj[j].y;
break;
}
}
// Stops a fixed client from getting too large
if (rect.width > adj[j].width) {
rect.x = adj[j].x;
rect.width = adj[j].width;
}
if (rect.height > adj[j].height) {
rect.y = adj[j].y;
rect.height = adj[j].height;
}
// Fixed
tiles[act][desk][i][j].geometry = rect;
} else {
// Regular