优化electron通信
This commit is contained in:
parent
dbd59cd958
commit
f72114c223
@ -1,5 +1,4 @@
|
|||||||
const {
|
const {
|
||||||
shell,
|
|
||||||
contextBridge,
|
contextBridge,
|
||||||
ipcRenderer
|
ipcRenderer
|
||||||
} = require("electron");
|
} = require("electron");
|
@ -14,7 +14,6 @@ const config = require('./package.json');
|
|||||||
let mainWindow = null,
|
let mainWindow = null,
|
||||||
subWindow = [],
|
subWindow = [],
|
||||||
willQuitApp = false,
|
willQuitApp = false,
|
||||||
inheritClose = false,
|
|
||||||
devloadUrl = "",
|
devloadUrl = "",
|
||||||
devloadCachePath = path.resolve(__dirname, ".devload"),
|
devloadCachePath = path.resolve(__dirname, ".devload"),
|
||||||
downloadList = [],
|
downloadList = [],
|
||||||
@ -70,7 +69,7 @@ function createMainWindow() {
|
|||||||
center: true,
|
center: true,
|
||||||
autoHideMenuBar: true,
|
autoHideMenuBar: true,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'electron-preload.js'),
|
||||||
webSecurity: true,
|
webSecurity: true,
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
nodeIntegrationInSubFrames: true,
|
nodeIntegrationInSubFrames: true,
|
||||||
@ -96,18 +95,9 @@ function createMainWindow() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
mainWindow.on('close', (e) => {
|
mainWindow.on('close', event => {
|
||||||
if (!willQuitApp) {
|
if (!willQuitApp) {
|
||||||
e.preventDefault();
|
utils.onBeforeUnload(event, app)
|
||||||
if (inheritClose) {
|
|
||||||
mainWindow.webContents.send("windowClose", {})
|
|
||||||
} else {
|
|
||||||
if (process.platform === 'darwin') {
|
|
||||||
app.hide();
|
|
||||||
} else {
|
|
||||||
app.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -154,7 +144,7 @@ function createSubWindow(args) {
|
|||||||
parent: mainWindow,
|
parent: mainWindow,
|
||||||
autoHideMenuBar: true,
|
autoHideMenuBar: true,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'electron-preload.js'),
|
||||||
devTools: args.devTools !== false,
|
devTools: args.devTools !== false,
|
||||||
webSecurity: true,
|
webSecurity: true,
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
@ -168,12 +158,18 @@ function createSubWindow(args) {
|
|||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
browser.on('close', () => {
|
|
||||||
|
browser.on('close', event => {
|
||||||
|
utils.onBeforeUnload(event)
|
||||||
|
})
|
||||||
|
|
||||||
|
browser.on('closed', () => {
|
||||||
let index = subWindow.findIndex(item => item.name == name);
|
let index = subWindow.findIndex(item => item.name == name);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
subWindow.splice(index, 1)
|
subWindow.splice(index, 1)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
subWindow.push({ name, browser })
|
subWindow.push({ name, browser })
|
||||||
}
|
}
|
||||||
browser.webContents.setUserAgent(browser.webContents.getUserAgent() + " SubTaskWindow/" + process.platform + "/" + os.arch() + "/1.0" + (args.userAgent ? (" " + args.userAgent) : ""));
|
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
|
willQuitApp = true
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* 继承关闭窗口事件
|
|
||||||
*/
|
|
||||||
ipcMain.on('inheritClose', (event) => {
|
|
||||||
inheritClose = true
|
|
||||||
event.returnValue = "ok"
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
* @param args {url}
|
* @param args {url}
|
||||||
@ -555,7 +543,7 @@ function exportVsdx(event, args, directFinalize) {
|
|||||||
height: 800,
|
height: 800,
|
||||||
show: false,
|
show: false,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'electron-preload.js'),
|
||||||
webSecurity: true,
|
webSecurity: true,
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
contextIsolation: true,
|
contextIsolation: true,
|
||||||
@ -648,7 +636,7 @@ function exportDiagram(event, args, directFinalize) {
|
|||||||
try {
|
try {
|
||||||
browser = new BrowserWindow({
|
browser = new BrowserWindow({
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'electron-preload.js'),
|
||||||
backgroundThrottling: false,
|
backgroundThrottling: false,
|
||||||
contextIsolation: true,
|
contextIsolation: true,
|
||||||
nativeWindowOpen: true
|
nativeWindowOpen: true
|
@ -2,7 +2,7 @@
|
|||||||
"name": "DooTask",
|
"name": "DooTask",
|
||||||
"version": "0.9.16",
|
"version": "0.9.16",
|
||||||
"description": "DooTask is task management system.",
|
"description": "DooTask is task management system.",
|
||||||
"main": "main.js",
|
"main": "electron.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "electron-forge start",
|
"start": "electron-forge start",
|
||||||
@ -51,8 +51,8 @@
|
|||||||
"artifactName": "${productName}-v${version}-${os}-${arch}.${ext}",
|
"artifactName": "${productName}-v${version}-${os}-${arch}.${ext}",
|
||||||
"files": [
|
"files": [
|
||||||
"public/**/*",
|
"public/**/*",
|
||||||
"main.js",
|
"electron-preload.js",
|
||||||
"preload.js",
|
"electron.js",
|
||||||
"utils.js"
|
"utils.js"
|
||||||
],
|
],
|
||||||
"mac": {
|
"mac": {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const {shell} = require("electron");
|
const {shell, dialog} = require("electron");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
/**
|
/**
|
||||||
@ -269,4 +269,39 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
return Math.round(time / 1000)
|
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()
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
@ -179,17 +179,15 @@ export default {
|
|||||||
if (!this.$Electron) {
|
if (!this.$Electron) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.$Electron.sendMessage('inheritClose');
|
window.__onBeforeUnload = () => {
|
||||||
this.$Electron.registerMsgListener('windowClose', () => {
|
|
||||||
if (this.$Modal.removeLast()) {
|
if (this.$Modal.removeLast()) {
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
if (this.cacheDrawerOverlay.length > 0) {
|
if (this.cacheDrawerOverlay.length > 0) {
|
||||||
this.cacheDrawerOverlay[this.cacheDrawerOverlay.length - 1].close();
|
this.cacheDrawerOverlay[this.cacheDrawerOverlay.length - 1].close();
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
this.$Electron.sendMessage('windowHidden');
|
}
|
||||||
})
|
|
||||||
this.$Electron.registerMsgListener('dispatch', (event, args) => {
|
this.$Electron.registerMsgListener('dispatch', (event, args) => {
|
||||||
if (!$A.isJson(args)) {
|
if (!$A.isJson(args)) {
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user