From ed91305763866af4b3bb446dfb0aad753bbed6b1 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 9 Dec 2018 20:06:01 -0600 Subject: [PATCH] [Close to tray] [Unmount fixes] --- electron.js | 36 +++++++++++++++++++++++++++--------- helpers.js | 7 +++++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/electron.js b/electron.js index 1e84032..a8d7d86 100644 --- a/electron.js +++ b/electron.js @@ -14,7 +14,7 @@ const AutoLaunch = require('auto-launch'); // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. -let mainContextWindow; +let trayContextMenu; let mainWindow; let mainWindowTray; let mountedPIDs = {}; @@ -22,6 +22,11 @@ let mountedLocations = []; let expectedUnmount = {}; let launchHidden = false; +let isQuiting = false; +app.on('before-quit', function () { + isQuiting = true; +}); + function closeApplication() { app.quit(); } @@ -58,7 +63,6 @@ function createWindow() { mainWindow = null; // Unmount all items - console.log(mountedLocations); for (const i in mountedLocations) { helpers.stopMountProcess(mountedPIDs[mountedLocations[i]], mountedLocations[i]); } @@ -77,7 +81,7 @@ function createWindow() { }); const image = nativeImage.createFromPath(path.join(__dirname, '/build/logo.png')); - mainContextWindow = Menu.buildFromTemplate([ + trayContextMenu = Menu.buildFromTemplate([ { label: 'Visible', type: 'checkbox', click(item) { if (item.checked) { @@ -115,20 +119,32 @@ function createWindow() { type: 'separator' }, { - label: 'Exit', click(item) { + label: 'Exit and Unmount', click(item) { closeApplication(); } } ]); + mainWindow.on('close', function (event) { + if (!isQuiting) { + event.preventDefault(); + if (mainWindow.isVisible()) { + mainWindow.hide(); + trayContextMenu.items[0].checked = false; + mainWindowTray.setContextMenu(trayContextMenu); + } + event.returnValue = false; + } + }); + autoLauncher .isEnabled() .then((enabled) => { - mainContextWindow.items[1].checked = enabled; + trayContextMenu.items[1].checked = enabled; mainWindowTray = new Tray(image); mainWindowTray.setToolTip('Repertory UI'); - mainWindowTray.setContextMenu(mainContextWindow) + mainWindowTray.setContextMenu(trayContextMenu) }) .catch(() => { closeApplication(); @@ -143,8 +159,9 @@ if (!instanceLock) { app.on('second-instance', () => { if (mainWindow) { mainWindow.show(); - if (mainContextWindow) { - mainContextWindow.items[0].checked = true; + if (trayContextMenu && mainWindowTray) { + trayContextMenu.items[0].checked = true; + mainWindowTray.setContextMenu(trayContextMenu) } if (mainWindow.isMinimized()) { mainWindow.restore(); @@ -589,7 +606,8 @@ ipcMain.on(Constants.IPC_Unmount_Drive, (event, data) => { expectedUnmount[data.StorageType] = true; helpers .stopMountProcess(data.PID, data.Location) - .then(()=> { + .then((result)=> { + console.log(result); }) .catch((e) => { console.log(e); diff --git a/helpers.js b/helpers.js index 665e853..2860a42 100644 --- a/helpers.js +++ b/helpers.js @@ -471,8 +471,11 @@ module.exports.stopMountProcess = (pid, location) => { process.on('error', (err) => { reject(err); }); - process.on('exit', () => { - setTimeout(()=>resolve(pid), 3000); + process.on('exit', (code) => { + resolve({ + PID: pid, + Code: code, + }); }); process.unref(); });