-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.ts
347 lines (307 loc) · 9.52 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import {
ipcMain,
globalShortcut,
app,
Tray,
BrowserWindow,
dialog,
shell,
remote,
screen
} from 'electron'
import * as fs from 'fs'
import { Ipc, ElmIpc } from './typescript/ipc'
import { DisplayManager } from './typescript/display-manager'
const transparencyDisabled = fs.existsSync(
`${app.getPath('userData')}/NO_TRANSPARENCY`
)
const autoUpdater = require('electron-updater').autoUpdater
autoUpdater.requestHeaders = { 'Cache-Control': 'no-cache' }
require('electron-debug')({
enabled: true // enable debug shortcuts in prod build
})
import * as ua from 'universal-analytics'
import { Analytics } from './typescript/analytics'
let analytics = new Analytics()
import * as path from 'path'
import * as url from 'url'
const log = require('electron-log')
const assetsDirectory = path.join(__dirname, 'assets')
const packageJson = require('./package.json')
const version: string = packageJson.version
const appDataPath = app.getPath('userData')
let currentMobstersFilePath: string = path.join(appDataPath, 'active-mobsters')
import * as bugsnag from 'bugsnag'
const isLocal = require('electron-is-dev')
log.info(`Running version ${version}`)
let checkForUpdates = true
let releaseStage = isLocal ? 'development' : 'production'
bugsnag.register('032040bba551785c7846442332cc067f', {
autoNotify: true,
appVersion: version,
releaseStage: releaseStage
})
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (displayManager) {
displayManager.showMain()
}
})
if (shouldQuit) {
app.exit()
}
function writeToFile(filePath: string, fileContents: string) {
fs.writeFile(filePath, fileContents, function(err) {
if (err) {
console.log(err)
}
})
}
function updateMobsterNamesFile(currentMobsterNames: string) {
writeToFile(currentMobstersFilePath, currentMobsterNames)
}
function showFeedbackForm() {
new BrowserWindow({ show: true, frame: true, alwaysOnTop: true }).loadURL(
'https://dillonkearns.typeform.com/to/k9P6iV'
)
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let timerWindow: Electron.BrowserWindow | null,
tray: Electron.Tray,
displayManager: DisplayManager
const timerHeight = 130
const timerWidth = 150
const onMac = /^darwin/.test(process.platform)
const onWindows = /^win/.test(process.platform)
function positionWindowLeft(window: Electron.BrowserWindow) {
let { width, height } = screen.getPrimaryDisplay().workAreaSize
window.setPosition(0, height - timerHeight)
}
function positionWindowRight(window: Electron.BrowserWindow) {
const { width, height } = screen.getPrimaryDisplay().workAreaSize
window.setPosition(width - timerWidth, height - timerHeight)
}
function startTimer(flags: any) {
timerWindow = newTransparentOnTopWindow({
width: timerWidth,
height: timerHeight,
focusable: false,
show: false
})
timerWindow.setAlwaysOnTop(true, 'floating', 1)
timerWindow.setVisibleOnAllWorkspaces(true)
timerWindow.once('ready-to-show', () => {
timerWindow && timerWindow.show()
})
timerWindow.webContents.on('crashed', function() {
bugsnag.notify('crashed', { details: 'timerWindow crashed' })
})
timerWindow.on('unresponsive', function() {
bugsnag.notify('unresponsive', { details: 'timerWindow unresponsive' })
})
positionWindowRight(timerWindow)
ipcMain.once('timer-flags', (event: any) => {
event.returnValue = flags
})
let timerFile = transparencyDisabled ? 'opaque-timer' : 'transparent-timer'
let timerPathName = path.join('pages', timerFile)
let nodeDevEnv = process.env.NODE_ENV === 'dev'
let timerProdUrl = url.format({
pathname: path.join(__dirname, '/pages', `${timerFile}.prod.html`),
protocol: 'file:'
})
let timerDevUrl = url.format({
hostname: 'localhost',
pathname: path.join('pages', `${timerFile}.dev.html`),
port: '8080',
protocol: 'http',
slashes: true
})
console.log('timer file name:', nodeDevEnv ? timerDevUrl : timerProdUrl)
timerWindow.loadURL(nodeDevEnv ? timerDevUrl : timerProdUrl)
}
ipcMain.on('timer-mouse-hover', (event: any) => {
if (timerWindow) {
let [x, y] = timerWindow.getPosition()
if (x === 0) {
positionWindowRight(timerWindow)
} else {
positionWindowLeft(timerWindow)
}
}
})
ipcMain.on('get-active-mobsters-path', (event: any) => {
event.returnValue = currentMobstersFilePath
})
function closeTimer() {
if (timerWindow) {
timerWindow.close()
timerWindow = null
}
}
function onClickTrayIcon() {
showStopTimerDialog()
}
const createTray = () => {
tray = new Tray(path.join(assetsDirectory, 'tray-icon.png'))
tray.on('right-click', onClickTrayIcon)
tray.on('double-click', onClickTrayIcon)
tray.on('click', onClickTrayIcon)
}
function newTransparentOnTopWindow(
additionalOptions: Electron.BrowserWindowConstructorOptions
) {
const transparentWindowDefaultOptions: Electron.BrowserWindowConstructorOptions = {
transparent: !transparencyDisabled,
frame: false,
alwaysOnTop: true
}
return new BrowserWindow({
...transparentWindowDefaultOptions,
...additionalOptions
})
}
function onReady() {
app.dock.hide() // needed to support fullscreen mode in OS X, see https://github.com/electron/electron/issues/3302
displayManager = new DisplayManager(
transparencyDisabled,
bugsnag,
closeTimer,
(ipc: ElmIpc) => {
if (ipc.message === 'ShowFeedbackForm') {
showFeedbackForm()
} else if (ipc.message === 'ShowScriptInstallInstructions') {
displayManager.showScripts()
} else if (ipc.message === 'Hide') {
displayManager.toggleMain()
} else if (ipc.message === 'Quit') {
app.quit()
} else if (ipc.message === 'QuitAndInstall') {
autoUpdater.quitAndInstall()
} else if (ipc.message === 'ChangeShortcut') {
globalShortcut.unregisterAll()
if (ipc.data !== '') {
setShowHideShortcut(ipc.data)
}
} else if (ipc.message === 'OpenExternalUrl') {
displayManager.hideMain()
shell.openExternal(ipc.data)
} else if (ipc.message === 'StartTimer') {
startTimer(ipc.data)
analytics.trackEvent({
category: 'timer',
action: ipc.data.isBreak ? 'start-break' : 'start-timer',
label: '',
value: ipc.data.minutes
})
displayManager.hideMain()
} else if (ipc.message === 'SaveActiveMobstersFile') {
updateMobsterNamesFile(ipc.data)
} else if (ipc.message === 'NotifySettingsDecodeFailed') {
bugsnag.notify('settings-decode-failure', { decodeError: ipc.data })
} else if (ipc.message === 'TrackEvent') {
analytics.trackEventParams(ipc.data)
} else if (ipc.message === 'TrackPage') {
analytics.trackPage(ipc.data)
} else {
const exhaustiveCheck: never = ipc
}
}
)
createTray()
setupAutoUpdater()
}
// 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.
app.on('ready', onReady)
app.on('before-quit', () => analytics.endSession())
app.on('activate', function() {
// 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 (displayManager) {
displayManager.showMain()
}
})
function setupAutoUpdater() {
autoUpdater.logger = log
autoUpdater.on('checking-for-update', () => {
log.info('checking-for-update')
})
autoUpdater.on('error', (ev: any, err: any) => {
bugsnag.notify('autoUpdate error', { details: err })
checkForUpdates = true
})
autoUpdater.on('update-available', () => {
log.info('update-available')
checkForUpdates = false
})
autoUpdater.on('update-downloaded', (versionInfo: any) => {
log.info('update-downloaded: ', versionInfo)
displayManager
.getMainWindow()
.webContents.send('update-downloaded', versionInfo)
})
autoUpdater.on('update-not-available', () => {
log.info('update-not-available')
})
if (!isLocal) {
autoUpdater.checkForUpdates()
log.info('About to set up interval')
let myCheckForUpdates = () => {
log.info('About to check for updates on interval')
if (checkForUpdates) {
autoUpdater.checkForUpdates()
}
}
setInterval(myCheckForUpdates, 120 * 1000)
}
}
function setShowHideShortcut(shortcutString: string) {
globalShortcut.register(shortcutString, () => {
analytics.trackEventParams({
ec: 'shortcut',
ea: 'show-hide',
el: shortcutString,
ev: undefined
})
showStopTimerDialog()
})
}
let dialogDisplayed = false
function showStopTimerDialog() {
if (dialogDisplayed) {
return
}
dialogDisplayed = true
if (timerWindow) {
app.focus() // ensure that app is focused so dialog appears in foreground
let dialogActionIndex = dialog.showMessageBox({
type: 'warning',
buttons: ['Stop timer', 'Keep it running'],
message: 'Stop the timer?',
cancelId: 1
})
if (dialogActionIndex === 1) {
analytics.trackEventParams({
ec: 'stop-timer',
ea: 'cancel',
el: '',
ev: undefined
})
} else {
closeTimer()
displayManager.showMain()
analytics.trackEventParams({
ec: 'stop-timer',
ea: 'confirm',
el: '',
ev: undefined
})
}
} else {
displayManager.toggleMain()
}
dialogDisplayed = false
}