-
Notifications
You must be signed in to change notification settings - Fork 6
/
util.js
86 lines (72 loc) · 2.66 KB
/
util.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
const Main = imports.ui.main;
const Gi = imports._gi;
const Gio = imports.gi.Gio;
const GioSSS = Gio.SettingsSchemaSource;
const ExtensionUtils = imports.misc.extensionUtils;
const Self = ExtensionUtils.getCurrentExtension();
function hookVfunc(proto, symbol, func) {
proto[Gi.hook_up_vfunc_symbol](symbol, func);
}
function overrideProto(proto, overrides) {
const backup = {};
for (var symbol in overrides) {
if (symbol.startsWith('after_')) {
const actualSymbol = symbol.slice('after_'.length);
const fn = proto[actualSymbol];
const afterFn = overrides[symbol]
proto[actualSymbol] = function() {
const args = Array.prototype.slice.call(arguments);
const res = fn.apply(this, args);
afterFn.apply(this, args);
return res;
};
backup[actualSymbol] = fn;
}
else {
backup[symbol] = proto[symbol];
if (symbol.startsWith('vfunc')) {
hookVfunc(proto[Gi.gobject_prototype_symbol], symbol.slice(6), overrides[symbol]);
}
else {
proto[symbol] = overrides[symbol];
}
}
}
return backup;
}
function bindSetting(label, callback, executeOnBind = true) {
let settings = global.vertical_overview.settings;
if (!settings) {
settings = global.vertical_overview.settings = {
object: ExtensionUtils.getSettings('org.gnome.shell.extensions.cosmic-workspaces'),
signals: {},
callbacks: {}
};
}
if (settings.signals[label])
settings.object.disconnect(settings.signals[label]);
const signal = global.vertical_overview.settings.object.connect('changed::' + label, callback);
global.vertical_overview.settings.signals[label] = signal;
settings.callbacks[label] = callback;
if (executeOnBind) callback(settings.object, label);
return signal;
}
// returns a cosmic dock object if safe to use else undefined
function getDock() {
const cosmicDock = Main.extensionManager.lookup("cosmic-dock@system76.com");
if (cosmicDock?.stateObj?.dockManager?.mainDock
&& cosmicDock.state === ExtensionUtils.ExtensionState.ENABLED)
return cosmicDock;
}
function unbindSetting(label, callback) {
let settings = global.vertical_overview.settings;
if (!settings || !settings.signals[label])
return;
if (callback)
callback(settings.object, label);
settings.object.disconnect(settings.signals[label]);
delete settings.signals[label];
if (settings.callbacks[label]) {
delete settings.callbacks[label];
}
}