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
|
/**
* V-Shell (Vertical Workspaces)
* util.js
*
* @author GdH <G-dH@github.com>
* @copyright 2022 - 2023
* @license GPL-3.0
*
*/
'use strict';
const Clutter = imports.gi.Clutter;
const Meta = imports.gi.Meta;
const Gi = imports._gi;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Shell = imports.gi.Shell;
const Main = imports.ui.main;
let Me;
let _installedExtensions;
function init(me) {
Me = me;
}
function cleanGlobals() {
Me = null;
_installedExtensions = null;
}
var Overrides = class {
constructor() {
this._overrides = {};
}
addOverride(name, prototype, overrideList) {
const backup = this.overrideProto(prototype, overrideList, name);
// don't update originals when override's just refreshing, keep initial content
let originals = this._overrides[name]?.originals;
if (!originals)
originals = backup;
this._overrides[name] = {
originals,
prototype,
};
}
removeOverride(name) {
const override = this._overrides[name];
if (!override)
return false;
this.overrideProto(override.prototype, override.originals, name);
delete this._overrides[name];
return true;
}
removeAll() {
for (let name in this._overrides) {
this.removeOverride(name);
delete this._overrides[name];
}
}
hookVfunc(proto, symbol, func) {
proto[Gi.hook_up_vfunc_symbol](symbol, func);
}
overrideProto(proto, overrides, name) {
const backup = {};
const originals = this._overrides[name]?.originals;
for (let symbol in overrides) {
if (symbol.startsWith('after_')) {
const actualSymbol = symbol.slice('after_'.length);
let fn;
if (originals && originals[actualSymbol])
fn = originals[actualSymbol];
else
fn = proto[actualSymbol];
const afterFn = overrides[symbol];
proto[actualSymbol] = function (...args) {
args = Array.prototype.slice.call(args);
const res = fn.apply(this, args);
afterFn.apply(this, args);
return res;
};
backup[actualSymbol] = fn;
} else {
backup[symbol] = proto[symbol];
if (symbol.startsWith('vfunc')) {
if (Me.shellVersion < 42)
this.hookVfunc(proto, symbol.slice(6), overrides[symbol]);
else
this.hookVfunc(proto[Gi.gobject_prototype_symbol], symbol.slice(6), overrides[symbol]);
} else if (overrides[symbol] !== null) {
proto[symbol] = overrides[symbol];
}
}
}
return backup;
}
};
function openPreferences(metadata) {
if (!metadata)
metadata = Me.metadata;
const windows = global.display.get_tab_list(Meta.TabList.NORMAL_ALL, null);
let tracker = Shell.WindowTracker.get_default();
let metaWin, isVW = null;
for (let win of windows) {
const app = tracker.get_window_app(win);
if (win.get_title()?.includes(metadata.name) && app.get_name() === 'Extensions') {
// this is our existing window
metaWin = win;
isVW = true;
break;
} else if (win.wm_class?.includes('org.gnome.Shell.Extensions')) {
// this is prefs window of another extension
metaWin = win;
isVW = false;
}
}
if (metaWin && !isVW) {
// other prefs window blocks opening another prefs window, so close it
metaWin.delete(global.get_current_time());
} else if (metaWin && isVW) {
// if prefs window already exist, move it to the current WS and activate it
metaWin.change_workspace(global.workspace_manager.get_active_workspace());
metaWin.activate(global.get_current_time());
}
if (!metaWin || (metaWin && !isVW)) {
GLib.idle_add(GLib.PRIORITY_LOW, () => {
try {
Main.extensionManager.openExtensionPrefs(metadata.uuid, '', {});
} catch (e) {
console.error(e);
}
});
}
}
function activateSearchProvider(prefix = '') {
const searchEntry = Main.overview.searchEntry;
const searchEntryText = searchEntry.get_text();
if (!searchEntryText || (searchEntryText && !searchEntry.get_text().startsWith(prefix))) {
prefix = `${prefix} `;
const position = prefix.length;
searchEntry.set_text(prefix);
searchEntry.get_first_child().set_cursor_position(position);
searchEntry.get_first_child().set_selection(position, position);
} else {
searchEntry.set_text('');
}
}
function dashNotDefault() {
return Main.overview.dash !== Main.overview._overview._controls.layoutManager._dash;
}
function dashIsDashToDock() {
return Main.overview.dash._isHorizontal !== undefined;
}
// Reorder Workspaces - callback for Dash and workspacesDisplay
function reorderWorkspace(direction = 0) {
let activeWs = global.workspace_manager.get_active_workspace();
let activeWsIdx = activeWs.index();
let targetIdx = activeWsIdx + direction;
if (targetIdx > -1 && targetIdx < global.workspace_manager.get_n_workspaces())
global.workspace_manager.reorder_workspace(activeWs, targetIdx);
}
function activateKeyboardForWorkspaceView() {
Main.ctrlAltTabManager._items.forEach(i => {
if (i.sortGroup === 1 && i.name === 'Windows')
Main.ctrlAltTabManager.focusGroup(i);
});
}
function exposeWindows(adjustment, activateKeyboard) {
// expose windows for static overview modes
if (!adjustment.value/* && !Main.overview._animationInProgress*/) {
if (adjustment.value === 0) {
adjustment.value = 0;
adjustment.ease(1, {
duration: 200,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
onComplete: () => {
if (activateKeyboard) {
Main.ctrlAltTabManager._items.forEach(i => {
if (i.sortGroup === 1 && i.name === 'Windows')
Main.ctrlAltTabManager.focusGroup(i);
});
}
},
});
}
}
}
function isShiftPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.SHIFT_MASK) !== 0;
}
function isCtrlPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.CONTROL_MASK) !== 0;
}
function isAltPressed(state = null) {
if (state === null)
[,, state] = global.get_pointer();
return (state & Clutter.ModifierType.MOD1_MASK) !== 0;
}
function fuzzyMatch(term, text) {
let pos = -1;
const matches = [];
// convert all accented chars to their basic form and to lower case
const _text = text.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
const _term = term.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
// if term matches the substring exactly, gains the highest weight
if (_text.includes(_term))
return 0;
for (let i = 0; i < _term.length; i++) {
let c = _term[i];
let p;
if (pos > 0)
p = _term[i - 1];
while (true) {
pos += 1;
if (pos >= _text.length)
return -1;
if (_text[pos] === c) {
matches.push(pos);
break;
} else if (_text[pos] === p) {
matches.pop();
matches.push(pos);
}
}
}
// add all position to get a weight of the result
// results closer to the beginning of the text and term characters closer to each other will gain more weight.
return matches.reduce((r, p) => r + p) - matches.length * matches[0] + matches[0];
}
function strictMatch(term, text) {
// remove diacritics and accents from letters
let s = text.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
let p = term.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase();
let ps = p.split(/ +/);
// allows to use multiple exact patterns separated by a space in arbitrary order
for (let w of ps) { // escape regex control chars
if (!s.match(w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')))
return -1;
}
return 0;
}
function isMoreRelevant(stringA, stringB, pattern) {
let regex = /[^a-zA-Z\d]/;
let strSplitA = stringA.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().split(regex);
let strSplitB = stringB.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase().split(regex);
let aAny = false;
strSplitA.forEach(w => {
aAny = aAny || w.startsWith(pattern);
});
let bAny = false;
strSplitB.forEach(w => {
bAny = bAny || w.startsWith(pattern);
});
// if both strings contain a word that starts with the pattern
// prefer the one whose first word starts with the pattern
if (aAny && bAny)
return !strSplitA[0].startsWith(pattern) && strSplitB[0].startsWith(pattern);
else
return !aAny && bAny;
}
function getEnabledExtensions(pattern = '') {
let result = [];
// extensionManager is unreliable at startup (if not all extensions were loaded)
// but gsettings key can contain removed extensions...
// therefore we have to look into filesystem, what's really installed
if (!_installedExtensions) {
const extensionFiles = [...collectFromDatadirs('extensions', true)];
_installedExtensions = extensionFiles.map(({ info }) => {
let fileType = info.get_file_type();
if (fileType !== Gio.FileType.DIRECTORY)
return null;
const uuid = info.get_name();
return uuid;
});
}
const enabled = Main.extensionManager._enabledExtensions;
result = _installedExtensions.filter(ext => enabled.includes(ext));
return result.filter(uuid => uuid !== null && uuid.includes(pattern));
}
function* collectFromDatadirs(subdir, includeUserDir) {
let dataDirs = GLib.get_system_data_dirs();
if (includeUserDir)
dataDirs.unshift(GLib.get_user_data_dir());
for (let i = 0; i < dataDirs.length; i++) {
let path = GLib.build_filenamev([dataDirs[i], 'gnome-shell', subdir]);
let dir = Gio.File.new_for_path(path);
let fileEnum;
try {
fileEnum = dir.enumerate_children('standard::name,standard::type',
Gio.FileQueryInfoFlags.NONE, null);
} catch (e) {
fileEnum = null;
}
if (fileEnum !== null) {
let info;
while ((info = fileEnum.next_file(null)))
yield { dir: fileEnum.get_child(info), info };
}
}
}
function getScrollDirection(event) {
// scroll wheel provides two types of direction information:
// 1. Clutter.ScrollDirection.DOWN / Clutter.ScrollDirection.UP
// 2. Clutter.ScrollDirection.SMOOTH + event.get_scroll_delta()
// first SMOOTH event returns 0 delta,
// so we need to always read event.direction
// since mouse without smooth scrolling provides exactly one SMOOTH event on one wheel rotation click
// on the other hand, under X11, one wheel rotation click sometimes doesn't send direction event, only several SMOOTH events
// so we also need to convert the delta to direction
let direction = event.get_scroll_direction();
if (direction !== Clutter.ScrollDirection.SMOOTH)
return direction;
let [, delta] = event.get_scroll_delta();
if (!delta)
return null;
direction = delta > 0 ? Clutter.ScrollDirection.DOWN : Clutter.ScrollDirection.UP;
return direction;
}
|