-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
2797 lines (2359 loc) · 93.4 KB
/
app.ts
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
// GJS import system
declare var imports: any;
declare var global: any;
import { ClutterActor, GridLayout, ShellApp, ShellWindowTracker, StBin, StBoxLayout, StButton, StLabel, StWidget, Window, WindowType, WorkspaceManager as WorkspaceManagerInterface } from "./gnometypes";
import { Bindings, bind as bindHotkeys, unbind as unbindHotkeys } from './hotkeys';
import { log, setLoggingEnabled } from './logging';
import * as PresetParser from './preset_parser';
import { BoolSettingName, NumberSettingName, StringSettingName } from './settings_data';
import { ShellVersion } from './shellversion';
import { snapToNeighbors } from './snaptoneighbors';
import * as tilespec from "./tilespec";
type MoveResizeOp = 'contract' | 'move' | 'expand' | 'resize';
type MoveResizeSide = 'bottom' | 'top' | 'left' | 'right' | 'up' | 'down';
type TileDirection = 'left' | 'right';
/*****************************************************************
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
import * as Clutter from 'gi://Clutter?version=12';
import * as GObject from 'gi://GObject';
import * as Meta from 'gi://Meta?version=12';
import * as Shell from 'gi://Shell';
import * as St from 'gi://St';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import '@girs/gjs'
import '@girs/gjs/dom'
import '@girs/gtk-4.0'
import Gtk from 'gi://Gtk?version=4.0';
// Getter for accesing "get_active_workspace" on GNOME <=2.28 and >= 2.30
const WorkspaceManager: WorkspaceManagerInterface = (
global.screen || global.workspace_manager);
const Signals = imports.signals;
const Mainloop = imports.mainloop;
// Extension imports
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Settings = Me.imports.settings;
interface WorkArea {
x: number;
y: number;
width: number;
height: number;
}
// Globals
const SETTINGS_GRID_SIZES = 'grid-sizes';
const SETTINGS_AUTO_CLOSE = 'auto-close';
const SETTINGS_AUTO_CLOSE_KEYBOARD_SHORTCUT = "auto-close-keyboard-shortcut";
const SETTINGS_ANIMATION = 'animation';
const SETTINGS_SHOW_ICON = 'show-icon';
const SETTINGS_GLOBAL_AUTO_TILING = 'global-auto-tiling';
const SETTINGS_GLOBAL_PRESETS = 'global-presets';
const SETTINGS_TARGET_PRESETS_TO_MONITOR_OF_MOUSE = "target-presets-to-monitor-of-mouse";
const SETTINGS_MOVERESIZE_ENABLED = 'moveresize-enabled';
const SETTINGS_WINDOW_MARGIN = 'window-margin';
const SETTINGS_WINDOW_MARGIN_FULLSCREEN_ENABLED = 'window-margin-fullscreen-enabled';
const SETTINGS_MAX_TIMEOUT = 'max-timeout';
const SETTINGS_MAIN_WINDOW_SIZES = 'main-window-sizes';
const SETTINGS_SHOW_GRID_LINES = 'show-grid-lines';
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';
const SETTINGS_THEME = 'theme';
interface ParsedSettings {
[SETTINGS_GRID_SIZES]: tilespec.GridSize[];
[SETTINGS_AUTO_CLOSE]: any;
[SETTINGS_AUTO_CLOSE_KEYBOARD_SHORTCUT]: any;
[SETTINGS_ANIMATION]: any;
[SETTINGS_SHOW_ICON]: any;
[SETTINGS_GLOBAL_AUTO_TILING]: any;
[SETTINGS_GLOBAL_PRESETS]: any;
[SETTINGS_TARGET_PRESETS_TO_MONITOR_OF_MOUSE]: any;
[SETTINGS_MOVERESIZE_ENABLED]: any;
[SETTINGS_WINDOW_MARGIN]: number;
[SETTINGS_WINDOW_MARGIN_FULLSCREEN_ENABLED]: boolean;
[SETTINGS_MAX_TIMEOUT]: any;
[SETTINGS_MAIN_WINDOW_SIZES]: Array<string>;
[SETTINGS_SHOW_GRID_LINES]: boolean;
[SETTINGS_INSETS_PRIMARY]: number;
[SETTINGS_INSETS_PRIMARY_LEFT]: number;
[SETTINGS_INSETS_PRIMARY_RIGHT]: number;
[SETTINGS_INSETS_PRIMARY_TOP]: number;
[SETTINGS_INSETS_PRIMARY_BOTTOM]: number;
[SETTINGS_INSETS_SECONDARY]: number;
[SETTINGS_INSETS_SECONDARY_LEFT]: number;
[SETTINGS_INSETS_SECONDARY_RIGHT]: number;
[SETTINGS_INSETS_SECONDARY_TOP]: number;
[SETTINGS_INSETS_SECONDARY_BOTTOM]: number;
[SETTINGS_DEBUG]: any;
[SETTINGS_THEME]: any;
}
const gridSettings: ParsedSettings = {
[SETTINGS_GRID_SIZES]: [],
[SETTINGS_AUTO_CLOSE]: null,
[SETTINGS_AUTO_CLOSE_KEYBOARD_SHORTCUT]: null,
[SETTINGS_ANIMATION]: null,
[SETTINGS_SHOW_ICON]: null,
[SETTINGS_GLOBAL_AUTO_TILING]: null,
[SETTINGS_GLOBAL_PRESETS]: null,
[SETTINGS_TARGET_PRESETS_TO_MONITOR_OF_MOUSE]: null,
[SETTINGS_MOVERESIZE_ENABLED]: null,
[SETTINGS_WINDOW_MARGIN]: 0,
[SETTINGS_WINDOW_MARGIN_FULLSCREEN_ENABLED]: false,
[SETTINGS_MAX_TIMEOUT]: null,
[SETTINGS_MAIN_WINDOW_SIZES]: [],
[SETTINGS_SHOW_GRID_LINES]: false,
[SETTINGS_INSETS_PRIMARY]: 0,
[SETTINGS_INSETS_PRIMARY_LEFT]: 0,
[SETTINGS_INSETS_PRIMARY_RIGHT]: 0,
[SETTINGS_INSETS_PRIMARY_TOP]: 0,
[SETTINGS_INSETS_PRIMARY_BOTTOM]: 0,
[SETTINGS_INSETS_SECONDARY]: 0,
[SETTINGS_INSETS_SECONDARY_LEFT]: 0,
[SETTINGS_INSETS_SECONDARY_RIGHT]: 0,
[SETTINGS_INSETS_SECONDARY_TOP]: 0,
[SETTINGS_INSETS_SECONDARY_BOTTOM]: 0,
[SETTINGS_DEBUG]: null,
[SETTINGS_THEME]: null,
};
interface SettingsObject {
get_boolean(name: BoolSettingName): boolean | undefined;
get_string(name: StringSettingName): string | undefined;
get_int(name: NumberSettingName): number | undefined;
connect(eventName: string, callback: () => void): void;
};
let launcher: GTileStatusButtonInterface | null;
let tracker: ShellWindowTracker;
let nbCols = 0;
let nbRows = 0;
let focusMetaWindow: Window | null = null;
let focusConnect: any = false;
let settings: SettingsObject = Settings.get();
settings.connect('changed', changed_settings);
let keyControlBound: any = false;
let enabled = false;
let mainWindowSizes = new Array();;
let monitorsChangedConnect: any = false;
const SHELL_VERSION = ShellVersion.defaultVersion();
interface ResizeActionInfo {
variantIndex: number;
lastCallTime: Date;
presetName: string;
windowTitle: string;
};
const lastResizeInfo: ResizeActionInfo = {
variantIndex: 0,
lastCallTime: new Date(), // now
presetName: '',
windowTitle: '',
};
let theme = getTheme();
// Hangouts workaround
let excludedApplications = new Array(
"Unknown"
);
const keyBindings: Bindings = new Map([
['show-toggle-tiling', () => { globalApp.toggleTiling(); }],
['show-toggle-tiling-alt', () => { globalApp.toggleTiling(); }],
]);
const key_bindings_tiling: Bindings = new Map([
['move-left', () => { keyMoveResizeEvent('move', 'left'); }],
['move-right', () => { keyMoveResizeEvent('move', 'right'); }],
['move-up', () => { keyMoveResizeEvent('move', 'up'); }],
['move-down', () => { keyMoveResizeEvent('move', 'down'); }],
['resize-left', () => { keyMoveResizeEvent('resize', 'left'); }],
['resize-right', () => { keyMoveResizeEvent('resize', 'right'); }],
['resize-up', () => { keyMoveResizeEvent('resize', 'up'); }],
['resize-down', () => { keyMoveResizeEvent('resize', 'down'); }],
['move-left-vi', () => { keyMoveResizeEvent('move', 'left'); }],
['move-right-vi', () => { keyMoveResizeEvent('move', 'right'); }],
['move-up-vi', () => { keyMoveResizeEvent('move', 'up'); }],
['move-down-vi', () => { keyMoveResizeEvent('move', 'down'); }],
['resize-left-vi', () => { keyMoveResizeEvent('resize', 'left'); }],
['resize-right-vi', () => { keyMoveResizeEvent('resize', 'right'); }],
['resize-up-vi', () => { keyMoveResizeEvent('resize', 'up'); }],
['resize-down-vi', () => { keyMoveResizeEvent('resize', 'down'); }],
['cancel-tiling', () => { keyCancelTiling(); }],
['set-tiling', () => { keySetTiling(); }],
['change-grid-size', () => { keyChangeTiling(); }],
['snap-to-neighbors', () => { snapToNeighborsBind(); }],
['snap-to-neighbors', () => { snapToNeighborsBind(); }],
]);
const key_bindings_auto_tiling: Bindings = new Map([
['autotile-main', () => { AutoTileMain('left'); }],
['autotile-main-inverted', () => { AutoTileMain('right'); }],
['autotile-1', () => { autoTileNCols(1); }],
['autotile-2', () => { autoTileNCols(2); }],
['autotile-3', () => { autoTileNCols(3); }],
['autotile-4', () => { autoTileNCols(4); }],
['autotile-5', () => { autoTileNCols(5); }],
['autotile-6', () => { autoTileNCols(6); }],
['autotile-7', () => { autoTileNCols(7); }],
['autotile-8', () => { autoTileNCols(8); }],
['autotile-9', () => { autoTileNCols(9); }],
['autotile-10', () => { autoTileNCols(10); }],
]);
const key_bindings_presets: Bindings = new Map([
['preset-resize-1', () => { presetResize(1, 'resize1'); }],
['preset-resize-2', () => { presetResize(2, 'resize2'); }],
['preset-resize-3', () => { presetResize(3, 'resize3'); }],
['preset-resize-4', () => { presetResize(4, 'resize4'); }],
['preset-resize-5', () => { presetResize(5, 'resize5'); }],
['preset-resize-6', () => { presetResize(6, 'resize6'); }],
['preset-resize-7', () => { presetResize(7, 'resize7'); }],
['preset-resize-8', () => { presetResize(8, 'resize8'); }],
['preset-resize-9', () => { presetResize(9, 'resize9'); }],
['preset-resize-10', () => { presetResize(10, 'resize10'); }],
['preset-resize-11', () => { presetResize(11, 'resize11'); }],
['preset-resize-12', () => { presetResize(12, 'resize12'); }],
['preset-resize-13', () => { presetResize(13, 'resize13'); }],
['preset-resize-14', () => { presetResize(14, 'resize14'); }],
['preset-resize-15', () => { presetResize(15, 'resize15'); }],
['preset-resize-16', () => { presetResize(16, 'resize16'); }],
['preset-resize-17', () => { presetResize(17, 'resize17'); }],
['preset-resize-18', () => { presetResize(18, 'resize18'); }],
['preset-resize-19', () => { presetResize(19, 'resize19'); }],
['preset-resize-20', () => { presetResize(20, 'resize20'); }],
['preset-resize-21', () => { presetResize(21, 'resize21'); }],
['preset-resize-22', () => { presetResize(22, 'resize22'); }],
['preset-resize-23', () => { presetResize(23, 'resize23'); }],
['preset-resize-24', () => { presetResize(24, 'resize24'); }],
['preset-resize-25', () => { presetResize(25, 'resize25'); }],
['preset-resize-26', () => { presetResize(26, 'resize26'); }],
['preset-resize-27', () => { presetResize(27, 'resize27'); }],
['preset-resize-28', () => { presetResize(28, 'resize28'); }],
['preset-resize-29', () => { presetResize(29, 'resize29'); }],
['preset-resize-30', () => { presetResize(30, 'resize30'); }],
]);
const keyBindingGlobalResizes: Bindings = new Map([
['action-change-tiling', () => { keyChangeTiling(); }],
['action-contract-bottom', () => { keyMoveResizeEvent('contract', 'bottom', true); }],
['action-contract-left', () => { keyMoveResizeEvent('contract', 'left', true); }],
['action-contract-right', () => { keyMoveResizeEvent('contract', 'right', true); }],
['action-contract-top', () => { keyMoveResizeEvent('contract', 'top', true); }],
['action-expand-bottom', () => { keyMoveResizeEvent('expand', 'bottom', true); }],
['action-expand-left', () => { keyMoveResizeEvent('expand', 'left', true); }],
['action-expand-right', () => { keyMoveResizeEvent('expand', 'right', true); }],
['action-expand-top', () => { keyMoveResizeEvent('expand', 'top', true); }],
['action-move-down', () => { keyMoveResizeEvent('move', 'down', true); }],
['action-move-left', () => { keyMoveResizeEvent('move', 'left', true); }],
['action-move-right', () => { keyMoveResizeEvent('move', 'right', true); }],
['action-move-up', () => { keyMoveResizeEvent('move', 'up', true); }],
['action-move-next-monitor', () => { moveWindowToNextMonitor(); }],
['action-autotile-main', () => { AutoTileMain('left', true); }],
['action-autotile-main-inverted', () => { AutoTileMain('right', true); }],
]);
export default class App {
private readonly gridsByMonitorKey: Record<string, Grid> = {};
private gridShowing: boolean = false;
private gridWidget: StBoxLayout | null = null;
private gridLinesTimeout: Object = null;
private gridTiles: Array<StBoxLayout> = [];
enable() {
this.gridShowing = false;
tracker = Shell.WindowTracker.get_default();
initSettings();
const gridWidget: StBoxLayout = (new St.BoxLayout({ style_class: `${theme}__preview` }));
this.gridWidget = gridWidget;
Main.uiGroup.add_actor(gridWidget);
this.initGrids(gridWidget);
log("Create Button on Panel");
launcher = new GTileStatusButton(`${theme}__icon`);
if (gridSettings[SETTINGS_SHOW_ICON]) {
Main.panel.addToStatusArea("GTileStatusButton", launcher);
}
bindHotkeys(keyBindings);
if (gridSettings[SETTINGS_GLOBAL_AUTO_TILING]) {
bindHotkeys(key_bindings_auto_tiling);
}
if (gridSettings[SETTINGS_GLOBAL_PRESETS]) {
bindHotkeys(key_bindings_presets);
}
if (gridSettings[SETTINGS_MOVERESIZE_ENABLED]) {
bindHotkeys(keyBindingGlobalResizes);
}
if (monitorsChangedConnect) {
Main.layoutManager.disconnect(monitorsChangedConnect);
}
log("Connecting monitors-changed");
monitorsChangedConnect = Main.layoutManager.connect('monitors-changed', () => {
log("Reinitializing grids on monitors-changed");
this.destroyGrids();
this.initGrids(gridWidget);
});
enabled = true;
log("Extention enable completed");
}
getGrid(monitor: Monitor): Grid | null {
return this.gridsByMonitorKey[getMonitorKey(monitor)];
}
initGrids(gridWidget: StBoxLayout) {
log("initGrids");
log("initGrids nobCols " + nbCols + " nbRows " + nbRows);
const monitors = activeMonitors();
for (let monitorIdx = 0; monitorIdx < monitors.length; monitorIdx++) {
log("New Grid for monitor " + monitorIdx);
let monitor = monitors[monitorIdx];
let grid = new Grid(gridWidget, monitorIdx, monitor, "gTile", nbCols, nbRows);
const key = getMonitorKey(monitor);
this.gridsByMonitorKey[key] = grid;
log("initGrids adding grid key " + key);
// TODO: addChrome is poorly documented. I can't find any reference
// to it in the gjs-docs site.
Main.layoutManager.addChrome(grid.actor, { trackFullscreen: true });
grid.actor.set_opacity(0);
grid.hide(true);
log("Connect hide-tiling for monitor " + monitorIdx);
grid.connectHideTiling = grid.connect('hide-tiling', () => this.hideTiling());
}
log("Init grid done");
}
destroyGrids() {
log("destroyGrids");
for (let gridKey in this.gridsByMonitorKey) {
const grid = this.gridsByMonitorKey[gridKey];
grid.hide(true);
Main.layoutManager.removeChrome(grid.actor);
log("Disconnect hide-tiling for monitor " + grid.monitor_idx);
grid.disconnect(grid.connectHideTiling);
delete this.gridsByMonitorKey[gridKey];
}
log("destroyGrids done");
for (let gridKey in this.gridsByMonitorKey) {
log("ERROR: gridKey still found in this.gridsByMonitorKey: " + gridKey);
}
}
refreshGrids() {
log("refreshGrids");
for (let gridIdx in this.gridsByMonitorKey) {
const grid = this.gridsByMonitorKey[gridIdx];
log("refreshGrids calling refresh on " + gridIdx);
grid.refresh();
}
log("refreshGrids done");
}
setGridSize(gridSize: tilespec.GridSize): void {
nbCols = gridSize.width;
nbRows = gridSize.height;
this.refreshGrids();
if (gridSettings[SETTINGS_SHOW_GRID_LINES]) {
this._showGridLines(gridSize);
}
}
moveGrids() {
log("moveGrids");
if (!this.gridShowing) {
return;
}
let window = focusMetaWindow;
if (window) {
for (let gridKey in this.gridsByMonitorKey) {
let grid = this.gridsByMonitorKey[gridKey];
let pos_x;
let pos_y;
const monitor = grid.monitor;
if (!monitor) {
return;
}
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;
let [mouse_x, mouse_y, mask] = global.get_pointer();
let act_x = pos_x - grid.actor.width / 2;
let act_y = pos_y - grid.actor.height / 2;
if (mouse_x >= act_x
&& mouse_x <= act_x + grid.actor.width
&& mouse_y >= act_y
&& mouse_y <= act_y + grid.actor.height) {
log("Mouse x " + mouse_x + " y " + mouse_y +
" is inside grid actor rectangle, changing actor X from " + pos_x + " to " + (mouse_x + grid.actor.width / 2) +
", Y from " + pos_y + " to " + (mouse_y + grid.actor.height / 2));
pos_x = mouse_x + grid.actor.width / 2;
pos_y = mouse_y + grid.actor.height / 2;
}
}
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;
(grid.actor as any).ease({
time: time,
x: pos_x,
y: pos_y,
transition: Clutter.AnimationMode.EASE_OUT_QUAD,
/*onComplete:updateRegions*/
});
}
}
}
updateRegions() {
/*Main.layoutManager._chrome.updateRegions();*/
log("updateRegions");
this.refreshGrids();
for (let idx in this.gridsByMonitorKey) {
this.gridsByMonitorKey[idx].elementsDelegate?.reset();
}
}
logState() {
let summary = ``;
let count = 0;
let states = '';
for (let gridKey in this.gridsByMonitorKey) {
states += `; ${this.gridsByMonitorKey[gridKey].state()}`;
count++;
}
log(`${count} grids; showing = ${this.gridShowing}: ${states}`);
}
showTiling() {
// TODO(#168): See https://github.com/gTile/gTile/issues/168. Without
// these two lines, the grid UI does not properly respond to mouseover
// and other events except for the first time it is shown.
log("showTiling with fix");
this.hideTiling();
this.destroyGrids();
this.initGrids(this.gridWidget!);
this.logState();
log("issue#168/showTiling with fix");
const window = getFocusApp();
// TODO: remove this global side effect
focusMetaWindow = window;
if (!this.gridWidget) {
return;
}
const shouldShowTiling = ((): boolean => {
if (!window) {
// The tiling UI is for manipulating a particular window.
return false;
}
const wmType = window.get_window_type();
const layer = window.get_layer();
return wmType !== WindowType.DESKTOP && layer > 0;
})();
if (!shouldShowTiling) {
return;
}
this.gridWidget.visible = true;
const monitors = activeMonitors();
for (let monitorIdx = 0; monitorIdx < monitors.length; monitorIdx++) {
const monitor = monitors[monitorIdx];
const grid = this.getGrid(monitor);
if (grid === null) {
log(`issue#168/showTiling ERROR: did not find grid for monitor ${getMonitorKey(monitor)}`);
continue;
}
let pos_x;
let pos_y;
if (window && window.get_monitor() === monitorIdx) {
// Set pos_x, pos_y to center of the selected window initially.
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;
// Old logic with no justifying comments: If the mouse is active
// and within the rectangle, set pos_x and pos_y at mouse
// position + half the size of the Grid window.
let [mouse_x, mouse_y, mask] = global.get_pointer();
let act_x = pos_x - grid.actor.width / 2;
let act_y = pos_y - grid.actor.height / 2;
if (mouse_x >= act_x
&& mouse_x <= act_x + grid.actor.width
&& mouse_y >= act_y
&& mouse_y <= act_y + grid.actor.height) {
log("Mouse x " + mouse_x + " y " + mouse_y +
" is inside grid actor rectangle, changing actor X from " + pos_x + " to " + (mouse_x + grid.actor.width / 2) +
", Y from " + pos_y + " to " + (mouse_y + grid.actor.height / 2));
pos_x = mouse_x + grid.actor.width / 2;
pos_y = mouse_y + grid.actor.height / 2;
}
}
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();
}
this.gridShowing = true;
this.onFocus();
launcher?.activate();
bindKeyControls();
this.moveGrids();
}
disable() {
log("Extension disable begin");
enabled = false;
// Notice for extension reviewer - this will call Mainloop.RemoveTimeout
this._hideGridLines();
if (monitorsChangedConnect) {
log("Disconnecting monitors-changed");
Main.layoutManager.disconnect(monitorsChangedConnect);
monitorsChangedConnect = false;
}
unbindHotkeys(keyBindings);
unbindHotkeys(key_bindings_auto_tiling);
unbindHotkeys(key_bindings_presets);
unbindHotkeys(keyBindingGlobalResizes);
if (keyControlBound) {
unbindHotkeys(key_bindings_tiling);
keyControlBound = false;
}
launcher?.destroy();
launcher = null;
Main.uiGroup.remove_actor(this.gridWidget);
this.destroyGrids();
resetFocusMetaWindow();
log("Extention disable completed");
}
hideTiling() {
log("hideTiling");
for (let key in this.gridsByMonitorKey) {
const grid = this.gridsByMonitorKey[key];
grid.elementsDelegate?.reset();
grid.hide(false);
}
if (this.gridWidget) {
this.gridWidget.visible = false;
}
resetFocusMetaWindow();
launcher?.deactivate();
this.gridShowing = false;
unbindKeyControls();
}
toggleTiling(): boolean {
if (this.gridShowing) {
this.hideTiling();
}
else {
this.showTiling();
}
return this.gridShowing;
}
isGridShowing(): boolean {
return this.gridShowing;
}
_hideGridLines(removeTimeout: boolean = true): void {
if (this.gridLinesTimeout != null) {
log("Removing grid lines...");
if (removeTimeout) {
Mainloop.timeout_remove(this.gridLinesTimeout);
}
this.gridLinesTimeout = null;
for (let tile of this.gridTiles) {
Main.uiGroup.remove_actor(tile);
}
}
this.gridTiles = [];
}
_showGridLines(gridSize: tilespec.GridSize): void {
this._hideGridLines();
log("Started drawing grid lines...");
for (let monitorIdx = 0; monitorIdx < activeMonitors().length; monitorIdx++) {
const workArea = workAreaRectByMonitorIndex(monitorIdx);
const monitor = activeMonitors()[monitorIdx];
if (!workArea) { continue; }
let tileHeight = workArea.size.height / gridSize.height;
let tileWidth = workArea.size.width / gridSize.width;
let topOffset = workArea.topLeft().y;
let leftOffset = workArea.topLeft().x;
log(`Starting to draw grid lines for monitor ${JSON.stringify(monitor)}`);
for (let i = 1; i < gridSize.width; i++) {
const newGridWidget = new St.BoxLayout({ style_class: `${theme}__grid_lines_preview` });
const posX = leftOffset + tileWidth * i;
newGridWidget.width = 1;
newGridWidget.height = workArea.size.height;
newGridWidget.x = posX;
newGridWidget.y = 0;
this.gridTiles.push(newGridWidget);
Main.uiGroup.add_actor(newGridWidget);
log(`Grid vertical line of size ${tileWidth}:${tileHeight} is drawn at ${posX}:0 (monitor offset ${monitor.x}:${monitor.y})`);
}
for (let u = 1; u <= gridSize.height; u++) {
const newGridWidget = new St.BoxLayout({ style_class: `${theme}__grid_lines_preview` });
const posY = topOffset + tileHeight * u;
newGridWidget.width = workArea.size.width;
newGridWidget.height = 1;
newGridWidget.x = 0;
newGridWidget.y = posY;
this.gridTiles.push(newGridWidget);
Main.uiGroup.add_actor(newGridWidget);
log(`Grid horizontal line of size ${tileWidth}:${tileHeight} is drawn at 0:${posY} (monitor offset ${monitor.x}:${monitor.y})`);
}
}
this.gridLinesTimeout = Mainloop.timeout_add(1000, () => {
this._hideGridLines(false);
});
}
/**
* onFocus is called when the global focus changes.
*/
onFocus() {
log("_onFocus ");
resetFocusMetaWindow();
const window = getFocusApp();
if (window && this.gridShowing) {
log("_onFocus " + window.get_title());
focusMetaWindow = window;
let app = tracker.get_window_app(focusMetaWindow);
let title = focusMetaWindow.get_title();
const monitors = activeMonitors();
for (let monitorIdx = 0; monitorIdx < monitors.length; monitorIdx++) {
let monitor = monitors[monitorIdx];
let grid = this.getGrid(monitor);
if (app) {
grid?.topbar.setApp(app, title);
}
else {
grid?.topbar.setTitle(title);
}
}
this.moveGrids();
} else {
if (this.gridShowing) {
log("No focus window, hide tiling");
this.hideTiling();
} else {
log("tiling window not active");
}
}
}
}
const globalApp = new App();
function changed_settings() {
log("changed_settings");
if (enabled) {
disable();
enable();
}
log("changed_settings complete");
}
interface GTileStatusButtonInterface {
activate(): void;
deactivate(): void;
destroy(): void;
};
const GTileStatusButton = GObject.registerClass({
GTypeName: 'GTileStatusButton',
}, class GTileStatusButton extends PanelMenu.Button {
_init(classname: string) {
super._init(0.0, "gTile", false);
//Done by default in PanelMenuButton - Just need to override the method
const icon = new St.Icon({ style_class: 'system-status-icon' });
this.add_actor(icon);
this.add_style_class_name(classname);
this.connect('button-press-event', this._onButtonPress.bind(this));
log("GTileStatusButton _init done");
}
reset() {
this.activated = false;
this.remove_style_pseudo_class('activate');
}
activate() {
this.add_style_pseudo_class('activate');
}
deactivate() {
this.remove_style_pseudo_class('activate');
}
_onButtonPress(actor, event) {
log(`_onButtonPress Click Toggle Status on system panel ${this}`);
globalApp.toggleTiling();
}
_destroy() {
this.activated = null;
}
});
/*****************************************************************
SETTINGS
*****************************************************************/
function initGridSizes(configValue: string): void {
let gridSizes = tilespec.parseGridSizesIgnoringErrors(configValue);
if (gridSizes.length === 0) {
gridSizes = [
new tilespec.GridSize(8, 6),
new tilespec.GridSize(6, 4),
new tilespec.GridSize(4, 4),
];
}
gridSettings[SETTINGS_GRID_SIZES] = gridSizes;
}
function getBoolSetting(settingName: BoolSettingName): boolean {
const value = settings.get_boolean(settingName);
if (value === undefined) {
log("Undefined settings " + settingName);
gridSettings[settingName] = false;
return false;
} else {
gridSettings[settingName] = value;
}
return value;
}
function getIntSetting(settingsValue: NumberSettingName) {
let iss = settings.get_int(settingsValue);
if (iss === undefined) {
log("Undefined settings " + settingsValue);
return 0;
} else {
return iss;
}
}
function initSettings() {
log("Init settings");
theme = getTheme();
const gridSizes = settings.get_string(SETTINGS_GRID_SIZES) || '';
log(SETTINGS_GRID_SIZES + " set to " + gridSizes);
initGridSizes(gridSizes);
getBoolSetting(SETTINGS_AUTO_CLOSE);
getBoolSetting(SETTINGS_AUTO_CLOSE_KEYBOARD_SHORTCUT);
getBoolSetting(SETTINGS_ANIMATION);
getBoolSetting(SETTINGS_SHOW_ICON);
getBoolSetting(SETTINGS_GLOBAL_AUTO_TILING);
getBoolSetting(SETTINGS_GLOBAL_PRESETS);
getBoolSetting(SETTINGS_TARGET_PRESETS_TO_MONITOR_OF_MOUSE);
getBoolSetting(SETTINGS_MOVERESIZE_ENABLED);
getBoolSetting(SETTINGS_SHOW_GRID_LINES);
gridSettings[SETTINGS_WINDOW_MARGIN] = getIntSetting(SETTINGS_WINDOW_MARGIN);
gridSettings[SETTINGS_WINDOW_MARGIN_FULLSCREEN_ENABLED] = getBoolSetting(SETTINGS_WINDOW_MARGIN_FULLSCREEN_ENABLED);
gridSettings[SETTINGS_MAX_TIMEOUT] = getIntSetting(SETTINGS_MAX_TIMEOUT);
// initialize these from settings, the first set of sizes
if (nbCols == 0 || nbRows == 0) {
nbCols = gridSettings[SETTINGS_GRID_SIZES][0].width;
nbRows = gridSettings[SETTINGS_GRID_SIZES][0].height;
}
const mainWindowSizesString = settings.get_string(SETTINGS_MAIN_WINDOW_SIZES);
log(SETTINGS_MAIN_WINDOW_SIZES + " settings found " + mainWindowSizesString);
mainWindowSizes = [];
let mainWindowSizesArray = mainWindowSizesString.split(",");
for (var i in mainWindowSizesArray) {
let size = mainWindowSizesArray[i];
if (size.includes("/")) {
let fraction = size.split("/");
let ratio = parseFloat(fraction[0]) / parseFloat(fraction[1]);
mainWindowSizes.push(ratio);
} else {
mainWindowSizes.push(parseFloat(size));
}
};
log(SETTINGS_MAIN_WINDOW_SIZES + " set to " + mainWindowSizes);
log("Init complete, nbCols " + nbCols + " nbRows " + nbRows);
}
type MonitorTier = 'primary' | 'secondary';
function getMonitorTier(monitor: Monitor): MonitorTier {
return isPrimaryMonitor(monitor) ? 'primary' : 'secondary';
}
interface Insets {
top: number;
bottom: number;
left: number;
right: number;
}
function getMonitorInsets(tier: MonitorTier): Insets {
switch (tier) {
case 'primary':
return {
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
case 'secondary':
return {
top: getIntSetting(SETTINGS_INSETS_SECONDARY_TOP),
bottom: getIntSetting(SETTINGS_INSETS_SECONDARY_BOTTOM),
left: getIntSetting(SETTINGS_INSETS_SECONDARY_LEFT),
right: getIntSetting(SETTINGS_INSETS_SECONDARY_RIGHT)
};
default:
throw new Error(`unknown monitor name ${JSON.stringify(tier)}`);
}
}
/*****************************************************************
FUNCTIONS
*****************************************************************/
function init() {
}
export function enable() {
setLoggingEnabled(getBoolSetting(SETTINGS_DEBUG));
log("Extension enable begin");
SHELL_VERSION.print_version();
globalApp.enable();
}
export function disable() {
globalApp.disable();
}
function resetFocusMetaWindow() {
log("resetFocusMetaWindow");
focusMetaWindow = null;
}
function reset_window(metaWindow: Window) {
metaWindow.unmaximize(Meta.MaximizeFlags.HORIZONTAL);
metaWindow.unmaximize(Meta.MaximizeFlags.VERTICAL);
metaWindow.unmaximize(Meta.MaximizeFlags.BOTH);
}
function _getInvisibleBorderPadding(metaWindow: Window) {
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: Window) {
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: Window, x: number, y: number) {
const [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 moveResizeWindowWithMargins(metaWindow: Window, x: number, y: number, width: number, height: number): void {
let [borderX, borderY] = _getInvisibleBorderPadding(metaWindow);
let [vBorderX, vBorderY] = _getVisibleBorderPadding(metaWindow);
log("move_resize_window_with_margins " + metaWindow.get_title() + " " + x + ":" + y + " - " + width
+ ":" + height + " margin " + gridSettings[SETTINGS_WINDOW_MARGIN] + " borders invisible " +
borderX + ":" + borderY + " visible " + vBorderX + ":" + vBorderY);