-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
189 lines (164 loc) · 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
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* exported init */
'use strict';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as QuickSettings from 'resource:///org/gnome/shell/ui/quickSettings.js';
const KMonadToggle = GObject.registerClass(
class FeatureToggle extends QuickSettings.QuickToggle {
_init(extensionObject, icon) {
super._init({
title: _('KMonad'),
gicon: icon,
toggleMode: true,
});
this._settings = extensionObject.getSettings();
this._settings.bind(
'kmonad-running',
this,
'checked',
Gio.SettingsBindFlags.DEFAULT
);
}
}
);
const KMonadIndicator = GObject.registerClass(
class FeatureIndicator extends QuickSettings.SystemIndicator {
_init(extensionObject, icon) {
super._init();
// Create the icon for the indicator
this._indicator = this._addIndicator();
this._indicator.gicon = icon;
// Showing the indicator when the feature is enabled
this._settings = extensionObject.getSettings();
this._settings.bind(
'kmonad-running',
this._indicator,
'visible',
Gio.SettingsBindFlags.DEFAULT
);
}
}
);
export default class KMonadToggleExtension extends Extension {
enable() {
this._settings = this.getSettings();
// Watch for changes to a specific setting
this._handlerId = this._settings.connect('changed::kmonad-running', async (settings, key) => {
const isEnabled = settings.get_boolean(key);
if (isEnabled) {
this._cancellable = new Gio.Cancellable();
await this.startKmonad();
} else if (this._cancellable) {
this._cancellable.cancel();
this._cancellable = null;
}
});
this._indicator = new KMonadIndicator(this, this.getIcon());
this._indicator.quickSettingsItems.push(new KMonadToggle(this, this.getIcon()));
Main.panel.statusArea.quickSettings.addExternalIndicator(this._indicator);
this._settings.set_boolean('kmonad-running', false);
if (this._settings.get_boolean('autostart-kmonad'))
this._settings.set_boolean('kmonad-running', true);
}
disable() {
if (this._indicator) {
this._indicator.quickSettingsItems.forEach(item => item.destroy());
this._indicator.destroy();
this._indicator = null;
}
if (this._cancellable) {
this._cancellable.cancel();
this._cancellable = null;
}
if (this._handlerId) {
this._settings.disconnect(this._handlerId);
this._handlerId = null;
}
}
/**
* Starts the kmonad process
*/
async startKmonad() {
const command = GLib.shell_parse_argv(
this._settings.get_string('kmonad-command')
)[1];
try {
await execCommunicate(command, null, this._cancellable);
} catch (e) {
if (this._cancellable?.is_cancelled() === false)
Main.notifyError(_('KMonad failed'), e.message.trim());
} finally {
this._settings.set_boolean('kmonad-running', false);
}
}
getIcon() {
return Gio.icon_new_for_string(
GLib.build_filenamev([this.path, 'icons', 'kmonad-symbolic.svg'])
);
}
}
/**
* Execute a command asynchronously and return the output from `stdout` on
* success or throw an error with output from `stderr` on failure.
*
* If given, @input will be passed to `stdin` and @cancellable can be used to
* stop the process before it finishes.
*
* @param {string[]} argv - a list of string arguments
* @param {string} [input] - Input to write to `stdin` or %null to ignore
* @param {Gio.Cancellable} [cancellable] - optional cancellable object
* @returns {Promise<string>} - The process output
*/
function execCommunicate(argv, input = null, cancellable = null) {
let cancelId = 0;
let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;
if (input !== null)
flags |= Gio.SubprocessFlags.STDIN_PIPE;
let proc = new Gio.Subprocess({
argv,
flags,
});
proc.init(cancellable);
if (cancellable instanceof Gio.Cancellable)
cancelId = cancellable.connect(() => proc.force_exit());
return new Promise((resolve, reject) => {
proc.communicate_utf8_async(input, null, (proc_, res) => {
try {
let [, stdout, stderr] = proc_.communicate_utf8_finish(res);
let status = proc_.get_exit_status();
if (status !== 0) {
throw new Gio.IOErrorEnum({
code: Gio.io_error_from_errno(status),
message: stderr ? stderr.trim() : GLib.strerror(status),
});
}
resolve(stdout.trim());
} catch (e) {
reject(e);
} finally {
if (cancelId > 0)
cancellable.disconnect(cancelId);
}
});
});
}