From 64c1e8bc5c2a3ab1264d5d690d800b6a54f6f1ab Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sun, 1 Nov 2020 21:04:26 +1100 Subject: [PATCH] #113 only open a new window from the dock if already visible --- mods/core/tray.js | 40 ++++++++++++++++++++-------------------- pkg/apply.js | 13 +++++++++---- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/mods/core/tray.js b/mods/core/tray.js index d85c9fe..e729328 100644 --- a/mods/core/tray.js +++ b/mods/core/tray.js @@ -7,8 +7,6 @@ 'use strict'; -const { start } = require('repl'); - let tray, enhancer_menu; module.exports = (store, __exports) => { @@ -16,11 +14,25 @@ module.exports = (store, __exports) => { path = require('path'), is_mac = process.platform === 'darwin', is_win = process.platform === 'win32', - helpers = require('../../pkg/helpers.js'); + helpers = require('../../pkg/helpers.js'), + getAllWindows = electron.BrowserWindow.getAllWindows; + + function newWindow() { + require('./createWindow.js')( + store, + require(path.resolve(`${helpers.__notion}/app/main/createWindow.js`)) + )( + '', + getAllWindows().find((win) => win !== enhancer_menu) + ); + } electron.app.on('second-instance', (event, args, workingDirectory) => { - if (!store().openhidden) { - electron.BrowserWindow.getAllWindows().forEach((window) => { + const windows = getAllWindows(); + if (windows.some((win) => win.isVisible())) { + newWindow(); + } else { + windows.forEach((window) => { window.show(); window.focus(); if (store().maximized) window.maximize(); @@ -186,19 +198,7 @@ module.exports = (store, __exports) => { { type: 'normal', label: 'New Window', - click: () => { - require('./createWindow.js')( - store, - require(path.resolve( - `${helpers.__notion}/app/main/createWindow.js` - )) - )( - '', - electron.BrowserWindow.getAllWindows().find( - (win) => win !== enhancer_menu - ) - ); - }, + click: newWindow(), accelerator: 'CommandOrControl+Shift+N', }, { @@ -238,13 +238,13 @@ module.exports = (store, __exports) => { if (is_mac) electron.app.hide(); } function toggleWindows() { - const windows = electron.BrowserWindow.getAllWindows(); + const windows = getAllWindows(); if (windows.some((win) => win.isVisible())) hideWindows(windows); else showWindows(windows); } tray.on('click', toggleWindows); electron.globalShortcut.register(store().hotkey, () => { - const windows = electron.BrowserWindow.getAllWindows(); + const windows = getAllWindows(); if (windows.some((win) => win.isFocused() && win.isVisible())) hideWindows(windows); else showWindows(windows); diff --git a/pkg/apply.js b/pkg/apply.js index a91a6f6..3b03554 100644 --- a/pkg/apply.js +++ b/pkg/apply.js @@ -121,10 +121,15 @@ module.exports = async function ({ overwrite_version, friendly_errors } = {}) { if (err) throw err; fs.writeFile( insertion_file, - `${data.replace( - /process.platform === "win32"/g, - 'process.platform === "win32" || process.platform === "linux"' - )}\n\n//notion-enhancer\nrequire('${helpers.realpath( + `${data + .replace( + /process.platform === "win32"/g, + 'process.platform === "win32" || process.platform === "linux"' + ) + .replace( + /else \{[\s\n]+const win = createWindow_1\.createWindow\(relativeUrl\);/g, + 'else if (relativeUrl) { const win = createWindow_1.createWindow(relativeUrl);' + )}\n\n//notion-enhancer\nrequire('${helpers.realpath( __dirname )}/loader.js')(__filename, exports);`, 'utf8',