File: extension.js

package info (click to toggle)
gnome-shell-extension-gamemode 4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 296 kB
  • sloc: sh: 63; python: 43; makefile: 34; xml: 25
file content (292 lines) | stat: -rw-r--r-- 9,123 bytes parent folder | download
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
/*
 * Copyright © 2019 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

const Clutter = imports.gi.Clutter;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gdk = imports.gi.Gdk;
const GObject = imports.gi.GObject;
const Lang = imports.lang;
const Main = imports.ui.main;
const MessageTray = imports.ui.messageTray;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Signals = imports.signals;
const St = imports.gi.St;
const Shell = imports.gi.Shell;

const ExtensionUtils = imports.misc.extensionUtils;
const Extension = imports.misc.extensionUtils.getCurrentExtension();

const Gettext = imports.gettext.domain(Extension.metadata['gettext-domain']);
const _ = Gettext.gettext;

const GameMode = Extension.imports.client;

/* ui */
function getStatusText(is_on) {
    if (is_on) {
        return _("GameMode is active");
    }

    return _("GameMode is off");
}

var StatusMenuItem = GObject.registerClass(
    class StatusMenuItem extends PopupMenu.PopupBaseMenuItem {
        _init(client) {
            super._init();
            this._client = client;

            this._label = new St.Label({text: ('<status>'), x_expand: true});
            this.add_child(this._label);

            this._changedId = client.connect('state-changed',
                                             this._onStateChanged.bind(this));
            this._onStateChanged(this._client, this._client.clientCount > 0);
        }

        destroy() {
            if (this._changedId) {
                this._client.disconnect(this._changedId);
                this._changedId = 0;
            }

            super.destroy();
        }

        _onStateChanged(cli, is_on) {
            this._label.text = getStatusText(is_on);
        }
    });

var ClientCountMenuItem = GObject.registerClass(
    class ClientCountMenuItem extends PopupMenu.PopupBaseMenuItem {
        _init(client) {
            super._init();
            this._client = client;

            this._status = new St.Label({text: '<client count>', x_expand: true});
            this.add_child(this._status);

            this._changedId = client.connect('count-changed',
                                             this._onCountChanged.bind(this));
            this._onCountChanged(this._client, this._client.clientCount);
        }

        destroy() {
            if (this._changedId) {
                this._client.disconnect(this._changedId);
                this._changedId = 0;
            }

            super.destroy();
        }

        _onCountChanged(cli, count) {
            if (count === 0) {
                this._status.text = _("No active clients");
            } else {
                this._status.text = Gettext.ngettext("%d active client",
                                                     "%d active clients",
                                                     count).format(count);
            }
        }
    });

/* main button */
var GameModeIndicator = GObject.registerClass(
    class GameModeIndicator extends PanelMenu.Button {

        _init() {
            super._init(0.0, 'GameMode');
            this._settings = ExtensionUtils.getSettings();

            this.connect('destroy', this._onDestroy.bind(this));

            let box = new St.BoxLayout({style_class: 'panel-status-menu-box'});

            let icon = new St.Icon({
                icon_name: 'applications-games-symbolic',
                style_class: 'system-status-icon'
            });

            this._icon = icon;
            box.add_child(icon);
            this.add_child(box);

            this._signals = [];

            /* react to settings changes */
            this._connect(this._settings,
                          'changed::always-show-icon',
                          this._sync.bind(this));

            this._connect(this._settings,
                          'changed::active-tint',
                          this._sync.bind(this));

            this._connect(this._settings,
                          'changed::active-color',
                          this._update_active_color.bind(this));

            /* connect to GameMode */
            this._client = new GameMode.Client(null);
            this._client.connect('state-changed', this._onStateChanged.bind(this));

            /* react to session changes */
            Main.sessionMode.connect('updated', this._sync.bind(this));

            this._source = null; /* for the notification */

            /* update the icon */
            this._update_active_color(); /* calls this._sync() */

            /* the menu */
            this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
            this.menu.addMenuItem(new StatusMenuItem(this._client));
            this.menu.addMenuItem(new ClientCountMenuItem(this._client));

            log('GameMode extension initialized');
        }

        _connect(obj, signal, handler) {
            let id = obj.connect(signal, handler);
            this._signals.push([obj, id]);
        }

        _onDestroy() {
            log('Destroying GameMode extension');

            for (var i = 0; i < this._signals.length; i++) {
                let [obj, id] = this._signals[i];
                obj.disconnect(id);
            }
            this._signals = [];

            this._client.close();
        }

        _ensureSource() {
            if (!this._source) {
                this._source = new MessageTray.Source('GameMode',
                                                      'application-games-symbolic');
                this._source.connect('destroy', () => {
                    this._source = null;
                });

                Main.messageTray.add(this._source);
            }

            return this._source;
        }

        _notify(title, body) {
            if (this._notification)
                this._notification.destroy();

            let source = this._ensureSource();

            this._notification = new MessageTray.Notification(source, title, body);
            this._notification.setUrgency(MessageTray.Urgency.HIGH);
            this._notification.connect('destroy', () => {
                this._notification = null;
            });
            this._source.notify(this._notification);
        }

        /* Update the icon according to the current state */
        _sync() {
            let active = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;

            let on = this._client.clientCount > 0;
            let alwaysShow = this._settings.get_boolean('always-show-icon');
            let tintIcon = this._settings.get_boolean('active-tint');

            if (this._icon.has_effects()) {
                this._icon.remove_effect_by_name('color');
            }

            if (on && tintIcon) {
                this._icon.add_effect_with_name('color', this._color_effect);
            }

            this.visible = active && (alwaysShow || on);
        }

        _update_active_color() {
            let str = this._settings.get_string('active-color');
            let rgba = new Gdk.RGBA();
            rgba.parse(str);

            let color = new Clutter.Color({
                red: rgba.red * 255,
                green: rgba.green * 255,
                blue: rgba.blue * 255,
                alpha: rgba.alpha * 255
            });
            this._color_effect = new Clutter.ColorizeEffect({tint: color});

            /* sync the changes */
            this._sync();
        }

        /* GameMode.Client callbacks */
        _onStateChanged(cli, is_on) {
            /* update the icon */
            this._sync();

            if (this._settings.get_boolean('emit-notifications')) {
                let status = getStatusText(is_on);
                if (is_on)
                    this._notify(status, _("Computer performance is now optimized for playing games"));
                else
                    this._notify(status, _("Computer performance is reset for normal usage"));
            }

            this._sync();
        }

    });

/* entry points */

let indicator = null;

function init() {
    ExtensionUtils.initTranslations();
}


function enable() {
    if (indicator)
        return;

    indicator = new GameModeIndicator();
    Main.panel.addToStatusArea('GameMode', indicator);
}

function disable() {
    if (!indicator)
        return;

    indicator.destroy();
    indicator = null;
}