-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtiling.js
1832 lines (1499 loc) · 66.9 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
const Meta = imports.gi.Meta;
const St = imports.gi.St;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Clutter = imports.gi.Clutter;
const Tweener = imports.tweener && imports.tweener.tweener || imports.ui.tweener;
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
const Log = Extension.imports.logger.Logger.getLogger("ShellTile");
const Window = Extension.imports.window.Window;
const FakeWindow = Extension.imports.fakewindow.FakeWindow;
const Gdk = imports.gi.Gdk
const Config = imports.misc.config;
const Util = Extension.imports.util;
var version310 = Util.versionCompare(Config.PACKAGE_VERSION, "3.9") >= 0;
var WindowGroup = class WindowGroup{
constructor(first, second, type, splitPercent, extension){
if (!splitPercent) splitPercent = 0.5;
this.extension = extension;
this.first = first;
this.second = second;
this.type = type
this.splitPercent = splitPercent;
this.log = Log.getLogger("WindowGroup");
this.group = null;
}
static calc_split_percent (win_rect, cursor_rect, corner, type, num, tot, log){
if (corner == 0) var percwidth = cursor_rect.y - (win_rect.y + num / tot * 0.5 * win_rect.height);
else if (corner == 1) var percwidth = cursor_rect.x - (win_rect.x + 0.5 * win_rect.width + (tot - num - 1) / tot * 0.5 * win_rect.width);
else if (corner == 2) var percwidth = cursor_rect.y - (win_rect.y + 0.5 * win_rect.height + (tot - num - 1) / tot * 0.5 * win_rect.height);
else if (corner == 3) var percwidth = cursor_rect.x - (win_rect.x + num / tot * 0.5 * win_rect.width);
var perc = type == WindowGroup.VERTICAL_GROUP ? percwidth / win_rect.height * 2 * tot : percwidth / win_rect.width * 2 * tot;
if (perc > 0.999) perc = 0.999;
if (perc < 0.001) perc = 0.001;
return perc;
}
static find_cloned_child (original, clone, child_to_find){
if (original === child_to_find) return clone;
else if (original.first){
var ret = WindowGroup.find_cloned_child(original.first, clone.first, child_to_find);
if (ret) return ret;
var ret = WindowGroup.find_cloned_child(original.second, clone.second, child_to_find);
if (ret) return ret;
}
}
gap_between_windows (){
let ret = this.extension.gap_between_windows;
if (ret === undefined) ret = 10;
return ret;
}
toString (){
return "WindowGroup(first=" + this.first + ",second=" + this.second + ",type=" + this.type + ",splitPercent=" + this.splitPercent + ")";
}
id (){
return "(" + this.first.id() + "," + this.second.id() + ")";
}
ids (){
var ret = [];
if (this.first.ids) ret = ret.concat(this.first.ids());
else ret.push(this.first.id());
if (this.second.ids) ret = ret.concat(this.second.ids());
else ret.push(this.second.id());
return ret;
}
has_real_window (){
return true;
}
has_hole (){
return this.first.has_hole() || this.second.has_hole();
}
get_maximized_bounds (cursor){
if (this.first.has_real_window()) return this.first.get_maximized_bounds(cursor);
else return this.second.get_maximized_bounds(cursor);
}
get_workspace (){
if (this.first.has_real_window()) return this.first.get_workspace();
else return this.second.get_workspace();
}
async maximize_size (cursor){
var bounds = this.get_maximized_bounds(cursor);
return this.move_resize(bounds.x, bounds.y, bounds.width, bounds.height);
}
async switch_maximized (){
if (this._last_bounds){
return await this.unmaximize();
} else {
return await this.maximize();
}
}
async maximize (){
this.forget_last_bounds();
this._last_bounds = this.outer_rect();
await this.maximize_size();
this.save_bounds();
}
is_maximized (){
return false;
}
async unmaximize (){
if (this._last_bounds){
var bounds = this._last_bounds;
await this.move_resize(bounds.x, bounds.y, bounds.width, bounds.height);
this.save_bounds();
}
this.forget_last_bounds();
}
forget_last_bounds (){
if (this.first.forget_last_bounds) this.first.forget_last_bounds();
if (this.second.forget_last_bounds) this.second.forget_last_bounds();
if (this._last_bounds) delete this._last_bounds;
}
save_bounds (){
this.save_position();
this.save_size();
}
save_position (){
this.saved_position = this.outer_rect();
this.first.save_position();
this.second.save_position();
}
save_size (){
this.saved_size = this.outer_rect();
this.first.save_size();
this.second.save_size();
}
real_outer_rect (){
return this.outer_rect();
}
outer_rect (for_preview){
var first_rect = this.first.outer_rect();
var second_rect = this.second.outer_rect();
if (for_preview){
if (this.first.is_maximized()) return second_rect;
else if (this.second.is_maximized()) return first_rect;
}
var xleft = first_rect.x < second_rect.x ? first_rect.x : second_rect.x;
var yleft = first_rect.y < second_rect.y ? first_rect.y : second_rect.y;
if ((first_rect.x + first_rect.width) > (second_rect.x + second_rect.width)){
var xright = first_rect.x + first_rect.width;
} else {
var xright = second_rect.x + second_rect.width;
}
if ((first_rect.y + first_rect.height) > (second_rect.y + second_rect.height)){
var yright = first_rect.y + first_rect.height;
} else {
var yright = second_rect.y + second_rect.height;
}
var x = xleft;
var y = yleft;
var width = xright - xleft;
var height = yright - yleft;
// var maximized_bounds = this.get_maximized_bounds();
// if(x < maximized_bounds.x) x = maximized_bounds.x;
// if(y < maximized_bounds.y) y = maximized_bounds.y;
// if(width > maximized_bounds.width) width = maximized_bounds.width;
// if(height > maximized_bounds.width) height = maximized_bounds.height;
return new Meta.Rectangle({
x: x,
y: y,
width: width,
height: height
});
}
width (){
return this.outer_rect().width;
}
height (){
return this.outer_rect().height;
}
update_split_percent (bounds, changed, changed_rect){
//if(this.log.is_debug()) this.log.debug("update_split_percent: " + [bounds.x, bounds.y, bounds.width, bounds.height]);
var splitPercent = this.splitPercent;
var changed_chosen = changed || this.first;
if(!changed_rect) changed_rect = changed_chosen.outer_rect();
if (changed_chosen === this.first){
if (this.type == WindowGroup.HORIZONTAL_GROUP){
//if(this.log.is_debug()) this.log.debug("horizontal split changed: " + this.toString());
splitPercent = changed_rect.width / bounds.width;
} else if (this.type == WindowGroup.VERTICAL_GROUP){
//if(this.log.is_debug()) this.log.debug("vertical split changed: " + this.toString());
splitPercent = changed_rect.height / bounds.height;
}
this.splitPercent = splitPercent;
} else if (changed_chosen === this.second){
if (this.type == WindowGroup.HORIZONTAL_GROUP){
//if(this.log.is_debug()) this.log.debug("horizontal split changed: " + this.toString());
splitPercent = 1 - ((changed_rect.width + this.gap_between_windows()) / bounds.width);
} else if (this.type == WindowGroup.VERTICAL_GROUP){
//if(this.log.is_debug()) this.log.debug("vertical split changed: " + this.toString());
splitPercent = 1 - ((changed_rect.height + this.gap_between_windows()) / bounds.height);
}
this.splitPercent = splitPercent;
}
if (this.group && !this.group.group && changed){
var is_first = this.group.first === this
var is_changed_first = this.first === changed;
var other_side = is_first ? this.group.second : this.group.first;
if (other_side.first){
var other = changed === this.first ? this.second : this.first;
var sibling = is_changed_first ? other_side.first : other_side.second;
var opposite = sibling === other_side.first ? other_side.second : other_side.first;
if (!sibling.has_real_window() && opposite.has_real_window() ||
sibling.has_real_window() && opposite.has_real_window() &&
!other.has_real_window()
){
other_side.splitPercent = this.splitPercent;
}
}
}
}
async update_geometry (win, win_rect){
var bounds = this.outer_rect();
if (win){
if(!win_rect) win_rect = win.outer_rect();
if (this.type == WindowGroup.HORIZONTAL_GROUP){
bounds.height = win_rect.height;
bounds.y = win_rect.y;
} else if (this.type == WindowGroup.VERTICAL_GROUP){
bounds.width = win_rect.width;
bounds.x = win_rect.x;
}
this.update_split_percent(bounds, win, win_rect);
}
if (this.group) await this.group.update_geometry(this, bounds);
else {
var saved_position = this.saved_position;
var saved_size = this.saved_size;
var bounds = this.outer_rect();
if (saved_position){
bounds.x = saved_position.x;
bounds.y = saved_position.y;
}
if (saved_size){
bounds.width = saved_size.width;
bounds.height = saved_size.height;
}
await this.move_resize(bounds.x, bounds.y, bounds.width, bounds.height);
}
//var bounds = this.outer_rect();
//this.update_split_percent(bounds, win);
if (this.splitPercent <= 0.05 && !this.first.has_real_window()){
await this.detach(this.first);
} else if (this.splitPercent >= 0.95 && !this.second.has_real_window()){
await this.detach(this.second);
}
}
check_min_size (
{first_x,first_y,first_width,first_height},
{second_x,second_y,second_width,second_height},
{width, height}){
const first_min_size = this.first.get_min_size();
const second_min_size = this.second.get_min_size();
if (this.type == WindowGroup.HORIZONTAL_GROUP){
let first_width_tmp = Math.max(first_width, first_min_size.width);
let first_diff = first_width_tmp - first_width;
second_x += first_diff;
second_width -= first_diff;
let second_width_tmp = Math.max(second_width, second_min_size.width);
let second_diff = second_width_tmp - second_width;
second_x -= second_diff;
first_width_tmp = Math.max(first_width_tmp - second_diff, first_min_size.width);
const min_second_x = first_x + this.gap_between_windows();
if (second_x + second_width_tmp > first_x + width){
second_x += first_x + width - second_x - second_width_tmp;
}
if (second_x < min_second_x) second_x = min_second_x;
first_width = first_width_tmp;
second_width = second_width_tmp;
} else if (this.type == WindowGroup.VERTICAL_GROUP){
let first_height_tmp = Math.max(first_height, first_min_size.height);
let first_diff = first_height_tmp - first_height;
second_y += first_diff;
second_height -= first_diff;
let second_height_tmp = Math.max(second_height, second_min_size.height);
let second_diff = second_height_tmp - second_height;
second_y -= second_diff;
first_height_tmp = Math.max(first_height_tmp - second_diff, first_min_size.height);
const min_second_y = first_y + this.gap_between_windows();
if (second_y + second_height_tmp > first_y + height){
second_y += first_y + height - second_y - second_height_tmp;
}
if (second_y < min_second_y) second_y = min_second_y;
first_height = first_height_tmp;
second_height = second_height_tmp;
}
return {first_x,first_y,first_width,first_height,second_x,second_y,second_width,second_height};
}
get_min_size (){
const first_min_size = this.first.get_min_size();
const second_min_size = this.second.get_min_size();
let ret_width = 1;
let ret_height = 1;
if (this.type == WindowGroup.HORIZONTAL_GROUP){
ret_width = Math.max(first_min_size.width, second_min_size.width + this.gap_between_windows());
ret_height = Math.max(first_min_size.height, second_min_size.height);
} else if (this.type == WindowGroup.VERTICAL_GROUP){
ret_width = Math.max(first_min_size.width, second_min_size.width);
ret_height = Math.max(first_min_size.height, second_min_size.height + this.gap_between_windows());
}
return {width: ret_width,height: ret_height};
}
async move_resize (x, y, width, height){
if (x === undefined || y === undefined || width === undefined || height === undefined){
return;
}
let first_width = width;
let second_width = width;
let first_height = height;
let second_height = height;
let first_x = x;
let second_x = x;
let first_y = y;
let second_y = y;
if (this.type == WindowGroup.HORIZONTAL_GROUP){
first_width = Math.round(width * this.splitPercent);
second_width = width - first_width - this.gap_between_windows();
second_x = first_x + first_width + this.gap_between_windows();
} else if (this.type == WindowGroup.VERTICAL_GROUP){
first_height = Math.round(height * this.splitPercent);
second_height = height - first_height - this.gap_between_windows();
second_y = first_y + first_height + this.gap_between_windows();
}
const checked_min_size = this.check_min_size(
{first_x,first_y,first_width,first_height},
{second_x,second_y,second_width,second_height},
{width, height});
first_x = checked_min_size.first_x;
first_y = checked_min_size.first_y;
first_width = checked_min_size.first_width;
first_height = checked_min_size.first_height;
second_x = checked_min_size.second_x;
second_y = checked_min_size.second_y;
second_width = checked_min_size.second_width;
second_height = checked_min_size.second_height;
//if(this.log.is_debug()) this.log.debug("first: " + this.first.toString() + " " + [first_x, first_y, first_width, first_height])
await this.first.move_resize(first_x, first_y, first_width, first_height);
var first_rect = this.first.outer_rect();
if (first_rect.width > first_width || first_rect.height > first_height){
if (this.type == WindowGroup.HORIZONTAL_GROUP){
if (first_rect.height > first_height){
second_height = first_rect.height;
}
if (first_rect.width > first_width){
var diff_w = first_rect.width - first_width;
second_x += diff_w;
second_width -= diff_w;
}
} else if (this.type == WindowGroup.VERTICAL_GROUP){
if (first_rect.width > first_width){
second_width = first_rect.width;
}
if (first_rect.height > first_height){
var diff_h = first_rect.height - first_height;
second_y += diff_h;
second_height -= diff_h;
}
}
}
//if(this.log.is_debug()) this.log.debug("second: " + this.second.toString() + " " + [second_x, second_y, second_width, second_height])
await this.second.move_resize(second_x, second_y, second_width, second_height);
var second_rect = this.second.outer_rect();
if (second_rect.width > second_width || second_rect.height > second_height){
if (this.type == WindowGroup.HORIZONTAL_GROUP){
if (second_rect.height > second_height){
first_height = second_rect.height;
}
if (second_rect.width > second_width){
var diff_w = second_rect.width - second_width;
first_width -= diff_w;
second_x -= diff_w;
const min_second_x = first_rect.x + this.gap_between_windows();
if (second_x < min_second_x) second_x = min_second_x;
}
} else if (this.type == WindowGroup.VERTICAL_GROUP){
if (second_rect.width > second_width){
first_width = second_rect.width;
}
if (second_rect.height > second_height){
var diff_h = second_rect.height - second_height;
first_height -= diff_h;
second_y -= diff_h;
const min_second_y = first_rect.y + this.gap_between_windows();
if (second_y < min_second_y) second_y = min_second_y;
}
}
await this.first.move_resize(first_x, first_y, first_width, first_height);
await this.second.move_resize(second_x, second_y, second_width, second_height);
}
this.update_split_percent(this.outer_rect());
}
move_to_workspace (workspace, descending){
if (!descending && this.group){
this.group.move_to_workspace(workspace);
}
if (this.first.meta_window && this.first.get_workspace() !== workspace){
this.first.move_to_workspace(workspace);
} else {
this.first.move_to_workspace(workspace, true);
}
if (this.second.meta_window && this.second.get_workspace() !== workspace){
this.second.move_to_workspace(workspace);
} else {
this.second.move_to_workspace(workspace, true);
}
}
resolve_promises (descending){
if(descending || !this.group){
this.first.resolve_promises(true);
this.second.resolve_promises(true);
} else if(this.group){
this.group.resolve_promises(false);
}
}
raise (ascending){
if (this.group && ascending){
this.group.raise(true);
} else {
if (!this.first.is_maximized()) this.first.raise();
if (!this.second.is_maximized()) this.second.raise();
}
}
get_windows (objects){
var ret = [];
if (this.first.get_windows){
ret = ret.concat(this.first.get_windows(objects));
} else {
if (objects) ret.push(this.first);
else ret.push(this.first.id());
}
if (this.second.get_windows){
ret = ret.concat(this.second.get_windows(objects));
} else {
if (objects) ret.push(this.second);
else ret.push(this.second.id());
}
return ret;
}
minimize (ascending){
if (this.group && ascending){
this.group.minimize(true);
} else {
if (!this.first.is_maximized()) this.first.minimize();
if (!this.second.is_maximized()) this.second.minimize();
}
}
unminimize (ascending){
if (this.group && ascending){
this.group.unminimize(true);
} else {
if (!this.first.is_maximized()) this.first.unminimize();
if (!this.second.is_maximized()) this.second.unminimize();
}
}
async reposition (){
var group = this.get_topmost_group();
var existing_size = group.outer_rect();
return group.move_resize(existing_size.x, existing_size.y, existing_size.width, existing_size.height);
}
async attach (win, replace, cursor){
if (this.first.group){
var withGroup = this.first;
} else {
var withGroup = this.second;
}
if (!this.extension.keep_maximized){
var existing = this.first === win ? this.second : this.first;
if (existing.group) existing = existing.group.get_topmost_group();
var existing_size = existing.outer_rect();
}
var prevGroup = withGroup.group;
if (prevGroup){
if (prevGroup.first === withGroup){
prevGroup.first = this;
} else {
prevGroup.second = this;
}
this.group = prevGroup;
}
this.first.group = this;
this.second.group = this;
if (replace){
var to_detach = this.group.first === this ? this.group.second : this.group.first;
await this.group.detach(to_detach);
}
var group = this.get_topmost_group();
if (this.extension.keep_maximized){
await group.maximize_size(cursor);
} else {
await group.move_resize(existing_size.x, existing_size.y, existing_size.width, existing_size.height);
}
group.save_bounds();
group.raise();
}
get_topmost_group (not_fake){
var group = this;
while (group.group){
group = group.group;
}
if (not_fake) group = group.get_first_non_fake_window();
return group;
}
get_first_non_fake_window (){
var curr = this;
while (true){
if (!curr.get_first_non_fake_window) break;
if (!curr.first.has_real_window()){
curr = curr.second;
} else if (!curr.second.has_real_window()){
curr = curr.first;
} else break;
}
return curr;
}
async detach (win, noop, replace_with){
var other = this.first === win ? this.second : this.first;
if (other && !other.has_real_window()) replace_with = null;
if (replace_with){
if (this.first === win){
delete this.first.group;
this.first = replace_with;
} else {
delete this.second.group;
this.second = replace_with;
}
replace_with.group = this;
if (!noop){
var group = this.get_topmost_group();
await group.update_geometry();
}
} else if (this.group){
if (this.group.first === this){
if (this.first === win){
this.group.first = this.second;
this.second.group = this.group;
} else {
this.group.first = this.first;
this.first.group = this.group;
}
} else if (this.group.second === this){
if (this.first === win){
this.group.second = this.second;
this.second.group = this.group;
} else {
this.group.second = this.first;
this.first.group = this.group;
}
}
delete win.group;
if (!noop){
var group = this.get_topmost_group();
await group.update_geometry();
}
delete this.group;
delete this.first;
delete this.second;
} else {
delete this.first.group;
delete this.second.group;
if (!noop && this.extension.keep_maximized){
var maxi = this.first;
if (win === this.first) maxi = this.second;
await maxi.maximize_size();
maxi.save_bounds();
}
delete this.first;
delete this.second;
}
}
preview_rect (win, cursor_rect){
var preview = [
[0, 0, 1, 0.5],
[0.5, 0, 0.5, 1],
[0, 0.5, 1, 0.5],
[0, 0, 0.5, 1]
]
var perc = this.splitPercent;
if (win === this.first){
var corner = this.type == WindowGroup.VERTICAL_GROUP ? 0 : 3;
var win_rect = this.second.real_outer_rect();
} else if (win === this.second){
var corner = this.type == WindowGroup.VERTICAL_GROUP ? 2 : 1;
var win_rect = this.first.real_outer_rect();
}
var currentpreview = preview[corner].slice();
if (corner == 0){
currentpreview[3] = perc;
} else if (corner == 1){
currentpreview[0] = perc;
currentpreview[2] = 1 - perc;
} else if (corner == 2){
currentpreview[1] = perc;
currentpreview[3] = 1 - perc;
} else if (corner == 3){
currentpreview[2] = perc;
}
var preview_x = win_rect.x + currentpreview[0] * win_rect.width;
var preview_y = win_rect.y + currentpreview[1] * win_rect.height;
var preview_width = currentpreview[2] * win_rect.width;
var preview_height = currentpreview[3] * win_rect.height;
return new Meta.Rectangle({
x: preview_x,
y: preview_y,
width: preview_width,
height: preview_height
});
}
top_left_window (){
if (this.type == WindowGroup.HORIZONTAL_GROUP){
if (this.first.top_left_window) return this.first.top_left_window();
else if (this.first.has_real_window()) return this.first;
else if (this.second.top_left_window) return this.second.top_left_window();
else return this.second;
} else {
if (this.first.top_left_window) return this.first.top_left_window();
else if (this.first.has_real_window()) return this.first;
else if (this.second.top_left_window) return this.second.top_left_window();
else return this.second;
}
}
async clone (cursor){
if (!this.first.first){
var first = new FakeWindow(this.extension, this.first.has_real_window() ? this.first : undefined);
} else var first = await this.first.clone(cursor);
if (!this.second.first){
var second = new FakeWindow(this.extension, this.second.has_real_window() ? this.second : undefined);
} else var second = await this.second.clone(cursor);
var ret = new WindowGroup(first, second, this.type, this.splitPercent, this.extension);
await ret.attach(undefined, undefined, cursor);
return ret;
}
}
WindowGroup.HORIZONTAL_GROUP = "horizontal";
WindowGroup.VERTICAL_GROUP = "vertical";
var DefaultTilingStrategy = class DefaultTilingStrategy{
constructor(ext){
this.extension = ext;
this.log = Log.getLogger("DefaultTilingStrategy");
this.lastTime = null;
this.lastTimeCtrlPressed = null;
this.lastTimeShiftPressed = null;
this.preview = new St.BoxLayout({
style_class: 'tile-preview'
});
this.preview_for_edge_tiling = false;
this.preview.visible = false;
this._compatibility = this.extension.compatibility;
Main.uiGroup.add_actor(this.preview);
}
is_ctrl_pressed (){
//if(this.log.is_debug()) this.log.debug("Modifier key: " + this.extension.tile_modifier_key);
var modifiers = Clutter.ModifierType.CONTROL_MASK; // Default: Ctrl only
if (this.extension.tile_modifier_key === 'Super')
modifiers = Clutter.ModifierType.MOD4_MASK;
if (this.extension.tile_modifier_key === 'Ctrl or Super')
modifiers = modifiers | Clutter.ModifierType.MOD4_MASK;
let [,,mods] = global.get_pointer();
var ret = mods & modifiers;
if (ret){
this.lastTimeCtrlPressed = new Date().getTime();
} else {
var currTime = new Date().getTime();
if (this.lastTimeCtrlPressed && (currTime - this.lastTimeCtrlPressed) < 500){
ret = true;
}
}
return ret;
}
is_shift_pressed (){
let [x, y, mods] = global.get_pointer();
var ret = mods & Clutter.ModifierType.SHIFT_MASK;
if (ret){
this.lastTimeShiftPressed = new Date().getTime();
} else {
var currTime = new Date().getTime();
if (this.lastTimeShiftPressed && (currTime - this.lastTimeShiftPressed) < 500){
ret = true;
}
}
return ret;
}
async check_after_move (moving){
if (this._check_after_move){
//if(this.log.is_debug()) this.log.debug("check after move");
for (var i = 0; i < this._check_after_move.length; i++){
var c = this._check_after_move[i];
//if(this.log.is_debug()) this.log.debug("check after move1");
if (!c.group && c.after_group) await c.after_group(c === moving);
}
delete this._check_after_move;
}
}
async on_window_move (win){
//if(!win._dragging) return;
//win.unmaximize();
var me = this;
if(me.__timeout) return;
win.raise();
var currTime = new Date().getTime();
var interval = 200
if(this.lastTime && (currTime - this.lastTime) < interval){
var remaining = this.lastTime + interval - currTime + 10;
await new Promise(resolve => {
me.__timeout = this.extension.timeout_add(remaining, function (){
resolve();
return false;
});
});
if(!me.__timeout) return;
else delete me.__timeout;
}
var is_ctrl_pressed = !!this.is_ctrl_pressed();
var detach_window = is_ctrl_pressed != !!this.extension.keep_maximized;
if (!win.group){
this.lastTime = currTime;
var preview_rect = null;
if (win && is_ctrl_pressed){
var window_under = this.get_window_under(win);
if (window_under){
var groupPreview = this.get_window_group_preview(window_under, win);
if (groupPreview){
var preview_rect = groupPreview.preview_rect(win, this.get_cursor_rect());
groupPreview.first = null;
groupPreview.second = null;
//if(this.log.is_debug()) this.log.debug("preview_rect: " + preview_rect);
}
}
}
var for_edge_tiling = !preview_rect;
if (for_edge_tiling){
var preview_rect = await this.get_edge_preview(win);
}
this.preview_for_edge_tiling = for_edge_tiling;
this.update_preview(preview_rect);
} else if (win.group && detach_window && win.has_moved_enough_for_detach()){
if (!this._check_after_move) this._check_after_move = [];
this._check_after_move = this._check_after_move.concat([win.group.first, win.group.second]);
await this.detach_window(win);
win.raise();
}
}
async on_window_moved (win){
delete this.__timeout;
this.update_preview(null);
if (win.group){
await win.update_geometry(true, false);
win.raise();
} else {
var is_ctrl_pressed = this.is_ctrl_pressed();
var window_under = null;
var group_preview = null;
if (is_ctrl_pressed || (!this.preview_for_edge_tiling && this.preview.visible)){
var window_under = this.get_window_under(win);
if (window_under){
var group_preview = this.get_window_group_preview(window_under, win);
}
}
if (group_preview){
if (win.group) await win.group.detach(win);
else {
win.before_group();
window_under.before_group();
}
var is_maximized = window_under.is_maximized();
await window_under.unmaximize();
await group_preview.attach(win);
if (is_maximized){
var topmost = group_preview.get_topmost_group();
await topmost.maximize_size();
}
} else {
var preview_rect = await this.get_edge_preview(win, undefined, true);
if (preview_rect){
if (!preview_rect.type){
if (preview_rect.maximize) await win.maximize();
else await win.move_resize(preview_rect.x, preview_rect.y, preview_rect.width, preview_rect.height);
}
} else if(win.has_moved_enough_for_detach()){
this.check_after_move(win);
}
}
}
}
async on_accelerator (accel){
var meta_window = this._compatibility.get_display().focus_window;
if(accel) accel = accel.slice(5);
if (!meta_window) return;
var me = this;
var hasLastCode = !!this.lastCode;
var win = this.extension.get_window(meta_window, true);
var position = this.get_accelerator_position(accel);
var preview_rect = await this.get_edge_preview(win, position);
var apply = async (preview_rect, position) => {
await this.detach_window(win);
await win.unmaximize();
if(this.extension.grouping_edge_tiling){
await this.get_edge_tiling(win, position, false);
} else {
await win.move_resize(preview_rect.x, preview_rect.y, preview_rect.width, preview_rect.height);
}
}
if (preview_rect){
if (!this.__accelerator_timeout){
this.__accelerator_timeout = this.extension.timeout_add(DefaultTilingStrategy.ACCELERATOR_TIMEOUT + 10, () => {
if (!this.__accelerator_timeout) return false;
apply(preview_rect, position);
delete this.__accelerator_timeout;
return false;
});
} else if (hasLastCode){
this.extension.timeout_cancel(this.__accelerator_timeout);
delete me.__accelerator_timeout;
apply(preview_rect, position);
}
}
}
update_preview (preview_rect){
if (preview_rect){
if (!this.last_preview_rect || !this.last_preview_rect.equal(preview_rect)){
this.preview.visible = true;
Tweener.removeTweens(this.preview);
Tweener.addTween(this.preview, {
time: 0.125,
opacity: 255,
visible: true,
transition: 'easeOutQuad',
x: preview_rect.x,
y: preview_rect.y,
width: preview_rect.width,
height: preview_rect.height
});
// this.preview.x = preview_rect.x;
// this.preview.y = preview_rect.y;
// this.preview.width = preview_rect.width;
// this.preview.height = preview_rect.height;
} else {