-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.js
More file actions
168 lines (156 loc) · 5.74 KB
/
extension.js
File metadata and controls
168 lines (156 loc) · 5.74 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
"use strict";
import St from "gi://St";
import Clutter from "gi://Clutter";
import {
Extension,
gettext as _,
} from "resource:///org/gnome/shell/extensions/extension.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import { ServerStatusPanel } from "./serverStatusPanel.js";
import { Status } from "./status.js";
import { IconProvider } from "./iconProvider.js";
import { Indicator } from "./indicator.js";
import { SettingsParser } from "./settingsParser.js";
/**
* The main extension class. Creates an `Indicator` and keeps
* it updated based on status of specified servers settings.
*/
export default class ServerStatusIndicatorExtension extends Extension {
enable() {
this.iconProvider = new IconProvider(this.path + "/assets/");
this.indicator = new Indicator(
_(this.metadata.name),
this.iconProvider,
);
Main.panel.addToStatusArea(this.uuid, this.indicator);
// create a box to hold server panels
this.serversBox = new St.BoxLayout({
orientation: Clutter.Orientation.VERTICAL,
});
this.indicator.menu.box.add_child(this.serversBox);
// get settings stored in gsettings
this.rawSettings = this.getSettings();
this.savedSettings = SettingsParser.parseGioSettings(this.rawSettings);
// panel items, one per server setting
for (const savedSetting of this.savedSettings) {
if (savedSetting.visible) {
const panel = new ServerStatusPanel(
savedSetting,
() => this.updateIcon(),
this.iconProvider,
);
this.serversBox.add_child(panel);
this.indicator.addStatusPanel(panel);
}
}
// Open Prefs button
this.prefsButton = new St.Button({
icon_name: "preferences-system-symbolic",
style_class: "prefs-button padded",
track_hover: true,
reactive: true,
accessible_name: "Preferences",
});
this.prefsButtonId = this.prefsButton.connect("clicked", () => {
this.indicator.menu.close();
this.openPreferences();
});
this.indicator.menu.box.add_child(this.prefsButton);
// listen for changes to server settings in gsettings and update display
this.extensionListenerId = this.rawSettings.connect("changed", () => {
this.onPrefChanged();
});
}
/**
* Destroys and nulls artifacts for garbage collection.
*/
disable() {
// disconnect listeners for click events
if (this.prefsButton && this.prefsButtonId) {
this.prefsButton.disconnect(this.prefsButtonId);
this.prefsButton.destroy();
this.prefsButton = null;
this.prefsButtonId = null;
}
// disconnect listener for pref changes
if (this.rawSettings && this.extensionListenerId) {
this.rawSettings.disconnect(this.extensionListenerId);
this.extensionListenerId = null;
}
// clean up status panels through indicator
if (this.indicator) {
this.indicator.clearStatusPanels();
}
// clean up the serversBox
if (this.serversBox) {
this.serversBox.destroy();
this.serversBox = null;
}
// clean up the indicator
if (this.indicator) {
this.indicator.destroy();
this.indicator = null;
}
// destroy icon provider and its icons
if (this.iconProvider) {
this.iconProvider.destroy();
this.iconProvider = null;
}
// clean up other stuff
this.savedSettings.length = 0; // dereference elements
this.savedSettings = null;
this.rawSettings.length = 0; // dereference elements
this.rawSettings = null;
}
/**
* Preferences have changed the set of server settings so we
* need to update the indicator icon and menu server panels.
*/
onPrefChanged() {
this.indicator.updatePanelIcon(Status.Init);
// clear servers' box and repopulate
this.indicator.clearStatusPanels();
this.serversBox.destroy_all_children();
this.savedSettings = SettingsParser.parseGioSettings(this.rawSettings);
// recreate panel items, one per server setting
for (const savedSetting of this.savedSettings) {
if (savedSetting.visible) {
const panel = new ServerStatusPanel(
savedSetting,
() => this.updateIcon(),
this.iconProvider,
);
this.serversBox.add_child(panel);
this.indicator.addStatusPanel(panel);
}
}
this.updateIcon();
}
/**
* Update the indicator icon based on changes in server settings.
*/
updateIcon() {
if (!this.indicator) {
return;
}
const statusList = [];
const panels = this.indicator.getStatusPanels();
for (const panel of panels) {
const status = panel.getStatus();
statusList.push(status);
}
// determine worst status, check worst to best statuses
let worstStatus;
if (statusList.includes(Status.Down)) {
worstStatus = Status.Down;
} else if (statusList.includes(Status.Bad)) {
worstStatus = Status.Bad;
} else if (statusList.includes(Status.Init)) {
worstStatus = Status.Init;
} else if (statusList.includes(Status.Up)) {
worstStatus = Status.Up;
}
// update the panel icon
this.indicator.updatePanelIcon(worstStatus);
}
}