mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 18:41:47 +00:00
save temp icons for file:/// protocol
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
const { notificationImage, icons } = require("./utils");
|
||||
const { notificationImage, icons, save_temp_icons } = require("./utils");
|
||||
const getSongControls = require('../../providers/song-controls');
|
||||
const registerCallback = require("../../providers/song-info");
|
||||
const { changeProtocolHandler } = require("../../providers/protocol-handler");
|
||||
|
||||
const { Notification } = require("electron");
|
||||
const { Notification, app } = require("electron");
|
||||
const path = require('path');
|
||||
|
||||
let songControls;
|
||||
@ -13,19 +13,21 @@ let savedNotification;
|
||||
module.exports = (win, _config) => {
|
||||
songControls = getSongControls(win);
|
||||
config = _config;
|
||||
if (app.isPackaged && !config.smallInteractive) save_temp_icons();
|
||||
|
||||
let lastSongInfo = { url: undefined };
|
||||
|
||||
// Register songInfoCallback
|
||||
registerCallback((songInfo, cause) => {
|
||||
registerCallback(songInfo => {
|
||||
if (!songInfo.isPaused && (songInfo.url !== lastSongInfo.url || config.unpauseNotification)) {
|
||||
lastSongInfo = { ...songInfo };
|
||||
sendXML(songInfo);
|
||||
}
|
||||
});
|
||||
|
||||
win.webContents.once("closed", () => {
|
||||
savedNotification = undefined;
|
||||
//TODO on app before close, close notification
|
||||
app.once("before-quit", () => {
|
||||
savedNotification?.close();
|
||||
});
|
||||
|
||||
changeProtocolHandler(
|
||||
@ -42,7 +44,6 @@ module.exports = (win, _config) => {
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
function sendXML(songInfo) {
|
||||
const imgSrc = notificationImage(songInfo, true);
|
||||
|
||||
@ -63,7 +64,7 @@ function sendXML(songInfo) {
|
||||
<binding template="ToastImageAndText02">
|
||||
<image id="1" src="${imgSrc}" name="Image" />
|
||||
<text id="1">${songInfo.title}</text>
|
||||
<text id="2">${songInfo.artist}}</text>
|
||||
<text id="2">${songInfo.artist}</text>
|
||||
</binding>
|
||||
</visual>
|
||||
|
||||
@ -82,10 +83,35 @@ function sendXML(songInfo) {
|
||||
savedNotification.show();
|
||||
}
|
||||
|
||||
const iconLocation = app.isPackaged ?
|
||||
path.resolve(app.getPath("userData"), 'icons') :
|
||||
path.resolve(__dirname, '..', '..', 'assets/media-icons-black');
|
||||
|
||||
|
||||
const getButton = (kind) =>
|
||||
`<action ${display(kind)} activationType="protocol" arguments="youtubemusic://${kind}"/>`;
|
||||
|
||||
const display = (kind) =>
|
||||
config.smallInteractive ?
|
||||
`content="${icons[kind]}"` :
|
||||
`content="" imageUri="file:///${path.resolve(__dirname, "../../assets/media-icons-black", `${kind}.png`)}"`;
|
||||
`content="" imageUri="file:///${path.resolve(__dirname, iconLocation, `${kind}.png`)}"`;
|
||||
|
||||
|
||||
// TODO MAKE DIFFERENT TEMPLATES
|
||||
const xml = (songInfo, options) => {
|
||||
const xml = `
|
||||
<toast displayTimestamp="2018-01-05T13:35:00Z">
|
||||
<visual>
|
||||
<binding template="ToastGeneric">
|
||||
<text id="1">Header Text</text>
|
||||
<text id="2">Body Text</text>
|
||||
<text id="3">Body 2 Text</text>
|
||||
<text placement="attribution">Attribution Text</text>
|
||||
<image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC2C.tmp4e9214ef-f478-4cea-972a-3fdd6c3acac0.png" placement="appLogoOverride" hint-crop="circle" />
|
||||
<image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC2D.tmpeb4a5986-fd2a-4d7d-a69d-a78f0061d754.png" placement="hero" />
|
||||
<image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC1B.tmp43598461-7e59-4600-a95c-88edbc57b2ec.png" />
|
||||
</binding>
|
||||
</visual>
|
||||
</toast>
|
||||
`;
|
||||
}
|
||||
|
||||
@ -4,7 +4,8 @@ const { app } = require("electron");
|
||||
const fs = require("fs");
|
||||
|
||||
const icon = "assets/youtube-music.png";
|
||||
const tempIcon = path.join(app.getPath("userData"), "tempIcon.png");
|
||||
const userData = app.getPath("userData");
|
||||
const tempIcon = path.join(userData, "tempIcon.png");
|
||||
|
||||
module.exports.icons = {
|
||||
play: "\u{1405}", // ᐅ
|
||||
@ -24,7 +25,7 @@ module.exports.urgencyLevels = [
|
||||
{ name: "High", value: "critical" },
|
||||
];
|
||||
|
||||
module.exports.notificationImage = function (songInfo, saveIcon = false) {
|
||||
module.exports.notificationImage = (songInfo, saveIcon = false) => {
|
||||
//return local path to temp icon
|
||||
if (saveIcon && !!songInfo.image) {
|
||||
try {
|
||||
@ -44,6 +45,16 @@ module.exports.notificationImage = function (songInfo, saveIcon = false) {
|
||||
: icon
|
||||
};
|
||||
|
||||
module.exports.save_temp_icons = () => {
|
||||
for (const kind of Object.keys(module.exports.icons)) {
|
||||
const destinationPath = path.join(userData, 'icons', `${kind}.png`);
|
||||
if (fs.existsSync(destinationPath)) continue;
|
||||
const iconPath = path.resolve(__dirname, "../../assets/media-icons-black", `${kind}.png`);
|
||||
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
|
||||
fs.copyFile(iconPath, destinationPath, ()=>{});
|
||||
}
|
||||
};
|
||||
|
||||
function centerNativeImage(nativeImage) {
|
||||
const tempImage = nativeImage.resize({ height: 256 });
|
||||
const margin = Math.max((tempImage.getSize().width - 256), 0);
|
||||
|
||||
Reference in New Issue
Block a user