Skip to content

Commit dfba3d9

Browse files
committed
Support precise volume changes in MPRIS when possible
1 parent d9c5106 commit dfba3d9

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

plugins/precise-volume/front.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = (_options) => {
1111
document.addEventListener('apiLoaded', e => {
1212
api = e.detail;
1313
ipcRenderer.on('changeVolume', (_, toIncrease) => changeVolume(toIncrease));
14+
ipcRenderer.on('setVolume', (_, value) => setVolume(value));
1415
firstRun();
1516
}, { once: true, passive: true })
1617
};
@@ -163,26 +164,29 @@ function setupSliderObserver() {
163164
});
164165
}
165166

166-
/** if (toIncrease = false) then volume decrease */
167-
function changeVolume(toIncrease) {
168-
// Apply volume change if valid
169-
const steps = Number(options.steps || 1);
170-
api.setVolume(toIncrease ?
171-
Math.min(api.getVolume() + steps, 100) :
172-
Math.max(api.getVolume() - steps, 0));
173-
167+
function setVolume(value) {
168+
api.setVolume(value);
174169
// Save the new volume
175-
saveVolume(api.getVolume());
170+
saveVolume(value);
176171

177172
// change slider position (important)
178173
updateVolumeSlider();
179174

180175
// Change tooltips to new value
181-
setTooltip(options.savedVolume);
176+
setTooltip(value);
182177
// Show volume slider
183178
showVolumeSlider();
184179
// Show volume HUD
185-
showVolumeHud(options.savedVolume);
180+
showVolumeHud(value);
181+
}
182+
183+
/** if (toIncrease = false) then volume decrease */
184+
function changeVolume(toIncrease) {
185+
// Apply volume change if valid
186+
const steps = Number(options.steps || 1);
187+
setVolume(toIncrease ?
188+
Math.min(api.getVolume() + steps, 100) :
189+
Math.max(api.getVolume() - steps, 0));
186190
}
187191

188192
function updateVolumeSlider() {

plugins/shortcuts/mpris.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const mpris = require("mpris-service");
22
const { ipcMain } = require("electron");
33
const registerCallback = require("../../providers/song-info");
44
const getSongControls = require("../../providers/song-controls");
5+
const config = require("../../config");
56

67
function setupMPRIS() {
78
const player = mpris({
@@ -97,25 +98,30 @@ function registerMPRIS(win) {
9798
player.volume = value;
9899
});
99100
player.on('volume', (newVolume) => {
100-
// With keyboard shortcuts we can only change the volume in increments of 10, so round it.
101-
const deltaVolume = Math.round((newVolume - player.volume) / 10);
102-
103-
if (deltaVolume > 0) {
104-
for (let i = 0; i < deltaVolume; i++)
105-
volumePlus10();
101+
if (config.plugins.isEnabled('precise-volume')) {
102+
// With precise volume we can set the volume to the exact value.
103+
win.webContents.send('setVolume', newVolume)
106104
} else {
107-
for (let i = 0; i < -deltaVolume; i++)
108-
volumeMinus10();
105+
// With keyboard shortcuts we can only change the volume in increments of 10, so round it.
106+
const deltaVolume = Math.round((newVolume - player.volume) / 10);
107+
108+
if (deltaVolume > 0) {
109+
for (let i = 0; i < deltaVolume; i++)
110+
volumePlus10();
111+
} else {
112+
for (let i = 0; i < -deltaVolume; i++)
113+
volumeMinus10();
114+
}
109115
}
110116
});
111117

112-
registerCallback(songInfo => {
113-
if (player) {
114-
const data = {
115-
'mpris:length': secToMicro(songInfo.songDuration),
116-
'mpris:artUrl': songInfo.imageSrc,
117-
'xesam:title': songInfo.title,
118-
'xesam:artist': [songInfo.artist],
118+
registerCallback(songInfo => {
119+
if (player) {
120+
const data = {
121+
'mpris:length': secToMicro(songInfo.songDuration),
122+
'mpris:artUrl': songInfo.imageSrc,
123+
'xesam:title': songInfo.title,
124+
'xesam:artist': [songInfo.artist],
119125
'mpris:trackid': '/'
120126
};
121127
if (songInfo.album) data['xesam:album'] = songInfo.album;

0 commit comments

Comments
 (0)