This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
136 lines (116 loc) · 4.11 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import windowStateKeeper from 'electron-window-state';
import * as path from 'path';
import { format as formatUrl } from 'url';
/* eslint-disable import/no-extraneous-dependencies */
import { app, Menu, protocol, BrowserWindow } from 'electron';
import {
createProtocol,
installVueDevtools,
} from 'vue-cli-plugin-electron-builder/lib';
/* eslint-enable import/no-extraneous-dependencies */
import pkg from '../package.json';
app.setAppUserModelId(process.execPath);
const moduleReq = require('module');
const isDevelopment = process.env.NODE_ENV !== 'production';
if (isDevelopment) {
// Don't load any native (external) modules until the following line is run:
moduleReq.globalPaths.push(process.env.NODE_MODULES_PATH);
}
// global reference to mainWindow (necessary to prevent window from being garbage collected)
let mainWindow;
// Standard scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true } }]);
function createMainWindow() {
const mainWindowState = windowStateKeeper();
const window = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
webviewTag: true,
},
backgroundColor: '#22292f',
frame: false,
show: false,
titleBarStyle: 'hiddenInset',
// dimensions and position
height: mainWindowState.height,
width: mainWindowState.width,
x: mainWindowState.x,
y: mainWindowState.y,
});
mainWindowState.manage(window);
// remove electron and app specific user agents to prevent sideeffects like
// https://github.com/ramboxapp/community-edition/issues/1981
// https://github.com/meetfranz/franz/issues/1185
// https://github.com/meetfranz/franz/issues/1138
const removeUserAgents = `(Electron|${pkg.name}|${pkg.productName})/([0-9a-z-.]+) `;
window.webContents.setUserAgent(window.webContents.getUserAgent().replace(new RegExp(removeUserAgents, 'g'), ''));
if (isDevelopment) {
// Load the url of the dev server if in development mode
window.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
if (!process.env.IS_TEST) window.webContents.openDevTools();
} else {
createProtocol('app');
// Load the index.html when not in development
window.loadURL(formatUrl({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file',
slashes: true,
}));
}
window.on('ready-to-show', () => {
window.show();
window.focus();
});
window.on('closed', () => {
mainWindow = null;
});
window.webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
});
});
return window;
}
// quit application when all windows are closed
app.on('window-all-closed', () => {
// on macOS it is common for applications to stay open until the user explicitly quits
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// on macOS it is common to re-create a window even after all windows have been closed
if (mainWindow === null) {
mainWindow = createMainWindow();
}
});
// create main BrowserWindow when electron is ready
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
await installVueDevtools();
}
mainWindow = createMainWindow();
Menu.setApplicationMenu(Menu.buildFromTemplate([
{
label: 'Application',
submenu: [
{ label: 'About Application', selector: 'orderFrontStandardAboutPanel:' },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click() { app.quit(); } },
],
}, {
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', selector: 'undo:' },
{ label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', selector: 'redo:' },
{ type: 'separator' },
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', selector: 'cut:' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:' },
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', selector: 'selectAll:' },
],
},
]));
});