-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathextension.ts
More file actions
703 lines (617 loc) · 24.1 KB
/
extension.ts
File metadata and controls
703 lines (617 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import './styles/stylesheet.scss';
import { Gio, GLib, Meta } from '@gi.ext';
import { logger } from '@utils/logger';
import {
filterUnfocusableWindows,
getMonitors,
squaredEuclideanDistance,
} from '@/utils/ui';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { TilingManager } from '@/components/tilingsystem/tilingManager';
import Settings from '@settings/settings';
import SignalHandling from './utils/signalHandling';
import GlobalState from './utils/globalState';
import Indicator from './indicator/indicator';
import { ExtensionMetadata } from 'resource:///org/gnome/shell/extensions/extension.js';
import DBus from './dbus';
import KeyBindings, {
KeyBindingsDirection,
FocusSwitchDirection,
} from './keybindings';
import SettingsOverride from '@settings/settingsOverride';
import { ResizingManager } from '@components/tilingsystem/resizeManager';
import OverriddenWindowMenu from '@components/window_menu/overriddenWindowMenu';
import Tile from '@components/layout/Tile';
import { WindowBorderManager } from '@components/windowBorderManager';
import TilingShellWindowManager from '@components/windowManager/tilingShellWindowManager';
import ExtendedWindow from '@components/tilingsystem/extendedWindow';
import { Extension } from '@polyfill';
const debug = logger('extension');
export default class TilingShellExtension extends Extension {
private _indicator: Indicator | null;
private _tilingManagers: TilingManager[];
private _fractionalScalingEnabled: boolean;
private _dbus: DBus | null;
private _signals: SignalHandling | null;
private _keybindings: KeyBindings | null;
private _resizingManager: ResizingManager | null;
private _windowBorderManager: WindowBorderManager | null;
constructor(metadata: ExtensionMetadata) {
super(metadata);
this._signals = null;
this._fractionalScalingEnabled = false;
this._tilingManagers = [];
this._indicator = null;
this._dbus = null;
this._keybindings = null;
this._resizingManager = null;
this._windowBorderManager = null;
}
createIndicator() {
this._indicator = new Indicator(this.path, this.uuid);
this._indicator.enableScaling = !this._fractionalScalingEnabled;
this._indicator.enable();
}
private _validateSettings() {
if (Settings.LAST_VERSION_NAME_INSTALLED === '14.0') {
debug('apply compatibility changes');
Settings.save_selected_layouts([]);
}
// Setting used for compatibility changes if necessary
if (this.metadata['version-name']) {
Settings.LAST_VERSION_NAME_INSTALLED =
this.metadata['version-name'] || '0';
}
}
enable(): void {
if (this._signals) this._signals.disconnect();
this._signals = new SignalHandling();
Settings.initialize(this.getSettings());
this._validateSettings();
// force initialization and tracking of windows
TilingShellWindowManager.get();
this._fractionalScalingEnabled = this._isFractionalScalingEnabled(
new Gio.Settings({ schema: 'org.gnome.mutter' }),
);
if (this._keybindings) this._keybindings.destroy();
this._keybindings = new KeyBindings(this.getSettings());
// disable native edge tiling
if (Settings.ACTIVE_SCREEN_EDGES) {
SettingsOverride.get().override(
new Gio.Settings({ schema_id: 'org.gnome.mutter' }),
'edge-tiling',
new GLib.Variant('b', false),
);
}
if (Main.layoutManager._startingUp) {
this._signals.connect(
Main.layoutManager,
'startup-complete',
() => {
this._createTilingManagers();
this._setupSignals();
},
);
} else {
this._createTilingManagers();
this._setupSignals();
}
this._resizingManager = new ResizingManager();
this._resizingManager.enable();
if (this._windowBorderManager) this._windowBorderManager.destroy();
this._windowBorderManager = new WindowBorderManager(
!this._fractionalScalingEnabled,
);
this._windowBorderManager.enable();
this.createIndicator();
if (this._dbus) this._dbus.disable();
this._dbus = new DBus();
this._dbus.enable(this);
if (Settings.OVERRIDE_WINDOW_MENU) OverriddenWindowMenu.enable();
debug('extension is enabled');
}
public openLayoutEditor() {
this._indicator?.openLayoutEditor();
}
private _createTilingManagers() {
debug('building a tiling manager for each monitor');
this._tilingManagers.forEach((tm) => tm.destroy());
this._tilingManagers = getMonitors().map(
(monitor) =>
new TilingManager(monitor, !this._fractionalScalingEnabled),
);
this._tilingManagers.forEach((tm) => tm.enable());
}
private _setupSignals() {
if (!this._signals) return;
this._signals.connect(global.display, 'workareas-changed', () => {
const allMonitors = getMonitors();
if (this._tilingManagers.length !== allMonitors.length) {
// a monitor was disconnected or a new one was connected
GlobalState.get().validate_selected_layouts();
// finally build a tiling manager for each monitor
this._createTilingManagers();
} else {
// the number of monitors is the same, so update the workarea
this._tilingManagers.forEach((tm, index) => {
tm.workArea =
Main.layoutManager.getWorkAreaForMonitor(index);
});
}
});
this._signals.connect(
new Gio.Settings({ schema: 'org.gnome.mutter' }),
'changed::experimental-features',
(_mutterSettings: Gio.Settings) => {
if (!_mutterSettings) return;
const fractionalScalingEnabled =
this._isFractionalScalingEnabled(_mutterSettings);
if (this._fractionalScalingEnabled === fractionalScalingEnabled)
return;
this._fractionalScalingEnabled = fractionalScalingEnabled;
this._createTilingManagers();
if (this._indicator) {
this._indicator.enableScaling =
!this._fractionalScalingEnabled;
}
if (this._windowBorderManager)
this._windowBorderManager.destroy();
this._windowBorderManager = new WindowBorderManager(
this._fractionalScalingEnabled,
);
this._windowBorderManager.enable();
},
);
if (this._keybindings) {
this._signals.connect(
this._keybindings,
'move-window',
(
kb: KeyBindings,
dp: Meta.Display,
dir: KeyBindingsDirection,
) => {
this._onKeyboardMoveWin(dp, dir, false);
},
);
this._signals.connect(
this._keybindings,
'span-window',
(
kb: KeyBindings,
dp: Meta.Display,
dir: KeyBindingsDirection,
) => {
this._onKeyboardMoveWin(dp, dir, true);
},
);
this._signals.connect(
this._keybindings,
'span-window-all-tiles',
(kb: KeyBindings, dp: Meta.Display) => {
const window = dp.focus_window;
const monitorIndex = window.get_monitor();
const manager = this._tilingManagers[monitorIndex];
if (manager) manager.onSpanAllTiles(window);
},
);
this._signals.connect(
this._keybindings,
'untile-window',
this._onKeyboardUntileWindow.bind(this),
);
this._signals.connect(
this._keybindings,
'move-window-center',
(kb: KeyBindings, dp: Meta.Display) => {
this._onKeyboardMoveWin(
dp,
KeyBindingsDirection.NODIRECTION,
false,
);
},
);
this._signals.connect(
this._keybindings,
'focus-window',
(
kb: KeyBindings,
dp: Meta.Display,
dir: FocusSwitchDirection,
) => {
this._onKeyboardFocusWin(dp, dir);
},
);
this._signals.connect(
this._keybindings,
'focus-window-direction',
(
kb: KeyBindings,
dp: Meta.Display,
dir: KeyBindingsDirection,
) => {
this._onKeyboardFocusWinDirection(dp, dir);
},
);
}
// when Tiling Shell's edge-tiling is enabled/disable
// then enable/disable native edge-tiling
this._signals.connect(
Settings,
Settings.KEY_ACTIVE_SCREEN_EDGES,
() => {
const gioSettings = new Gio.Settings({
schema_id: 'org.gnome.mutter',
});
if (Settings.ACTIVE_SCREEN_EDGES) {
debug('disable native edge tiling');
// disable native edge tiling
SettingsOverride.get().override(
gioSettings,
'edge-tiling',
new GLib.Variant('b', false),
);
} else {
// bring back the value of native edge tiling
debug('bring back native edge tiling');
SettingsOverride.get().restoreKey(
gioSettings,
'edge-tiling',
);
}
},
);
// enable/disable window menu from preferences
this._signals.connect(
Settings,
Settings.KEY_OVERRIDE_WINDOW_MENU,
() => {
if (Settings.OVERRIDE_WINDOW_MENU)
OverriddenWindowMenu.enable();
else OverriddenWindowMenu.disable();
},
);
// tile a window when a tile or a button is clicked from the window menu
this._signals.connect(
OverriddenWindowMenu,
'tile-clicked',
(_, tile: Tile, window: Meta.Window) => {
const monitorIndex = window.get_monitor();
const manager = this._tilingManagers[monitorIndex];
if (manager) manager.onTileFromWindowMenu(tile, window);
},
);
/* todo move maximized to workspace
this._signals.connect(
global.window_manager,
'size-change',
this._moveMaximizedToWorkspace.bind(this),
);
this._signals.connect(
global.window_manager,
'size-changed',
this._onSizeChanged.bind(this),
);*/
}
/* todo private _moveMaximizedToWorkspace(
wm: Shell.WM,
winActor: Meta.WindowActor,
change: Meta.SizeChange,
) {
const window = winActor.metaWindow;
if (
window.wmClass === null ||
change !== Meta.SizeChange.MAXIMIZE || // handle maximize changes only
window.get_maximized() !== Meta.MaximizeFlags.BOTH || // handle maximized window only
window.is_attached_dialog() || // skip dialogs
window.is_on_all_workspaces() ||
window.windowType !== Meta.WindowType.NORMAL || // handle normal windows only
window.wmClass === 'gjs'
)
return;
const prevWorkspace = window.get_workspace();
// if it is the only window in the workspace, no new workspace is needed
if (
!prevWorkspace
.list_windows()
.find(
(otherWin) =>
otherWin !== window &&
otherWin.windowType === Meta.WindowType.NORMAL &&
!otherWin.is_always_on_all_workspaces() &&
otherWin.wmClass !== null &&
otherWin.wmClass !== 'gjs',
)
)
return;
// disable GNOME default fade out animation
// @ts-expect-error Main.wm has "_sizeChangeWindowDone" function
Main.wm._sizeChangeWindowDone(global.windowManager, winActor);
const wasActive = prevWorkspace.active;
// create a new workspace, do not focus it
const newWorkspace = global.workspace_manager.append_new_workspace(
false,
global.get_current_time(),
);
// place the workspace after the current one
global.workspace_manager.reorder_workspace(
newWorkspace,
prevWorkspace.index() + 1,
);
// queue focus the workspace, focusing the window too. This will trigger workspace slide-in animation
if (wasActive) window._queue_focus_ws = newWorkspace;
}
private _onSizeChanged(wm: Shell.WM, winActor: Meta.WindowActor) {
const window = winActor.metaWindow;
if (!window._queue_focus_ws) return;
const ws = window._queue_focus_ws;
delete window._queue_focus_ws;
console.log(`_onSizeChanged ${ws}`);
// move the window
ws.activate_with_focus(window, global.get_current_time());
window.change_workspace(ws);
// todo check the following
// If the selected window is on a different workspace, we don't
// want it to disappear, then slide in with the workspace; instead,
// always activate it on the active workspace ...
activeWs.activate_with_focus(window, global.get_current_time());
// ... then slide it over to the original workspace if necessary
Main.wm.actionMoveWindow(window, ws);
}*/
private _onKeyboardMoveWin(
display: Meta.Display,
direction: KeyBindingsDirection,
spanFlag: boolean,
) {
const focus_window = display.get_focus_window();
if (
!focus_window ||
!focus_window.has_focus() ||
(focus_window.get_wm_class() &&
focus_window.get_wm_class() === 'gjs')
)
return;
// if the window is maximized, it cannot be spanned
if (
(focus_window.maximizedHorizontally ||
focus_window.maximizedVertically) &&
spanFlag
)
return;
// handle unmaximize of maximized window
if (
(focus_window.maximizedHorizontally ||
focus_window.maximizedVertically) &&
direction === KeyBindingsDirection.DOWN
) {
focus_window.unmaximize(Meta.MaximizeFlags.BOTH);
return;
}
const monitorTilingManager =
this._tilingManagers[focus_window.get_monitor()];
if (!monitorTilingManager) return;
if (
Settings.ENABLE_AUTO_TILING &&
(focus_window.maximizedHorizontally ||
focus_window.maximizedVertically)
) {
focus_window.unmaximize(Meta.MaximizeFlags.BOTH);
return;
}
let displayDirection = Meta.DisplayDirection.DOWN;
switch (direction) {
case KeyBindingsDirection.LEFT:
displayDirection = Meta.DisplayDirection.LEFT;
break;
case KeyBindingsDirection.RIGHT:
displayDirection = Meta.DisplayDirection.RIGHT;
break;
case KeyBindingsDirection.UP:
displayDirection = Meta.DisplayDirection.UP;
break;
}
const neighborMonitorIndex = display.get_monitor_neighbor_index(
focus_window.get_monitor(),
displayDirection,
);
const success = monitorTilingManager.onKeyboardMoveWindow(
focus_window,
direction,
false,
spanFlag,
neighborMonitorIndex === -1, // clamp if there is NOT a monitor in this direction
);
if (
success ||
direction === KeyBindingsDirection.NODIRECTION ||
neighborMonitorIndex === -1
)
return;
// if the window is maximized, direction is UP and there is a monitor above, minimize the window
if (
(focus_window.maximizedHorizontally ||
focus_window.maximizedVertically) &&
direction === KeyBindingsDirection.UP
) {
// @ts-expect-error "Main.wm has skipNextEffect function"
Main.wm.skipNextEffect(focus_window.get_compositor_private());
focus_window.unmaximize(Meta.MaximizeFlags.BOTH);
(focus_window as ExtendedWindow).assignedTile = undefined;
}
const neighborTilingManager =
this._tilingManagers[neighborMonitorIndex];
if (!neighborTilingManager) return;
neighborTilingManager.onKeyboardMoveWindow(
focus_window,
direction,
true,
spanFlag,
false,
);
}
private _onKeyboardFocusWinDirection(
display: Meta.Display,
direction: KeyBindingsDirection | FocusSwitchDirection,
) {
const focus_window = display.get_focus_window();
const focusParent = focus_window.get_transient_for() || focus_window;
if (
!focus_window ||
!focus_window.has_focus() ||
focusParent.windowType !== Meta.WindowType.NORMAL ||
(focus_window.get_wm_class() &&
focus_window.get_wm_class() === 'gjs')
)
return;
let bestWindow: Meta.Window | undefined;
let bestWindowDistance = -1;
const focusWindowRect = focus_window.get_frame_rect();
const focusWindowCenter = {
x: focusWindowRect.x + focusWindowRect.width / 2,
y: focusWindowRect.y + focusWindowRect.height / 2,
};
const windowList = filterUnfocusableWindows(
focus_window.get_workspace().list_windows(),
);
windowList
.filter((win) => {
if (win === focus_window || win.minimized) return false;
const winRect = win.get_frame_rect();
switch (direction) {
case KeyBindingsDirection.RIGHT:
return winRect.x > focusWindowRect.x;
case KeyBindingsDirection.LEFT:
return winRect.x < focusWindowRect.x;
case KeyBindingsDirection.UP:
return winRect.y < focusWindowRect.y;
case KeyBindingsDirection.DOWN:
return winRect.y > focusWindowRect.y;
}
return false;
})
.forEach((win) => {
const winRect = win.get_frame_rect();
const winCenter = {
x: winRect.x + winRect.width / 2,
y: winRect.y + winRect.height / 2,
};
const euclideanDistance = squaredEuclideanDistance(
winCenter,
focusWindowCenter,
);
if (
!bestWindow ||
euclideanDistance < bestWindowDistance ||
(euclideanDistance === bestWindowDistance &&
bestWindow.get_frame_rect().y > winRect.y)
) {
bestWindow = win;
bestWindowDistance = euclideanDistance;
}
});
if (!bestWindow) return;
bestWindow.activate(global.get_current_time());
}
private _onKeyboardFocusWin(
display: Meta.Display,
direction: FocusSwitchDirection,
) {
const focus_window = display.get_focus_window();
const focusParent = focus_window.get_transient_for() || focus_window;
if (
!focus_window ||
!focus_window.has_focus() ||
focusParent.windowType !== Meta.WindowType.NORMAL ||
(focus_window.get_wm_class() &&
focus_window.get_wm_class() === 'gjs')
)
return;
const windowList = filterUnfocusableWindows(
focus_window.get_workspace().list_windows(),
);
const focusedIdx = windowList.findIndex((win) => {
// in case we are iterating over a modal dialog for our focused window
return win === focusParent;
});
let nextIndex = -1;
switch (direction) {
case FocusSwitchDirection.PREV:
if (focusedIdx === 0 && Settings.WRAPAROUND_FOCUS) {
windowList[windowList.length - 1].activate(
global.get_current_time(),
);
} else {
windowList[focusedIdx - 1].activate(
global.get_current_time(),
);
}
break;
case FocusSwitchDirection.NEXT:
nextIndex = (focusedIdx + 1) % windowList.length;
if (nextIndex > 0 || Settings.WRAPAROUND_FOCUS)
windowList[nextIndex].activate(global.get_current_time());
break;
}
}
private _onKeyboardUntileWindow(kb: KeyBindings, display: Meta.Display) {
const focus_window = display.get_focus_window();
if (
!focus_window ||
!focus_window.has_focus() ||
focus_window.windowType !== Meta.WindowType.NORMAL ||
(focus_window.get_wm_class() &&
focus_window.get_wm_class() === 'gjs')
)
return;
// if the window is maximized, unmaximize it
if (focus_window.get_maximized())
focus_window.unmaximize(Meta.MaximizeFlags.BOTH);
const monitorTilingManager =
this._tilingManagers[focus_window.get_monitor()];
if (!monitorTilingManager) return;
monitorTilingManager.onUntileWindow(focus_window, true);
}
private _isFractionalScalingEnabled(
_mutterSettings: Gio.Settings,
): boolean {
return (
_mutterSettings
.get_strv('experimental-features')
.find(
(feat) =>
feat === 'scale-monitor-framebuffer' ||
feat === 'x11-randr-fractional-scaling',
) !== undefined
);
}
disable(): void {
// bring back overridden keybindings
this._keybindings?.destroy();
this._keybindings = null;
// destroy indicator
this._indicator?.destroy();
this._indicator = null;
// destroy tiling managers
this._tilingManagers.forEach((tm) => tm.destroy());
this._tilingManagers = [];
// disconnect signals
this._signals?.disconnect();
this._signals = null;
this._resizingManager?.destroy();
this._resizingManager = null;
this._windowBorderManager?.destroy();
this._windowBorderManager = null;
// disable dbus
this._dbus?.disable();
this._dbus = null;
this._fractionalScalingEnabled = false;
OverriddenWindowMenu.destroy();
// restore native edge tiling and all the overridden settings
SettingsOverride.destroy();
// destroy state and settings
GlobalState.destroy();
Settings.destroy();
TilingShellWindowManager.destroy();
debug('extension is disabled');
}
}