-
Notifications
You must be signed in to change notification settings - Fork 269
/
electron.js
226 lines (179 loc) · 5.42 KB
/
electron.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
const { app, BrowserWindow, session, nativeTheme, ipcMain, powerMonitor, dialog } = require('electron');
const { is, fixPathForAsarUnpack } = require('electron-util');
const path = require('path');
const storage = require('electron-json-storage');
const port = process.env.SERVER_PORT;
const protocol = 'anytype';
const remote = require('@electron/remote/main');
const { installNativeMessagingHost } = require('./electron/js/lib/installNativeMessagingHost.js');
const binPath = fixPathForAsarUnpack(path.join(__dirname, 'dist', `anytypeHelper${is.windows ? '.exe' : ''}`));
// Fix notifications app name
if (is.windows) {
app.setAppUserModelId(app.name);
};
storage.setDataPath(app.getPath('userData'));
const Api = require('./electron/js/api.js');
const ConfigManager = require('./electron/js/config.js');
const UpdateManager = require('./electron/js/update.js');
const MenuManager = require('./electron/js/menu.js');
const WindowManager = require('./electron/js/window.js');
const Server = require('./electron/js/server.js');
const Util = require('./electron/js/util.js');
const Cors = require('./electron/json/cors.json');
const csp = [];
for (let i in Cors) {
csp.push([ i ].concat(Cors[i]).join(' '));
};
app.commandLine.appendSwitch('ignore-connections-limit', 'localhost, 127.0.0.1');
app.removeAsDefaultProtocolClient(protocol);
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(protocol, process.execPath, [ path.resolve(process.argv[1]) ]);
};
} else {
app.setAsDefaultProtocolClient(protocol);
};
powerMonitor.on('suspend', () => {
Util.log('info', '[PowerMonitor] suspend');
});
powerMonitor.on('resume', () => {
WindowManager.reloadAll();
Util.log('info', '[PowerMonitor] resume');
});
let deeplinkingUrl = '';
let waitLibraryPromise = null;
let mainWindow = null;
if (is.development && !port) {
console.error('ERROR: Please define SERVER_PORT env var');
Api.exit(mainWindow, '', false);
return;
};
if (app.isPackaged && !app.requestSingleInstanceLock()) {
Api.exit(mainWindow, '' ,false);
return;
};
remote.initialize();
Util.setAppPath(path.join(__dirname));
function waitForLibraryAndCreateWindows () {
const { userDataPath } = ConfigManager.config;
let currentPath = app.getPath('userData');
if (userDataPath && (userDataPath != currentPath)) {
currentPath = userDataPath;
app.setPath('userData', userDataPath);
};
if (process.env.ANYTYPE_USE_SIDE_SERVER) {
// use the grpc server started from the outside
Server.setAddress(process.env.ANYTYPE_USE_SIDE_SERVER);
waitLibraryPromise = Promise.resolve();
} else {
waitLibraryPromise = Server.start(binPath, currentPath);
};
Util.mkDir(Util.logPath());
waitLibraryPromise.then(() => {
global.serverAddress = Server.getAddress();
createWindow();
}, (err) => {
dialog.showErrorBox('Error: failed to run server', err.toString());
});
};
// MacOs 12.2 (M1): doesn't fire on manual theme switch
nativeTheme.on('updated', () => {
MenuManager.updateTrayIcon();
WindowManager.sendToAll('native-theme', Util.isDarkTheme());
});
function createWindow () {
mainWindow = WindowManager.createMain({ route: Util.getRouteFromUrl(deeplinkingUrl), isChild: false });
mainWindow.on('close', e => {
Util.log('info', 'closeMain: ' + app.isQuiting);
if (app.isQuiting) {
return;
};
e.preventDefault();
const onClose = () => {
const { config } = ConfigManager;
if (config.hideTray) {
Api.exit(mainWindow, '', false);
} else {
mainWindow.hide();
};
};
if (mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false);
mainWindow.once('leave-full-screen', () => onClose());
} else {
onClose();
};
return false;
});
UpdateManager.setWindow(mainWindow);
UpdateManager.init();
MenuManager.setWindow(mainWindow);
MenuManager.initMenu();
MenuManager.initTray();
Api.systemInfo(mainWindow);
installNativeMessagingHost();
ipcMain.removeHandler('Api');
ipcMain.handle('Api', (e, id, cmd, args) => {
const Api = require('./electron/js/api.js');
const win = BrowserWindow.fromId(id);
if (!win) {
console.error('[Api] window is not defined', cmd, id);
return;
};
if (Api[cmd]) {
return Api[cmd].apply(Api, [ win ].concat(args || []));
} else {
console.error('[Api] method not defined:', cmd);
return null;
};
});
};
app.on('ready', () => {
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
'Content-Security-Policy': [ csp.join('; ') ]
}
});
});
ConfigManager.init(waitForLibraryAndCreateWindows);
});
app.on('second-instance', (event, argv) => {
Util.log('info', 'second-instance');
if (!mainWindow) {
return;
};
if (!is.macos) {
deeplinkingUrl = argv.find(arg => arg.startsWith(`${protocol}://`));
};
if (deeplinkingUrl) {
Util.send(mainWindow, 'route', Util.getRouteFromUrl(deeplinkingUrl));
};
if (mainWindow.isMinimized()) {
mainWindow.restore();
};
mainWindow.show();
mainWindow.focus();
});
app.on('before-quit', e => {
Util.log('info', 'before-quit');
if (app.isQuiting) {
app.exit(0);
} else {
e.preventDefault();
Api.exit(mainWindow, '', false);
};
});
app.on('activate', () => {
WindowManager.list.size ? mainWindow.show() : createWindow();
});
app.on('open-url', (e, url) => {
e.preventDefault();
deeplinkingUrl = url;
if (mainWindow) {
Util.send(mainWindow, 'route', Util.getRouteFromUrl(url));
mainWindow.show();
};
});