From 1d49c7ae7e5d8bcea46762fca83848ab17765dca Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 13 Nov 2023 04:33:35 -0500 Subject: [PATCH] Adjustments/modernization --- .editorconfig | 12 ++ .github/workflows/main.yml | 2 +- .../extension.js | 180 +++++++++--------- .../metadata.json | 2 +- .../prefs.js | 64 +++---- 5 files changed, 132 insertions(+), 128 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..65b7749 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 96afa07..bf3d872 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Version id: version diff --git a/volume_scroller@francislavoie.github.io/extension.js b/volume_scroller@francislavoie.github.io/extension.js index 927b1f0..c69ad9f 100644 --- a/volume_scroller@francislavoie.github.io/extension.js +++ b/volume_scroller@francislavoie.github.io/extension.js @@ -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; + } } diff --git a/volume_scroller@francislavoie.github.io/metadata.json b/volume_scroller@francislavoie.github.io/metadata.json index 52435cc..45871c1 100644 --- a/volume_scroller@francislavoie.github.io/metadata.json +++ b/volume_scroller@francislavoie.github.io/metadata.json @@ -8,5 +8,5 @@ "url": "https://github.com/francislavoie/gnome-shell-volume-scroller", "uuid": "volume_scroller@francislavoie.github.io", "settings-schema": "org.gnome.shell.extensions.volume-scroller", - "version": 11 + "version": 12 } diff --git a/volume_scroller@francislavoie.github.io/prefs.js b/volume_scroller@francislavoie.github.io/prefs.js index 065106c..b25190c 100644 --- a/volume_scroller@francislavoie.github.io/prefs.js +++ b/volume_scroller@francislavoie.github.io/prefs.js @@ -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); } }