-
Notifications
You must be signed in to change notification settings - Fork 11
/
extension.js
569 lines (467 loc) · 16.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const {Clutter,Gio,GLib,GObject,St} = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const CurrentExtension = ExtensionUtils.getCurrentExtension();
const Volume = imports.ui.status.volume;
const { Players } = CurrentExtension.imports.players;
const { buildLabel } = CurrentExtension.imports.label;
let indicator = null;
function enable(){
indicator = new MprisLabel(ExtensionUtils.getSettings());
}
function disable(){
indicator._disable();
indicator.destroy();
indicator = null;
}
var MprisLabel = GObject.registerClass(
{ GTypeName: 'MprisLabel' },
class MprisLabel extends PanelMenu.Button {
_init(settings){
super._init(0.0,'Mpris Label',false);
this.settings = settings;
const EXTENSION_INDEX = this.settings.get_int('extension-index');
const EXTENSION_PLACE = this.settings.get_string('extension-place');
const REPOSITION_DELAY = this.settings.get_int('reposition-delay');
this.box = new St.BoxLayout({
x_align: Clutter.ActorAlign.FILL
});
this._onPaddingChanged();//apply padding
this.add_child(this.box);
this.label = new St.Label({
text: "",
y_align: Clutter.ActorAlign.CENTER
});
this._setLabelStyle();
this.box.add_child(this.label);
this.players = new Players(this.settings);
this.connect('button-press-event',(_a, event) => this._onClick(event));
this.connect('scroll-event', (_a, event) => this._onScroll(event));
this.volumeControl = Volume.getMixerControl();
this.volumeControl.connect("stream-added", this._getStream.bind(this));
this.volumeControl.connect("stream-removed",this._getStream.bind(this));
this.settings.connect('changed::left-padding',this._onPaddingChanged.bind(this));
this.settings.connect('changed::right-padding',this._onPaddingChanged.bind(this));
this.settings.connect('changed::extension-index',this._updateTrayPosition.bind(this));
this.settings.connect('changed::extension-place',this._updateTrayPosition.bind(this));
this.settings.connect('changed::show-icon',this._setIcon.bind(this));
this.settings.connect('changed::use-album',this._setIcon.bind(this));
this.settings.connect('changed::symbolic-source-icon', this._setIcon.bind(this));
this.settings.connect('changed::font-color', this._setLabelStyle.bind(this));
Main.panel.addToStatusArea('Mpris Label',this,EXTENSION_INDEX,EXTENSION_PLACE);
this._repositionTimeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT,REPOSITION_DELAY,this._updateTrayPosition.bind(this));
this.lastClick = new Map(); // place where occurrences of click actions will be stored
this._refresh();
}
_onPaddingChanged(){
const ICON_PLACE = this.settings.get_string('show-icon');
let LEFT_PADDING = this.settings.get_int('left-padding');
let RIGHT_PADDING = this.settings.get_int('right-padding');
const SHOW_ICON = this.settings.get_string('show-icon');
if(SHOW_ICON){
if (ICON_PLACE == "right")
RIGHT_PADDING = Math.max(0,RIGHT_PADDING - 5)
else
LEFT_PADDING = Math.max(0,LEFT_PADDING - 5)
}
this.box.set_style("padding-left: " + LEFT_PADDING + "px;"
+ "padding-right: " + RIGHT_PADDING + "px; ");
}
_determineColors(){
let themeNode = this.get_theme_node();
let fg = themeNode.get_foreground_color();
let bg = themeNode.get_background_color();
//Clutter.Color doesn't have a method for average mixing
const channels = [[fg.red,bg.red],[fg.green,bg.green],[fg.blue,bg.blue],[fg.alpha,bg.alpha]];
let new_channels = [];
channels.forEach(channel => {
new_channels.push(Math.round((channel[0] + channel[1]) / 2));
});
let mixedColor = Clutter.Color.new(new_channels[0],new_channels[1],new_channels[2],new_channels[3]);
let color_str = mixedColor.to_string();
this.unfocusColor = color_str.substring(0,7); //ignore alpha channel
}
_updateTrayPosition(){
const EXTENSION_PLACE = this.settings.get_string('extension-place');
const EXTENSION_INDEX = this.settings.get_int('extension-index');
if(this._timeout) //prevent refreshes while changing position
this._removeTimeout();
if (this.container.get_parent())
this.container.get_parent().remove_child(this.container);
if (EXTENSION_PLACE == "left"){
Main.panel._leftBox.insert_child_at_index(this.container, EXTENSION_INDEX);
}
else if (EXTENSION_PLACE == "center"){
Main.panel._centerBox.insert_child_at_index(this.container, EXTENSION_INDEX);
}
else if (EXTENSION_PLACE == "right"){
Main.panel._rightBox.insert_child_at_index(this.container, EXTENSION_INDEX);
}
this._refresh(); //call and re-enable the refresh loop
}
_onClick(event){
const REPOSITION_ON_BUTTON_PRESS = this.settings.get_boolean('reposition-on-button-press');
const DOUBLE_CLICK = this.settings.get_boolean('enable-double-clicks');
if (REPOSITION_ON_BUTTON_PRESS)
this._updateTrayPosition(); //force tray position update on button press
const button = event.get_button();
if (!DOUBLE_CLICK) {
this._activateButtonAction(button,false);
return Clutter.EVENT_STOP;
}
const DOUBLE_CLICK_TIME = this.settings.get_int('double-click-time');
const lastClickTimestamp = this.lastClick.get(button);
const currentTimestamp = Date.now();
// if is a double click, remove the scheduled action and activate the double click action
if (lastClickTimestamp && (currentTimestamp - lastClickTimestamp <= DOUBLE_CLICK_TIME)) {
GLib.source_remove(this._scheduledActionTimeout);
this._scheduledActionTimeout = null;
this._activateButtonAction(button,true);
return Clutter.EVENT_STOP;
}
// else register the button and current timestamp on 'this.lastClick', and schedule the single click action
this.lastClick.set(button,currentTimestamp);
this._scheduledActionTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, DOUBLE_CLICK_TIME, () => {
this._activateButtonAction(button,false);
return GLib.SOURCE_REMOVE; // callback function will be executed once
}
);
return Clutter.EVENT_STOP;
}
_onScroll(event) {
const SCROLL_ACTION = this.settings.get_string('scroll-action');
if(SCROLL_ACTION == 'none')
return Clutter.EVENT_STOP
if (event.is_pointer_emulated())
return Clutter.EVENT_PROPAGATE;
let delta = 0;
const time_delta = Date.now() - this.last_scroll;
switch (event.get_scroll_direction()) {
// UP=0,DOWN=1,LEFT=2,RIGHT=3,SMOOTH=4
case Clutter.ScrollDirection.SMOOTH:
delta = -event.get_scroll_delta()[1];
delta = Math.clamp(-1,delta,1);
this.last_scroll = new Date().getTime();
break;
case Clutter.ScrollDirection.UP:
if (!this.last_scroll || time_delta > 500)
delta = 0.25;
break;
case Clutter.ScrollDirection.DOWN:
if (!this.last_scroll || time_delta > 500)
delta = -0.25;
break;
default: //exit (do nothing)
return Clutter.EVENT_PROPAGATE;
}
if(!delta == 0)
switch(SCROLL_ACTION) {
case "volume-controls":
this._changeVolume(delta);
break;
case "track-change":
const time_delta = Date.now() - this.last_scroll;
const SCROLL_DELAY = this.settings.get_int('scroll-delay');
if (!this.last_scroll || time_delta > SCROLL_DELAY) {
if (delta > 0)
this._activateAction("next-track");
else if (delta < 0)
this._activateAction("prev-track");
}
this.last_scroll = new Date().getTime();
break;
}
return Clutter.EVENT_STOP;
}
_activateButtonAction(button,isDoubleClick) {
let option = '';
switch(button){
case Clutter.BUTTON_PRIMARY:
option = isDoubleClick ? 'left-double-click-action' : 'left-click-action';
break;
case Clutter.BUTTON_MIDDLE:
option = isDoubleClick ? 'middle-double-click-action' : 'middle-click-action';
break;
case Clutter.BUTTON_SECONDARY:
option = isDoubleClick ? 'right-double-click-action' : 'right-click-action';
break;
case 8:
option = isDoubleClick ? 'thumb-double-backward-action' : 'thumb-backward-action';
break;
case 9:
option = isDoubleClick ? 'thumb-double-forward-action' : 'thumb-forward-action';
break;
}
this._activateAction(this.settings.get_string(option));
}
_activateAction(value) {
switch(value){
case 'play-pause':
if(this.player)
this.player.toggleStatus();
break;
case 'next-track':
if(this.player)
this.player.goNext();
break;
case 'prev-track':
if(this.player)
this.player.goPrevious();
break;
case 'activate-player':
if(this.player)
this.player.activatePlayer();
break;
case 'open-menu':
this._buildMenu();
this.menu.toggle();
break;
case 'next-player':
this.player = this.players.next();
this._refresh();
break;
case 'volume-up':
this._changeVolume(1);
break;
case 'volume-down':
this._changeVolume(-1);
break;
case 'volume-mute':
this._changeVolume(0);
break;
}
}
_changeVolume(delta){
let stream = [];
stream[0] = this.volumeControl.get_default_sink();
let streamName = 'System Volume (Global)';
const CONTROL_SCHEME = this.settings.get_string('volume-control-scheme');
if(CONTROL_SCHEME == 'application' && this.player){
if(!this.stream || this.stream.length == 0)
this._getStream();
if (this.stream && this.stream.length > 0){ //will fall back to System Volume (Global)
stream = this.stream;
streamName = this.player.identity;
}
}
let max = this.volumeControl.get_vol_max_norm()
let step = max / 30;
let volume = stream[0].volume;
stream.forEach(stream => {//if multiple stream, use the lowest as base reference
if (stream.volume < volume)
volume = stream.volume;
});
let newVolume = volume + step * delta;
newVolume = Math.round(Math.clamp(0,newVolume,max));
stream.forEach(stream => {
stream.volume = newVolume;
stream.push_volume();
});
let volumeRatio = newVolume/max;
let monitor = global.display.get_current_monitor(); //identify current monitor for OSD
if(delta == 0){//toggle mute
stream.forEach(stream => {
stream.change_is_muted(!stream.is_muted);
if(!stream.is_muted) //set mute icon
volumeRatio = 0
});
}
const icon = Gio.Icon.new_for_string(this._setVolumeIcon(volumeRatio));
Main.osdWindowManager.show(monitor, icon, streamName, volumeRatio);
}
_getStream(){
if(!this.player || !this.player.identity)
return
const streamList = this.volumeControl.get_streams();
this.stream = [];
streamList.forEach(stream => {
if(stream.get_name() && stream.get_name().toLowerCase() == this.player.identity.toLowerCase())
this.stream.push(stream);
});
if (this.stream.length > 0)
return this.stream
streamList.forEach(stream => {
if(
stream.get_name() && (
stream.get_name().match(new RegExp(this.player.identity,"i")) ||
this.player.identity.match(new RegExp(stream.get_name(),"i")) )
)
this.stream.push(stream);
});
return this.stream
}
_setVolumeIcon(volume) {
let volume_icon = 'audio-volume-high-symbolic';
switch (true) {
case (volume == 0):
volume_icon = 'audio-volume-muted-symbolic';
break
case (volume < 0.33):
volume_icon = 'audio-volume-low-symbolic';
break
case (volume < 0.67):
volume_icon = 'audio-volume-medium-symbolic';
break
}
return volume_icon
}
_buildMenu(){
const AUTO_SWITCH_TO_MOST_RECENT = this.settings.get_boolean('auto-switch-to-most-recent');
this.menu.removeAll(); //start by deleting everything
//player selection submenu:
this.players.list.forEach(player => {
let settingsMenuItem = new PopupMenu.PopupMenuItem(player.identity);
settingsMenuItem.setOrnament(PopupMenu.Ornament.NONE); //to force item horizontal alignment
if (AUTO_SWITCH_TO_MOST_RECENT){
if(!this.unfocusColor)
this._determineColors();
settingsMenuItem.label.set_style('font-style:italic');
settingsMenuItem.set_style('color:' + this.unfocusColor);
}
//if item is active player, include DOT if auto mode, CHECK if manual mode
if (this.player) {
if (this.player.address == player.address) {
if (AUTO_SWITCH_TO_MOST_RECENT)
settingsMenuItem.setOrnament(PopupMenu.Ornament.DOT);
else {
settingsMenuItem.setOrnament(PopupMenu.Ornament.CHECK);
settingsMenuItem.label.set_style('font-weight:bold');
}
}
}
settingsMenuItem.connect('activate', () => {
if (AUTO_SWITCH_TO_MOST_RECENT)
this.settings.set_boolean('auto-switch-to-most-recent',false);
this.players.selected = player; //this.player should sync with this on the next refresh
this._refresh(); //so let's refresh right away
});
this.menu.addMenuItem(settingsMenuItem);
});
//automode entry:
if (this.players.list.length > 0){
let settingsMenuItem = new PopupMenu.PopupMenuItem('Switch Automatically');
settingsMenuItem.setOrnament(PopupMenu.Ornament.NONE); //to force item horizontal alignment
if (AUTO_SWITCH_TO_MOST_RECENT) {
settingsMenuItem.setOrnament(PopupMenu.Ornament.CHECK);
settingsMenuItem.label.set_style('font-weight:bold');
}
this.menu.addMenuItem(settingsMenuItem);
settingsMenuItem.connect('activate', () =>{
this.settings.set_boolean('auto-switch-to-most-recent',!AUTO_SWITCH_TO_MOST_RECENT);
});
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); //add separator
}
//settings shortcut:
let settingsMenuItem = new PopupMenu.PopupMenuItem('Settings');
settingsMenuItem.setOrnament(PopupMenu.Ornament.NONE); //to force item horizontal alignment
settingsMenuItem.connect('activate', () => ExtensionUtils.openPrefs() );
this.menu.addMenuItem(settingsMenuItem);
}
_refresh() {
const REFRESH_RATE = this.settings.get_int('refresh-rate');
if(this._timeout) //prevent simultaneous timeouts
this._removeTimeout();
let prevPlayer = this.player;
try {
this.players.updateFilterList();
this.players.updateActiveList();
}
catch {
; //do nothing
}
if (this.players.list == 0){ //terminate function early, reset timer, and hide label
if(this.visible)
this.hide();
this._timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT,
REFRESH_RATE, this._refresh.bind(this));
return
}
if(!this.visible)
this.show();
this.player = this.players.pick();
if(this.player != prevPlayer)
this._getStream();
this._setText();
this._setIcon();
this._timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT,
REFRESH_RATE, this._refresh.bind(this));
}
_setIcon(){
const ICON_PLACE = this.settings.get_string('show-icon');
const ICON_PADDING = this.settings.get_int('icon-padding');
const PLACEHOLDER = this.settings.get_string('button-placeholder');
const SYMBOLIC_ICON = this.settings.get_boolean('symbolic-source-icon');
const USE_ALBUM = this.settings.get_boolean('use-album');
const ALBUM_BLACKLIST = this.settings.get_string('album-blacklist').trim();
if(this.icon){
this.box.remove_child(this.icon);
this.icon = null;
}
if(!ICON_PLACE || !this.player || this.label.get_text() == "" || this.label.get_text() == PLACEHOLDER)
return
if(USE_ALBUM){
const ALBUM_SIZE = this.settings.get_int('album-size');
let size = Math.floor(Main.panel.height*ALBUM_SIZE/100);
const blacklist = ALBUM_BLACKLIST.toLowerCase().replaceAll(' ','').split(',');
if(!this.player.identity || !blacklist.includes(this.player.identity.toLowerCase()))
this.icon = this.player.getArtUrlIcon(size);
}
if(this.icon == null){
this.icon = this.player.getIcon(SYMBOLIC_ICON);
if (SYMBOLIC_ICON)
this.icon.set_style('-st-icon-style: symbolic;');
}
if (this.icon != null | undefined){
if (ICON_PLACE == "right"){
this.icon.set_style(this.icon.get_style() + "padding-left: " + ICON_PADDING + "px;padding-right: 0px;");
this.box.add_child(this.icon);
}
else if (ICON_PLACE == "left"){
this.icon.set_style(this.icon.get_style() + "padding-left: 0px;padding-right: " + ICON_PADDING + "px;");
this.box.insert_child_at_index(this.icon,0);
}
}
}
_setText() {
try{
if(this.player == null || undefined)
this.label.set_text("");
else
this.label.set_text(buildLabel(this.players,this.settings));
}
catch(err){
log("Mpris Label: " + err);
this.label.set_text("");
}
}
_setLabelStyle() {
const FONT_COLOR = this.settings.get_string('font-color');
this.label.set_style('color: '+FONT_COLOR);
}
_removeTimeout() {
if(this._timeout) {
GLib.Source.remove(this._timeout);
this._timeout = null;
}
}
vfunc_event(event){
return Clutter.EVENT_PROPAGATE;
}
_disable(){
if(this.icon)
this.box.remove_child(this.icon);
this.box.remove_child(this.label);
this.remove_child(this.box);
this._removeTimeout();
if (this._scheduledActionTimeout){
GLib.Source.remove(this._scheduledActionTimeout);
this._repositionTimeout = null;
}
if (this._repositionTimeout){
GLib.Source.remove(this._repositionTimeout);
this._repositionTimeout = null;
}
}
});