forked from OpenSlides/openslides-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron-main.ts
181 lines (161 loc) · 4.81 KB
/
electron-main.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { app, protocol, BrowserWindow, Menu, session, shell, screen, globalShortcut } from 'electron';
import { BrowserWindowConstructorOptions, MenuItemConstructorOptions } from 'electron/main';
import { autoUpdater } from 'electron-updater';
import * as path from 'path';
import * as url from 'url';
/**
* parse arguments
*/
const args = process.argv.slice(1);
const debug = args.includes('--debug');
const serve = args.includes('--serve');
let win: BrowserWindow;
function createMenu(): void {
const template: MenuItemConstructorOptions[] = [
{
label: 'File',
submenu: [
{ role: 'reload' },
{ label: 'Logout', click: async () => cleanStorage() },
{ type: 'separator' },
{ role: 'quit' }
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' }
]
},
{
label: 'View',
submenu: [{ role: 'togglefullscreen' }, { role: 'toggleDevTools' }]
},
{
role: 'help',
submenu: [
{
label: 'Report Issue',
click: async () => openExternal('https://github.com/OpenSlides/OpenSlides/issues/new/choose')
},
{
label: 'OpenSlides',
click: async () => openExternal('https://openslides.com/de')
},
{
label: 'Electron',
click: async () => openExternal('https://electronjs.org')
}
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
function createWindow(): void {
createMenu();
const size = screen.getPrimaryDisplay().workAreaSize;
const windowProperties: BrowserWindowConstructorOptions = {
x: 0,
y: 0,
width: size.width,
height: size.height,
webPreferences: {
contextIsolation: false,
webviewTag: true,
nativeWindowOpen: true,
webSecurity: !debug,
allowRunningInsecureContent: debug
}
};
win = new BrowserWindow(windowProperties);
if (serve) {
win.loadURL('http://localhost:4200');
} else {
const urlFormat = url.format({
pathname: 'index.html',
protocol: 'file',
slashes: true
});
win.loadURL(urlFormat);
win.webContents.on('did-fail-load', () => {
win.loadURL(urlFormat);
});
}
win.on('closed', () => {
(win as any) = null;
});
}
async function cleanStorage(): Promise<void> {
await session.defaultSession.clearStorageData();
win.webContents.goToIndex(0);
}
async function openExternal(link: string): Promise<void> {
await shell.openExternal(link);
}
/**
* Add custom global shortcuts
*/
function registerShortcuts(): void {
globalShortcut.register('f12', () => {
if (win) {
win.webContents.toggleDevTools();
}
});
}
try {
/**
* On certificate error we disable default behavior (stop loading the page)
* and we then say "it is all fine - true" to the callback.
* This is necessary to develop against self signed certificates
*/
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
if (debug) {
event.preventDefault();
callback(true);
}
});
app.on('ready', () => {
registerShortcuts();
/**
* Intercept the file protocol, so we can access the assets folder to
* call css, js and json files.
*/
protocol.interceptFileProtocol('file', (request, callback) => {
/**
* all urls start with 'file://'
*/
const url = request.url.substr(7);
callback({ path: path.normalize(`${__dirname}/dist/${url}`) });
});
autoUpdater.checkForUpdatesAndNotify();
createWindow();
});
/**
* open links in new tab, also works in web views
*/
app.on('web-contents-created', (e, contents) => {
contents.on('new-window', async (e, url) => {
e.preventDefault();
await openExternal(url);
});
});
// on macOS, closing the window doesn't quit the app
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
} catch (e) {
console.error(e);
}