-
Notifications
You must be signed in to change notification settings - Fork 62
/
extension.js
1843 lines (1518 loc) · 56.7 KB
/
extension.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
/*****************************************************************
This extension has been developed by vibou
With the help of the gnome-shell community
Edited by Kvis for gnome 3.8
Edited by Lundal for gnome 3.18
Edited by Sergey to add keyboard shortcuts and prefs dialog
******************************************************************/
/*****************************************************************
CONST & VARS
*****************************************************************/
// Library imports
const St = imports.gi.St;
const Main = imports.ui.main;
const Shell = imports.gi.Shell;
const WindowManager = imports.ui.windowManager;
const MessageTray = imports.ui.messageTray;
const Lang = imports.lang;
const PanelMenu = imports.ui.panelMenu;
const DND = imports.ui.dnd;
const Meta = imports.gi.Meta;
const Clutter = imports.gi.Clutter;
const Signals = imports.signals;
const Tweener = imports.ui.tweener;
const Workspace = imports.ui.workspace;
// Extension imports
const Extension = imports.misc.extensionUtils.getCurrentExtension();
const Settings = Extension.imports.settings;
const hotkeys = Extension.imports.hotkeys;
// Globals
const SETTINGS_GRID_SIZES = 'grid-sizes';
const SETTINGS_AUTO_CLOSE = 'auto-close';
const SETTINGS_ANIMATION = 'animation';
const SETTINGS_TOP_PANEL = 'top-panel';
const SETTINGS_BOTTOM_PANEL = 'bottom-panel';
const SETTINGS_GLOBAL_PRESETS = 'global-presets';
const SETTINGS_WINDOW_MARGIN = 'window-margin';
const SETTINGS_INSETS_PRIMARY = 'insets-primary';
const SETTINGS_INSETS_PRIMARY_LEFT = 'insets-primary-left';
const SETTINGS_INSETS_PRIMARY_RIGHT = 'insets-primary-right';
const SETTINGS_INSETS_PRIMARY_TOP = 'insets-primary-top';
const SETTINGS_INSETS_PRIMARY_BOTTOM = 'insets-primary-bottom';
const SETTINGS_INSETS_SECONDARY = 'insets-secondary';
const SETTINGS_INSETS_SECONDARY_LEFT = 'insets-secondary-left';
const SETTINGS_INSETS_SECONDARY_RIGHT = 'insets-secondary-right';
const SETTINGS_INSETS_SECONDARY_TOP = 'insets-secondary-top';
const SETTINGS_INSETS_SECONDARY_BOTTOM = 'insets-secondary-bottom';
const SETTINGS_DEBUG = 'debug';
let status;
let launcher;
let grids;
let monitors;
let tracker;
let nbCols;
let nbRows;
let area;
let focusMetaWindow = false;
let focusWindowActor = false;
let focusMetaWindowConnections = new Array();
let focusMetaWindowPrivateConnections = new Array();
let tracker;
let gridSettings = new Object();
let settings = Settings.get();
let toggleSettingListener;
let keyControlBound = false;
let debug = false;
// Hangouts workaround
let excludedApplications = new Array(
"Unknown"
);
const key_bindings = {
'show-toggle-tiling': function() { toggleTiling(); }
};
const key_bindings_tiling = {
'move-left' : function() { keyMoveResizeEvent('move' , 'left' );},
'move-right' : function() { keyMoveResizeEvent('move' , 'right');},
'move-up' : function() { keyMoveResizeEvent('move' , 'up' );},
'move-down' : function() { keyMoveResizeEvent('move' , 'down' );},
'resize-left' : function() { keyMoveResizeEvent('resize', 'left' );},
'resize-right' : function() { keyMoveResizeEvent('resize', 'right');},
'resize-up' : function() { keyMoveResizeEvent('resize', 'up' );},
'resize-down' : function() { keyMoveResizeEvent('resize', 'down' );},
'cancel-tiling' : function() { keyCancelTiling();},
'set-tiling' : function() { keySetTiling() ;},
'change-grid-size': function() { keyChangeTiling();}
}
const key_bindings_presets = {
'preset-resize-1' : function() { presetResize(1) ;},
'preset-resize-2' : function() { presetResize(2) ;},
'preset-resize-3' : function() { presetResize(3) ;},
'preset-resize-4' : function() { presetResize(4) ;},
'preset-resize-5' : function() { presetResize(5) ;},
'preset-resize-6' : function() { presetResize(6) ;},
'preset-resize-7' : function() { presetResize(7) ;},
'preset-resize-8' : function() { presetResize(8) ;},
'preset-resize-9' : function() { presetResize(9) ;},
'preset-resize-10': function() { presetResize(10) ;},
'preset-resize-11': function() { presetResize(11) ;},
'preset-resize-12': function() { presetResize(12) ;},
'preset-resize-13': function() { presetResize(13) ;},
'preset-resize-14': function() { presetResize(14) ;},
'preset-resize-15': function() { presetResize(15) ;},
'preset-resize-16': function() { presetResize(16) ;},
'preset-resize-17': function() { presetResize(17) ;},
'preset-resize-18': function() { presetResize(18) ;},
'preset-resize-19': function() { presetResize(19) ;},
'preset-resize-20': function() { presetResize(20) ;}
}
function log(log_string) {
if(debug) {
global.log("gTile " + log_string);
}
}
const GTileStatusButton = new Lang.Class({
Name: 'GTileStatusButton',
Extends: PanelMenu.Button,
_init: function(classname) {
this.parent(0.0, "gTile", false);
this.actor.add_style_class_name(classname);
//Done by default in PanelMenuButton - Just need to override the method
this.actor.connect('button-press-event', Lang.bind(this, this._onButtonPress));
},
reset: function() {
this.activated = false;
launcher.actor.remove_style_pseudo_class('activate');
},
activate: function() {
launcher.actor.add_style_pseudo_class('activate');
},
deactivate: function() {
launcher.actor.remove_style_pseudo_class('activate');
},
_onButtonPress: function(actor, event) {
log("Click Toggle Status on system panel");
toggleTiling();
},
_destroy: function() {
this.activated = null;
}
});
/*****************************************************************
SETTINGS
*****************************************************************/
function parseTuple(format, delimiter) {
// parsing grid size in format XdelimY, like 6x4 or 1:2
let gssk = format.split(delimiter);
if(gssk.length != 2
|| isNaN(gssk[0]) || gssk[0] < 0 || gssk[0] > 40
|| isNaN(gssk[1]) || gssk[1] < 0 || gssk[1] > 40) {
log("Bad format " + format + ", delimiter " + delimiter);
return {X: Number(-1), Y: Number(-1)};
}
//log("Parsed format " + gssk[0] + delimiter + gssk[1]);
return {X: Number(gssk[0]), Y: Number(gssk[1]) };
}
function initGridSizes(grid_sizes) {
gridSettings[SETTINGS_GRID_SIZES] = [
new GridSettingsButton('4x4',4,4),
new GridSettingsButton('6x4',6,4),
new GridSettingsButton('8x6',8,6),
];
let grid_sizes_orig = true;
let gss = grid_sizes.split(",");
for (var key in gss) {
let grid_format = parseTuple(gss[key], "x");
if(grid_format.X == -1) {
continue;
}
if(grid_sizes_orig) {
gridSettings[SETTINGS_GRID_SIZES] = [];
grid_sizes_orig = false;
}
gridSettings[SETTINGS_GRID_SIZES].push(new GridSettingsButton(grid_format.X + "x" + grid_format.Y, grid_format.X, grid_format.Y));
}
}
function getBoolSetting (settings_string) {
gridSettings[settings_string] = settings.get_boolean(settings_string);
if(gridSettings[settings_string] === undefined) {
log("Undefined settings " + settings_string);
gridSettings[settings_string] = false;
} else {
log(settings_string + " set to " + gridSettings[settings_string]);
}
}
function getIntSetting (settings_string) {
let iss = settings.get_int(settings_string);
if(iss === undefined) {
log("Undefined settings " + settings_string);
return 0;
} else {
log(settings_string + " set to " + iss);
return iss;
}
}
function initSettings() {
let gridSizes = settings.get_string(SETTINGS_GRID_SIZES);
log(SETTINGS_GRID_SIZES + " set to " + gridSizes);
initGridSizes(gridSizes);
getBoolSetting(SETTINGS_AUTO_CLOSE);
getBoolSetting(SETTINGS_ANIMATION);
getBoolSetting(SETTINGS_TOP_PANEL);
getBoolSetting(SETTINGS_BOTTOM_PANEL);
getBoolSetting(SETTINGS_GLOBAL_PRESETS);
getBoolSetting(SETTINGS_DEBUG);
gridSettings[SETTINGS_WINDOW_MARGIN] = getIntSetting(SETTINGS_WINDOW_MARGIN);
gridSettings[SETTINGS_INSETS_PRIMARY] =
{ top: getIntSetting(SETTINGS_INSETS_PRIMARY_TOP),
bottom: getIntSetting(SETTINGS_INSETS_PRIMARY_BOTTOM),
left: getIntSetting(SETTINGS_INSETS_PRIMARY_LEFT),
right: getIntSetting(SETTINGS_INSETS_PRIMARY_RIGHT) }; // Insets on primary monitor
gridSettings[SETTINGS_INSETS_SECONDARY] =
{ top: getIntSetting(SETTINGS_INSETS_SECONDARY_TOP),
bottom: getIntSetting(SETTINGS_INSETS_SECONDARY_BOTTOM),
left: getIntSetting(SETTINGS_INSETS_SECONDARY_LEFT),
right: getIntSetting(SETTINGS_INSETS_SECONDARY_RIGHT) };
}
/*****************************************************************
FUNCTIONS
*****************************************************************/
function init() {
}
function enable() {
status = false;
monitors = Main.layoutManager.monitors;
tracker = Shell.WindowTracker.get_default();
area = new St.BoxLayout({style_class: 'grid-preview'});
Main.uiGroup.add_actor(area);
log("Create Button");
launcher = new GTileStatusButton('tiling-icon');
log("Init settings");
initSettings();
debug = gridSettings[SETTINGS_DEBUG];
// initialize these from settings, the first set of sizes
nbCols = gridSettings[SETTINGS_GRID_SIZES][0].cols;
nbRows = gridSettings[SETTINGS_GRID_SIZES][0].rows;
log("Init Grids");
initGrids();
log("Starting...");
//global.display.connect('notify::focus-window', Lang.bind(this, _onFocus));
Main.panel.addToStatusArea("GTileStatusButton", launcher);
hotkeys.bind(key_bindings);
if(gridSettings[SETTINGS_GLOBAL_PRESETS]) {
hotkeys.bind(key_bindings_presets);
}
log("Extention Enabled !");
}
function disable() {
hotkeys.unbind(key_bindings);
hotkeys.unbind(key_bindings_presets);
hotkeys.unbind(key_bindings_tiling);
destroyGrids();
launcher.destroy();
launcher = null;
resetFocusMetaWindow();
}
function resetFocusMetaWindow() {
log("resetFocusMetaWindow");
if (focusMetaWindowConnections.length>0) {
for (var idx in focusMetaWindowConnections) {
log("disconnect focusMetaWindowConnections " + idx);
focusMetaWindow.disconnect(focusMetaWindowConnections[idx]);
}
}
if (focusMetaWindowPrivateConnections.length>0) {
let actor = focusMetaWindow.get_compositor_private();
if (actor) {
for(var idx in focusMetaWindowPrivateConnections) {
log("disconnect focusMetaWindowPrivateConnections " + idx);
actor.disconnect(focusMetaWindowPrivateConnections[idx]);
}
}
}
focusMetaWindow = false;
focusMetaWindowConnections = new Array();
focusMetaWindowPrivateConnections = new Array();
}
function initGrids() {
log("initGrids");
grids = new Array();
for (let monitorIdx in monitors) {
log("New Grid for monitor " + monitorIdx);
let monitor = monitors[monitorIdx];
let grid = new Grid(monitorIdx, monitor, "gTile", nbCols, nbRows);
let key = getMonitorKey(monitor);
grids[key] = grid;
Main.layoutManager.addChrome(grid.actor, { trackFullscreen: true });
grid.actor.set_opacity(0);
grid.hide(true);
grid.connect('hide-tiling', Lang.bind(this, hideTiling));
}
}
function destroyGrids() {
for (let monitorIdx in monitors) {
let monitor = monitors[monitorIdx];
let key = getMonitorKey(monitor);
let grid = grids[key];
grid.hide(true);
Main.layoutManager.removeChrome(grid.actor);
}
}
function refreshGrids() {
log("Refresh");
for (var gridIdx in grids) {
let grid = grids[gridIdx];
grid.refresh();
}
}
function moveGrids() {
if (!status) {
return;
}
let window = focusMetaWindow;
if (window) {
for (var gridIdx in grids) {
let grid = grids[gridIdx];
let pos_x;
let pos_y;
let monitor = grid.monitor;
if (window.get_monitor() == grid.monitor_idx) {
pos_x = window.get_frame_rect().width / 2 + window.get_frame_rect().x;
pos_y = window.get_frame_rect().height / 2 + window.get_frame_rect().y;
}
else {
pos_x = monitor.x + monitor.width/2;
pos_y = monitor.y + monitor.height/2;
}
pos_x = Math.floor(pos_x - grid.actor.width / 2);
pos_y = Math.floor(pos_y - grid.actor.height / 2);
if (window.get_monitor() == grid.monitor_idx) {
pos_x = (pos_x < monitor.x) ? monitor.x : pos_x;
pos_x = ((pos_x + grid.actor.width) > (monitor.width+monitor.x)) ? monitor.x + monitor.width - grid.actor.width : pos_x;
pos_y = (pos_y < monitor.y) ? monitor.y : pos_y;
pos_y = ((pos_y + grid.actor.height) > (monitor.height+monitor.y)) ? monitor.y + monitor.height - grid.actor.height : pos_y;
}
let time = (gridSettings[SETTINGS_ANIMATION]) ? 0.3 : 0.1;
Tweener.addTween(
grid.actor, {
time: time,
x:pos_x,
y:pos_y,
transition: 'easeOutQuad',
/*onComplete:updateRegions*/
});
}
}
}
function updateRegions() {
/*Main.layoutManager._chrome.updateRegions();*/
refreshGrids();
for (let idx in grids) {
let grid = grids[idx];
grid.elementsDelegate.reset();
}
}
function reset_window(metaWindow) {
metaWindow.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
metaWindow.unmaximize(Meta.MaximizeFlags.VERTICAL);
metaWindow.unmaximize(Meta.MaximizeFlags.HORIZONTAL | Meta.MaximizeFlags.VERTICAL);
}
function _getInvisibleBorderPadding(metaWindow) {
let outerRect = metaWindow.get_frame_rect();
let inputRect = metaWindow.get_buffer_rect();
let borderX = outerRect.x - inputRect.x;
let borderY = outerRect.y - inputRect.y;
return [borderX, borderY];
}
function _getVisibleBorderPadding(metaWindow) {
let clientRect = metaWindow.get_frame_rect();
let outerRect = metaWindow.get_frame_rect();
let borderX = outerRect.width - clientRect.width
let borderY = outerRect.height - clientRect.height;
return [borderX, borderY];
}
function move_maximize_window(metaWindow, x, y) {
let borderX,borderY,vBorderX,vBorderY;
[borderX,borderY] = _getInvisibleBorderPadding(metaWindow);
x = x - borderX;
y = y - borderY;
metaWindow.move_frame(true,x,y);
metaWindow.maximize(Meta.MaximizeFlags.HORIZONTAL | Meta.MaximizeFlags.VERTICAL);
}
/**
* Resizes window considering margin settings
* @param metaWindow
* @param x
* @param y
* @param width
* @param height
*/
function move_resize_window_with_margins(metaWindow, x, y, width, height){
move_resize_window(
metaWindow,
x + gridSettings[SETTINGS_WINDOW_MARGIN],
y + gridSettings[SETTINGS_WINDOW_MARGIN],
width - gridSettings[SETTINGS_WINDOW_MARGIN] * 2,
height - gridSettings[SETTINGS_WINDOW_MARGIN] * 2
)
}
function move_resize_window(metaWindow, x, y, width, height) {
let [borderX,borderY] = _getInvisibleBorderPadding(metaWindow);
let [vBorderX,vBorderY] = _getVisibleBorderPadding(metaWindow);
log(metaWindow.get_title() + " " + borderX + "-" + borderY);
x = x ;//+ (vBorderX - 1);
y = y ;
width = width - vBorderX;
height = height - vBorderY ;
/* To TEST
win._overviewHint = {
x: actor.x,
y: actor.y,
scale: actor.scale_x
};
*/
metaWindow.move_resize_frame(true,x,y,width,height);
/*
let actor = metaWindow.get_compositor_private();
let origX = actor.x;
let origY = actor.y;
actor.x = origX;
actor.y = origY;
Tweener.addTween(
actor,{
time:0.2,
transition: "easeOutQuad",
opacity:0,
onComplete:_onMovedAndResize,
onCompleteParams: [metaWindow,x,y,width,height]
});
*/
}
function _isMyWindow(win) {
//log("meta-window: "+this.focusMetaWindow+" : "+win.meta_window);
return (this.focusMetaWindow == win.meta_window);
}
function getWindowActor() {
let windows = global.get_window_actors().filter(this._isMyWindow, this);
focusWindowActor = windows[0];
//log("window actor: "+focusWindowActor+":"+focusMetaWindow.get_compositor_private() );
}
function getNotFocusedWindowsOfMonitor(monitor) {
let windows = global.get_window_actors().filter(function(w) {
let app = tracker.get_window_app(w.meta_window);
if (app == null) {
return false;
}
let appName = app.get_name();
//log("NotFocused - AppName: " + appName);
return !contains(excludedApplications, appName)
&& w.meta_window.get_window_type() == Meta.WindowType.NORMAL
&& w.meta_window.get_workspace() == global.screen.get_active_workspace()
&& w.meta_window.showing_on_its_workspace()
&& monitors[w.meta_window.get_monitor()] == monitor
&& focusMetaWindow != w.meta_window;
});
return windows;
}
function getWindowsOfMonitor(monitor) {
let windows = global.get_window_actors().filter(function(w) {
return w.meta_window.get_window_type() != Meta.WindowType.DESKTOP
&& w.meta_window.get_workspace() == global.screen.get_active_workspace()
&& w.meta_window.showing_on_its_workspace()
&& monitors[w.meta_window.get_monitor()] == monitor;
});
return windows;
}
function _onFocus() {
log("_onFocus");
resetFocusMetaWindow();
let window = getFocusApp();
if (window) {
log("_onFocus, connecting " + window.get_title());
//log("Connect window: "+window.get_title());
focusMetaWindow = window;
focusMetaWindowConnections.push(focusMetaWindow.connect('notify::title',Lang.bind(this,_onFocus)));
let actor = focusMetaWindow.get_compositor_private();
if (actor) {
focusMetaWindowPrivateConnections.push(focusMetaWindow.connect('size-changed',Lang.bind(this,moveGrids)));
focusMetaWindowPrivateConnections.push(focusMetaWindow.connect('position-changed',Lang.bind(this,moveGrids)));
}
//log("End Connect window: "+window.get_title());
let app = tracker.get_window_app(focusMetaWindow);
let title = focusMetaWindow.get_title();
for (let monitorIdx in monitors) {
let monitor = monitors[monitorIdx];
let key = getMonitorKey(monitor);
let grid = grids[key];
if (app) {
grid.topbar._set_app(app,title);
}
else {
grid.topbar._set_title(title);
}
}
moveGrids();
}
else {
log("No focus window, hide tiling");
hideTiling();
}
}
function showTiling() {
log("showTiling");
focusMetaWindow = getFocusApp();
//let wm_class = focusMetaWindow.get_wm_class();
let wm_type = focusMetaWindow.get_window_type();
let layer = focusMetaWindow.get_layer();
//log("type:"+wm_type+" class:"+wm_class+" layer:"+layer);
//log("focus app: "+focusMetaWindow);
area.visible = true;
if (focusMetaWindow && wm_type != 1 && layer > 0) {
for (let monitorIdx in monitors) {
let monitor = monitors[monitorIdx];
let key = getMonitorKey(monitor);
let grid = grids[key];
//log("ancestor: "+grid.actor.get_parent());
let window = getFocusApp();
let pos_x;
let pos_y;
if (window.get_monitor() == monitorIdx) {
pos_x = window.get_frame_rect().width / 2 + window.get_frame_rect().x;
pos_y = window.get_frame_rect().height / 2 + window.get_frame_rect().y;
}
else {
pos_x = monitor.x + monitor.width/2;
pos_y = monitor.y + monitor.height/2;
}
grid.set_position(
Math.floor(pos_x - grid.actor.width / 2),
Math.floor(pos_y - grid.actor.height / 2)
);
grid.show();
}
_onFocus();
status = true;
launcher.activate();
bindKeyControls();
}
moveGrids();
}
function hideTiling() {
log("hideTiling");
for (let gridIdx in grids) {
let grid = grids[gridIdx];
grid.elementsDelegate.reset();
grid.hide(false);
}
log("After reseting grid");
area.visible = false;
resetFocusMetaWindow();
launcher.deactivate();
status = false;
unbindKeyControls();
}
function toggleTiling() {
if (status) {
hideTiling();
}
else {
showTiling();
}
return status;
}
function getMonitorKey(monitor) {
return monitor.x+":"+monitor.width+":"+monitor.y+":"+monitor.height;
}
function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}
function getFocusApp() {
if (tracker.focus_app == null) {
return false;
}
let focusedAppName = tracker.focus_app.get_name();
if (contains(excludedApplications, focusedAppName)) {
return false;
}
let windows = global.screen.get_active_workspace().list_windows();
let focusedWindow = false;
for (let i = 0; i < windows.length; ++i) {
let metaWindow = windows[i];
if (metaWindow.has_focus()) {
focusedWindow = metaWindow;
break;
}
}
return focusedWindow;
}
function isPrimaryMonitor(monitor) {
return Main.layoutManager.primaryMonitor.x == monitor.x && Main.layoutManager.primaryMonitor.y == monitor.y;
}
function getWorkArea(monitor) {
let insets = (isPrimaryMonitor(monitor)) ? gridSettings[SETTINGS_INSETS_PRIMARY] : gridSettings[SETTINGS_INSETS_SECONDARY];
let panelPosition = Main.layoutManager.panelBox.anchor_y == 0 ? St.Side.TOP : St.Side.BOTTOM;
let topPanelSize = (isPrimaryMonitor(monitor) && gridSettings[SETTINGS_TOP_PANEL]) ? Main.panel.actor.height : 0;
let bottomPanelSize = (isPrimaryMonitor(monitor) && gridSettings[SETTINGS_BOTTOM_PANEL]) ? Main.panel.actor.height : 0;
return {
x: monitor.x + insets.left,
y: panelPosition == St.Side.TOP ? monitor.y + insets.top + topPanelSize : monitor.y + insets.top,
width: monitor.width - insets.left - insets.right,
height: monitor.height - insets.top - insets.bottom - topPanelSize - bottomPanelSize
};
}
function bindKeyControls() {
if(!keyControlBound) {
hotkeys.bind(key_bindings_tiling);
log("Connect notify:focus-window");
global.display.connect('notify::focus-window', Lang.bind(this, _onFocus));
if(!gridSettings[SETTINGS_GLOBAL_PRESETS]) {
hotkeys.bind(key_bindings_presets);
}
keyControlBound = true;
}
}
function unbindKeyControls() {
if(keyControlBound) {
hotkeys.unbind(key_bindings_tiling);
log("Disconnect notify:focus-window");
global.display.disconnect('notify::focus-window', Lang.bind(this, _onFocus));
if(!gridSettings[SETTINGS_GLOBAL_PRESETS]) {
hotkeys.unbind(key_bindings_presets);
}
keyControlBound = false;
}
}
function keyCancelTiling() {
log("Cancel key event");
hideTiling();
}
function keySetTiling() {
log("keySetTiling");
if (focusMetaWindow) {
let mind = focusMetaWindow.get_monitor();
let monitor = monitors[mind];
let mkey = getMonitorKey(monitor);
let grid = grids[mkey];
log("In grid " + grid);
if(grid.elementsDelegate.currentElement) {
grid.elementsDelegate.currentElement._onButtonPress();
}
}
}
function keyChangeTiling() {
log("keyChangeTiling");
let grid_settings_sizes = gridSettings[SETTINGS_GRID_SIZES];
let next_key = 0;
let found = false;
for (let key in grid_settings_sizes) {
if(found) {
next_key = key;
break;
}
log("Checking grid settings size " + key + " have cols " + grid_settings_sizes[key].cols + " and rows " + grid_settings_sizes[key].rows);
if(grid_settings_sizes[key].cols == nbCols && grid_settings_sizes[key].rows == nbRows) {
found = true;
}
}
log("Found matching grid nbCols " + nbCols + " nbRows " + nbRows + " next key is " + next_key);
log("New settings will be nbCols " + grid_settings_sizes[next_key].cols + " nbRows " + grid_settings_sizes[next_key].rows);
grid_settings_sizes[next_key]._onButtonPress();
log("New settings are nbCols " + nbCols + " nbRows " + nbRows);
setInitialSelection();
}
function setInitialSelection() {
if (!focusMetaWindow) {
return;
}
let mind = focusMetaWindow.get_monitor();
let monitor = monitors[mind];
let workArea = getWorkArea(monitor);
let wx = focusMetaWindow.get_frame_rect().x;
let wy = focusMetaWindow.get_frame_rect().y;
let wwidth = focusMetaWindow.get_frame_rect().width;
let wheight = focusMetaWindow.get_frame_rect().height;
let mkey = getMonitorKey(monitor);
let grid = grids[mkey];
let delegate = grid.elementsDelegate;
log("Set initial selection");
log("Focus window position x " + wx + " y " + wy + " width " + wwidth + " height " + wheight);
log("Focus monitor position x " + monitor.x + " y " + monitor.y + " width " + monitor.width + " height " + monitor.height);
log("Workarea position x " + workArea.x + " y " + workArea.y + " width " + workArea.width + " height " + workArea.height);
let wax = Math.max(wx - workArea.x, 0);
let way = Math.max(wy - workArea.y, 0);
let grid_element_width = Math.floor(workArea.width / nbCols);
let grid_element_height = Math.floor(workArea.height / nbRows);
log("width " + grid_element_width + " height " + grid_element_height);
let lux = Math.min(Math.floor(wax / grid_element_width), nbCols - 1);
log("wx " + (wx - workArea.x) + " el_width " + grid_element_width + " max " + (nbCols - 1) + " res " + lux);
let luy = Math.min(Math.floor(way / grid_element_height), grid.rows - 1);
log("wy " + (wy - workArea.y) + " el_height " + grid_element_height + " max " + (nbRows - 1) + " res " + luy);
let rdx = Math.min(Math.floor((wax + wwidth - 1) / grid_element_width), grid.cols - 1);
log("wx + wwidth " + (wx + wwidth - workArea.x - 1) + " el_width " + grid_element_width + " max " + (nbCols - 1) + " res " + rdx);
let rdy = Math.min(Math.floor((way + wheight - 1) / grid_element_height), grid.rows - 1);
log("wy + wheight " + (wy + wheight - workArea.y - 1) + " el_height " + grid_element_height + " max " + (nbRows - 1) + " res " + rdy);
log("Initial tile selection is " + lux + ":" + luy + " - " + rdx + ":" + rdy);
grid.elements[luy] [lux]._onButtonPress();
grid.elements[rdy] [rdx]._onHoverChanged();
let cX = delegate.currentElement.coordx;
let cY = delegate.currentElement.coordy;
let fX = delegate.first.coordx;
let fY = delegate.first.coordy;
log("After initial selection first fX " + fX + " fY " + fY + " current cX " + cX + " cY " + cY);
}
function keyMoveResizeEvent(type, key) {
log("Got key event " + type + " " + key);
if (!focusMetaWindow) {
return;
}
let mind = focusMetaWindow.get_monitor();
let monitor = monitors[mind];
let mkey = getMonitorKey(monitor);
let grid = grids[mkey];
let delegate = grid.elementsDelegate;
if(!delegate.currentElement) {
log("Key event while no mouse activation - set current and second element");
setInitialSelection();
} else {
if(!delegate.first){
log("currentElement is there but no first yet");
delegate.currentElement._onButtonPress();
}
}
if(!delegate.currentElement) {
log("gTime currentElement is not set!");
}
let cX = delegate.currentElement.coordx;
let cY = delegate.currentElement.coordy;
let fX = delegate.first.coordx;
let fY = delegate.first.coordy;
log("Before move/resize first fX " + fX + " fY " + fY + " current cX " + cX + " cY " + cY);
log("Grid cols " + nbCols + " rows " + nbRows);
if(type == 'move') {
switch(key) {
case 'right':
if(fX < nbCols - 1 && cX < nbCols - 1) {
delegate.first = grid.elements [fY] [fX + 1];
grid.elements[cY] [cX + 1]._onHoverChanged();
}
break;
case 'left':
if(fX > 0 && cX > 0) {
delegate.first = grid.elements [fY] [fX - 1];
grid.elements[cY] [cX - 1]._onHoverChanged();
}
break;
case 'up':
if(fY > 0 && cY > 0) {
delegate.first = grid.elements [fY - 1] [fX];
grid.elements[cY - 1] [cX]._onHoverChanged();
}
break;
case 'down':
if(fY < nbRows - 1 && cY < nbRows - 1) {
delegate.first = grid.elements [fY + 1] [fX];
grid.elements[cY + 1] [cX]._onHoverChanged();
}
break;
}
} else {
switch(key) {
case 'right':
if(cX < nbCols - 1) {
grid.elements[cY] [cX + 1]._onHoverChanged();
}
break;
case 'left':
if(cX > 0) {
grid.elements[cY] [cX - 1]._onHoverChanged();
}
break;
case 'up':
if(cY > 0 ) {
grid.elements[cY - 1] [cX]._onHoverChanged();
}
break;
case 'down':
if(cY < nbRows - 1) {
grid.elements[cY + 1] [cX]._onHoverChanged();
}
break;
}
}
cX = delegate.currentElement.coordx;
cY = delegate.currentElement.coordy;
fX = delegate.first.coordx;
fY = delegate.first.coordy;
log("After move/resize first fX " + fX + " fY " + fY + " current cX " + cX + " cY " + cY);
}
function presetResize(preset) {
// Expected preset format is XxY x1:y1 x2:y2
// XxY is grid size like 6x8
// x1:y1 is left upper corner coordinates in grid tiles, starting from 0
// x2:y2 is right down corner coordinates in grid tiles
let window = getFocusApp();
if (!window) {
log("No focused window - ignoring keyboard shortcut");
return;
}
reset_window(window);
let preset_string = settings.get_string("resize" + preset);
log("Preset resize " + preset + " is " + preset_string);
let ps = preset_string.split(" ");
if(ps.length != 3) {
log("Bad preset " + preset + " settings " + preset_string);
return;
}
let grid_format = parseTuple(ps[0], "x");
let luc = parseTuple(ps[1], ":");
let rdc = parseTuple(ps[2], ":");
log("Parsed " + grid_format.X + "x" + grid_format.Y + " "
+ luc.X + ":" + luc.Y + " " + rdc.X + ":" + rdc.Y);
if ( grid_format.X < 1 || luc.X < 0 || rdc.X < 0
|| grid_format.Y < 1 || luc.Y < 0 || rdc.Y < 0
|| grid_format.X <= luc.X || grid_format.X <= rdc.X
|| grid_format.Y <= luc.Y || grid_format.Y <= rdc.Y
|| luc.X > rdc.X || luc.Y > rdc.Y) {
log("Bad preset " + preset + " settings " + preset_string);
return;
}
log("Parsed preset " + preset + " " + grid_format.X + "x" + grid_format.Y +
" " + luc.X + ":" + luc.Y + " " + rdc.X + ":" + rdc.Y);
let mind = window.get_monitor();
let monitor = monitors[mind];
let work_area = getWorkArea(monitor);
let grid_element_width = Math.floor(work_area.width / grid_format.X);
let grid_element_height = Math.floor(work_area.height / grid_format.Y);
let wx = work_area.x + luc.X * grid_element_width + gridSettings[SETTINGS_WINDOW_MARGIN];
let wy = work_area.y + luc.Y * grid_element_height + gridSettings[SETTINGS_WINDOW_MARGIN];
let ww = (rdc.X + 1 - luc.X) * grid_element_width - 2 * gridSettings[SETTINGS_WINDOW_MARGIN];
let wh = (rdc.Y + 1 - luc.Y) * grid_element_height- 2 * gridSettings[SETTINGS_WINDOW_MARGIN];
log("Resize preset " + preset + " resizing to wx " + wx + " wy " + wy + " ww " + ww + " wh " + wh);
window.move_resize_frame(true, wx, wy, ww, wh);
}
/*****************************************************************
PROTOTYPES
*****************************************************************/
function TopBar(title) {
this._init(title);
}
TopBar.prototype = {
_init: function(title) {