forked from emasab/shelltile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverview.js
More file actions
697 lines (479 loc) · 19.9 KB
/
overview.js
File metadata and controls
697 lines (479 loc) · 19.9 KB
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
const Config = imports.misc.config;
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
const Log = Extension.imports.logger.Logger.getLogger("ShellTile");
const Util = Extension.imports.util;
const Lang = imports.lang;
const Meta = imports.gi.Meta;
const GSWorkspace = imports.ui.workspace.Workspace;
const WindowGroup = Extension.imports.tiling.WindowGroup;
const OverviewModifierBase = function(){
this.computeGroupData = function(clones){
let groupOrder = [];
let groupGeometry = {};
let groupedSlots = [];
let singleSlots = [];
let cloneGroup = {};
let cloneGroupObject = {};
let clones1 = [];
let idClone = {};
for(var i=0; i<clones.length; i++){
var clone = clones[i];
var clone_meta_window = clone.metaWindow;
var myWindow = this.extension.get_window(clone_meta_window, true);
var windowId = myWindow.id();
//if(this.log.is_debug()) this.log.debug(myWindow);
clones1.push(windowId);
idClone[windowId] = clone;
if(myWindow.group && !myWindow.is_maximized()){
var topmost_group = myWindow.group.get_topmost_group();
var topmost_group_id = topmost_group.id();
cloneGroupObject[topmost_group_id] = topmost_group;
if(groupOrder.indexOf(topmost_group_id) < 0){
groupOrder.push(topmost_group_id);
groupedSlots.push(topmost_group_id);
groupGeometry[topmost_group_id] = topmost_group.outer_rect(true);
}
cloneGroup[windowId] = topmost_group_id;
} else {
groupOrder.push(windowId);
groupGeometry[windowId] = myWindow.outer_rect();
singleSlots.push(windowId);
cloneGroup[windowId] = windowId;
}
}
this.groupOrder = groupOrder;
this.groupGeometry = groupGeometry;
this.groupedSlots = groupedSlots;
this.singleSlots = singleSlots;
this.cloneGroup = cloneGroup;
this.cloneGroupObject = cloneGroupObject;
this.clones = clones1;
this.idClone = idClone;
this.groupWindowLayouts = {};
this.lastGroupPosition = {};
}
this.simplifyWindows = function(windows){
this.computeGroupData(windows);
var windows1 = [];
var windowIds1 = [];
if(this.log.is_debug()) this.log.debug("simplifyWindows");
for(var i=0; i<this.singleSlots.length; i++){
var singleSlot = this.singleSlots[i];
//if(this.log.is_debug()) this.log.debug("singleSlot: " + singleSlot);
var clone = this.idClone[singleSlot];
clone._id = ''+singleSlot;
//if(this.log.is_debug()) this.log.debug("clone.actor.x" + clone.actor.x);
//if(this.log.is_debug()) this.log.debug("clone.actor.y" + clone.actor.y);
//if(this.log.is_debug()) this.log.debug("clone.actor.width" + clone.actor.width);
//if(this.log.is_debug()) this.log.debug("clone.actor.height" + clone.actor.height);
windows1.push(clone);
windowIds1.push('' + singleSlot);
}
for(var i=0; i<this.groupedSlots.length; i++){
var groupedSlot = this.groupedSlots[i];
var clone = {actor: {}};
var cloneGroupObject = this.cloneGroupObject[groupedSlot];
var top_left_window = cloneGroupObject.top_left_window();
var top_left_clone = this.idClone[top_left_window.id()];
var geometry = this.groupGeometry[groupedSlot];
clone.actor.x = geometry.x;
clone.actor.y = geometry.y;
clone.actor.width = geometry.width;
clone.actor.height = geometry.height;
clone.x = geometry.x;
clone.y = geometry.y;
clone.width = geometry.width;
clone.height = geometry.height;
clone._ids = cloneGroupObject.ids();
//clone.metaWindow = top_left_window.meta_window;
clone.realWindow = top_left_window.get_actor();
//clone.overlay = top_left_clone.overlay;
windows1.push(clone);
windowIds1.push('' + groupedSlot);
}
return [windows1, windowIds1];
}
this.explodeSlots = function(ret){
if(this.log.is_debug()) this.log.debug("explodeSlots");
var idRet = {};
for(var i=0; i<ret.length; i++){
var ret11 = ret[i][3];
if(ret11._ids){
for(var j=0; j<ret11._ids.length; j++){
var id1 = ret11._ids[j];
var clone = this.idClone[id1];
if(clone){
var myW = this.extension.get_window(clone.metaWindow);
if(!myW.is_maximized())
idRet[''+id1] = ret[i];
}
}
} else {
idRet[''+ret11._id] = ret[i];
}
}
var ret1 = [];
for(var i=0; i<this.clones.length; i++){
var cloneId = '' + this.clones[i];
var clone = this.idClone[cloneId];
var myWindow = this.extension.get_window(clone.metaWindow);
var isGroup = this.cloneGroup[cloneId] != cloneId && myWindow.group;
if(isGroup){
var groupId = this.cloneGroup[cloneId];
var ret2 = idRet[cloneId].slice();
let [x, y, scale, clone2] = ret2;
var update = !this.groupWindowLayouts[groupId] || !this.lastGroupPosition[groupId];
if(!update){
var ret_last = this.lastGroupPosition[groupId];
update = ret_last[0] != x || ret_last[1] != y || ret_last[2] != scale;
}
if(update){
var groupGeometry = this.groupGeometry[groupId];
let width = groupGeometry.width * scale, height = groupGeometry.height * scale;
var scaled_group_rect = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
var topmost_group = myWindow.group.get_topmost_group();
this.groupWindowLayouts[groupId] = this.calculateGroupWindowLayouts(topmost_group, scaled_group_rect, scale);
this.lastGroupPosition[groupId] = ret2;
}
var groupWindowLayouts = this.groupWindowLayouts[groupId];
var windowLayout = groupWindowLayouts[cloneId];
//if(this.log.is_debug()) this.log.debug("slot: " + windowLayout);
ret2[0] = windowLayout[0];
ret2[1] = windowLayout[1];
ret2[3] = clone;
} else {
var ret2 = idRet[cloneId].slice();
}
ret1.push(ret2);
}
if(this.log.is_debug()) this.log.debug("explodeSlots end");
return ret1;
}
this.calculateGroupWindowLayouts = function(topmost_group, scaled_group_rect, scale){
var ret = {};
var log = this.log;
var scaled_gap = topmost_group.gap_between_windows() * scale;
var calculateWindowLayout = function(group, rect){
let first = group.first;
let second = group.second;
let x,y,width,height,x1,y1;
if(group.type == WindowGroup.HORIZONTAL_GROUP){
x = rect.x;
y = rect.y;
width = rect.width * group.splitPercent;
height = rect.height;
var first_scaled = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
x = x + width + scaled_gap;
width = rect.x + rect.width - x;
var second_scaled = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
} else {
x = rect.x;
y = rect.y;
width = rect.width;
height = rect.height * group.splitPercent;
var first_scaled = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
y = y + height + scaled_gap;
height = rect.y + rect.height - y;
var second_scaled = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
}
if(first.first){
calculateWindowLayout(first, first_scaled);
} else {
x1 = first_scaled.x;
y1 = first_scaled.y;
ret[first.id()] = [x1,y1,scale];
}
if(second.first){
calculateWindowLayout(second, second_scaled);
} else {
x1 = second_scaled.x;
y1 = second_scaled.y;
ret[second.id()] = [x1,y1,scale];
}
}
calculateWindowLayout(topmost_group, scaled_group_rect);
return ret;
}
}
const OverviewModifier36 = function(gsWorkspace, extension){
this.gsWorkspace = gsWorkspace;
this.extension = extension;
this.log = Log.getLogger("OverviewModifier36");
this.computeNumWindowSlots = function(){
let clones = this.gsWorkspace._windows.slice();
this.computeGroupData(clones);
return this.groupOrder.length;
}
this._prevComputeWindowLayout = function(prevComputeWindowLayout, outer_rect, workspace, slot){
var fakeWindow = {
get_outer_rect: function(){return outer_rect;}
,get_workspace: function(){
return workspace;
}
}
return prevComputeWindowLayout(fakeWindow, slot);
}
this.computeWindowSlots = function(numSlots, prev, prevComputeWindowLayout){
let groupSlot = {};
if(numSlots < 3){
let basicWindowSlots = prev(numSlots);
for(var i=0; i<this.groupOrder.length; i++){
var group = this.groupOrder[i];
var slot = basicWindowSlots[i];
groupSlot[group] = slot;
}
} else {
let singleWeight = 1.;
let groupedWeight = 1.5;
let numberOfWindows = this.groupedSlots.length + this.singleSlots.length;
let slots = [];
//if(this.log.is_debug()) this.log.debug("this.clones.length : " + this.clones.length);
//if(this.log.is_debug()) this.log.debug("numberOfWindows : " + numberOfWindows);
let gridWidth = Math.ceil(Math.sqrt(numberOfWindows));
let gridHeight = Math.ceil(numberOfWindows / gridWidth);
let gridWidthRest = numberOfWindows % gridWidth;
let gridWidthSub = 0;
if(gridWidthRest > 0 && gridHeight > (gridWidth - gridWidthRest)){
gridWidthSub = gridWidth - gridWidthRest;
}
//if(this.log.is_debug()) this.log.debug("gridWidth : " + gridWidth);
//if(this.log.is_debug()) this.log.debug("gridHeight : " + gridHeight);
//if(this.log.is_debug()) this.log.debug("gridWidthRest : " + gridWidthRest);
//if(this.log.is_debug()) this.log.debug("this.groupedSlots.length : " + this.groupedSlots.length);
//if(this.log.is_debug()) this.log.debug("this.groupedSlots.length : " + this.singleSlots.length);
var col=0;
var row=0;
var colIdx = 0;
var rowIdx = 0;
var singleSlotIdx = 0;
let xCenter, yCenter, fraction, slot, singleSlot;
let nextSlots, groupedSlots, singleSlots, nextSlotsIds, currentGrouped = 0, currentSingle = 0;
let currentHeight, currentWidth, fractionW, fractionH;
let log = this.log;
let addToRow = Lang.bind(this, function(groupedSlots, ret){
for(let j=0; j<groupedSlots.length; j++){
let groupedSlot = groupedSlots[j];
fractionW = currentWidth * 0.95;
fractionH = currentHeight * 0.95;
xCenter = currentWidth/ 2. + col;
yCenter = currentHeight/ 2. + row;
slot = [xCenter, yCenter, fractionW, fractionH];
//if(log.is_debug()) log.debug("slot : " + slot);
ret.push(slot);
groupSlot[groupedSlot] = slot;
col += currentWidth;
}
});
let calculateNextRow = Lang.bind(this, function(){
let ret = [];
let gridWidth1 = gridWidth;
if(rowIdx < gridWidthSub){
gridWidth1--;
}
groupedSlots = this.groupedSlots.slice(currentGrouped, currentGrouped + gridWidth1);
currentGrouped += groupedSlots.length;
singleSlots = this.singleSlots.slice(currentSingle, currentSingle + (gridWidth1 - groupedSlots.length));
currentSingle += singleSlots.length;
//if(log.is_debug()) log.debug("groupedSlots : " + groupedSlots);
//if(log.is_debug()) log.debug("singleSlots : " + singleSlots);
currentHeight = singleWeight / gridHeight;
currentWidth = singleWeight / gridWidth1;
if(groupedSlots.length>0){
currentHeight = groupedWeight / gridHeight;
currentWidth = groupedWeight / gridWidth1;
}
addToRow(groupedSlots, ret);
currentWidth = singleWeight / gridWidth1;
addToRow(singleSlots, ret);
if(col > singleWeight){
for(let j=0; j<ret.length; j++){
let slot1 = ret[j];
slot1[0] = slot1[0] / col * singleWeight;
slot1[2] = slot1[2] / col * singleWeight;
}
} else {
for(let j=0; j<ret.length; j++){
let slot1 = ret[j];
slot1[0] += (singleWeight - col) / 2.;
}
}
for(let j=0; j<ret.length; j++){
let slot1 = ret[j];
//if(log.is_debug()) log.debug("slot1 : " + slot1);
}
return ret;
});
while(!nextSlots || nextSlots.length > 0){
nextSlots = calculateNextRow();
if(nextSlots.length > 0){
slots = slots.concat(nextSlots);
row += currentHeight;
rowIdx++;
col = 0;
}
}
for(let j=0; j<slots.length; j++){
let slot1 = slots[j];
slot1[1] = slot1[1] / row;
slot1[3] = slot1[3] / row;
}
}
var ret = [];
for(var i=0; i<this.clones.length; i++){
var cloneId = this.clones[i];
var cloneGroup = this.cloneGroup[cloneId];
var cloneSlot = groupSlot[cloneGroup];
ret.push(cloneSlot);
}
return ret;
}
this.getSlotGeometry = function(slot, workspace, prev){
if(slot.length == 3){
return prev(slot);
}
let [xCenter, yCenter, fractionW, fractionH] = slot;
let width = workspace._width * fractionW;
let height = workspace._height * fractionH;
let x = workspace._x + xCenter * workspace._width - width / 2 ;
let y = workspace._y + yCenter * workspace._height - height / 2;
return [x, y, width, height];
}
this.computeWindowLayout = function(metaWindow, slot, prev){
var myWindow = this.extension.get_window(metaWindow);
if(!myWindow.group || myWindow.is_maximized()){
return prev(metaWindow, slot);
} else {
var topmost_group = myWindow.group.get_topmost_group();
var id = topmost_group.id();
if(this.groupWindowLayouts[id]){
return this.groupWindowLayouts[id][myWindow.id()];
} else {
var outer_rect = topmost_group.outer_rect(true);
let [x,y,scale] = this._prevComputeWindowLayout(prev, outer_rect, metaWindow.get_workspace(), slot);
let width = outer_rect.width * scale;
let height = outer_rect.height * scale;
var scaled_group_rect = new Meta.Rectangle({ x: x, y: y, width: width, height: height});
let groupWindowLayout = this.calculateGroupWindowLayouts(topmost_group, scaled_group_rect, scale);
this.groupWindowLayouts[id] = groupWindowLayout;
return groupWindowLayout[myWindow.id()];
}
}
}
this.orderWindowsByMotionAndStartup = function(clones, slots){
let arraySequences = {}
for(let j=0; j<clones.length; j++){
let clone = clones[j];
arraySequences[clone.metaWindow.get_stable_sequence()] = j;
}
clones.sort(function(w1, w2) {
return w2.metaWindow.get_stable_sequence() - w1.metaWindow.get_stable_sequence();
});
let ret = [];
for(let j=0; j<clones.length; j++){
let clone = clones[j];
let arraySequence = arraySequences[clone.metaWindow.get_stable_sequence()];
ret.push(slots[arraySequence]);
}
slots.splice(0, slots.length);
for(let j=0; j<ret.length; j++){
let slot = ret[j];
slots.push(slot);
}
return clones;
};
}
OverviewModifier36.prototype = new OverviewModifierBase();
const OverviewModifier38 = function(extension){
this.extension = extension;
this.log = Log.getLogger("OverviewModifier38");
this.computeWindowSlots = function(windows, prev){
let [windows1, windowsIds1] = this.simplifyWindows(windows);
var slots = prev(windows1);
return this.explodeSlots(slots);
}
}
OverviewModifier38.prototype = new OverviewModifierBase();
const OverviewModifier310 = function(extension){
this.extension = extension;
this.log = Log.getLogger("OverviewModifier310");
this.computeLayout = function(windows, prev){
var me = this;
let [windows1, windowsIds1] = this.simplifyWindows(windows);
var layout = prev(windows1);
var prevComputeWindowSlots = layout.strategy.computeWindowSlots;
layout.strategy.computeWindowSlots = function(layout, area){
var prevC = Lang.bind(this, prevComputeWindowSlots);
var slots = prevC(layout, area);
return me.explodeSlots(slots);
}
return layout;
}
}
OverviewModifier310.prototype = new OverviewModifierBase();
const OverviewModifier = function(){};
OverviewModifier.register = function(extension){
if(OverviewModifier._registered) return;
var prevComputeAllWindowSlots = GSWorkspace.prototype._computeAllWindowSlots;
var prevDestroy = GSWorkspace.prototype.destroy;
var prevComputeWindowLayout = GSWorkspace.prototype._computeWindowLayout;
var prevOrderWindowsByMotionAndStartup = GSWorkspace.prototype._orderWindowsByMotionAndStartup
var prevGetSlotGeometry = GSWorkspace.prototype._getSlotGeometry;
var prevComputeLayout = GSWorkspace.prototype._computeLayout;
let version36 = Util.versionCompare(Config.PACKAGE_VERSION, "3.6") >= 0 && Util.versionCompare(Config.PACKAGE_VERSION, "3.7") < 0;
version36 = version36 && prevComputeAllWindowSlots;
version36 = version36 && prevDestroy;
version36 = version36 && prevComputeWindowLayout;
version36 = version36 && prevOrderWindowsByMotionAndStartup;
version36 = version36 && prevGetSlotGeometry;
let version38 = Util.versionCompare(Config.PACKAGE_VERSION, "3.7") >= 0 && Util.versionCompare(Config.PACKAGE_VERSION, "3.9") < 0;
version38 = version38 && prevComputeAllWindowSlots;
let version310 = Util.versionCompare(Config.PACKAGE_VERSION, "3.9") >= 0;
version310 = version310 && prevComputeLayout;
if(version36){
GSWorkspace.prototype._computeAllWindowSlots = function(totalWindows){
var prev = Lang.bind(this, prevComputeAllWindowSlots);
if(!extension.enabled) return prev(totalWindows);
this._shellTileOverviewModifier = new OverviewModifier36(this, extension);
var numSlots = this._shellTileOverviewModifier.computeNumWindowSlots();
var prevComputeWindowLayout1 = Lang.bind(this, prevComputeWindowLayout);
return this._shellTileOverviewModifier.computeWindowSlots(numSlots, prev, prevComputeWindowLayout1);
}
GSWorkspace.prototype.destroy = function(){
var prev = Lang.bind(this, prevDestroy);
if(!extension.enabled) return prev();
delete this._shellTileOverviewModifier;
return prev();
}
GSWorkspace.prototype._computeWindowLayout = function(metaWindow, slot){
let prev = Lang.bind(this, prevComputeWindowLayout);
if(!extension.enabled) return prev(metaWindow, slot);
return this._shellTileOverviewModifier.computeWindowLayout(metaWindow, slot, prev);
}
GSWorkspace.prototype._orderWindowsByMotionAndStartup = function(clones, slots) {
let prev = Lang.bind(this, prevOrderWindowsByMotionAndStartup);
if(!extension.enabled) return prev(clones, slots);
return this._shellTileOverviewModifier.orderWindowsByMotionAndStartup(clones, slots);
}
GSWorkspace.prototype._getSlotGeometry = function(slot){
let prev = Lang.bind(this, prevGetSlotGeometry);
if(!extension.enabled) return prev(slot);
return this._shellTileOverviewModifier.getSlotGeometry(slot, this, prev);
}
} else if(version38){
GSWorkspace.prototype._computeAllWindowSlots = function(windows){
var prev = Lang.bind(this, prevComputeAllWindowSlots);
if(!extension.enabled) return prev(windows);
this._shellTileOverviewModifier = new OverviewModifier38(extension);
return this._shellTileOverviewModifier.computeWindowSlots(windows, prev);
}
} else if(version310){
GSWorkspace.prototype._computeLayout = function(windows){
var prev = Lang.bind(this, prevComputeLayout);
if(!extension.enabled) return prev(windows);
this._shellTileOverviewModifier = new OverviewModifier310(extension);
return this._shellTileOverviewModifier.computeLayout(windows, prev);
}
}
OverviewModifier._registered = true;
}