-
Notifications
You must be signed in to change notification settings - Fork 4
/
extension.js
154 lines (144 loc) · 5.07 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
/* extension.js
*
* 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 2 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 <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
"use strict";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as Config from "resource:///org/gnome/shell/misc/config.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import Clutter from "gi://Clutter";
import GObject from "gi://GObject";
import St from "gi://St";
import GLib from "gi://GLib";
let simpleMessage = null; // Variable to hold our extension
export default class SimpleMessageExtension extends Extension {
/**
* This class is constructed once when your extension is loaded, not
* enabled. This is a good time to setup translations or anything else you
* only do once.
*
* You MUST NOT make any changes to GNOME Shell, connect any signals or add
* any event sources here.
*
* @param {ExtensionMeta} metadata - An extension meta object
*/
constructor(metadata) {
super(metadata);
}
/**
* This function is called when your extension is enabled, which could be
* done in GNOME Extensions, when you log in or when the screen is unlocked.
*
* This is when you should setup any UI for your extension, change existing
* widgets, connect signals or modify GNOME Shell's behavior.
*/
enable() {
simpleMessage = new SimpleMessage(this);
}
/**
* This function is called when your extension is uninstalled, disabled in
* GNOME Extensions or when the screen locks.
*
* Anything you created, modified or setup in enable() MUST be undone here.
* Not doing so is the most common reason extensions are rejected in review!
*/
disable() {
simpleMessage.destroy();
simpleMessage = null;
}
}
// Create the extension object
let SimpleMessage = GObject.registerClass(
{ GTypeName: "SimpleMessage" },
class SimpleMessage extends PanelMenu.Button {
_init(extensionObject) {
super._init(/*St.Align.START*/);
const [major] = Config.PACKAGE_VERSION.split(".");
const shellVersion = Number.parseInt(major);
this._settings = extensionObject.getSettings();
this.message = this._settings.get_string("message");
this.command = this._settings.get_string("command");
this.message_alignment = this._settings.get_int("panel-alignment");
this.message_position = this._settings.get_int("panel-position");
// Create the object that holds and displays the message
this.messageBox = new St.Label({
name: "simple-message-message",
y_align: Clutter.ActorAlign.CENTER,
y_expand: true,
});
if (shellVersion >= 46) {
this.actor.add_child(this.messageBox);
} else {
this.add_actor(this.messageBox);
}
// Add message text
this.messageBox.set_text(this.message);
// Add message to panel
Main.panel.addToStatusArea(
"simpleMessage",
this,
this.message_position,
this.message_alignment,
);
// Connect change in values settings to functions to update the message and position
this._settings.connect(
"changed::message",
this._rewriteMessage.bind(this),
);
this._settings.connect(
"changed::command",
this._rewriteCommand.bind(this),
);
this._settings.connect(
"changed::panel-alignment",
this._moveMessage.bind(this),
);
this._settings.connect(
"changed::panel-position",
this._moveMessage.bind(this),
);
this.connect("button-press-event", () => {
if (this.command) {
GLib.spawn_command_line_async(this.command);
}
});
}
_rewriteMessage() {
this.message = this._settings.get_string("message");
this.messageBox.set_text(this.message);
}
_moveMessage() {
this.get_parent().remove_actor(this); // Remove from the old location
this.message_alignment = this._settings.get_int("panel-alignment");
this.message_position = this._settings.get_int("panel-position");
// Allows easily addressable boxes
let boxes = {
0: Main.panel._leftBox,
1: Main.panel._centerBox,
2: Main.panel._rightBox,
};
// Insert at new location
boxes[this.message_alignment].insert_child_at_index(
this,
this.message_position,
);
}
_rewriteCommand() {
this.command = this._settings.get_string("command");
}
},
);