-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
120 lines (99 loc) · 4.01 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
import GObject from 'gi://GObject';
import St from 'gi://St';
import GLib from 'gi://GLib';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
export default class TwingateStatusIndicatorExtension extends Extension {
enable() {
this._indicator = new SocketIconSwitcher();
Main.panel.addToStatusArea(this.uuid, this._indicator);
}
disable() {
if (this._indicator) {
this._indicator.stop();
this._indicator.destroy();
this._indicator = null;
}
}
}
const SocketIconSwitcher = GObject.registerClass(
class SocketIconSwitcher extends PanelMenu.Button {
constructor() {
super(0.0, 'Socket Icon Switcher');
this._socketPath = '/run/twingate/auth.sock';
this._iconNameOn = 'twingate_on';
this._iconNameOff = 'twingate_off';
this._fastPollInterval = 1000;
this._slowPollInterval = 10000;
this._connected = false;
this.icon = new St.Icon({ style_class: this._iconNameOff });
this.add_child(this.icon);
this._menuItem = new PopupMenu.PopupMenuItem('Disconnected');
this.menu.addMenuItem(this._menuItem);
this._menuConnectionHandle = this._menuItem.connect('activate', () => {
this._handleMenuItemClick();
});
this._addFileWatch(this._slowPollInterval);
}
_setIconState() {
if (this._connected === true) {
this.icon.style_class = this._iconNameOn;
this._menuItem.label.text = 'Connected';
} else {
this.icon.style_class = this._iconNameOff;
this._menuItem.label.text = 'Disconnected';
}
}
_handleMenuItemClick() {
this._removeFileWatch();
this._addFileWatch(this._fastPollInterval, () => {
this._removeFileWatch();
this._addFileWatch(this._slowPollInterval);
})
if (this._connected === true) {
GLib.spawn_command_line_async('systemctl stop twingate');
GLib.spawn_command_line_async('systemctl stop --user twingate-desktop-notifier');
} else {
GLib.spawn_command_line_async('systemctl start twingate');
GLib.spawn_command_line_async('systemctl start --user twingate-desktop-notifier');
}
}
_addFileWatch(pollInterval, onChange) {
this._pollerTimeoutHandle = GLib.timeout_add(GLib.PRIORITY_DEFAULT, pollInterval, () => {
if (GLib.file_test(this._socketPath, GLib.FileTest.EXISTS)) {
if(!this._connected) {
this._connected = true;
this._setIconState();
if(onChange) onChange();
}
} else {
if(this._connected){
this._connected = false;
this._setIconState();
if(onChange) onChange();
}
}
return GLib.SOURCE_CONTINUE;
});
}
_removeFileWatch() {
if (this._pollerTimeoutHandle) {
GLib.Source.remove(this._pollerTimeoutHandle);
this._pollerTimeoutHandle = null;
}
}
stop() {
if (this._menuItem && this._menuConnectionHandle) {
this._menuItem.disconnect(this._menuConnectionHandle);
}
this.icon?.destroy();
this.icon = null;
this._menuItem?.destroy();
this._menuItem = null;
this._menuConnectionHandle = null;
this._removeFileWatch();
}
}
);