-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathmain.js
195 lines (172 loc) · 5.38 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
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
import { app, globalShortcut, BrowserWindow, Menu, protocol, ipcMain, Tray, powerMonitor } from 'electron'
import log from 'electron-log'
import { autoUpdater } from "electron-updater"
import path from 'path'
import Analytics from 'electron-google-analytics';
const analytics = new Analytics('UA-111389782-1');
import Config from './config.json'
import {machineIdSync} from 'node-machine-id'
import Raven from 'raven'
import Positioner from 'electron-positioner'
import Store from 'electron-store'
import open from "open"
const store = new Store();
let mainWindow
let updateAvailable = false
let tray = null
// Capture user's unique machine ID
let clientID;
try {
clientID = machineIdSync()
} catch (error) {
clientID = 'no-machineid-detected'
}
//-------------------------------------------------------------------
// Logging
//-------------------------------------------------------------------
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
Raven.config('https://e254805a5b5149d48d6561ae035dd19c:[email protected]/260576').install();
//-------------------------------------------------------------------
// Main app logic
//-------------------------------------------------------------------
const sendStatusToWindow = (text) => {
log.info(text);
if (text === 'Update downloaded') {
updateAvailable = true
}
mainWindow.webContents.send('update', {
updateAvailable: updateAvailable,
updateInfo: text
});
}
function createWindow() {
// Auto Update logic
autoUpdater.checkForUpdatesAndNotify();
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
})
autoUpdater.on('update-available', (info) => {
sendStatusToWindow('Update available.');
})
autoUpdater.on('update-not-available', (info) => {
sendStatusToWindow('Update not available.');
})
autoUpdater.on('error', (err) => {
sendStatusToWindow('Error in auto-updater. ' + err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
sendStatusToWindow(log_message);
})
autoUpdater.on('update-downloaded', (info) => {
updateAvailable = true
sendStatusToWindow('Update downloaded');
});
mainWindow = new BrowserWindow({
width: 340,
height: 435,
transparent: true,
frame: false,
webPreferences: {
devTools: true
}
})
mainWindow.setVisibleOnAllWorkspaces(true);
app.dock.hide();
tray = new Tray(path.join(__dirname, 'assets', 'btcTemplate.png'))
tray.setTitle("Fetching...")
// helper to get image from config file
const getImage = type => {
let crypto = Config.tickers.filter(x => type === x.symbol)[0];
if (crypto && crypto.image && crypto.image.length > 0) {
return path.join(__dirname, 'assets', crypto.image)
} else {
return path.join(__dirname, 'assets', 'blankTemplate.png')
}
}
// Hidden shortcut for debugging
globalShortcut.register('CommandOrControl+Shift+Control+Option+Space+D+F', () => {
app.dock.show();
mainWindow.webContents.openDevTools()
})
// Record event of initial load
analytics.event('App', 'initialLoad', {
evLabel: `version ${app.getVersion()}`,
clientID
})
.then((response) => {
log.info(response)
}).catch((err) => {
log.error(err)
});
// Heartbeat and Check for updates
setInterval(() => {
if(!updateAvailable){
autoUpdater.checkForUpdatesAndNotify();
}
analytics.event('App', 'heartBeat', {
evLabel: `version ${app.getVersion()}`,
clientID
})
.then((response) => {
log.info(response)
}).catch((err) => {
log.error(err)
});
}, 30000);
// set tray tooltip and load react app (renderer)
tray.setToolTip('Crypto Bar')
mainWindow.loadURL('file://' + __dirname + '/index.html')
// Get default preferences or use saved preferences
store.set('preferences', store.get('preferences') || Config.defaultPreferences);
// position window to the tray area
const positioner = new Positioner(mainWindow)
let bounds = tray.getBounds()
positioner.move('trayCenter', bounds)
// Handle sleep/resume events
powerMonitor.on('suspend', () => {
mainWindow.webContents.send('suspend', 'suspended');
})
powerMonitor.on('resume', () => {
mainWindow.webContents.send('resume', 'resumed');
})
// Main window behavior
mainWindow.on('blur', () => {
mainWindow.hide()
})
tray.on('click', () => {
bounds = tray.getBounds()
positioner.move('trayCenter', bounds)
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show()
})
mainWindow.on('show', () => {
tray.setHighlightMode('always')
})
mainWindow.on('hide', () => {
tray.setHighlightMode('never')
})
mainWindow.on('closed', () => {
mainWindow = null
})
// Give renderer access to the following methods
exports.store = store
exports.app = app
exports.open = open
exports.getImage = getImage
exports.tray = tray
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})