Fixed recursive volume changes that caused cpu spike, Switched Repeat Modes to NONE|ONE|ALL

This commit is contained in:
David Metzler
2023-02-03 12:02:25 +01:00
parent a81476100b
commit 9da0e4305f
2 changed files with 27 additions and 9 deletions

View File

@ -36,11 +36,11 @@ function registerMPRIS(win) {
ipcMain.on('timeChanged', (_, t) => currentSeconds = t);
ipcMain.on("repeatChanged", (_, mode) => {
if (mode === "Repeat off")
if (mode === "NONE")
player.loopStatus = mpris.LOOP_STATUS_NONE;
else if (mode === "Repeat one") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons
else if (mode === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons
player.loopStatus = mpris.LOOP_STATUS_PLAYLIST;
else if (mode === "Repeat all")
else if (mode === "ALL")
player.loopStatus = mpris.LOOP_STATUS_TRACK;
});
player.on("loopStatus", (status) => {
@ -88,14 +88,33 @@ function registerMPRIS(win) {
shuffle();
});
ipcMain.on('volumeChanged', (_, value) => {
player.volume = value / 100;
let mprisVolNewer = false;
let autoUpdate = false;
ipcMain.on('volumeChanged', (_, newVol) => {
if (parseInt(player.volume * 100) !== newVol) {
if (mprisVolNewer) {
mprisVolNewer = false;
autoUpdate = false;
} else {
autoUpdate = true;
player.volume = parseFloat((newVol / 100).toFixed(2));
mprisVolNewer = false;
autoUpdate = false;
}
}
});
player.on('volume', (newVolume) => {
if (config.plugins.isEnabled('precise-volume')) {
// With precise volume we can set the volume to the exact value.
win.webContents.send('setVolume', newVolume * 100)
let newVol = parseInt(newVolume * 100);
if (parseInt(player.volume * 100) !== newVol) {
if (!autoUpdate){
mprisVolNewer = true;
autoUpdate = false;
win.webContents.send('setVolume', newVol);
}
}
} else {
// With keyboard shortcuts we can only change the volume in increments of 10, so round it.
let deltaVolume = Math.round((newVolume - player.volume) * 10);

View File

@ -67,14 +67,13 @@ function setupTimeChangeListener() {
}
function setupRepeatChangeListener() {
const mp = { NONE: "Repeat off", ONE: "Repeat one", ALL: "Repeat all" }
const repeatObserver = new MutationObserver(mutations => {
ipcRenderer.send('repeatChanged', mp[mutations[0].target.__dataHost.getState().queue.repeatMode])
ipcRenderer.send('repeatChanged', mutations[0].target.__dataHost.getState().queue.repeatMode)
});
repeatObserver.observe($('#right-controls .repeat'), {attributeFilter: ["title"]});
// Emit the initial value as well; as it's persistent between launches.
ipcRenderer.send('repeatChanged', mp[$('ytmusic-player-bar').getState().queue.repeatMode]);
ipcRenderer.send('repeatChanged', $('ytmusic-player-bar').getState().queue.repeatMode);
}
function setupVolumeChangeListener(api) {