From 6b29ee1e974cb3ca6a534a634db152c60be09a5e Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sat, 19 Feb 2022 16:39:02 +0800 Subject: [PATCH] improve electron.js --- public/electron.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/public/electron.js b/public/electron.js index 6f6fcb69..6b39c30e 100644 --- a/public/electron.js +++ b/public/electron.js @@ -32,7 +32,6 @@ let rendererReady = false; let newVersion; const openFiles = (paths) => mainWindow.webContents.send('file-opened', paths); -const openFile = (path) => openFiles([path]); // https://github.com/electron/electron/issues/526#issuecomment-563010533 @@ -137,11 +136,10 @@ app.on('ready', async () => { const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer'); // eslint-disable-line global-require,import/no-extraneous-dependencies installExtension(REACT_DEVELOPER_TOOLS) - .then(name => console.log(`Added Extension: ${name}`)) - .catch(err => console.log('An error occurred: ', err)); + .then(name => console.log('Added Extension', name)) + .catch(err => console.log('Failed to add extension', err)); } - createWindow(); updateMenu(); @@ -166,6 +164,13 @@ app.on('activate', () => { }); let openFileInitial; +let fileOpened = false; + +function openFilesOnce(files) { + if (fileOpened) return; + fileOpened = true; + openFiles(files); +} electron.ipcMain.on('renderer-ready', () => { rendererReady = true; @@ -181,13 +186,13 @@ electron.ipcMain.on('renderer-ready', () => { ? process.argv.slice(ignoreFirstArgs).filter((arg) => arg && !arg.startsWith('-')) : []; - if (filesToOpen.length > 0) openFiles(filesToOpen); - else if (openFileInitial) openFile(openFileInitial); + if (filesToOpen.length > 0) openFilesOnce(filesToOpen); + else if (openFileInitial) openFilesOnce([openFileInitial]); }); // Mac OS open with LosslessCut app.on('open-file', (event, path) => { - if (rendererReady) openFile(path); + if (rendererReady) openFilesOnce([path]); else openFileInitial = path; });