-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
647 lines (554 loc) · 22.3 KB
/
Copy pathextension.js
File metadata and controls
647 lines (554 loc) · 22.3 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Loadshed — Pause configured background maintenance services from GNOME Quick Settings.
// Copyright (C) 2026 yurij.de
//
// 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 3 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 <https://www.gnu.org/licenses/>.
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import St from 'gi://St';
import { PopupAnimation } from 'resource:///org/gnome/shell/ui/boxpointer.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as QuickSettings from 'resource:///org/gnome/shell/ui/quickSettings.js';
import { Extension, gettext as _, ngettext } from 'resource:///org/gnome/shell/extensions/extension.js';
import { AppTargets } from './appTargets.js';
import { GSettingsTargets } from './gsettingsTargets.js';
import { FileTargets } from './fileTargets.js';
import { summarizeStatus } from './statusSummary.js';
const HELPER_INSTALL_PATH = '/usr/local/bin/loadshed-helper';
const DEFAULT_REFRESH_INTERVAL = 10;
// Fixed slot in the 2-column Quick Settings grid, right column of the row
// below hotspot@yurij.de (which pins itself to 4) and beside epp-modes
// (which pins itself to 6, the left column of that same row). Self-contained
// on purpose: an earlier attempt to have epp-modes reach into loadshed at
// runtime via Extension.lookupByUUID() and reposition it from the outside
// was unreliable, most likely because extension enable() order across
// independently-loaded extensions isn't guaranteed, so the lookup could run
// before loadshed had even added its own toggle to the grid.
const TARGET_POSITION = 7;
function formatCountLabel(label, count) {
return label.replace('%d', String(count));
}
class LoadshedManager {
constructor(settings) {
this._targets = new GSettingsTargets(settings);
this._files = new FileTargets(settings);
this._apps = new AppTargets(settings);
}
async reloadTargets(pauseActive) {
this._targets.reload(pauseActive);
this._files.reload(pauseActive);
await this._apps.reload(pauseActive);
}
async applyPause() {
this._targets.applyPause();
this._files.applyPause();
await this._apps.applyPause();
}
applyResume() {
// AppTargets.applyResume() only fires GLib.spawn_async() (already
// non-blocking) and never awaits a subprocess result, so it stays
// synchronous here too.
this._targets.applyResume();
this._files.applyResume();
this._apps.applyResume();
}
async enforceTargets() {
this._targets.enforce();
this._files.enforce();
await this._apps.enforce();
}
async targetsStatus() {
const appEntries = await this._apps.status();
return this._targets.status().concat(this._files.status(), appEntries);
}
hideOwnToggles() {
this._targets.hideOwnToggles();
}
restoreOwnToggles() {
this._targets.restoreOwnToggles();
}
_helperCommand(action) {
const sudoBin = GLib.find_program_in_path('sudo');
if (!sudoBin) {
throw new Error('sudo was not found');
}
if (!GLib.file_test(HELPER_INSTALL_PATH, GLib.FileTest.IS_EXECUTABLE)) {
throw new Error('setup required');
}
return [sudoBin, '-n', HELPER_INSTALL_PATH, action];
}
run(action) {
let argv;
try {
argv = this._helperCommand(action);
} catch (error) {
return Promise.reject(error);
}
return new Promise((resolve, reject) => {
try {
const proc = new Gio.Subprocess({
argv,
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
});
proc.init(null);
proc.communicate_utf8_async(null, null, (source, res) => {
try {
const [, stdout, stderr] = source.communicate_utf8_finish(res);
let payload = null;
if (stdout?.trim()) {
payload = JSON.parse(stdout.trim());
}
if (source.get_successful() && payload) {
resolve(payload);
return;
}
const message = payload?.error || stderr?.trim() || stdout?.trim() || _('Helper command failed');
const error = new Error(message);
error.payload = payload;
reject(error);
} catch (error) {
reject(error);
}
});
} catch (error) {
reject(error);
}
});
}
resumeDetached() {
let argv;
try {
argv = this._helperCommand('resume');
} catch {
return;
}
try {
GLib.spawn_async(
null,
argv,
null,
GLib.SpawnFlags.SEARCH_PATH,
null
);
} catch (error) {
logError(error, 'Loadshed: failed to resume on disable');
}
}
}
const LoadshedToggle = GObject.registerClass(
class LoadshedToggle extends QuickSettings.QuickMenuToggle {
_init(extensionObject, manager, indicator) {
super._init({
title: _('Loadshed'),
subtitle: _('Loading'),
iconName: 'media-playback-pause-symbolic',
toggleMode: true,
});
this._settings = extensionObject._settings;
this._extensionObject = extensionObject;
this._manager = manager;
this._indicator = indicator;
this._busy = false;
this._entryItems = [];
this._refreshSourceId = 0;
this._settingsSignalIds = [];
this.menu.setHeader('media-playback-pause-symbolic', _('Loadshed'), _('Background services'));
this._itemsSection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._itemsSection);
this.menu.addAction(_('Refresh'), () => this._refresh());
this.menu.addAction(_('Recover frozen targets'), () => this._runAction('recover'));
this.menu.addAction(_('Settings'), () => this._openPreferences());
this.menu.addAction(_('Setup help'), () => this._notifySetupRequired());
this._clickedId = this.connect('clicked', () => this._togglePaused());
this._settingsSignalIds.push(this._settings.connect('changed::refresh-interval', () => {
this._restartRefreshTimer();
}));
this._settingsSignalIds.push(this._settings.connect('changed::show-quick-settings', () => {
this.visible = this._settings.get_boolean('show-quick-settings');
}));
this.visible = this._settings.get_boolean('show-quick-settings');
this._restartRefreshTimer();
this._refresh();
}
destroy() {
if (this._refreshSourceId) {
GLib.Source.remove(this._refreshSourceId);
this._refreshSourceId = 0;
}
if (this._clickedId) {
this.disconnect(this._clickedId);
this._clickedId = 0;
}
this._settingsSignalIds.forEach(id => this._settings.disconnect(id));
this._settingsSignalIds = [];
this._clearEntryItems();
super.destroy();
}
_restartRefreshTimer() {
if (this._refreshSourceId) {
GLib.Source.remove(this._refreshSourceId);
this._refreshSourceId = 0;
}
const interval = Math.max(
2,
this._settings.get_int('refresh-interval') || DEFAULT_REFRESH_INTERVAL
);
this._refreshSourceId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, interval, () => {
this._refresh();
return GLib.SOURCE_CONTINUE;
});
}
_togglePaused() {
if (this._busy) {
return;
}
this._runAction(this.checked ? 'pause' : 'resume');
}
_openPreferences() {
this._extensionObject.openPreferences();
Main.panel.statusArea.quickSettings.menu.close(PopupAnimation.FADE);
}
_refresh() {
if (this._busy) {
return;
}
const action = this.checked ? 'enforce' : 'status';
// The helper is authoritative for the persistent pause intent. Do
// not change target state before its response tells us whether the
// pause is still active; _applyStatus() reconciles targets after that
// response is known.
const preAction = Promise.resolve();
this._busy = true;
this.subtitle = _('Refreshing');
preAction
.then(() => this._manager.run(action))
.then(status => this._applyStatus(status))
.catch(error => this._handleError(error))
.finally(() => {
this._busy = false;
});
}
_runAction(action) {
if (this._busy) {
return;
}
// Let the root helper perform the transition first. _applyStatus()
// then applies or releases the user-session targets according to the
// confirmed pause intent, preserving the gate during resume.
const preAction = Promise.resolve();
this._busy = true;
this.subtitle = action === 'pause'
? _('Pausing')
: action === 'recover' ? _('Recovering') : _('Resuming');
preAction
.then(() => this._manager.run(action))
.then(status => this._applyStatus(status))
.catch(error => this._handleError(error))
.finally(() => {
this._busy = false;
});
}
_handleError(error) {
if (error?.payload) {
this._applyStatus(error.payload);
} else {
// No usable helper response (e.g. helper not set up yet). Fall
// through the normal status path with no helper entries — any
// GSettings targets are still managed directly by us and keep
// showing their real status instead of the menu going blank.
this._applyStatus({});
}
const message = error?.message || _('Unknown error');
if (message === 'setup required') {
this.subtitle = _('Setup required');
this._notifySetupRequired();
return;
}
this.subtitle = _('Error');
Main.notify(_('Loadshed'), message);
}
_notifySetupRequired() {
Main.notify(
_('Loadshed setup required'),
_('Run install.sh from the extension directory, then reload GNOME Shell or re-enable the extension.')
);
}
async _applyStatus(status) {
const helperStatus = status && typeof status === 'object' ? status : {};
const helperEntries = Array.isArray(helperStatus.entries) ? helperStatus.entries : [];
// A normal helper response explicitly tells us whether the root
// pause state is known. Keep the target snapshots while the root
// intent is active (this also repairs targets after an extension
// reload), and clear stale snapshots only after a known resume.
if (helperStatus.pause_state_known === true) {
if (helperStatus.pause_intent === true) {
await this._manager.applyPause();
} else {
this._manager.applyResume();
}
}
const targetEntries = await this._manager.targetsStatus();
const entries = helperEntries.concat(targetEntries);
const summary = summarizeStatus(helperStatus, targetEntries);
this.checked = summary.strictPaused;
this._indicator.visible = summary.strictPaused;
if (entries.length === 0) {
this.subtitle = _('No services configured');
} else if (summary.pausedCount > 0) {
this.subtitle = formatCountLabel(
ngettext('1 target paused', '%d targets paused', summary.pausedCount),
summary.pausedCount
);
} else if (summary.runningCount > 0) {
this.subtitle = formatCountLabel(
ngettext('1 target running', '%d targets running', summary.runningCount),
summary.runningCount
);
} else if (summary.protectedCount > 0) {
this.subtitle = formatCountLabel(
ngettext('1 target protected', '%d targets protected', summary.protectedCount),
summary.protectedCount
);
} else if (helperStatus.recovery_required || Number(helperStatus.external_frozen_count) > 0) {
this.subtitle = _('Recovery required');
} else {
this.subtitle = _('Nothing running');
}
if (Array.isArray(helperStatus.errors) && helperStatus.errors.length > 0) {
this.subtitle = _('Partial error');
Main.notify(_('Loadshed'), helperStatus.errors.join('\n'));
}
this._rebuildEntryItems(entries);
}
_clearEntryItems() {
this._entryItems.forEach(item => item.destroy());
this._entryItems = [];
}
_rebuildEntryItems(entries) {
this._clearEntryItems();
const visibleEntries = [];
const hiddenEntries = [];
entries.forEach(entry => {
if (this._entryVisible(entry)) {
visibleEntries.push(entry);
} else {
hiddenEntries.push(entry);
}
});
visibleEntries.forEach(entry => {
this._addEntryItem(entry.label || entry.id || _('Unknown'), this._entryIcon(entry));
});
if (hiddenEntries.length > 0) {
this._addEntryItem(
this._hiddenEntriesLabel(hiddenEntries),
this._hiddenEntriesIcon(hiddenEntries),
'loadshed-summary-entry'
);
}
}
_addEntryItem(label, iconName, extraStyleClass = '') {
const item = new PopupMenu.PopupBaseMenuItem({
reactive: false,
can_focus: false,
style_class: `popup-menu-item loadshed-entry ${extraStyleClass}`,
});
const box = new St.BoxLayout({
vertical: false,
x_expand: true,
style_class: 'popup-menu-item-content loadshed-entry-content',
});
const icon = new St.Icon({
icon_name: iconName,
style_class: 'popup-menu-icon loadshed-entry-icon',
});
const title = new St.Label({
text: label,
x_expand: true,
x_align: Clutter.ActorAlign.START,
y_align: Clutter.ActorAlign.CENTER,
style_class: 'loadshed-entry-label',
});
box.add_child(icon);
box.add_child(title);
item.actor.add_child(box);
this._itemsSection.addMenuItem(item);
this._entryItems.push(item);
}
_entryVisible(entry) {
return Boolean(
entry.error ||
entry.external_frozen ||
entry.protected ||
(entry.service_active && !entry.paused)
);
}
_entryPaused(entry) {
return Boolean(entry.paused);
}
_hiddenEntriesLabel(entries) {
const pausedCount = entries.filter(entry => this._entryPaused(entry)).length;
const releasedCount = entries.length - pausedCount;
if (releasedCount === 0) {
return formatCountLabel(ngettext('1 service paused', '%d services paused', pausedCount), pausedCount);
}
if (pausedCount === 0) {
return formatCountLabel(ngettext('1 service released', '%d services released', releasedCount), releasedCount);
}
const pausedLabel = formatCountLabel(ngettext('1 paused', '%d paused', pausedCount), pausedCount);
const releasedLabel = formatCountLabel(ngettext('1 released', '%d released', releasedCount), releasedCount);
return _('%s, %s').format(pausedLabel, releasedLabel);
}
_hiddenEntriesIcon(entries) {
const pausedCount = entries.filter(entry => this._entryPaused(entry)).length;
if (pausedCount === entries.length) {
return 'media-playback-pause-symbolic';
}
if (pausedCount === 0) {
return 'emblem-ok-symbolic';
}
return 'dialog-information-symbolic';
}
_entryIcon(entry) {
if (entry.error) {
return 'dialog-warning-symbolic';
}
if (entry.paused) {
return 'media-playback-pause-symbolic';
}
if (entry.protected) {
return 'changes-prevent-symbolic';
}
if (entry.external_frozen) {
return 'dialog-warning-symbolic';
}
if (entry.service_active) {
return 'media-playback-start-symbolic';
}
return 'emblem-ok-symbolic';
}
});
const LoadshedIndicator = GObject.registerClass(
class LoadshedIndicator extends QuickSettings.SystemIndicator {
_init(extensionObject, manager) {
super._init();
this._indicator = this._addIndicator();
this._indicator.icon_name = 'media-playback-pause-symbolic';
this._indicator.visible = false;
this._toggle = new LoadshedToggle(extensionObject, manager, this._indicator);
this.quickSettingsItems.push(this._toggle);
Main.panel.statusArea.quickSettings.addExternalIndicator(this);
// Delay until after the mainloop turn in which addExternalIndicator
// ran, so every extension's toggle is already in the grid — same
// one-shot technique as hotspot@yurij.de's own moveToDefaultPosition().
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => this._reorderToggle());
}
get paused() {
return Boolean(this._toggle?.checked);
}
_reorderToggle() {
const grid = Main.panel?.statusArea?.quickSettings?.menu?._grid;
if (!grid?.get_children)
return GLib.SOURCE_REMOVE;
const toggles = grid.get_children().filter(child =>
child instanceof QuickSettings.QuickToggle ||
child instanceof QuickSettings.QuickMenuToggle);
const currentIndex = toggles.indexOf(this._toggle);
if (currentIndex === -1)
return GLib.SOURCE_REMOVE;
const desired = Math.min(Math.max(0, TARGET_POSITION), toggles.length - 1);
if (desired !== currentIndex) {
toggles.splice(currentIndex, 1);
toggles.splice(desired, 0, this._toggle);
let last = null;
for (const item of toggles) {
grid.set_child_above_sibling(item, last);
last = item;
}
}
return GLib.SOURCE_REMOVE;
}
destroy() {
this.quickSettingsItems.forEach(item => item.destroy());
super.destroy();
}
});
export default class LoadshedExtension extends Extension {
constructor(metadata) {
super(metadata);
this._settings = null;
this._manager = null;
this._indicator = null;
this._targetsSignalId = 0;
this._fileTargetsSignalId = 0;
this._appTargetsSignalId = 0;
}
enable() {
this._settings = this.getSettings();
this._manager = new LoadshedManager(this._settings);
// Loadshed takes over pausing for enabled GSettings targets,
// so their own Quick Settings toggle would be redundant while we
// manage it. (Folder Size no longer has a GSettings-backed toggle
// to hide - it's managed via file-targets instead, see B3/B4.)
this._manager.hideOwnToggles();
// GObject signal callbacks can't be awaited, so these fire the
// (now async) reload and just log if it ever rejects.
this._targetsSignalId = this._settings.connect('changed::gsettings-targets', () => {
const pauseActive = Boolean(this._indicator?.paused);
this._manager.reloadTargets(pauseActive)
.catch(error => logError(error, 'Loadshed: failed to reload GSettings targets'));
});
this._fileTargetsSignalId = this._settings.connect('changed::file-targets', () => {
const pauseActive = Boolean(this._indicator?.paused);
this._manager.reloadTargets(pauseActive)
.catch(error => logError(error, 'Loadshed: failed to reload file targets'));
});
this._appTargetsSignalId = this._settings.connect('changed::app-targets', () => {
const pauseActive = Boolean(this._indicator?.paused);
this._manager.reloadTargets(pauseActive)
.catch(error => logError(error, 'Loadshed: failed to reload app targets'));
});
this._indicator = new LoadshedIndicator(this, this._manager);
}
disable() {
if (this._settings && this._targetsSignalId) {
this._settings.disconnect(this._targetsSignalId);
}
this._targetsSignalId = 0;
if (this._settings && this._fileTargetsSignalId) {
this._settings.disconnect(this._fileTargetsSignalId);
}
this._fileTargetsSignalId = 0;
if (this._settings && this._appTargetsSignalId) {
this._settings.disconnect(this._appTargetsSignalId);
}
this._appTargetsSignalId = 0;
if (this._settings?.get_boolean('auto-resume-on-disable')) {
this._manager?.resumeDetached();
this._manager?.applyResume();
}
// Give back any own toggles we hid so the user never ends up
// without a way to control the target once we stop managing it.
this._manager?.restoreOwnToggles();
if (this._indicator) {
this._indicator.destroy();
this._indicator = null;
}
this._manager = null;
this._settings = null;
}
}