Skip to content

Commit 6e1c50e

Browse files
committed
Support MPRIS loop
1 parent b458925 commit 6e1c50e

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

plugins/shortcuts/mpris.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ function registerMPRIS(win) {
3434
let currentSeconds = 0;
3535
ipcMain.on('timeChanged', (_, t) => currentSeconds = t);
3636

37+
let currentLoopStatus = undefined;
38+
let manuallySwitchingStatus = false;
39+
ipcMain.on("repeatChanged", (_, mode) => {
40+
if (manuallySwitchingStatus)
41+
return;
42+
43+
if (mode == "Repeat off")
44+
currentLoopStatus = "None";
45+
else if (mode == "Repeat one")
46+
currentLoopStatus = "Track";
47+
else if (mode == "Repeat all")
48+
currentLoopStatus = "Playlist";
49+
50+
player.loopStatus = currentLoopStatus;
51+
});
52+
player.on("loopStatus", (status) => {
53+
// switchRepeat cycles between states in that order
54+
const switches = ["None", "Playlist", "Track"];
55+
const curIdx = switches.indexOf(currentLoopStatus);
56+
const newIdx = switches.indexOf(status);
57+
58+
// Get a delta in the range [0,2]
59+
const delta = ((newIdx - curIdx) % 3 + 3) % 3;
60+
61+
manuallySwitchingStatus = true;
62+
songControls.switchRepeat(delta);
63+
manuallySwitchingStatus = false;
64+
})
65+
3766
player.getPosition = () => secToMicro(currentSeconds)
3867

3968
player.on("raise", () => {

providers/song-controls.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ module.exports = (win) => {
2020
go1sBack: () => pressKey(win, "h", ["shift"]),
2121
go1sForward: () => pressKey(win, "l", ["shift"]),
2222
shuffle: () => pressKey(win, "s"),
23-
switchRepeat: () => pressKey(win, "r"),
23+
switchRepeat: (n = 1) => {
24+
for (let i = 0; i != n; ++i)
25+
pressKey(win, "r");
26+
},
2427
// General
2528
volumeMinus10: () => pressKey(win, "-"),
2629
volumePlus10: () => pressKey(win, "="),

providers/song-info-front.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = () => {
2222
if (config.plugins.isEnabled('tuna-obs') ||
2323
(is.linux() && config.plugins.isEnabled('shortcuts'))) {
2424
setupTimeChangeListener();
25+
setupRepeatChangeListener();
2526
}
2627
const video = $('video');
2728
// name = "dataloaded" and abit later "dataupdated"
@@ -63,3 +64,13 @@ function setupTimeChangeListener() {
6364
});
6465
progressObserver.observe($('#progress-bar'), { attributeFilter: ["value"] })
6566
}
67+
68+
function setupRepeatChangeListener() {
69+
const repeatObserver = new MutationObserver(mutations => {
70+
ipcRenderer.send('repeatChanged', mutations[0].target.title);
71+
});
72+
repeatObserver.observe($('#right-controls .repeat'), { attributeFilter: ["title"] });
73+
74+
// Emit the initial value as well; as it's persistent between launches.
75+
ipcRenderer.send('repeatChanged', $('#right-controls .repeat').title);
76+
}

0 commit comments

Comments
 (0)