-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
94 lines (79 loc) · 2.93 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
const { GObject, St, Gio, GLib } = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = imports.misc.util;
const { GsettingsManager } = Me.imports.gsettingsManager;
class ToggleAWG {
constructor(gsettingsManager) {
this._isActive = false;
this.gsettingsManager = gsettingsManager;
}
_getIconPath(isActive) {
const manualTheme = this.gsettingsManager.getValue('manual-theme');
const iconType = manualTheme === 'dark' ? 'dark' : 'light';
return `${Me.path}/icons/${isActive ? 'active-' : 'inactive-'}${iconType}.png`;
}
toggleService() {
const iface = this.gsettingsManager.getValue('interface');
if (!iface) {
Main.notify('Toggle AWG', 'No interface set in settings!');
return;
}
const action = this._isActive ? 'stop' : 'start';
const command = ['sudo', 'systemctl', action, `awg-quick@${iface}`];
Util.spawn(command);
this._isActive = !this._isActive;
}
}
var ToggleAWGButton = GObject.registerClass(
class ToggleAWGButton extends PanelMenu.Button {
_init(toggleAWG, gsettingsManager) {
super._init(0.0, 'Toggle AWG Button');
this.toggleAWG = toggleAWG;
this.gsettingsManager = gsettingsManager;
this._icon = this._createIcon(false);
this.add_child(this._icon);
this.connect('button-press-event', () => {
this.toggleAWG.toggleService();
this._updateIcon();
});
this.gsettingsManager.settings.connect('changed', () => this._updateIcon());
}
_createIcon(isActive) {
return new St.Icon({
gicon: Gio.icon_new_for_string(this.toggleAWG._getIconPath(isActive)),
icon_size: this.gsettingsManager.getValue('icon-size'),
style_class: 'system-status-icon',
});
}
_updateIcon() {
const isActive = this.toggleAWG._isActive;
this._icon.gicon = Gio.icon_new_for_string(this.toggleAWG._getIconPath(isActive));
this._icon.icon_size = this.gsettingsManager.getValue('icon-size');
}
}
);
class Extension {
constructor() {}
enable() {
this._button = null;
this._gsettingsManager = new GsettingsManager();
const toggleAWG = new ToggleAWG(this._gsettingsManager);
this._button = new ToggleAWGButton(toggleAWG, this._gsettingsManager);
Main.panel.addToStatusArea('toggle-awg-button', this._button);
}
disable() {
if (this._button) {
this._button.destroy();
this._button = null;
}
if (this._gsettingsManager) {
this._gsettingsManager = null;
}
}
}
function init() {
return new Extension();
}