forked from cncjs/cncjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
117 lines (100 loc) · 3.76 KB
/
main.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
import '@babel/polyfill';
import { app, Menu } from 'electron';
import Store from 'electron-store';
import chalk from 'chalk';
import mkdirp from 'mkdirp';
import menuTemplate from './electron-app/menu-template';
import WindowManager from './electron-app/WindowManager';
import launchServer from './server-cli';
import pkg from './package.json';
// The selection menu
const selectionMenu = Menu.buildFromTemplate([
{ role: 'copy' },
{ type: 'separator' },
{ role: 'selectall' }
]);
// The input menu
const inputMenu = Menu.buildFromTemplate([
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ type: 'separator' },
{ role: 'selectall' }
]);
let windowManager = null;
const main = () => {
// https://github.com/electron/electron/blob/master/docs/api/app.md#apprequestsingleinstancelock
const gotSingleInstanceLock = app.requestSingleInstanceLock();
const shouldQuitImmediately = !gotSingleInstanceLock;
if (shouldQuitImmediately) {
app.quit();
return;
}
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (!windowManager) {
return;
}
const window = windowManager.getWindow();
if (window) {
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
});
const store = new Store();
// Create the user data directory if it does not exist
const userData = app.getPath('userData');
mkdirp.sync(userData);
app.on('ready', async () => {
try {
const res = await launchServer();
const { address, port, mountPoints } = { ...res };
if (!(address && port)) {
console.error('Unable to start the server at ' + chalk.cyan(`http:https://${address}:${port}`));
return;
}
const menu = Menu.buildFromTemplate(menuTemplate({ address, port, mountPoints }));
Menu.setApplicationMenu(menu);
windowManager = new WindowManager();
const url = `http:https://${address}:${port}`;
// The bounds is a rectangle object with the following properties:
// * `x` Number - The x coordinate of the origin of the rectangle.
// * `y` Number - The y coordinate of the origin of the rectangle.
// * `width` Number - The width of the rectangle.
// * `height` Number - The height of the rectangle.
const bounds = {
width: 1280, // Defaults to 1280
height: 768, // Defaults to 768
...store.get('bounds')
};
const options = {
...bounds,
title: `${pkg.name} ${pkg.version}`
};
const window = windowManager.openWindow(url, options);
// Save window size and position
window.on('close', () => {
store.set('bounds', window.getBounds());
});
// https://github.com/electron/electron/issues/4068#issuecomment-274159726
window.webContents.on('context-menu', (event, props) => {
const { selectionText, isEditable } = props;
if (isEditable) {
// Shows an input menu if editable
inputMenu.popup(window);
} else if (selectionText && String(selectionText).trim() !== '') {
// Shows a selection menu if there was selected text
selectionMenu.popup(window);
}
});
} catch (err) {
console.error('Error:', err);
}
});
};
main();