forked from KhalisFoundation/sttm-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
250 lines (231 loc) · 6.42 KB
/
app.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const electron = require('electron');
const Store = require('./www/js/store.js');
const defaultPrefs = require('./www/js/defaults.json');
const { autoUpdater } = require('electron-updater');
const log = require('electron-log');
const { app, BrowserWindow, dialog, ipcMain } = electron;
const store = new Store({
configName: 'user-preferences',
defaults: defaultPrefs,
});
const appVersion = app.getVersion();
let mainWindow;
let viewerWindow = false;
let changelogWindow = false;
let manualUpdate = false;
const viewerWindowPos = {};
function openChangelog() {
changelogWindow = new BrowserWindow({
width: 725,
height: 800,
show: false,
});
changelogWindow.webContents.on('did-finish-load', () => {
changelogWindow.show();
});
changelogWindow.loadURL(`file://${__dirname}/www/changelog.html`);
// Update changelog last seen pref when seen
changelogWindow.on('close', () => {
store.set('changelog-seen', appVersion);
changelogWindow = false;
});
}
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
// autoUpdater events
autoUpdater.on('checking-for-update', () => {
mainWindow.webContents.send('checking-for-update');
});
autoUpdater.on('update-available', () => {
mainWindow.webContents.send('update-available');
});
autoUpdater.on('update-not-available', () => {
mainWindow.webContents.send('update-not-available');
if (manualUpdate) {
dialog.showMessageBox({
type: 'info',
buttons: [
'OK',
],
defaultId: 0,
title: 'No update available.',
message: 'No update available.',
detail: `Version ${appVersion} is the latest version.`,
});
}
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('update-downloaded');
dialog.showMessageBox({
type: 'info',
buttons: [
'Dismiss',
'Install & Restart',
],
defaultId: 1,
title: 'Update available.',
message: 'Update available.',
detail: 'Update downloaded and ready to install',
cancelId: 0,
}, (response) => {
if (response === 1) {
autoUpdater.quitAndInstall();
}
});
});
autoUpdater.on('error', () => {
if (manualUpdate) {
// showUpdate('update-error');
}
});
function checkForUpdates(manual = false) {
if (process.env.NODE_ENV !== 'development') {
if (manual) {
manualUpdate = true;
}
autoUpdater.checkForUpdates();
}
}
function checkForExternalDisplay() {
const electronScreen = electron.screen;
const displays = electronScreen.getAllDisplays();
let externalDisplay = null;
Object.keys(displays).forEach((i) => {
if (displays[i].bounds.x !== 0 || displays[i].bounds.y !== 0) {
externalDisplay = displays[i];
}
});
if (externalDisplay) {
viewerWindowPos.x = externalDisplay.bounds.x + 50;
viewerWindowPos.y = externalDisplay.bounds.y + 50;
viewerWindowPos.w = externalDisplay.size.width;
viewerWindowPos.h = externalDisplay.size.height;
return true;
}
return false;
}
function createViewer(ipcData) {
const isExternal = checkForExternalDisplay();
if (isExternal) {
viewerWindow = new BrowserWindow({
width: 800,
height: 600,
x: viewerWindowPos.x,
y: viewerWindowPos.y,
fullscreen: true,
autoHideMenuBar: true,
show: false,
titleBarStyle: 'hidden',
frame: (process.platform !== 'win32'),
});
viewerWindow.loadURL(`file://${__dirname}/www/viewer.html`);
viewerWindow.webContents.on('did-finish-load', () => {
viewerWindow.show();
const [width, height] = viewerWindow.getSize();
mainWindow.webContents.send('presenter-view', {
width,
height,
});
mainWindow.focus();
if (typeof ipcData !== 'undefined') {
viewerWindow.webContents.send(ipcData.send, ipcData.data);
}
});
viewerWindow.on('enter-full-screen', () => {
mainWindow.focus();
});
viewerWindow.on('focus', () => {
// mainWindow.focus();
});
viewerWindow.on('closed', () => {
viewerWindow = false;
mainWindow.webContents.send('remove-presenter-view');
});
viewerWindow.on('resize', () => {
const [width, height] = viewerWindow.getSize();
mainWindow.webContents.send('presenter-view', {
width,
height,
});
});
}
}
app.on('ready', () => {
const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({
minWidth: 800,
minHeight: 600,
width,
height,
frame: (process.platform !== 'win32'),
show: false,
titleBarStyle: 'hidden',
});
mainWindow.webContents.on('did-finish-load', () => {
if (checkForExternalDisplay()) {
mainWindow.webContents.send('presenter-view', {
width: viewerWindowPos.w,
height: viewerWindowPos.h,
});
}
mainWindow.show();
checkForUpdates();
// Show changelog if last version wasn't seen
const lastSeen = store.get('changelog-seen');
if (lastSeen !== appVersion) {
openChangelog();
}
if (!viewerWindow) {
createViewer();
}
});
mainWindow.loadURL(`file://${__dirname}/www/index.html`);
// Close all other windows if closing the main
mainWindow.on('close', () => {
if (viewerWindow && !viewerWindow.isDestroyed()) {
viewerWindow.close();
}
if (changelogWindow && !changelogWindow.isDestroyed()) {
changelogWindow.close();
}
});
});
// 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();
// }
});
ipcMain.on('checkForUpdates', checkForUpdates);
ipcMain.on('quitAndInstall', () => autoUpdater.quitAndInstall());
ipcMain.on('show-line', (event, arg) => {
if (viewerWindow) {
viewerWindow.webContents.send('show-line', arg);
} else {
createViewer({
send: 'show-line',
data: arg,
});
}
});
ipcMain.on('show-text', (event, arg) => {
if (viewerWindow) {
viewerWindow.webContents.send('show-text', arg);
} else {
createViewer({
send: 'show-text',
data: arg,
});
}
});
ipcMain.on('update-settings', () => {
if (viewerWindow) {
viewerWindow.webContents.send('update-settings');
}
});
exports.openChangelog = openChangelog;
exports.appVersion = appVersion;
exports.checkForUpdates = checkForUpdates;
exports.autoUpdater = autoUpdater;