-
Notifications
You must be signed in to change notification settings - Fork 11
/
extension.js
318 lines (263 loc) · 11.6 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
/**
* AATWS - Advanced Alt-Tab Window Switcher
* Extension
*
* @author GdH <G-dH@github.com>
* @copyright 2021-2024
* @license GPL-3.0
*/
'use strict';
import GLib from 'gi://GLib';
import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as AltTab from 'resource:///org/gnome/shell/ui/altTab.js';
import * as Layout from 'resource:///org/gnome/shell/ui/layout.js';
import * as WindowSwitcherPopup from './src/windowSwitcherPopup.js';
import * as Settings from './src/settings.js';
import * as Util from './src/util.js';
import * as SwitcherList from './src/switcherList.js';
import * as SwitcherItems from './src/switcherItems.js';
import * as WindowMenu from './src/windowMenu.js';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
const HOT_CORNER_PRESSURE_TIMEOUT = 1000; // ms
export default class AATWS extends Extension {
constructor(metadata) {
super(metadata);
this._originalOverlayKeyHandlerId = null;
this._signalOverlayKey = null;
this._wmFocusToActiveHandlerId = 0;
this._monitorsChangedConId = 0;
this._monitorsChangedDelayId = 0;
this._pressureBarriers = null;
}
enable() {
const Me = {
metadata: this.metadata,
gSettings: this.getSettings(),
_: this.gettext.bind(this),
};
Me.opt = new Settings.Options(Me);
this._opt = Me.opt;
this.Me = Me;
WindowSwitcherPopup.init(Me);
SwitcherList.init(Me);
SwitcherItems.init(Me);
WindowMenu.init(Me);
this._overrides = new Util.Overrides();
this._overrides.addOverride('WindowSwitcherPopup', AltTab.WindowSwitcherPopup.prototype, WindowSwitcherPopup.WindowSwitcherPopup);
// AppSwitcherPopup is handled by the WindowSwitcherPopup
this._overrides.addOverride('AppSwitcherPopup', AltTab.AppSwitcherPopup.prototype, WindowSwitcherPopup.WindowSwitcherPopup);
this._overrides.addOverride('AppSwitcherPopupInit', AltTab.AppSwitcherPopup.prototype, WindowSwitcherPopup.AppSwitcherPopup);
if (this._opt.get('superKeyMode') > 1)
this._updateOverlayKeyHandler();
this._updateHotTrigger();
this._updateDashVisibility();
this._updateSettings();
this._opt.connect('changed', this._updateSettings.bind(this));
console.debug(`${this.metadata.name}: enabled`);
}
disable() {
if (this._wmFocusToActiveHandlerId)
global.display.disconnect(this._wmFocusToActiveHandlerId);
Main.layoutManager.aatws?.destroy();
Main.layoutManager.aatws = null;
if (this._overrides) {
this._overrides.removeOverride('WindowSwitcherPopup');
this._overrides.removeOverride('AppSwitcherPopup');
}
this._overrides = null;
this._restoreOverlayKeyHandler();
this._removePressureBarrier();
this._updateDashVisibility(true);
WindowSwitcherPopup.cleanGlobal();
SwitcherList.cleanGlobal();
SwitcherItems.cleanGlobal();
WindowMenu.cleanGlobal();
this._opt.destroy();
this._opt = null;
this.Me = null;
console.debug(`${this.metadata.name}: enabled`);
}
_updateSettings(settings, key) {
// Option 3 - Show Window has been removed, switch to option 2 - Show Preview
const previewMode = this._opt.get('switcherPopupPreviewSelected');
if (previewMode === 3)
this._opt.set('switcherPopupPreviewSelected', 2);
if (key === 'super-key-mode')
this._updateOverlayKeyHandler();
if (key === 'hot-edge-position' || key === 'hot-edge-monitor' ||
key === 'hot-edge-pressure' || key === 'hot-edge-width')
this._updateHotTrigger();
if (key === 'show-dash')
this._updateDashVisibility();
}
_updateDashVisibility(reset) {
const visible = this._opt.get('showDash', true);
if (!visible) {
// pass
} else if (reset || visible === 1) {
Main.overview.dash.visible = true;
} else {
Main.overview.dash.visible = false;
}
}
_updateOverlayKeyHandler() {
// Block original overlay key handler
this._restoreOverlayKeyHandler();
if (this._opt.get('superKeyMode', true) === 1)
return;
this._originalOverlayKeyHandlerId = GObject.signal_handler_find(global.display, { signalId: 'overlay-key' });
if (this._originalOverlayKeyHandlerId !== null)
global.display.block_signal_handler(this._originalOverlayKeyHandlerId);
// Connect modified overlay key handler
let _a11ySettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.a11y.keyboard' });
this._signalOverlayKey = global.display.connect('overlay-key', () => {
if (_a11ySettings.get_boolean('stickykeys-enable'))
return;
this._toggleSwitcher();
});
}
_restoreOverlayKeyHandler() {
// Disconnect modified overlay key handler
if (this._signalOverlayKey !== null) {
global.display.disconnect(this._signalOverlayKey);
this._signalOverlayKey = null;
}
// Unblock original overlay key handler
if (this._originalOverlayKeyHandlerId !== null) {
global.display.unblock_signal_handler(this._originalOverlayKeyHandlerId);
this._originalOverlayKeyHandlerId = null;
}
}
_toggleSwitcher(mouseTriggered = false) {
if (Main.overview._visible) {
if (!mouseTriggered)
Main.overview.toggle();
return;
}
const altTabPopup = new AltTab.WindowSwitcherPopup();
if (mouseTriggered) {
altTabPopup._keyboardTriggered = false;
altTabPopup._popupPosition = this._opt.get('hotEdgePosition') === 1 ? 1 : 3; // 1-top, 2-bottom > 1-top, 2-center, 3-bottom
const appSwitcherMode = this._opt.get('hotEdgeMode') === 0;
altTabPopup._showApps = !!appSwitcherMode;
altTabPopup._switcherMode = appSwitcherMode ? 1 : 0;
altTabPopup._monitorIndex = global.display.get_current_monitor();
} else {
altTabPopup._keyboardTriggered = true;
const hotEdgePosition = this._opt.get('hotEdgePosition');
let position = hotEdgePosition ? hotEdgePosition : null;
if (position)
altTabPopup._popupPosition = position === 1 ? 1 : 3;
const appSwitcherMode = this._opt.get('superKeyMode') === 2;
altTabPopup._switcherMode = appSwitcherMode ? 1 : 0;
altTabPopup._showApps = !!appSwitcherMode;
altTabPopup._overlayKeyTriggered = true;
}
altTabPopup._modifierMask = 0;
altTabPopup._positionPointer = false;
altTabPopup.show();
}
_updateHotTrigger() {
this._removePressureBarrier();
const position = this._opt.get('hotEdgePosition', true);
if (!position)
return;
this._pressureBarriers = [];
const primaryMonitor = global.display.get_primary_monitor();
for (let i = 0; i < Main.layoutManager.monitors.length; ++i) {
if (!this._opt.get('hotEdgeMonitor', true) && i !== primaryMonitor)
continue;
// Use code of parent class to remove old barriers but new barriers
// must be created here since the properties are construct only.
// super.setBarrierSize(0);
const geometry = global.display.get_monitor_geometry(i);
const BD = Meta.BarrierDirection;
// for X11 session:
// right vertical and bottom horizontal pointer barriers must be 1px further to match the screen edge
// ...because barriers are actually placed between pixels, along the top/left edge of the addressed pixels
// ...Wayland behave differently and addressed pixel means the one behind which pointer can't go
// but avoid barriers that are at the same position
// ...and block opposite directions. Neither with X nor with Wayland
// ...such barriers work.
const scale = this._opt.get('hotEdgeWidth', true) / 100;
const offset = Math.round(geometry.width * (1 - scale) / 2);
const x1 = geometry.x + offset;
const x2 = geometry.x + geometry.width - offset;
let y = position === 1 ? geometry.y : geometry.y + geometry.height;
y -= Meta.is_wayland_compositor() ? 1 : 0;
let horizontalBarrier;
// GS 46+ replaced the Meta.Barrier.display property with backend
if (Meta.Barrier.prototype.backend) {
horizontalBarrier = new Meta.Barrier({
backend: global.backend,
x1,
x2,
y1: y,
y2: y,
directions: position === 1 ? BD.POSITIVE_Y : BD.NEGATIVE_Y,
});
} else {
horizontalBarrier = new Meta.Barrier({
display: global.display,
x1,
x2,
y1: y,
y2: y,
directions: position === 1 ? BD.POSITIVE_Y : BD.NEGATIVE_Y,
});
}
const pressureBarrier = new Layout.PressureBarrier(
this._opt.get('hotEdgePressure', true), // pressure threshold
HOT_CORNER_PRESSURE_TIMEOUT,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW
);
pressureBarrier.connect('trigger', this._onPressureTriggered.bind(this, Main.layoutManager.monitors[i]));
pressureBarrier.addBarrier(horizontalBarrier);
this._pressureBarriers.push([pressureBarrier, horizontalBarrier]);
if (!this._monitorsChangedConId) {
this._monitorsChangedConId = Main.layoutManager.connect('monitors-changed', () => {
// avoid unnecessary executions, the signal is being emitted multiple times
if (!this._monitorsChangedDelayId) {
this._monitorsChangedDelayId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
1,
() => {
this._updateHotTrigger();
this._monitorsChangedDelayId = 0;
return GLib.SOURCE_REMOVE;
}
);
}
return GLib.SOURCE_CONTINUE;
});
}
}
}
_removePressureBarrier() {
if (this._pressureBarriers !== null) {
this._pressureBarriers.forEach(barrier => {
barrier[0].removeBarrier(barrier[1]);
barrier[1].destroy();
barrier[0].destroy();
});
this._pressureBarriers = null;
}
if (this._monitorsChangedConId) {
Main.layoutManager.disconnect(this._monitorsChangedConId);
this._monitorsChangedConId = 0;
}
if (this._monitorsChangedDelayId) {
GLib.source_remove(this._monitorsChangedDelayId);
this._monitorsChangedDelayId = 0;
}
}
_onPressureTriggered(monitor) {
const fsAllowed = this._opt.get('hotEdgeFullScreen');
if (!(!fsAllowed && monitor.inFullscreen))
this._toggleSwitcher(true);
}
}