forked from phocean/TopIcons-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
307 lines (264 loc) · 10.2 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright (C) phocean <[email protected]>
Credits to:
- "ag" for the original extension,
- "Mjnaderi" for the Toptray first fork.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
const St = imports.gi.St;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Clutter = imports.gi.Clutter;
const PanelMenu = imports.ui.panelMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
let settings = null;
let tray = null;
let trayIconImplementations = null;
let trayAddedId = 0;
let trayRemovedId = 0;
let icons = [];
let iconsBoxLayout = null;
let iconsContainer = null;
function init() { }
function enable() {
GLib.idle_add(GLib.PRIORITY_LOW, moveToTop);
tray = Main.legacyTray;
settings = Convenience.getSettings();
settings.connect('changed::icon-opacity', Lang.bind(this, refreshOpacity));
settings.connect('changed::icon-saturation', Lang.bind(this, refreshSaturation));
settings.connect('changed::icon-brightness', Lang.bind(this, refreshBrightnessContrast));
settings.connect('changed::icon-contrast', Lang.bind(this, refreshBrightnessContrast));
settings.connect('changed::icon-size', Lang.bind(this, refreshTray));
settings.connect('changed::icon-spacing', Lang.bind(this, refreshTray));
settings.connect('changed::tray-pos', Lang.bind(this, refreshPos));
settings.connect('changed::tray-order', Lang.bind(this, refreshPos));
}
function disable() {
moveToTray();
settings.run_dispose();
}
function onTrayIconAdded(o, icon, role, delay=500) {
let wmClass = icon.wm_class ? icon.wm_class.toLowerCase() : '';
// Icon properties
icon.reactive = true;
let trayPosition = settings.get_string('tray-pos');
let trayOrder = settings.get_int('tray-order');
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
// Container
let iconContainer = new St.Button({child: icon, visible: false});
icon.connect("destroy", function() {
icon.clear_effects();
iconContainer.destroy();
});
iconContainer.connect('button-release-event', function(actor, event) {
icon.click(event);
});
applyPreferences(icon, scaleFactor); // user settings
iconsBoxLayout.insert_child_at_index(iconContainer, 0);
// Display icons (with a blacklist filter for specific extension like Skype integration)
let blacklist = [];
// blacklist: array of uuid and wmClass (icon application name)
blacklist.push(["skype","[email protected]"]);
// loop through the array and hide the extension if extension X is enabled and corresponding application is running
for (let i = 0; i < blacklist.length; i++) {
if (ExtensionUtils.extensions[blacklist[i][1]] !== undefined && ExtensionUtils.extensions[blacklist[i][1]].state == 1 && wmClass == blacklist[i][0]) {
GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, Lang.bind(this, function()
{
iconContainer.visible = false;
return GLib.SOURCE_REMOVE;
}));
}
else {
GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, Lang.bind(this, function()
{
iconContainer.visible = true;
iconsContainer.actor.visible = true;
return GLib.SOURCE_REMOVE;
}));
}
}
icons.push(icon);
}
function onTrayIconRemoved(o, icon) {
let parent = icon.get_parent();
parent.destroy();
icon.destroy();
icons.splice(icons.indexOf(icon), 1);
if (icons.length === 0)
iconsContainer.actor.visible = false;
}
function widgetNumber() {
return Main.panel._leftBox.get_n_children();
}
function moveToTop() {
// Replace signal handlers
if (tray._trayIconAddedId)
tray._trayManager.disconnect(tray._trayIconAddedId);
if (tray._trayIconRemovedId)
tray._trayManager.disconnect(tray._trayIconRemovedId);
trayAddedId = tray._trayManager.connect('tray-icon-added', onTrayIconAdded);
trayRemovedId = tray._trayManager.connect('tray-icon-removed', onTrayIconRemoved);
// Create box layout for icon containers
iconsBoxLayout = new St.BoxLayout();
let boxLayoutSpacing = settings.get_int('icon-spacing');
iconsBoxLayout.set_style('spacing: ' + boxLayoutSpacing + 'px; margin_top: 2px; margin_bottom: 2px;');
// An empty ButtonBox will still display padding,
// therefore create it without visibility.
iconsContainer = new PanelMenu.ButtonBox({visible: false});
let iconsContainerActor = iconsContainer.actor;
iconsContainerActor.add_actor(iconsBoxLayout);
let parent = iconsContainerActor.get_parent();
if (parent)
parent.remove_actor(iconsContainerActor);
// Position
let trayPosition = settings.get_string('tray-pos');
let trayOrder = settings.get_int('tray-order');
if (trayPosition == 'left') {
let index = Main.panel._leftBox.get_n_children() - trayOrder -1;
Main.panel._leftBox.insert_child_at_index(iconsContainerActor, index);
}
else {
let index = Main.panel._rightBox.get_n_children() - trayOrder -1;
Main.panel._rightBox.insert_child_at_index(iconsContainerActor, index);
}
// Move each tray icon to the top
let length = tray._iconBox.get_n_children();
for (let i = 0; i < length; i++) {
let button = tray._iconBox.get_child_at_index(0);
let icon = button.child;
button.remove_actor(icon);
button.destroy();
// Icon already loaded, no need to delay insertion
onTrayIconAdded(this, icon, '', 0);
}
}
function moveToTray() {
// Replace signal handlers
if (trayAddedId) {
tray._trayManager.disconnect(trayAddedId);
trayAddedId = 0;
}
if (trayRemovedId) {
tray._trayManager.disconnect(trayRemovedId);
trayRemovedId = 0;
}
tray._trayIconAddedId = tray._trayManager.connect(
'tray-icon-added', Lang.bind(tray, tray._onTrayIconAdded));
tray._trayIconRemovedId = tray._trayManager.connect(
'tray-icon-removed', Lang.bind(tray, tray._onTrayIconRemoved));
// Clean and move each icon back to the Legacy Tray;
for (let i = 0; i < icons.length; i++) {
let icon = icons[i];
icon.opacity = 255;
icon.clear_effects();
let parent = icon.get_parent();
parent.remove_actor(icon);
parent.destroy();
tray._onTrayIconAdded(tray, icon);
}
// Clean containers
icons = [];
if (iconsBoxLayout) {
iconsBoxLayout.destroy();
iconsBoxLayout = null;
}
if (iconsContainer) {
if (iconsContainer.actor) {
iconsContainer.actor.destroy();
iconsContainer.actor = null;
}
iconsContainer = null;
}
}
// These functions read settings and apply user preferences per icon
function applyPreferences(icon, scaleFactor) {
applyOpacity(icon);
applyBrightnessContrast(icon);
applySaturation(icon);
applySize(icon, scaleFactor);
}
function applySaturation(icon) {
let desaturationValue = settings.get_double('icon-saturation');
let effect = new Clutter.DesaturateEffect({factor : desaturationValue});
icon.add_effect(effect);
effect.set_factor(desaturationValue);
}
function applyBrightnessContrast(icon) {
let brightnessValue = settings.get_double('icon-brightness');
let contrastValue = settings.get_double('icon-contrast');
let effect = new Clutter.BrightnessContrastEffect({});
effect.set_brightness(brightnessValue);
effect.set_contrast(contrastValue);
icon.add_effect(effect);
}
function applyOpacity(icon) {
let opacityValue = settings.get_int('icon-opacity');
// Apply mouse events to iconContainer,
// since click events are also received by it
icon.opacityEnterId = icon.get_parent().connect(
'enter-event', function(actor, event) { icon.opacity = 255; });
icon.opacityLeaveId = icon.get_parent().connect(
'leave-event', function(actor, event) { icon.opacity = opacityValue; });
icon.opacity = opacityValue;
}
function applySize(icon, scaleFactor) {
let iconSize = settings.get_int('icon-size');
icon.get_parent().set_size(iconSize * scaleFactor, iconSize * scaleFactor);
icon.set_size(iconSize * scaleFactor, iconSize * scaleFactor);
}
// These functions are called by signals on preference change and loop through icons to apply it
function refreshOpacity() {
for (let i = 0; i < icons.length; i++) {
let icon = icons[i];
applyOpacity(icon);
}
}
function refreshSaturation() {
for (let i = 0; i < icons.length; i++) {
let icon = icons[i];
applySaturation(icon);
}
refreshTray();
}
function refreshBrightnessContrast() {
for (let i = 0; i < icons.length; i++) {
let icon = icons[i];
applyBrightnessContrast(icon);
}
refreshTray();
}
function refreshTray() {
moveToTray();
moveToTop();
}
function refreshPos() {
let trayPosition = settings.get_string('tray-pos');
let value = settings.get_int('tray-order');
// Dirty hack but could not find how to access Main items from prefs.js to set a dynamic max value for the current tray
if (trayPosition == 'left') {
if (value >= Main.panel._leftBox.get_n_children()) {
value = 0;
settings.set_int('tray-order',value);
}
}
else {
if (value >= Main.panel._rightBox.get_n_children()) {
value = 0;
settings.set_int('tray-order',value);
}
}
refreshTray();
}