Skip to content

Commit

Permalink
Adjustments/modernization
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Nov 13, 2023
1 parent 9990c0f commit 1d49c7a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 128 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Indentation override for all JS
[*.{js,json}]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Version
id: version
Expand Down
180 changes: 87 additions & 93 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,104 @@ import * as Volume from "resource:///org/gnome/shell/ui/status/volume.js";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";

const VolumeScrollerIcons = [
"audio-volume-muted-symbolic",
"audio-volume-low-symbolic",
"audio-volume-medium-symbolic",
"audio-volume-high-symbolic",
"audio-volume-muted-symbolic",
"audio-volume-low-symbolic",
"audio-volume-medium-symbolic",
"audio-volume-high-symbolic",
];

export default class VolumeScrollerExtension extends Extension {
enable() {
this.settings = this.getSettings(
"org.gnome.shell.extensions.volume-scroller"
);
const setGranularity = () => {
this.volume_granularity = this.settings.get_int("granularity") / 100.0;
};

this.controller = Volume.getMixerControl();
this.panel = Main.panel;

this.enabled = false;
this.sink = null;

this.volume_max = this.controller.get_vol_max_norm();
setGranularity();

this.scroll_binding = null;
this.sink_binding = null;

this.settings.connect("changed::granularity", setGranularity);

this.enabled = true;
this.sink = this.controller.get_default_sink();

this.scroll_binding = this.panel.connect("scroll-event", (actor, event) =>
this._handle_scroll(actor, event)
);

this.sink_binding = this.controller.connect(
"default-sink-changed",
(controller, id) => this._handle_sink_change(controller, id)
);
enable() {
this.settings = this.getSettings();
const setGranularity = () => {
this.volume_granularity = this.settings.get_int("granularity") / 100.0;
};

this.controller = Volume.getMixerControl();
this.panel = Main.panel;

this.enabled = false;
this.sink = null;

this.volume_max = this.controller.get_vol_max_norm();
setGranularity();

this.scroll_binding = null;
this.sink_binding = null;

this.settings.connect("changed::granularity", setGranularity);

this.enabled = true;
this.sink = this.controller.get_default_sink();

this.scroll_binding = this.panel.connect(
"scroll-event",
this._handle_scroll
);
this.sink_binding = this.controller.connect(
"default-sink-changed",
this._handle_sink_change
);
}

disable() {
this.settings = null;
this.enabled = false;
this.sink = null;

this.panel.disconnect(this.scroll_binding);
this.scroll_binding = null;
this.panel = null;

this.controller.disconnect(this.sink_binding);
this.sink_binding = null;
this.controller = null;
}

_handle_scroll(_actor, event) {
let volume = this.sink.volume;

switch (event.get_scroll_direction()) {
case Clutter.ScrollDirection.UP:
volume += this._get_step();
break;

case Clutter.ScrollDirection.DOWN:
volume -= this._get_step();
break;

default:
return Clutter.EVENT_PROPAGATE;
}

disable() {
this.enabled = false;
this.sink = null;

this.panel.disconnect(this.scroll_binding);
this.scroll_binding = null;

this.controller.disconnect(this.sink_binding);
this.sink_binding = null;
}
volume = Math.clamp(volume, 0, this.volume_max);

_handle_scroll(actor, event) {
let volume = this.sink.volume;
this.sink.volume = volume;
this.sink.push_volume();

switch (event.get_scroll_direction()) {
case Clutter.ScrollDirection.UP:
volume += this._get_step();
break;
this._show_volume(volume);

case Clutter.ScrollDirection.DOWN:
volume -= this._get_step();
break;
return Clutter.EVENT_STOP;
}

default:
return Clutter.EVENT_PROPAGATE;
}
_handle_sink_change(controller, id) {
this.sink = controller.lookup_stream_id(id);
}

volume = Math.min(volume, this.volume_max);
volume = Math.max(volume, 0);
_show_volume(volume) {
const percentage = volume / this.volume_max;
const iconIndex = volume === 0
? 0
: Math.clamp(Math.floor(3 * percentage + 1), 1, 3);

this.sink.volume = volume;
this.sink.push_volume();
const monitor = -1; // Display volume window on all monitors.
const icon = Gio.Icon.new_for_string(VolumeScrollerIcons[iconIndex]);
const label = this.sink.get_port().human_port;

this._show_volume(volume);
Main.osdWindowManager.show(monitor, icon, label, percentage);
}

return Clutter.EVENT_STOP;
}

_handle_sink_change(controller, id) {
this.sink = controller.lookup_stream_id(id);
}

_show_volume(volume) {
const percentage = volume / this.volume_max;
let n;

if (volume === 0) {
n = 0;
} else {
n = parseInt(3 * percentage + 1);
n = Math.max(1, n);
n = Math.min(3, n);
}

const monitor = -1; // Display volume window on all monitors.
const icon = Gio.Icon.new_for_string(VolumeScrollerIcons[n]);
const label = this.sink.get_port().human_port;

Main.osdWindowManager.show(monitor, icon, label, percentage);
}

_get_step() {
return this.volume_max * this.volume_granularity;
}
_get_step() {
return this.volume_max * this.volume_granularity;
}
}
2 changes: 1 addition & 1 deletion [email protected]/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"url": "https://github.com/francislavoie/gnome-shell-volume-scroller",
"uuid": "[email protected]",
"settings-schema": "org.gnome.shell.extensions.volume-scroller",
"version": 11
"version": 12
}
64 changes: 31 additions & 33 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,37 @@ import Gtk from 'gi://Gtk';
import {ExtensionPreferences} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

export default class VolumeScrollerExtensionPreferences extends ExtensionPreferences {

fillPreferencesWindow(window) {
// Use the same GSettings schema as in `extension.js`
window._settings = this.getSettings('org.gnome.shell.extensions.volume-scroller')

// Create a preferences page and group
const page = new Adw.PreferencesPage();
const group = new Adw.PreferencesGroup();
page.add(group);

// Create a new preferences row
const row = new Adw.ActionRow({ title: 'Granularity' });
group.add(row);

// Create the value picker
const granularityEntry = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({ step_increment: 1, lower: 1, upper: 50 }),
value: window._settings.get_int('granularity'),
valign: Gtk.Align.CENTER,
halign: Gtk.Align.CENTER,
});
window._settings.bind(
'granularity',
granularityEntry,
'value',
Gio.SettingsBindFlags.DEFAULT
);

// Add the value picker to the row
row.add_suffix(granularityEntry);
row.activatable_widget = granularityEntry;

// Add our page to the window
window.add(page);
window._settings = this.getSettings();

// Create a preferences page and group
const page = new Adw.PreferencesPage();
const group = new Adw.PreferencesGroup();
page.add(group);

// Create a new preferences row
const row = new Adw.ActionRow({ title: 'Granularity' });
group.add(row);

// Create the value picker
const granularityEntry = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({ step_increment: 1, lower: 1, upper: 50 }),
value: window._settings.get_int('granularity'),
valign: Gtk.Align.CENTER,
halign: Gtk.Align.CENTER,
});
window._settings.bind(
'granularity',
granularityEntry,
'value',
Gio.SettingsBindFlags.DEFAULT
);

// Add the value picker to the row
row.add_suffix(granularityEntry);
row.activatable_widget = granularityEntry;

// Add our page to the window
window.add(page);
}
}

0 comments on commit 1d49c7a

Please sign in to comment.