mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
Add 2 more config options
refreshOnPlayPause: false trayControls: true,
This commit is contained in:
@ -58,6 +58,8 @@ const defaultConfig = {
|
|||||||
// the following has effect only on Windows
|
// the following has effect only on Windows
|
||||||
interactive: true,
|
interactive: true,
|
||||||
toastStyle: 1, // see plugins/notifications/utils for more info
|
toastStyle: 1, // see plugins/notifications/utils for more info
|
||||||
|
refreshOnPlayPause: false,
|
||||||
|
trayControls: true,
|
||||||
hideButtonText: false
|
hideButtonText: false
|
||||||
},
|
},
|
||||||
"precise-volume": {
|
"precise-volume": {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ const { notificationImage, icons, save_temp_icons, secondsToMinutes, ToastStyles
|
|||||||
const getSongControls = require('../../providers/song-controls');
|
const getSongControls = require('../../providers/song-controls');
|
||||||
const registerCallback = require("../../providers/song-info");
|
const registerCallback = require("../../providers/song-info");
|
||||||
const { changeProtocolHandler } = require("../../providers/protocol-handler");
|
const { changeProtocolHandler } = require("../../providers/protocol-handler");
|
||||||
|
const { setTrayOnClick, setTrayOnDoubleClick } = require("../../tray");
|
||||||
|
|
||||||
const { Notification, app, ipcMain } = require("electron");
|
const { Notification, app, ipcMain } = require("electron");
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@ -22,31 +23,59 @@ module.exports = (win) => {
|
|||||||
|
|
||||||
if (app.isPackaged) save_temp_icons();
|
if (app.isPackaged) save_temp_icons();
|
||||||
|
|
||||||
let lastSongInfo = { url: undefined };
|
let savedSongInfo;
|
||||||
|
let lastUrl;
|
||||||
|
|
||||||
// Register songInfoCallback
|
// Register songInfoCallback
|
||||||
registerCallback(songInfo => {
|
registerCallback(songInfo => {
|
||||||
|
savedSongInfo = { ...songInfo };
|
||||||
if (!songInfo.isPaused &&
|
if (!songInfo.isPaused &&
|
||||||
(songInfo.url !== lastSongInfo.url || config.get("unpauseNotification"))
|
(songInfo.url !== lastUrl || config.get("unpauseNotification"))
|
||||||
) {
|
) {
|
||||||
lastSongInfo = { ...songInfo };
|
lastUrl = songInfo.url
|
||||||
sendNotification(songInfo);
|
sendNotification(songInfo);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO EXPERIMENTAL
|
||||||
|
if (config.get("trayControls")) {
|
||||||
|
setTrayOnClick(() => {
|
||||||
|
if (savedNotification) {
|
||||||
|
savedNotification.close();
|
||||||
|
savedNotification = undefined;
|
||||||
|
} else if (savedSongInfo) {
|
||||||
|
sendNotification({
|
||||||
|
...savedSongInfo,
|
||||||
|
elapsedSeconds: currentSeconds
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTrayOnDoubleClick(() => {
|
||||||
|
if (win.isVisible()) {
|
||||||
|
win.hide();
|
||||||
|
} else win.show();
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
app.once("before-quit", () => {
|
app.once("before-quit", () => {
|
||||||
savedNotification?.close();
|
savedNotification?.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
changeProtocolHandler(
|
changeProtocolHandler(
|
||||||
(cmd) => {
|
(cmd) => {
|
||||||
if (Object.keys(songControls).includes(cmd)) {
|
if (Object.keys(songControls).includes(cmd)) {
|
||||||
songControls[cmd]();
|
songControls[cmd]();
|
||||||
if (cmd === 'pause' || (cmd === 'play' && !config.get("unpauseNotification"))) {
|
if (config.get("refreshOnPlayPause") && (
|
||||||
|
cmd === 'pause' ||
|
||||||
|
(cmd === 'play' && !config.get("unpauseNotification"))
|
||||||
|
)
|
||||||
|
) {
|
||||||
setImmediate(() =>
|
setImmediate(() =>
|
||||||
sendNotification({
|
sendNotification({
|
||||||
...lastSongInfo,
|
...savedSongInfo,
|
||||||
isPaused: cmd === 'pause',
|
isPaused: cmd === 'pause',
|
||||||
elapsedSeconds: currentSeconds
|
elapsedSeconds: currentSeconds
|
||||||
})
|
})
|
||||||
@ -82,7 +111,7 @@ function sendNotification(songInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const get_xml = (songInfo, iconSrc) => {
|
const get_xml = (songInfo, iconSrc) => {
|
||||||
switch (config.get("style")) {
|
switch (config.get("toastStyle")) {
|
||||||
default:
|
default:
|
||||||
case ToastStyles.logo:
|
case ToastStyles.logo:
|
||||||
case ToastStyles.legacy:
|
case ToastStyles.legacy:
|
||||||
@ -105,11 +134,11 @@ const iconLocation = app.isPackaged ?
|
|||||||
path.resolve(__dirname, '..', '..', 'assets/media-icons-black');
|
path.resolve(__dirname, '..', '..', 'assets/media-icons-black');
|
||||||
|
|
||||||
const display = (kind) => {
|
const display = (kind) => {
|
||||||
if (config.get("style") === ToastStyles.legacy ) {
|
if (config.get("toastStyle") === ToastStyles.legacy ) {
|
||||||
return `content="${icons[kind]}"`;
|
return `content="${icons[kind]}"`;
|
||||||
} else {
|
} else {
|
||||||
return `\
|
return `\
|
||||||
content="${kind.charAt(0).toUpperCase() + kind.slice(1)}"\
|
content="${config.get("hideButtonText") ? "" : kind.charAt(0).toUpperCase() + kind.slice(1)}"\
|
||||||
imageUri="file:///${path.resolve(__dirname, iconLocation, `${kind}.png`)}"
|
imageUri="file:///${path.resolve(__dirname, iconLocation, `${kind}.png`)}"
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,30 @@ module.exports = (_win, options) => [
|
|||||||
// doesn't update until restart
|
// doesn't update until restart
|
||||||
click: (item) => config.setAndMaybeRestart("interactive", item.checked),
|
click: (item) => config.setAndMaybeRestart("interactive", item.checked),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// submenu with settings for interactive notifications (name shouldn't be too long)
|
||||||
|
label: "Interactive Settings",
|
||||||
|
submenu: [
|
||||||
|
{
|
||||||
|
label: "Open/Close on tray click",
|
||||||
|
type: "checkbox",
|
||||||
|
checked: options.trayControls,
|
||||||
|
click: (item) => config.set("trayControls", item.checked),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Hide Button Text",
|
||||||
|
type: "checkbox",
|
||||||
|
checked: options.hideButtonText,
|
||||||
|
click: (item) => config.set("hideButtonText", item.checked),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Refresh on Play/Pause",
|
||||||
|
type: "checkbox",
|
||||||
|
checked: options.refreshOnPlayPause,
|
||||||
|
click: (item) => config.set("refreshOnPlayPause", item.checked),
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: "Toast Style",
|
label: "Toast Style",
|
||||||
submenu: getToastStyleMenuItems(options)
|
submenu: getToastStyleMenuItems(options)
|
||||||
@ -40,20 +64,11 @@ module.exports = (_win, options) => [
|
|||||||
];
|
];
|
||||||
|
|
||||||
function getToastStyleMenuItems(options) {
|
function getToastStyleMenuItems(options) {
|
||||||
const arr = new Array(Object.keys(ToastStyles).length + 2);
|
const arr = new Array(Object.keys(ToastStyles).length);
|
||||||
|
|
||||||
arr[0] = {
|
|
||||||
label: "Hide Button Text",
|
|
||||||
type: "checkbox",
|
|
||||||
checked: options.hideButtonText,
|
|
||||||
click: (item) => config.set("hideButtonText", item.checked),
|
|
||||||
}
|
|
||||||
|
|
||||||
arr[1] = { type: "separator" };
|
|
||||||
|
|
||||||
// ToastStyles index starts from 1
|
// ToastStyles index starts from 1
|
||||||
for (const [name, index] of Object.entries(ToastStyles)) {
|
for (const [name, index] of Object.entries(ToastStyles)) {
|
||||||
arr[index + 1] = {
|
arr[index - 1] = {
|
||||||
label: snakeToCamel(name),
|
label: snakeToCamel(name),
|
||||||
type: "radio",
|
type: "radio",
|
||||||
checked: options.style === index,
|
checked: options.style === index,
|
||||||
|
|||||||
@ -35,7 +35,7 @@ module.exports.notificationImage = (songInfo) => {
|
|||||||
if (!songInfo.image) return icon;
|
if (!songInfo.image) return icon;
|
||||||
if (!config.get("interactive")) return nativeImageToLogo(songInfo.image);
|
if (!config.get("interactive")) return nativeImageToLogo(songInfo.image);
|
||||||
|
|
||||||
switch(config.get("style")) {
|
switch(config.get("toastStyle")) {
|
||||||
case module.exports.ToastStyles.logo:
|
case module.exports.ToastStyles.logo:
|
||||||
case module.exports.ToastStyles.legacy:
|
case module.exports.ToastStyles.legacy:
|
||||||
return this.saveImage(nativeImageToLogo(songInfo.image), tempIcon);
|
return this.saveImage(nativeImageToLogo(songInfo.image), tempIcon);
|
||||||
|
|||||||
23
tray.js
23
tray.js
@ -1,14 +1,29 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const { app, Menu, nativeImage, Tray } = require("electron");
|
const { Menu, nativeImage, Tray } = require("electron");
|
||||||
|
|
||||||
const { restart } = require("./providers/app-controls");
|
const { restart } = require("./providers/app-controls");
|
||||||
const config = require("./config");
|
const config = require("./config");
|
||||||
const getSongControls = require("./providers/song-controls");
|
const getSongControls = require("./providers/song-controls");
|
||||||
|
|
||||||
// Prevent tray being garbage collected
|
// Prevent tray being garbage collected
|
||||||
|
|
||||||
|
/** @type {Electron.Tray} */
|
||||||
let tray;
|
let tray;
|
||||||
|
|
||||||
|
module.exports.setTrayOnClick = (fn) => {
|
||||||
|
if (!tray) return;
|
||||||
|
tray.removeAllListeners('click');
|
||||||
|
tray.on("click", fn);
|
||||||
|
};
|
||||||
|
|
||||||
|
// wont do anything on macos since its disabled
|
||||||
|
module.exports.setTrayOnDoubleClick = (fn) => {
|
||||||
|
if (!tray) return;
|
||||||
|
tray.removeAllListeners('double-click');
|
||||||
|
tray.on("double-click", fn);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.setUpTray = (app, win) => {
|
module.exports.setUpTray = (app, win) => {
|
||||||
if (!config.get("options.tray")) {
|
if (!config.get("options.tray")) {
|
||||||
tray = undefined;
|
tray = undefined;
|
||||||
@ -17,13 +32,19 @@ module.exports.setUpTray = (app, win) => {
|
|||||||
|
|
||||||
const { playPause, next, previous } = getSongControls(win);
|
const { playPause, next, previous } = getSongControls(win);
|
||||||
const iconPath = path.join(__dirname, "assets", "youtube-music-tray.png");
|
const iconPath = path.join(__dirname, "assets", "youtube-music-tray.png");
|
||||||
|
|
||||||
let trayIcon = nativeImage.createFromPath(iconPath).resize({
|
let trayIcon = nativeImage.createFromPath(iconPath).resize({
|
||||||
width: 16,
|
width: 16,
|
||||||
height: 16,
|
height: 16,
|
||||||
});
|
});
|
||||||
|
|
||||||
tray = new Tray(trayIcon);
|
tray = new Tray(trayIcon);
|
||||||
|
|
||||||
tray.setToolTip("Youtube Music");
|
tray.setToolTip("Youtube Music");
|
||||||
|
|
||||||
|
// macOS only
|
||||||
tray.setIgnoreDoubleClickEvents(true);
|
tray.setIgnoreDoubleClickEvents(true);
|
||||||
|
|
||||||
tray.on("click", () => {
|
tray.on("click", () => {
|
||||||
if (config.get("options.trayClickPlayPause")) {
|
if (config.get("options.trayClickPlayPause")) {
|
||||||
playPause();
|
playPause();
|
||||||
|
|||||||
Reference in New Issue
Block a user