Colubrina/frontend/src/background.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-07-31 23:57:47 +10:00
"use strict"
2022-08-03 19:49:15 +10:00
const electron = require("electron")
const VueCLI = require("vue-cli-plugin-electron-builder/lib")
const {
default: installExtension,
VUEJS_DEVTOOLS
} = require("electron-devtools-installer")
2022-07-31 23:57:47 +10:00
const isDevelopment = process.env.NODE_ENV !== "production"
2022-10-02 17:12:25 +11:00
const windowStateKeeper = require("electron-window-state")
2022-07-31 23:57:47 +10:00
// Scheme must be registered before the app is ready
2022-08-03 19:49:15 +10:00
electron.protocol.registerSchemesAsPrivileged([
2022-07-31 23:57:47 +10:00
{ scheme: "app", privileges: { secure: true, standard: true } }
])
async function createWindow() {
// Create the browser window.
2022-08-03 19:49:15 +10:00
const { bounds } = electron.screen.getDisplayNearestPoint(
electron.screen.getCursorScreenPoint()
2022-08-03 19:11:19 +10:00
)
const height = Math.floor(bounds.height * (2 / 3))
const width = Math.floor(bounds.width * (2 / 3))
const y = Math.floor(bounds.y + (bounds.height - height) / 2)
const x = Math.floor(bounds.x + (bounds.width - width) / 2)
2022-10-02 17:12:25 +11:00
let mainWindowState = windowStateKeeper({
defaultWidth: width,
defaultHeight: height,
x: x,
y: y
})
2022-08-03 19:49:15 +10:00
const win = new electron.BrowserWindow({
2022-10-02 17:12:25 +11:00
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
2022-07-31 23:57:47 +10:00
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
preload: "window-preload.js",
2022-08-03 19:11:19 +10:00
// required for multi-instance support
2022-09-03 13:45:18 +10:00
webSecurity: false,
// Auto-scroll for Linux/MacOS
enableBlinkFeatures: "MiddleClickAutoscroll"
2022-07-31 23:57:47 +10:00
}
})
2022-10-02 17:12:25 +11:00
mainWindowState.manage(win)
2022-07-31 23:57:47 +10:00
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
2022-08-03 19:49:15 +10:00
VueCLI.createProtocol("app")
2022-07-31 23:57:47 +10:00
// Load the index.html when not in development
win.loadURL("app://./index.html")
}
}
// Quit when all windows are closed.
2022-08-03 19:49:15 +10:00
electron.app.on("window-all-closed", () => {
2022-07-31 23:57:47 +10:00
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
2022-08-03 19:49:15 +10:00
electron.app.quit()
2022-07-31 23:57:47 +10:00
}
})
2022-08-03 19:49:15 +10:00
electron.app.on("activate", () => {
2022-07-31 23:57:47 +10:00
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
2022-08-03 19:49:15 +10:00
if (electron.BrowserWindow.getAllWindows().length === 0) createWindow()
2022-07-31 23:57:47 +10:00
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
2022-08-03 19:49:15 +10:00
electron.app.on("ready", async () => {
2022-07-31 23:57:47 +10:00
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()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === "win32") {
process.on("message", (data) => {
if (data === "graceful-exit") {
2022-08-03 19:49:15 +10:00
electron.app.quit()
2022-07-31 23:57:47 +10:00
}
})
} else {
process.on("SIGTERM", () => {
2022-08-03 19:49:15 +10:00
electron.app.quit()
2022-07-31 23:57:47 +10:00
})
}
}