-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.ts
215 lines (189 loc) · 5.13 KB
/
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
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
import { app, BrowserWindow, screen, Menu, Tray, ipcMain } from 'electron';
import * as path from 'path';
import * as url from 'url';
import Client from './lib/client';
import * as log from 'electron-log';
import * as settings from './lib/settings';
import * as remoteMain from '@electron/remote/main';
remoteMain.initialize();
log.transports.console.level = 'info'
log.transports.file.level = 'info'
let mainWindow, serve, client, multiInstance;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');
multiInstance = args.some(val => val === '--multi-instance');
let tray: Tray
let isHidden = false;
// listen if we toggle the tray icon
settings.setOnToggleTrayHander(toggleTray);
function createApp() {
createWindow();
setupIPC();
}
function createWindow() {
// Create the browser window.
const electronScreen = screen;
const size = electronScreen.getPrimaryDisplay().workAreaSize;
let height = 600;
let width = 900;
if (!app.isPackaged) {
height = size.height;
width = size.width;
}
mainWindow = new BrowserWindow({
center: true,
minWidth: 800,
minHeight: 535,
title: 'Altitude',
width: width < size.width ? width : size.width,
height: height < size.height ? height : size.height,
icon: path.join(__dirname, 'assets/icons/png/512x512.png'),
webPreferences: { webSecurity: false, nodeIntegration: true, contextIsolation: false },
frame: process.platform !== 'win32',
titleBarStyle: process.platform === 'linux' ? 'default' : 'hidden'
});
if (serve) {
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`)
});
mainWindow.loadURL('http://localhost:4200');
} else {
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'dist/index.html'),
protocol: 'file:',
slashes: true
}));
}
// setup settings
log.info('Initiate settings');
remoteMain.enable(mainWindow.webContents);
settings.setWindow(mainWindow);
// make any adjustments when settings are ready
const handler = () => {
const appSettings = settings.getSettings();
if (!appSettings) setTimeout(handler, 100);
else {
if (appSettings.fullScreen) mainWindow.maximize();
toggleTray(appSettings.hideTrayIcon);
}
}
handler();
// start client
log.info('Start client');
client = new Client(mainWindow);
// open dev tools
if (!app.isPackaged) mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', () => {
client.destroy();
mainWindow = null;
});
mainWindow.on("close", e => {
if (client.proc) {
closeApp(e);
} else {
e.returnValue = true;
}
});
}
function createTray(force: boolean = false) {
// Create the Tray icon
tray = new Tray(path.resolve(__dirname, 'assets/icons/png/16x16.png'));
tray.setToolTip('Altitude')
tray.on('click', toggleMainWindows);
updateTray(force)
}
function toggleTray(hide) {
if (hide && tray) tray.destroy();
else if (!hide) createTray();
}
function toggleMainWindows(): void {
if (isHidden) {
mainWindow.show()
isHidden = false
updateTray()
} else {
mainWindow.hide()
isHidden = true
updateTray()
}
}
function updateTray(force: boolean = false): void {
if (force || !settings.getSettings().hideTrayIcon) {
const contextMenu: Menu = Menu.buildFromTemplate(
(isHidden
? [{
click: toggleMainWindows,
label: 'Show Altitude',
}]
: [{
click: toggleMainWindows,
label: 'Hide Altitude',
}]
).concat(
[{
click: () => app.quit(),
label: 'Exit',
}]
)
)
tray.setContextMenu(contextMenu)
} else if (isHidden) {
createTray(true);
} else {
toggleTray(true)
}
}
function closeApp(event) {
if (client.proc) {
event.preventDefault();
client.stop().then(() => {
app.quit();
});
}
}
try {
// check single instance
if (!multiInstance && !app.requestSingleInstanceLock()) {
app.quit()
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
// create app
app.on('ready', createApp);
// register on quit handler to close client if running
app.on('before-quit', closeApp);
// 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();
}
});
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 (mainWindow === null) {
createApp();
}
});
}
} catch (e) {
// Catch Error
// throw e;
}
function setupIPC() {
ipcMain.on('window', (event, cmd, data) => {
log.debug('Received IPC:window', cmd, data);
switch (cmd) {
case 'HIDE':
toggleMainWindows();
break;
}
});
}