From f0c50de80a491200182758af2deedd353aed779c Mon Sep 17 00:00:00 2001 From: Dong Xia Date: Mon, 6 Feb 2023 05:20:58 +0000 Subject: [PATCH] =?UTF-8?q?electron=E5=85=A5=E5=8F=A3=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dong Xia --- src/background.js | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/background.js diff --git a/src/background.js b/src/background.js new file mode 100644 index 0000000..02b26ea --- /dev/null +++ b/src/background.js @@ -0,0 +1,107 @@ +'use strict' + +import { app, protocol, BrowserWindow, Menu, globalShortcut } from 'electron' +import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' + +const isDevelopment = process.env.NODE_ENV !== 'production' +const path = require('path') +let toOpenDevTools = null +protocol.registerSchemesAsPrivileged([ + { scheme: 'app', privileges: { secure: true, standard: true }} +]) +let mainWin = null +async function createWindow() { + mainWin = new BrowserWindow({ + width: 1400, + height: 1000, + show: false, + backgroundColor: '#2e2c29', + icon: path.join(__dirname, './bundled/favicon.ico'), + title: '凝思系统软件安装管理平台', + webPreferences: { + nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION, + contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION, + webSecurity: false + } + }) + const menuTemplate = Menu.buildFromTemplate([ + { + label: '返回&(Ctrl+B)', + accelerator: 'Ctrl+B', + click: () => { + mainWin.webContents.goBack() + } + }, + { + label: '全屏&(Ctrl+F)', + accelerator: 'Ctrl+F', + role: 'togglefullscreen' + }, + { + label: '退出&(Ctrl+Q)', + accelerator: 'Ctrl+Q', + role: 'close' + }, + { + label: '强制刷新&(Ctrl+S)', + accelerator: 'Ctrl+S', + role: 'reload' + } + ]) + Menu.setApplicationMenu(menuTemplate) + mainWin.once('ready-to-show', () => { + mainWin.maximize() + mainWin.show() + }) + if (process.env.WEBPACK_DEV_SERVER_URL) { + // await mainWin.loadURL(process.env.WEBPACK_DEV_SERVER_URL) + // if (!process.env.IS_TEST) mainWin.webContents.openDevTools() + } else { + createProtocol('app') + mainWin.loadURL('app://./index.html') + } + toOpenDevTools = function() { + mainWin.webContents.openDevTools() + } +} + +app.on('window-all-closed', () => { + if (process.platform !== 'darwin') { + app.quit() + } +}) +app.whenReady().then(() => { + globalShortcut.register('ctrl+shift+alt+d', () => { + toOpenDevTools() + }) +}) + +app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) createWindow() +}) + +app.on('ready', async() => { + if (isDevelopment && !process.env.IS_TEST) { + // Install Vue Devtools + try { + // await installExtension(VUEJS_DEVTOOLS) + } catch (e) { + console.error('Vue Devtools failed to install:', e.toString()) + } + } + createWindow() +}) + +if (isDevelopment) { + if (process.platform === 'win32') { + process.on('message', (data) => { + if (data === 'graceful-exit') { + app.quit() + } + }) + } else { + process.on('SIGTERM', () => { + app.quit() + }) + } +} -- Gitee