generated from maximegris/angular-electron
-
Notifications
You must be signed in to change notification settings - Fork 16
/
electron-shell.ts
262 lines (216 loc) · 7.14 KB
/
electron-shell.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import * as electron from 'electron';
import * as os from 'os';
import {app, BrowserWindow, dialog, ipcMain, Menu, MessageBoxSyncOptions, nativeImage, shell, Tray} from 'electron';
import * as path from 'path';
import {join} from 'path';
import {NEW_CONFIG_PATH} from "./server/path.utils";
import {bootstrapTsED} from "./server/server.bootstrap";
import {getAppRootPath, getElectronPath, isInElectron} from "./server/file.utilts";
// TODO Refactor / Abstract the electron usages
app.disableHardwareAcceleration();
var fs = require("fs");
let win: BrowserWindow = null;
const args = process.argv.slice(1),
serve = args.some(val => val === '--serve')
&& !args.some(val => val === '--built-serve');
// todo add electron "dev"-mode to not use "--serve"
// Starts the Backend and gets the port to use it
const portPromise = bootstrapTsED().then(({expressServer}) => {
return expressServer.get('port');
});
// fix https://github.com/electron/electron/issues/1344#issuecomment-464480796
const openExternalLinksInOSBrowser = (event, url) => {
if (url.match(/.*localhost.*/gi) === null && (url.startsWith('http:') || url.startsWith('https:'))) {
event.preventDefault();
shell.openExternal(url);
}
};
/**
* Adds the source image in several layers of scaling to the target image as "presentation", which
* define the same image at different scaling factors.
*
* The base size defines the standard size at 1.0 scale.
*/
const addPresentationIcons = (
target: electron.NativeImage,
source: electron.NativeImage,
baseSize: number,
): void => {
[1, 1.25, 1.5, 2, 3].forEach(scale => {
target.addRepresentation({
scaleFactor: scale,
width: baseSize * scale,
height: baseSize * scale,
buffer: source.resize({ width: baseSize * scale, height: baseSize * scale }).toBitmap(),
});
});
};
/**
* Create the tray icon for the OSs tray area. The shape of the image is different, depending on the
* platform, like different coloring and different base sizes.
*/
const createTrayIcon = (appRootPath: string): electron.NativeImage => {
const trayIconPath = join(appRootPath, 'assets/icons/icon-128x128.png');
const trayIcon = nativeImage.createFromPath(trayIconPath);
const scaledIcon = nativeImage.createEmpty();
switch (os.platform()) {
case 'darwin':
addPresentationIcons(scaledIcon, trayIcon, 16);
scaledIcon.setTemplateImage(true);
case 'win32':
addPresentationIcons(scaledIcon, trayIcon, 16);
default: // Linux, OpenBSD, FreeBSD and everything else
addPresentationIcons(scaledIcon, trayIcon, 32);
}
return scaledIcon;
};
var initPath = path.join(NEW_CONFIG_PATH, "init.json");
var data = { bounds: { width: 800, height: 600 }};
try {
data = JSON.parse(fs.readFileSync(initPath, 'utf8'));
}
catch(e) {
}
let alreadyAllowedToBeHidden = false;
let tray;
/**
* Create the application's tray icon in the system menu bar, setting up the platform dependent icon
* as well as menu items and further tray information.
*/
function createTray () {
function toggle () {
if (win !== null) {
win.close();
} else {
createWindow();
}
}
const appRootPath = getAppRootPath();
const trayIcon = createTrayIcon(appRootPath);
tray = new Tray(trayIcon);
let template = [{
label: 'Toggle',
click: () => {
// when that option is pressed, you don't need another question if hide / quit
alreadyAllowedToBeHidden = true;
toggle();
}
},
{
label: 'Quit',
click: () => {
// when that option is pressed, you don't need another question if hide / quit
alreadyAllowedToBeHidden = true;
app.quit();
}
}];
var trayMenu = Menu.buildFromTemplate(template);
tray.setContextMenu(trayMenu);
tray.setToolTip('Meme-Box');
tray.addListener('double-click', (event: KeyboardEvent) => {
alreadyAllowedToBeHidden = true;
toggle();
});
}
function createWindow(): BrowserWindow {
const {width, height} = data.bounds;
const electronPath = getElectronPath();
console.info({ isInElectron, __dirname });
// Create the browser window.
win = new BrowserWindow({
width, height,
webPreferences: {
// Disabled Node integration
nodeIntegration: false,
// protect against prototype pollution
contextIsolation: true,
// Preload script
preload: path.join(electronPath, 'preload.js'),
},
});
portPromise.then((port) => {
if (serve) {
win.webContents.openDevTools();
require('electron-reload')(__dirname, {
electron: electron,
argv: ['--serve']
});
win.loadURL(`http://localhost:4200?port=${port}`);
} else {
win.loadURL( `http://localhost:${port}`);
}
});
win.on("close", function(e) {
var data = {
bounds: win.getBounds()
};
fs.writeFileSync(initPath, JSON.stringify(data));
// ask it hide or close if its triggered from the Electron Window
if (!alreadyAllowedToBeHidden) {
const result = dialog.showMessageBoxSync(win, {
message: 'Hide to tray or quit?',
buttons: [
'To Tray',
'Quit Meme-Box'
]
} as MessageBoxSyncOptions);
if (result === 0) {
// do nothing :)
alreadyAllowedToBeHidden = true;
}
if (result === 1) {
app.quit();
}
}
});
const ipcSelectDirEvent = async (event) => {
const result = await dialog.showOpenDialog(win, {
properties: ['openDirectory']
})
console.log('directories selected', result.filePaths);
const arg = result.filePaths?.[0] ?? '';
event.sender.send('dir-selected', arg);
};
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
ipcMain.off('select-dirs', ipcSelectDirEvent);
});
win.webContents.on('new-window', openExternalLinksInOSBrowser);
win.webContents.on('will-navigate', openExternalLinksInOSBrowser);
ipcMain.on('select-dirs', ipcSelectDirEvent);
return win;
}
try {
// 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.
// Added 400 ms to fix the black background issue while using transparent window. More detais at https://github.com/electron/electron/issues/15947
app.on('ready', () => {
// create the first window on startup
setTimeout(createWindow, 400)
createTray();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
// app.quit();
// TODO check if it still works with Cmd Q on Mac - because of the tray icon
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
} catch (e) {
// Catch Error
throw e;
}