-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathextension.ts
492 lines (407 loc) · 21.5 KB
/
extension.ts
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
/* extension.js
*
* Copyright (C) 2024 Zacharie DUBRULLE
*
* 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 3
* 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 <https://www.gnu.org/licenses/>.
*/
import Clutter from 'gi://Clutter';
import type Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gvc from 'gi://Gvc';
import St from 'gi://St';
import { gettext as _, Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { MprisSource } from 'resource:///org/gnome/shell/ui/mpris.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import { QuickSettingsMenu } from 'resource:///org/gnome/shell/ui/quickSettings.js';
import * as Volume from 'resource:///org/gnome/shell/ui/status/volume.js';
import { LibPanel, Panel } from './libs/libpanel/main.js';
import { update_settings } from './libs/preferences.js';
import { cleanup_idle_ids, get_pactl_path, wait_property } from './libs/utils.js';
import { ApplicationsMixer, ApplicationsMixerToggle, AudioProfileSwitcher, BalanceSlider, MprisList, SinkMixer } from './libs/widgets.js';
const DateMenu = Main.panel.statusArea.dateMenu;
const QuickSettings = Main.panel.statusArea.quickSettings;
const CalendarMessageList = DateMenu._messageList;
const MessageView_DateMenu = CalendarMessageList._messageView;
const SystemItem = QuickSettings._system._systemItem;
// _volumeOutput is always defined here because `./libs/widgets.js` wait on it
const OutputVolumeSlider = QuickSettings._volumeOutput._output;
export default class QSAP extends Extension {
settings!: Gio.Settings;
async enable() {
this.InputVolumeIndicator = await wait_property(QuickSettings, '_volumeInput');
this.InputVolumeSlider = this.InputVolumeIndicator._input;
this.settings = this.getSettings();
update_settings(this.settings);
this._scasis_callback = this.settings.connect(
'changed::always-show-input-volume-slider',
() => this._set_always_show_input(this.settings.get_boolean('always-show-input-volume-slider'))
);
this.settings.emit('changed::always-show-input-volume-slider', 'always-show-input-volume-slider');
this._scscd_callback = this.settings.connect(
'changed::master-volume-sliders-show-current-device',
() => {
if (this.settings.get_boolean('master-volume-sliders-show-current-device')) {
this._patch_show_current_device(OutputVolumeSlider);
this._patch_show_current_device(this.InputVolumeSlider);
} else {
this._unpatch_show_current_device(OutputVolumeSlider);
this._unpatch_show_current_device(this.InputVolumeSlider);
}
}
);
this.settings.emit('changed::master-volume-sliders-show-current-device', 'master-volume-sliders-show-current-device');
this._scabaortd_callback = this.settings.connect(
'changed::add-button-applications-output-reset-to-default',
() => {
if (this.settings.get_boolean('add-button-applications-output-reset-to-default')) {
this._add_reset_applications_output();
} else {
this._remove_reset_applications_output();
}
}
);
this.settings.emit('changed::add-button-applications-output-reset-to-default', 'add-button-applications-output-reset-to-default');
this._master_volumes = [];
this._sc_callback = this.settings.connect('changed', (_, name) => {
if (name !== "autohide-profile-switcher") {
this._refresh_panel();
}
});
this._refresh_panel();
}
disable() {
this.settings.disconnect(this._scscd_callback);
this._unpatch_show_current_device(OutputVolumeSlider);
this._unpatch_show_current_device(this.InputVolumeSlider);
this.settings.disconnect(this._scabaortd_callback);
this._remove_reset_applications_output();
this.settings.disconnect(this._scasis_callback);
this.settings.disconnect(this._sc_callback);
cleanup_idle_ids();
this._set_always_show_input(false);
this._cleanup_panel();
this.settings = null;
}
_refresh_panel() {
this._cleanup_panel();
const panel_type = this.settings.get_string("panel-type");
const merged_panel_position = this.settings.get_string("merged-panel-position");
const remove_output_volume_slider = this.settings.get_boolean("remove-output-volume-slider");
const move_output_volume_slider = this.settings.get_boolean('move-output-volume-slider');
const move_input_volume_slider = this.settings.get_boolean('move-input-volume-slider');
const create_mpris_controllers = this.settings.get_boolean("create-mpris-controllers");
const create_applications_volume_sliders = this.settings.get_boolean('create-applications-volume-sliders');
const create_perdevice_volume_sliders = this.settings.get_boolean('create-perdevice-volume-sliders');
const create_balance_slider = this.settings.get_boolean('create-balance-slider');
const create_profile_switcher = this.settings.get_boolean('create-profile-switcher');
const widgets_order = this.settings.get_strv('widgets-order');
if (move_output_volume_slider || move_input_volume_slider || create_mpris_controllers || create_applications_volume_sliders || create_perdevice_volume_sliders || remove_output_volume_slider || create_balance_slider || create_profile_switcher) {
if (panel_type === "independent-panel")
LibPanel.enable();
this._panel = LibPanel.main_panel;
let index = -1;
if (panel_type === "separate-indicator") {
this._indicator = new PanelMenu.Button(0.0, "Audio panel", true);
this._indicator.add_child(new St.Icon({ style_class: 'system-status-icon', icon_name: 'audio-x-generic-symbolic' }));
this._panel = new QuickSettingsMenu(this._indicator, 2);
// Since the panel contains no element that have a minimal width (like QuickToggle)
// we need to force it to take the same with as a normal panel
this._panel.box.add_constraint(new Clutter.BindConstraint({
coordinate: Clutter.BindCoordinate.WIDTH,
source: LibPanel.main_panel._boxPointer || LibPanel.main_panel,
}));
// Hide the indicator when empty
const update_visibility = () => {
for (const child of this._panel._grid.get_children()) {
if (child != this._panel._grid.layout_manager._overlay && child.visible) {
this._indicator.show();
return;
}
}
this._indicator.hide();
}
this._panel._grid.connect("child-added", (_self, child) => {
child._qsap_vis_changed_callback = child.connect("notify::visible", () => {
update_visibility();
});
update_visibility();
});
this._panel._grid.connect("child-removed", (_self, child) => {
child.disconnect(child._qsap_vis_changed_callback);
delete child._qsap_vis_changed_callback;
update_visibility();
});
this._indicator.setMenu(this._panel);
Main.panel.addToStatusArea(this.uuid, this._indicator);
} else if (panel_type === "independent-panel") {
this._panel = new Panel('main');
// Since the panel contains no element that have a minimal width (like QuickToggle)
// we need to force it to take the same with as a normal panel
this._panel.add_constraint(new Clutter.BindConstraint({
coordinate: Clutter.BindCoordinate.WIDTH,
source: LibPanel.main_panel,
}));
LibPanel.addPanel(this._panel);
}
if (panel_type === "merged-panel" && merged_panel_position === 'top') {
widgets_order.reverse();
index = this._panel.getItems().indexOf(SystemItem) + 2;
}
for (const widget of widgets_order) {
if (widget === 'output-volume-slider' && move_output_volume_slider) {
this._move_slider(index, OutputVolumeSlider);
} else if (widget === 'input-volume-slider' && move_input_volume_slider) {
this._move_slider(index, this.InputVolumeSlider);
} else if (widget === 'mpris-controllers' && create_mpris_controllers) {
this._create_media_controls(index);
if (this.settings.get_boolean("mpris-controllers-are-moved")) {
this._remove_base_media_controls();
}
} else if (widget === 'applications-volume-sliders' && create_applications_volume_sliders) {
this._create_app_mixer(index, this.settings.get_boolean("group-applications-volume-sliders"), this.settings.get_string("applications-volume-sliders-filter-mode"), this.settings.get_strv("applications-volume-sliders-filters"));
} else if (widget === "perdevice-volume-sliders" && create_perdevice_volume_sliders) {
this._create_sink_mixer(index, this.settings.get_string("perdevice-volume-sliders-filter-mode"), this.settings.get_strv("perdevice-volume-sliders-filters"));
} else if (widget === "balance-slider" && create_balance_slider) {
this._create_balance_slider(index);
} else if (widget === "profile-switcher" && create_profile_switcher) {
this._create_profile_switcher(index);
}
}
if (remove_output_volume_slider) {
OutputVolumeSlider.visible = false;
}
}
}
_cleanup_panel() {
OutputVolumeSlider.visible = true;
if (!this._panel) return;
if (this._profile_switcher) {
this._panel.removeItem(this._profile_switcher);
this._profile_switcher.destroy();
this._profile_switcher = null;
}
if (this._balance_slider) {
this._panel.removeItem(this._balance_slider);
this._balance_slider.destroy();
this._balance_slider = null;
}
if (this._sink_mixer) {
this._sink_mixer.destroy();
this._sink_mixer = null;
}
if (this._applications_mixer) {
this._applications_mixer.destroy();
this._applications_mixer = null;
}
if (this._applications_mixer_combined) {
this._panel.removeItem(this._applications_mixer_combined);
this._applications_mixer_combined.destroy();
this._applications_mixer_combined = null;
}
if (this._media_section) {
this._panel.removeItem(this._media_section);
this._media_section = null;
}
if (MessageView_DateMenu._qsap_media_removed) {
MessageView_DateMenu._setupMpris();
delete MessageView_DateMenu._qsap_media_removed;
}
this._master_volumes.reverse();
for (const [slider, index] of this._master_volumes) {
this._panel.removeItem(slider);
LibPanel.main_panel.addItem(slider, 2);
LibPanel.main_panel._grid.set_child_at_index(slider, index);
}
this._master_volumes = [];
if (this._indicator) {
this._indicator.destroy(); // also destroys `this._panel``
delete this._indicator;
} else if (this._panel !== LibPanel.main_panel) {
LibPanel.removePanel(this._panel); // prevent the panel's position being forgotten
this._panel.destroy();
};
this._panel = null;
LibPanel.disable();
}
_move_slider(index: number, slider) {
const old_index = slider.get_parent().get_children().indexOf(slider);
LibPanel.main_panel.removeItem(slider);
this._panel.addItem(slider, 2);
this._panel._grid.set_child_at_index(slider, index);
this._master_volumes.push([slider, old_index]);
}
_create_media_controls(index: number) {
this._media_section = new MprisList();
this._media_section.add_style_class_name('QSAP-media-section');
if (!this.settings.get_boolean('ignore-css')) {
this._media_section.add_style_class_name('QSAP-media-section-optional');
}
this._panel.addItem(this._media_section, 2);
this._panel._grid.set_child_at_index(this._media_section, index);
}
_remove_base_media_controls() {
MessageView_DateMenu._mediaSource.disconnectObject(MessageView_DateMenu);
for (const player of MessageView_DateMenu._mediaSource.players) {
MessageView_DateMenu._removePlayer(player);
}
MessageView_DateMenu._qsap_media_removed = true;
}
_create_app_mixer(index: number, type, filter_mode, filters) {
if (type === "combined") {
this._applications_mixer_combined = new ApplicationsMixerToggle(this.settings, filter_mode, filters);
this._panel.addItem(this._applications_mixer_combined, 2);
this._panel._grid.set_child_at_index(this._applications_mixer_combined, index);
} else {
this._applications_mixer = new ApplicationsMixer(this._panel, index, filter_mode, filters, this.settings);
}
}
_create_sink_mixer(index: number, filter_mode, filters) {
this._sink_mixer = new SinkMixer(this._panel, index, filter_mode, filters);
}
_create_balance_slider(index: number) {
this._balance_slider = new BalanceSlider(this.settings);
this._panel.addItem(this._balance_slider, 2);
this._panel._grid.set_child_at_index(this._balance_slider, index);
}
_create_profile_switcher(index: number) {
this._profile_switcher = new AudioProfileSwitcher(this.settings);
this._panel.addItem(this._profile_switcher, 1);
this._panel._grid.set_child_at_index(this._profile_switcher, index);
}
_set_always_show_input(enabled: boolean) {
if (enabled) {
this._ivs_vis_callback = this.InputVolumeSlider.connect("notify::visible", this._reset_input_slider_vis.bind(this));
// make sure to check if the icon should be shown when some events are fired.
// we need this because we make the slider always visible, so notify::visible isn't
// fired when gnome-shell tries to show it (because it was already visible)
this._ivsc_sa_callback = this.InputVolumeSlider._control.connect("stream-added", this._reset_input_slider_vis.bind(this));
this._ivsc_sr_callback = this.InputVolumeSlider._control.connect("stream-removed", this._reset_input_slider_vis.bind(this));
this._ivsc_dsc_callback = this.InputVolumeSlider._control.connect("default-source-changed", this._reset_input_slider_vis.bind(this));
this.InputVolumeSlider.visible = true;
} else {
if (this._ivs_vis_callback) this.InputVolumeSlider.disconnect(this._ivs_vis_callback);
this._ivs_vis_callback = null;
if (this._ivsc_sa_callback) this.InputVolumeSlider._control.disconnect(this._ivsc_sa_callback);
this._ivsc_sa_callback = null;
if (this._ivsc_sr_callback) this.InputVolumeSlider._control.disconnect(this._ivsc_sr_callback);
this._ivsc_sr_callback = null;
if (this._ivsc_dsc_callback) this.InputVolumeSlider._control.disconnect(this._ivsc_dsc_callback);
this._ivsc_dsc_callback = null;
this.InputVolumeSlider.visible = this.InputVolumeSlider._shouldBeVisible();
this.InputVolumeIndicator.visible = this.InputVolumeSlider._shouldBeVisible();
}
}
_reset_input_slider_vis() {
if (!this.InputVolumeSlider.visible) {
this.InputVolumeSlider.visible = true;
}
this.InputVolumeIndicator.visible = this.InputVolumeSlider._shouldBeVisible();
}
// Base slider
// slider: OutputStreamSlider
// box: StBoxLayout
// slider._iconButton: StButton
// sliderBin: StBin
// slider._menuButton: StButton
//
// Modified slider
// slider: OutputStreamSlider
// box: StBoxLayout
// slider._iconButton: StButton
// vbox: StBoxLayout (NEW)
// label: StLabel (NEW)
// hbox: StBoxLayout (NEW)
// sliderBin: StBin
// slider._menuButton: StButton
_patch_show_current_device(slider) {
if (slider._qsap_callback) return;
slider._iconButton._qsap_y_expand = slider._iconButton.y_expand;
slider._iconButton._qsap_y_align = slider._iconButton.y_align;
slider._iconButton.y_expand = false;
slider._iconButton.y_align = Clutter.ActorAlign.CENTER;
const box = slider.child;
const sliderBin = box.get_children()[1];
box.remove_child(sliderBin);
const menu_button_visible = slider._menuButton.visible;
box.remove_child(slider._menuButton);
const vbox = new St.BoxLayout({ orientation: Clutter.Orientation.VERTICAL, x_expand: true });
box.insert_child_at_index(vbox, 1);
const hbox = new St.BoxLayout();
hbox.add_child(sliderBin);
hbox.add_child(slider._menuButton);
slider._menuButton.visible = menu_button_visible; // we need to reset `actor.visible` when changing parent
// this prevent the tall panel bug when the button is shown
slider._menuButton._qsap_y_expand = slider._menuButton.y_expand;
slider._menuButton.y_expand = false;
const label = new St.Label({ natural_width: 0 });
label.style_class = "QSAP-application-volume-slider-label";
const signal_name = slider == OutputVolumeSlider ? "active-output-update" : "active-input-update";
slider._qsap_callback = slider._control.connect(signal_name, () => {
const device_id = slider._control.lookup_device_from_stream(slider._stream).get_id();
// using the item's text allow for compatibility with extensions that changes it, let's hope this won't break
slider._deviceItems.get(device_id).label.bind_property('text', label, 'text', GObject.BindingFlags.SYNC_CREATE);
});
if (slider._stream) {
const device_id = slider._control.lookup_device_from_stream(slider._stream).get_id();
slider._deviceItems.get(device_id).label.bind_property('text', label, 'text', GObject.BindingFlags.SYNC_CREATE);
};
vbox.add_child(label);
vbox.add_child(hbox);
}
_unpatch_show_current_device(slider) {
if (!slider._qsap_callback) return;
slider._control.disconnect(slider._qsap_callback);
slider._iconButton.y_expand = slider._iconButton._qsap_y_expand;
slider._iconButton.y_align = slider._iconButton._qsap_y_align;
const menu_button_visible = slider._menuButton.visible;
const box = slider.child;
const vbox = box.get_children()[1];
const hbox = vbox.get_children()[1];
const sliderBin = hbox.get_children()[0];
box.remove_child(vbox);
hbox.remove_child(sliderBin);
hbox.remove_child(slider._menuButton);
box.add_child(sliderBin);
box.add_child(slider._menuButton);
// we need to reset `actor.visible` when changing parent
slider._menuButton.visible = menu_button_visible;
slider._menuButton.y_expand = slider._menuButton._qsap_y_expand;
delete slider._qsap_callback;
delete slider._iconButton._qsap_y_expand;
delete slider._iconButton._qsap_y_align;
delete slider._menuButton._qsap_y_expand;
}
_add_reset_applications_output() {
this._action_application_reset_output = OutputVolumeSlider.menu.addAction(_("Reset all applications to default output"), () => {
const control = Volume.getMixerControl();
for (const stream of control.get_streams()) {
if (stream.is_event_stream || !(stream instanceof Gvc.MixerSinkInput)) {
continue;
}
GLib.spawn_command_line_async(`${get_pactl_path(this.settings)[0]} move-sink-input ${stream.index} @DEFAULT_SINK@`);
}
if (this._applications_mixer) {
for (const slider of this._applications_mixer._slider_manager._sliders.values()) {
slider._checkUsedSink()
}
}
});
}
_remove_reset_applications_output() {
if (this._action_application_reset_output) {
this._action_application_reset_output.destroy();
}
delete this._action_application_reset_output;
}
}