diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index 973feb51..7c656624 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -34,6 +34,35 @@ function registerMPRIS(win) { let currentSeconds = 0; ipcMain.on('timeChanged', (_, t) => currentSeconds = t); + let currentLoopStatus = undefined; + let manuallySwitchingStatus = false; + ipcMain.on("repeatChanged", (_, mode) => { + if (manuallySwitchingStatus) + return; + + if (mode == "Repeat off") + currentLoopStatus = "None"; + else if (mode == "Repeat one") + currentLoopStatus = "Track"; + else if (mode == "Repeat all") + currentLoopStatus = "Playlist"; + + player.loopStatus = currentLoopStatus; + }); + player.on("loopStatus", (status) => { + // switchRepeat cycles between states in that order + const switches = ["None", "Playlist", "Track"]; + const curIdx = switches.indexOf(currentLoopStatus); + const newIdx = switches.indexOf(status); + + // Get a delta in the range [0,2] + const delta = ((newIdx - curIdx) % 3 + 3) % 3; + + manuallySwitchingStatus = true; + songControls.switchRepeat(delta); + manuallySwitchingStatus = false; + }) + player.getPosition = () => secToMicro(currentSeconds) player.on("raise", () => { diff --git a/providers/song-controls.js b/providers/song-controls.js index d4659504..bd9d048e 100644 --- a/providers/song-controls.js +++ b/providers/song-controls.js @@ -20,7 +20,10 @@ module.exports = (win) => { go1sBack: () => pressKey(win, "h", ["shift"]), go1sForward: () => pressKey(win, "l", ["shift"]), shuffle: () => pressKey(win, "s"), - switchRepeat: () => pressKey(win, "r"), + switchRepeat: (n = 1) => { + for (let i = 0; i != n; ++i) + pressKey(win, "r"); + }, // General volumeMinus10: () => pressKey(win, "-"), volumePlus10: () => pressKey(win, "="), diff --git a/providers/song-info-front.js b/providers/song-info-front.js index e8bb8bcf..3a704598 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -22,6 +22,7 @@ module.exports = () => { if (config.plugins.isEnabled('tuna-obs') || (is.linux() && config.plugins.isEnabled('shortcuts'))) { setupTimeChangeListener(); + setupRepeatChangeListener(); } const video = $('video'); // name = "dataloaded" and abit later "dataupdated" @@ -63,3 +64,13 @@ function setupTimeChangeListener() { }); progressObserver.observe($('#progress-bar'), { attributeFilter: ["value"] }) } + +function setupRepeatChangeListener() { + const repeatObserver = new MutationObserver(mutations => { + ipcRenderer.send('repeatChanged', mutations[0].target.title); + }); + repeatObserver.observe($('#right-controls .repeat'), { attributeFilter: ["title"] }); + + // Emit the initial value as well; as it's persistent between launches. + ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title); +}