diff --git a/electron/preload.js b/electron/electron-preload.js similarity index 99% rename from electron/preload.js rename to electron/electron-preload.js index 5ba22329..44f4b252 100644 --- a/electron/preload.js +++ b/electron/electron-preload.js @@ -1,5 +1,4 @@ const { - shell, contextBridge, ipcRenderer } = require("electron"); diff --git a/electron/main.js b/electron/electron.js similarity index 97% rename from electron/main.js rename to electron/electron.js index 3bc68012..d807fa74 100644 --- a/electron/main.js +++ b/electron/electron.js @@ -14,7 +14,6 @@ const config = require('./package.json'); let mainWindow = null, subWindow = [], willQuitApp = false, - inheritClose = false, devloadUrl = "", devloadCachePath = path.resolve(__dirname, ".devload"), downloadList = [], @@ -70,7 +69,7 @@ function createMainWindow() { center: true, autoHideMenuBar: true, webPreferences: { - preload: path.join(__dirname, 'preload.js'), + preload: path.join(__dirname, 'electron-preload.js'), webSecurity: true, nodeIntegration: true, nodeIntegrationInSubFrames: true, @@ -96,18 +95,9 @@ function createMainWindow() { } }) - mainWindow.on('close', (e) => { + mainWindow.on('close', event => { if (!willQuitApp) { - e.preventDefault(); - if (inheritClose) { - mainWindow.webContents.send("windowClose", {}) - } else { - if (process.platform === 'darwin') { - app.hide(); - } else { - app.quit(); - } - } + utils.onBeforeUnload(event, app) } }) @@ -154,7 +144,7 @@ function createSubWindow(args) { parent: mainWindow, autoHideMenuBar: true, webPreferences: { - preload: path.join(__dirname, 'preload.js'), + preload: path.join(__dirname, 'electron-preload.js'), devTools: args.devTools !== false, webSecurity: true, nodeIntegration: true, @@ -168,12 +158,18 @@ function createSubWindow(args) { event.preventDefault() } }) - browser.on('close', () => { + + browser.on('close', event => { + utils.onBeforeUnload(event) + }) + + browser.on('closed', () => { let index = subWindow.findIndex(item => item.name == name); if (index > -1) { subWindow.splice(index, 1) } }) + subWindow.push({ name, browser }) } browser.webContents.setUserAgent(browser.webContents.getUserAgent() + " SubTaskWindow/" + process.platform + "/" + os.arch() + "/1.0" + (args.userAgent ? (" " + args.userAgent) : "")); @@ -209,14 +205,6 @@ app.on('before-quit', () => { willQuitApp = true }) -/** - * 继承关闭窗口事件 - */ -ipcMain.on('inheritClose', (event) => { - inheritClose = true - event.returnValue = "ok" -}) - /** * 下载文件 * @param args {url} @@ -555,7 +543,7 @@ function exportVsdx(event, args, directFinalize) { height: 800, show: false, webPreferences: { - preload: path.join(__dirname, 'preload.js'), + preload: path.join(__dirname, 'electron-preload.js'), webSecurity: true, nodeIntegration: true, contextIsolation: true, @@ -648,7 +636,7 @@ function exportDiagram(event, args, directFinalize) { try { browser = new BrowserWindow({ webPreferences: { - preload: path.join(__dirname, 'preload.js'), + preload: path.join(__dirname, 'electron-preload.js'), backgroundThrottling: false, contextIsolation: true, nativeWindowOpen: true diff --git a/electron/package.json b/electron/package.json index d393fc86..3b74f3d6 100644 --- a/electron/package.json +++ b/electron/package.json @@ -2,7 +2,7 @@ "name": "DooTask", "version": "0.9.16", "description": "DooTask is task management system.", - "main": "main.js", + "main": "electron.js", "license": "MIT", "scripts": { "start": "electron-forge start", @@ -51,8 +51,8 @@ "artifactName": "${productName}-v${version}-${os}-${arch}.${ext}", "files": [ "public/**/*", - "main.js", - "preload.js", + "electron-preload.js", + "electron.js", "utils.js" ], "mac": { diff --git a/electron/utils.js b/electron/utils.js index ca1507c9..41469d78 100644 --- a/electron/utils.js +++ b/electron/utils.js @@ -1,5 +1,5 @@ const fs = require("fs"); -const {shell} = require("electron"); +const {shell, dialog} = require("electron"); module.exports = { /** @@ -269,4 +269,39 @@ module.exports = { } return Math.round(time / 1000) }, + + /** + * 窗口关闭事件 + * @param event + * @param app + */ + onBeforeUnload(event, app) { + const sender = event.sender + const contents = sender.webContents + if (contents != null) { + const destroy = () => { + if (typeof app === "undefined") { + sender.destroy() + } else { + if (process.platform === 'darwin') { + app.hide() + } else { + app.quit() + } + } + } + contents.executeJavaScript('if(typeof window.__onBeforeUnload === \'function\'){window.__onBeforeUnload()}', true).then(options => { + if (this.isJson(options)) { + let choice = dialog.showMessageBoxSync(sender, options) + if (choice === 1) { + contents.executeJavaScript('if(typeof window.__removeBeforeUnload === \'function\'){window.__removeBeforeUnload()}', true).catch(() => {}); + destroy() + } + } else if (options !== true) { + destroy() + } + }) + event.preventDefault() + } + }, } diff --git a/resources/assets/js/App.vue b/resources/assets/js/App.vue index f634fed6..70b29625 100755 --- a/resources/assets/js/App.vue +++ b/resources/assets/js/App.vue @@ -179,17 +179,15 @@ export default { if (!this.$Electron) { return; } - this.$Electron.sendMessage('inheritClose'); - this.$Electron.registerMsgListener('windowClose', () => { + window.__onBeforeUnload = () => { if (this.$Modal.removeLast()) { - return; + return true; } if (this.cacheDrawerOverlay.length > 0) { this.cacheDrawerOverlay[this.cacheDrawerOverlay.length - 1].close(); - return; + return true; } - this.$Electron.sendMessage('windowHidden'); - }) + } this.$Electron.registerMsgListener('dispatch', (event, args) => { if (!$A.isJson(args)) { return;