Support MPRIS loop

This commit is contained in:
Jonathan Müller
2022-06-14 15:45:58 +02:00
parent b458925aa6
commit 6e1c50ede1
3 changed files with 44 additions and 1 deletions

View File

@ -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", () => {

View File

@ -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, "="),

View File

@ -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);
}