From 1248f1c8ec96a48402905b40713fcf5735d7821f Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Thu, 27 Jan 2022 09:58:50 +0200 Subject: [PATCH] add single instance lock option --- index.js | 20 ++++++++++++++++---- menu.js | 29 +++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 8360f147..c8022891 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,22 @@ unhandled({ process.env.NODE_OPTIONS = ""; const app = electron.app; +// Prevent window being garbage collected +let mainWindow; +autoUpdater.autoDownload = false; + +if(config.get("options.singleInstanceLock")){ + const gotTheLock = app.requestSingleInstanceLock(); + if (!gotTheLock) app.quit(); + + app.on('second-instance', () => { + if (!mainWindow) return; + if (mainWindow.isMinimized()) mainWindow.restore(); + if (!mainWindow.isVisible()) mainWindow.show(); + mainWindow.focus(); + }); +} + app.commandLine.appendSwitch( "js-flags", // WebAssembly flags @@ -54,10 +70,6 @@ require("electron-debug")({ showDevTools: false //disable automatic devTools on new window }); -// Prevent window being garbage collected -let mainWindow; -autoUpdater.autoDownload = false; - let icon = "assets/youtube-music.png"; if (process.platform == "win32") { icon = "assets/generated/icon.ico"; diff --git a/menu.js b/menu.js index 0a62249b..0344a2bb 100644 --- a/menu.js +++ b/menu.js @@ -87,6 +87,19 @@ const mainMenuTemplate = (win) => { config.set("options.removeUpgradeButton", item.checked); }, }, + { + label: "Single instance lock", + type: "checkbox", + checked: config.get("options.singleInstanceLock"), + click: (item) => { + config.set("options.singleInstanceLock", item.checked); + if (item.checked && !app.hasSingleInstanceLock()) { + app.requestSingleInstanceLock(); + } else if (!item.checked && app.hasSingleInstanceLock()) { + app.releaseSingleInstanceLock(); + } + }, + }, ...(is.windows() || is.linux() ? [ { @@ -166,14 +179,14 @@ const mainMenuTemplate = (win) => { { label: "Advanced options", submenu: [ - { - label: "Proxy", - type: "checkbox", - checked: !!config.get("options.proxy"), - click: (item) => { - setProxy(item, win); - }, - }, + { + label: "Proxy", + type: "checkbox", + checked: !!config.get("options.proxy"), + click: (item) => { + setProxy(item, win); + }, + }, { label: "Disable hardware acceleration", type: "checkbox",