-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
255 lines (223 loc) · 8.47 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
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
import GObject from "gi://GObject";
import Gio from "gi://Gio";
import St from "gi://St";
import {
Extension,
gettext as _,
} 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";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as GameMode from "./client.js";
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init(extension, settings) {
super._init(0.0, _("GameMode Status Indicator"));
this._extension = extension;
this._settings = settings;
this._client = null;
this._addIcon();
this._createMenuItems();
this._initializeClient();
this._observeSettings();
}
_addIcon() {
this._icon = new St.Icon({
icon_name: "applications-games-symbolic",
style_class: "system-status-icon",
});
this.add_child(this._icon);
const inactiveColor = this._settings.get_string("inactive-color");
this._icon.set_style('color: ' + inactiveColor + ';');
if (this._settings.get_boolean("show-icon-only-when-active")) {
this.visible = false;
}
}
_createMenuItems() {
this._statusItem = new PopupMenu.PopupMenuItem(_("GameMode is Off"));
this._statusItem.actor.reactive = false;
this.menu.addMenuItem(this._statusItem, 0);
this._statusItem.actor.add_style_class_name("status-item");
this._clientSection = new PopupMenu.PopupSubMenuMenuItem(
_("Active Clients: 0")
);
this.menu.addMenuItem(this._clientSection);
this._addNotificationSettings();
this._addVisibilitySettings();
this._addPreferencesButton();
}
_initializeClient() {
try {
this._client = new GameMode.Client(null);
this._client.connect(
"count-changed",
this._updateClientList.bind(this)
);
this._client.connect(
"game-registered",
this._updateClientList.bind(this)
);
this._client.connect(
"game-unregistered",
this._updateClientList.bind(this)
);
this._client.connect(
"state-changed",
this._handleStatusChange.bind(this)
);
} catch (e) {
this._handleClientInitializationError();
}
if (!this._client) this._handleClientInitializationError();
}
_addNotificationSettings() {
const notificationSection = new PopupMenu.PopupSubMenuMenuItem(
_("Show Notification when:")
);
this.menu.addMenuItem(notificationSection);
this._notificationLaunchToggle = new PopupMenu.PopupSwitchMenuItem(
_("GameMode is Enabled"),
this._settings.get_boolean("show-launch-notification")
);
this._notificationLaunchToggle.connect("toggled", (item, value) => {
this._settings.set_boolean("show-launch-notification", value);
});
notificationSection.menu.addMenuItem(this._notificationLaunchToggle);
this._notificationCloseToggle = new PopupMenu.PopupSwitchMenuItem(
_("GameMode is Disabled"),
this._settings.get_boolean("show-close-notification")
);
this._notificationCloseToggle.connect("toggled", (item, value) => {
this._settings.set_boolean("show-close-notification", value);
});
notificationSection.menu.addMenuItem(this._notificationCloseToggle);
}
_addVisibilitySettings() {
const visibilitySection = new PopupMenu.PopupSubMenuMenuItem(
_("Visibility Settings")
);
this.menu.addMenuItem(visibilitySection);
this._iconVisibilityToggle = new PopupMenu.PopupSwitchMenuItem(
_("Show Icon Only When Active"),
this._settings.get_boolean("show-icon-only-when-active")
);
this._iconVisibilityToggle.connect("toggled", (item, value) => {
this._settings.set_boolean("show-icon-only-when-active", value);
});
visibilitySection.menu.addMenuItem(this._iconVisibilityToggle);
const colorSettingsItem = new PopupMenu.PopupMenuItem(_("Color Settings"));
this.menu.addMenuItem(colorSettingsItem);
colorSettingsItem.connect('activate', () => {
this._extension.openPreferences();
});
}
_addPreferencesButton() {
this.menu.addAction(_("Settings Menu"), () => {
this._extension.openPreferences();
});
}
_handleClientInitializationError() {
this._client = null;
const unavailableItem = new PopupMenu.PopupMenuItem(
_("GameMode is Not Available! Refer to GitHub to Install.")
);
unavailableItem.actor.add_style_class_name("unavailable-text");
this.menu.addMenuItem(unavailableItem, 0);
this.menu._getMenuItems().forEach((item) => {
if (item !== unavailableItem) item.setSensitive(false);
});
unavailableItem.connect("activate", () => {
Gio.app_info_launch_default_for_uri(
"https://github.com/FeralInteractive/gamemode",
null
);
});
}
_handleStatusChange() {
const isActive = this._client.current_state;
if (isActive) {
this._statusItem.label.set_text(_("GameMode is On"));
if (this._settings.get_boolean("show-launch-notification")) {
Main.notify(_("GameMode Status"), _("GameMode is Enabled!"));
}
} else {
this._statusItem.label.set_text(_("GameMode is Off"));
if (this._settings.get_boolean("show-close-notification")) {
Main.notify(_("GameMode Status"), _("GameMode is Disabled!"));
}
}
this._updateIcon(isActive);
}
_observeSettings() {
this._settings.connect("changed::show-icon-only-when-active", () => {
const iconVisibilitySetting = this._settings.get_boolean(
"show-icon-only-when-active"
);
this._iconVisibilityToggle.setToggleState(iconVisibilitySetting);
this.visible = !iconVisibilitySetting;
});
this._settings.connect("changed::show-launch-notification", () => {
const showLaunchNotification = this._settings.get_boolean(
"show-launch-notification"
);
this._notificationLaunchToggle.setToggleState(showLaunchNotification);
});
this._settings.connect("changed::show-close-notification", () => {
const showCloseNotification = this._settings.get_boolean(
"show-close-notification"
);
this._notificationCloseToggle.setToggleState(showCloseNotification);
});
this._settings.connect("changed::active-color", () => {
if (this._client && this._client.current_state) {
const activeColor = this._settings.get_string("active-color");
this._icon.set_style('color: ' + activeColor + ';');
}
});
this._settings.connect("changed::inactive-color", () => {
if (this._client && !this._client.current_state) {
const inactiveColor = this._settings.get_string("inactive-color");
this._icon.set_style('color: ' + inactiveColor + ';');
}
});
}
_updateIcon(isActive) {
const activeColor = this._settings.get_string("active-color");
const inactiveColor = this._settings.get_string("inactive-color");
this._icon.set_style('color: ' + (isActive ? activeColor : inactiveColor) + ';');
const showIconOnlyWhenActive = this._settings.get_boolean(
"show-icon-only-when-active"
);
this.visible = !showIconOnlyWhenActive || isActive;
}
_updateClientList() {
this._clientSection.menu.removeAll();
if (!this._client || !this._client.process_map) {
this._clientSection.label.set_text(_("Active Clients: 0"));
return;
}
const clientCount = this._client.process_map.size;
this._clientSection.label.set_text(
_("Active Clients: " + (clientCount || 0))
);
this._client.process_map.forEach((client, index) => {
const clientItem = new PopupMenu.PopupMenuItem(
`Client ${index}: ${client.name}`
);
this._clientSection.menu.addMenuItem(clientItem);
});
}
}
);
export default class GameModeShellExtension extends Extension {
enable() {
this._settings = this.getSettings();
this._indicator = new Indicator(this, this._settings);
Main.panel.addToStatusArea(this.uuid, this._indicator);
}
disable() {
this._indicator.destroy();
this._indicator = null;
this._settings = null;
}
}