Files
youtube-music/plugins/shortcuts/back.js
Manish 88ee0fb989 fix: mpris was not registering itself before.
Sorry I missed that somehow, because playerctl controls were working.
That was because of chromium was also registering itself for mpris.
2021-10-13 12:26:38 +05:30

93 lines
2.6 KiB
JavaScript

const { globalShortcut } = require("electron");
const is = require("electron-is");
const electronLocalshortcut = require("electron-localshortcut");
const getSongControls = require("../../providers/song-controls");
const { setupMPRIS } = require("./mpris");
const registerCallback = require("../../providers/song-info");
let player;
function _registerGlobalShortcut(webContents, shortcut, action) {
globalShortcut.register(shortcut, () => {
action(webContents);
});
}
function _registerLocalShortcut(win, shortcut, action) {
electronLocalshortcut.register(win, shortcut, () => {
action(win.webContents);
});
}
function registerShortcuts(win, options) {
const songControls = getSongControls(win);
const { playPause, next, previous, search } = songControls;
_registerGlobalShortcut(win.webContents, "MediaPlayPause", playPause);
_registerGlobalShortcut(win.webContents, "MediaNextTrack", next);
_registerGlobalShortcut(win.webContents, "MediaPreviousTrack", previous);
_registerLocalShortcut(win, "CommandOrControl+F", search);
_registerLocalShortcut(win, "CommandOrControl+L", search);
registerCallback(songInfo => {
player.metadata = {
'mpris:length': songInfo.songDuration * 60 * 1000 * 1000, // In microseconds
'mpris:artUrl': songInfo.imageSrc,
'xesam:title': songInfo.title,
'xesam:artist': songInfo.artist
};
}
)
if (is.linux()) {
try {
const MPRISPlayer = setupMPRIS();
player = MPRISPlayer
MPRISPlayer.on("raise", () => {
win.setSkipTaskbar(false);
win.show();
});
MPRISPlayer.on("play", () => {
console.log('Event:', "play", arguments);
player.playbackStatus = 'Playing';
playPause()
});
MPRISPlayer.on("pause", () => {
console.log('Event:', "pause", arguments);
player.playbackStatus = 'Paused';
playPause()
});
MPRISPlayer.on("next", () => {
next()
});
MPRISPlayer.on("previous", () => {
previous()
});
} catch (e) {
console.warn("Error in MPRIS", e);
}
}
const { global, local } = options;
(global || []).forEach(({ shortcut, action }) => {
console.debug("Registering global shortcut", shortcut, ":", action);
if (!action || !songControls[action]) {
console.warn("Invalid action", action);
return;
}
_registerGlobalShortcut(win.webContents, shortcut, songControls[action]);
});
(local || []).forEach(({ shortcut, action }) => {
console.debug("Registering local shortcut", shortcut, ":", action);
if (!action || !songControls[action]) {
console.warn("Invalid action", action);
return;
}
_registerLocalShortcut(win, shortcut, songControls[action]);
});
}
module.exports = registerShortcuts;