From dfba3d9c2d9aba0751150ca1558010267b81252e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Mon, 11 Jul 2022 20:20:13 +0200 Subject: [PATCH] Support precise volume changes in MPRIS when possible --- plugins/precise-volume/front.js | 26 ++++++++++++++---------- plugins/shortcuts/mpris.js | 36 +++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/plugins/precise-volume/front.js b/plugins/precise-volume/front.js index 0bf3e36d..38a2a60b 100644 --- a/plugins/precise-volume/front.js +++ b/plugins/precise-volume/front.js @@ -11,6 +11,7 @@ module.exports = (_options) => { document.addEventListener('apiLoaded', e => { api = e.detail; ipcRenderer.on('changeVolume', (_, toIncrease) => changeVolume(toIncrease)); + ipcRenderer.on('setVolume', (_, value) => setVolume(value)); firstRun(); }, { once: true, passive: true }) }; @@ -163,26 +164,29 @@ function setupSliderObserver() { }); } -/** if (toIncrease = false) then volume decrease */ -function changeVolume(toIncrease) { - // Apply volume change if valid - const steps = Number(options.steps || 1); - api.setVolume(toIncrease ? - Math.min(api.getVolume() + steps, 100) : - Math.max(api.getVolume() - steps, 0)); - +function setVolume(value) { + api.setVolume(value); // Save the new volume - saveVolume(api.getVolume()); + saveVolume(value); // change slider position (important) updateVolumeSlider(); // Change tooltips to new value - setTooltip(options.savedVolume); + setTooltip(value); // Show volume slider showVolumeSlider(); // Show volume HUD - showVolumeHud(options.savedVolume); + showVolumeHud(value); +} + +/** if (toIncrease = false) then volume decrease */ +function changeVolume(toIncrease) { + // Apply volume change if valid + const steps = Number(options.steps || 1); + setVolume(toIncrease ? + Math.min(api.getVolume() + steps, 100) : + Math.max(api.getVolume() - steps, 0)); } function updateVolumeSlider() { diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index 3bb256d0..bf427b58 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -2,6 +2,7 @@ const mpris = require("mpris-service"); const { ipcMain } = require("electron"); const registerCallback = require("../../providers/song-info"); const getSongControls = require("../../providers/song-controls"); +const config = require("../../config"); function setupMPRIS() { const player = mpris({ @@ -97,25 +98,30 @@ function registerMPRIS(win) { player.volume = value; }); player.on('volume', (newVolume) => { - // With keyboard shortcuts we can only change the volume in increments of 10, so round it. - const deltaVolume = Math.round((newVolume - player.volume) / 10); - - if (deltaVolume > 0) { - for (let i = 0; i < deltaVolume; i++) - volumePlus10(); + if (config.plugins.isEnabled('precise-volume')) { + // With precise volume we can set the volume to the exact value. + win.webContents.send('setVolume', newVolume) } else { - for (let i = 0; i < -deltaVolume; i++) - volumeMinus10(); + // With keyboard shortcuts we can only change the volume in increments of 10, so round it. + const deltaVolume = Math.round((newVolume - player.volume) / 10); + + if (deltaVolume > 0) { + for (let i = 0; i < deltaVolume; i++) + volumePlus10(); + } else { + for (let i = 0; i < -deltaVolume; i++) + volumeMinus10(); + } } }); - registerCallback(songInfo => { - if (player) { - const data = { - 'mpris:length': secToMicro(songInfo.songDuration), - 'mpris:artUrl': songInfo.imageSrc, - 'xesam:title': songInfo.title, - 'xesam:artist': [songInfo.artist], + registerCallback(songInfo => { + if (player) { + const data = { + 'mpris:length': secToMicro(songInfo.songDuration), + 'mpris:artUrl': songInfo.imageSrc, + 'xesam:title': songInfo.title, + 'xesam:artist': [songInfo.artist], 'mpris:trackid': '/' }; if (songInfo.album) data['xesam:album'] = songInfo.album;