System tray support

pull/93/head
Andrew 3 years ago
parent 6062db13cb
commit 2a203c4755

@ -154,6 +154,12 @@
<span id="maxTxt">Maximum number of active downloads</span>
<input type="number" min="1" class="input" id="maxDownloads" value="5">
</div>
<br>
<div class="prefBox">
<span id="trayTxt">Close to system tray</span>
<input type="checkbox" id="closeToTray">
</div>
</body>
</html>

@ -1,13 +1,71 @@
const { app, BrowserWindow, dialog, ipcMain, shell } = require("electron");
const {
app,
BrowserWindow,
dialog,
ipcMain,
shell,
Tray,
Menu,
clipboard
} = require("electron");
const { autoUpdater } = require("electron-updater");
const fs = require("fs");
const path = require("path");
autoUpdater.autoDownload = false;
let win, secondaryWindow;
let tray = null;
let isQuiting = false;
let indexIsOpen = true;
let trayEnabled = false;
app.commandLine.appendSwitch("--enable-features", "Metal");
const contextMenu = Menu.buildFromTemplate([
{
label: "Open app",
click() {
win.show();
},
},
{
label:"Paste video link",
click(){
const text = clipboard.readText()
if (indexIsOpen){
win.show()
win.webContents.send("link", text);
}
else{
win.loadFile("html/index.html")
win.show()
indexIsOpen = true;
setTimeout(() => {
win.webContents.send("link", text);
}, 1200);
}
}
},
{
label:"Download playlist",
click(){
indexIsOpen = false;
win.loadFile("html/playlist.html")
win.show()
}
},
{
label: "Quit",
click() {
isQuiting = true;
app.quit();
},
},
]);
function createWindow() {
let isTransparent = false;
if (process.platform == "linux") {
@ -16,7 +74,7 @@ function createWindow() {
win = new BrowserWindow({
show: false,
icon: __dirname + "/public/icon.png",
icon: __dirname + "/assets/images/icon.png",
spellcheck: false,
transparent: isTransparent,
webPreferences: {
@ -24,6 +82,13 @@ function createWindow() {
contextIsolation: false,
},
});
win.on("close", (event)=>{
if(!isQuiting && trayEnabled){
event.preventDefault();
win.hide();
}
return false
})
win.loadFile("html/index.html");
win.maximize();
// win.setMenu(null)
@ -81,6 +146,12 @@ ipcMain.on("get-version", () => {
});
ipcMain.on("load-win", (event, file) => {
if (file.includes("playlist.html")){
indexIsOpen = false;
}
else{
indexIsOpen = true;
}
win.loadFile(file);
});
ipcMain.on("load-page", (event, file) => {
@ -123,13 +194,21 @@ ipcMain.on("select-config", () => {
secondaryWindow.webContents.send("configPath", location);
}
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
let trayInUse = false;
ipcMain.on("useTray", (_, enabled)=>{
if (enabled && !trayInUse){
console.log("Using tray");
trayEnabled = true;
trayInUse = true;
tray = new Tray(__dirname + "/assets/images/icon.png");
tray.setToolTip("ytDownloader");
tray.setContextMenu(contextMenu);
}
else{
trayEnabled = false;
}
});
})
// Auto updater events
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
// For macOS

@ -4,7 +4,7 @@
"yt-dlp-wrap-extended": "^2.3.15"
},
"name": "ytdownloader",
"version": "3.9.1",
"version": "3.10.0",
"main": "main.js",
"scripts": {
"start": "electron .",

@ -189,5 +189,23 @@ if (localStorage.getItem("maxActiveDownloads")){
getId("maxDownloads").value = localStorage.getItem("maxActiveDownloads")
}
// Closing app to system tray
const closeToTray = getId("closeToTray");
closeToTray.addEventListener("change", (event) => {
if (closeToTray.checked) {
localStorage.setItem("closeToTray", "true");
ipcRenderer.send("useTray", true)
} else {
localStorage.setItem("closeToTray", "false");
ipcRenderer.send("useTray", false)
}
});
const trayEnabled = localStorage.getItem("closeToTray")
if(trayEnabled == "true"){
closeToTray.checked = true;
ipcRenderer.send("useTray", true)
}
// Translation file
require("../src/translate_preferences");

@ -25,6 +25,13 @@ const i18n = new (require("../translations/i18n"))();
fs.mkdir(hiddenDir, { recursive: true }, () => {});
// System tray
const trayEnabled = localStorage.getItem("closeToTray")
if(trayEnabled == "true"){
console.log("Tray is Enabled");
ipcRenderer.send("useTray", true)
}
// Download directory
let downloadDir = "";
@ -200,6 +207,14 @@ function pasteUrl() {
getInfo(url);
}
function pasteFromTray(url){
defaultVideoToggle();
getId("hidden").style.display = "none";
getId("loadingWrapper").style.display = "flex";
getId("incorrectMsg").textContent = "";
getInfo(url);
}
getId("closeHidden").addEventListener("click", () => {
getId("hidden").style.display = "none";
getId("loadingWrapper").style.display = "none";
@ -1016,3 +1031,7 @@ getId("playlistWin").addEventListener("click", () => {
closeMenu();
ipcRenderer.send("load-win", __dirname + "/playlist.html");
});
ipcRenderer.on("link", (event, text) => {
pasteFromTray(text)
})

@ -36,3 +36,4 @@ getId("dirFormatTxt").textContent = i18n.__("Folder name format for playlists");
getId("resetFilenameFormat").textContent = i18n.__("Reset to default");
getId("resetFoldernameFormat").textContent = i18n.__("Reset to default");
getId("maxTxt").textContent = i18n.__("Maximum number of active downloads");
getId("trayTxt").textContent = i18n.__("Close to system tray");

Loading…
Cancel
Save