-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
115 lines (97 loc) · 2.78 KB
/
index.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
const { spawn } = require('child_process');
const { app, BrowserWindow, Menu, Tray, ipcMain } = require('electron');
const url = require('url');
const path = require('path');
let mainWindow;
let appTray;
app.on('ready', createMainWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createMainWindow();
}
});
function createMainWindow() {
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
mainWindow = new BrowserWindow();
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'views/index.html'),
protocol: 'file',
slashes: true
}));
mainWindow.on('closed', () => {
console.log('Closing app...');
mainWindow = null;
});
mainWindow.on('minimize', function (event) {
event.preventDefault();
mainWindow.hide();
});
mainWindow.on('show', function () {
appTray.setHighlightMode('always');
});
appTray = new Tray(path.join(__dirname, 'assets/icons/png/smol_logo.jpeg'));
appTray.setToolTip('Hex');
appTray.on('click', () => {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
});
}
// Main menu template
const mainMenuTemplate = [
{
label: 'Options',
submenu: [
{
label: 'Created by da3m0ns',
},
{
label: 'Show Dev Tools',
accelerator: process.platform === 'darwin' ? 'command+D' : 'Ctrl+D',
click(e, focusedWindow) {
focusedWindow.toggleDevTools();
}
},
{
label: 'Exit',
accelerator: process.platform === 'darwin' ? 'command+Q' : 'Ctrl+Q',
click() {
app.quit();
}
}
]
}
];
// If macOS, add first menu item
if (process.platform === 'darwin') {
mainMenuTemplate.unshift({
label: app.getName()
});
}
// IPC communication to handle launching external apps
ipcMain.on('launch-app', (event, appName) => {
if (appName === 'notepad') {
runExternalProcess('notepad.exe');
} else if (appName === 'vscode') {
runExternalProcess('code');
}
});
function runExternalProcess(command) {
const child = spawn(command, [], { windowsHide: false });
child.on('error', (err) => {
console.error(`Error launching ${command}: ${err}`);
});
child.on('exit', (code) => {
if (code !== 0) {
console.error(`${command} process exited with code ${code}`);
}
});
}
module.exports = {
createMainWindow,
runExternalProcess
};