forked from fflewddur/tophat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
112 lines (92 loc) · 3.44 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
'use strict';
// TopHat: An elegant system resource monitor for the GNOME shell
// Copyright (C) 2020 Todd Kulesza <[email protected]>
// This file is part of TopHat.
// TopHat 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.
// TopHat 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 TopHat. If not, see <https://www.gnu.org/licenses/>.
let depFailures = [];
let missingLibs = [];
const GLib = imports.gi.GLib;
const Main = imports.ui.main;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
let GTop = null;
let Cpu = null;
let Mem = null;
let Net = null;
try {
// eslint-disable-next-line no-unused-vars
GTop = imports.gi.GTop;
Cpu = Me.imports.lib.cpu;
Mem = Me.imports.lib.mem;
Net = Me.imports.lib.net;
} catch (err) {
depFailures.push(err);
missingLibs.push('GTop');
}
// Declare `tophat` in the scope of the whole script so it can
// be accessed in both `enable()` and `disable()`
let tophat = null;
class TopHat {
constructor() {
this.addTimeout = 0;
this.cpu = new Cpu.TopHatCpuIndicator();
this.mem = new Mem.TopHatMemIndicator();
this.net = new Net.TopHatNetIndicator();
}
addToPanel() {
// Wait 500 ms to allow other indicators to queue up first
this.addTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 500, () => {
Main.panel.addToStatusArea(`${Me.metadata.name} Network Indicator`, this.net);
Main.panel.addToStatusArea(`${Me.metadata.name} Memory Indicator`, this.mem);
Main.panel.addToStatusArea(`${Me.metadata.name} CPU Indicator`, this.cpu);
this.addTimeout = 0;
});
}
destroy() {
if (this.addTimeout !== 0) {
GLib.source_remove(this.addTimeout);
this.addTimeout = 0;
}
this.cpu.destroy();
this.mem.destroy();
this.net.destroy();
}
}
// eslint-disable-next-line no-unused-vars
function init() {
}
// eslint-disable-next-line no-unused-vars
function enable() {
log(`[${Me.metadata.name}] enabling version ${Me.metadata.version}`);
if (depFailures.length > 0) {
log(`[${Me.metadata.name}] missing dependencies, showing problem reporter instead`);
const Problem = Me.imports.lib.problem;
tophat = new Problem.TopHatProblemReporter();
let msg = `It looks like your computer is missing GIRepository (gir) bindings for the following libraries: ${missingLibs.join(', ')}`;
tophat.setMessage(msg);
tophat.setDetails(depFailures.join('\n'));
Main.panel.addToStatusArea(`${Me.metadata.name} Problem Reporter`, tophat);
} else {
tophat = new TopHat();
tophat.addToPanel();
}
log(`[${Me.metadata.name}] enabled`);
}
// eslint-disable-next-line no-unused-vars
function disable() {
log(`[${Me.metadata.name}] disabling version ${Me.metadata.version}`);
if (tophat !== null) {
tophat.destroy();
tophat = null;
}
log(`[${Me.metadata.name}] disabled`);
}