-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
315 lines (267 loc) · 8.59 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
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
const { app, BrowserWindow, ipcMain, shell, Tray, Menu, dialog } = require('electron')
const log = require('electron-log')
const { PATHS, settings, cache, DELETE_ALL_ICONS } = require('./modules/data')
const hass = require('./modules/hass')
const itemBuilder = require('./modules/itemBuilder')
const { importConfig, exportConfig, openHASSInBrowser } = require('./modules/configuration')
const { loadIcons } = require('./modules/iconDownloader')
// set some variables
const isWindows = process.platform === 'win32'
let refreshInterval = settings.get('refreshInterval') * 60 * 1000 // default to 30 minutes
let win, tray, downloader
if (isWindows && app.isPackaged && process.argv.length >= 2) {
(async () => {
await app.whenReady()
dialog.showErrorBox({
title: 'Feature Not Supported',
message: 'Please import the configuration file from the preferences window.'
})
process.exit()
})()
}
// function to open icon downloader
const openIconDownloader = () => {
loadIcons()
if (downloader && downloader.isDestroyed && !downloader.isDestroyed()) return downloader.show()
downloader = new BrowserWindow({
width: 600,
height: 350,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
downloader.loadURL(`file://${PATHS.PAGES.ICON_DOWNLOADER}`)
}
// open the preferences window
const openPreferences = () => {
log.info('Opening preferences window')
// show the preference window if one is already open
if (win) return win.show()
// create the preference window
win = new BrowserWindow({
width: 850,
height: 720,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
// once the window loads, send the settings and hass status
win.webContents.on('did-finish-load', async () => {
log.info('Preferences window finished loading. Sending settings, and hass status.')
win.webContents.send('settings', {
...settings.getAll(),
version: app.getVersion()
})
win.webContents.send('hassStatus', await hass.status())
})
// load the html
win.loadURL(`file://${PATHS.PAGES.PREFERENCES}`)
}
// open a configuration file
const openLoadConfigDialog = () => {
log.info('Opening load config flow')
const deleteAll = dialog.showMessageBoxSync({
type: 'question',
buttons: ['Cancel', 'Delete', 'Merge'],
defaultId: 2,
cancelId: 0,
message: 'What would you like to do with your pre-existing icons?',
title: 'Delete or Merge?'
})
// if cancel, return
if (deleteAll === 0) return
// if delete, delete icons
if (deleteAll === 1) DELETE_ALL_ICONS()
// log the action
log.info(`The user chose to ${deleteAll === 1 ? 'delete' : 'merge'} all icons.`)
const path = dialog.showOpenDialogSync({
title: 'Open Configuration File',
message: 'This file should end in .bar',
properties: ['openFile'],
filters: [
{ name: 'bar', extensions: ['bar'] }
]
})
log.info(path ? `Loading config file ${path}` : 'No config file selected. Doing nothing.')
if (!path) return
importConfig(path[0], () => { buildTray(openPreferences) })
}
// build tray menu
const buildTray = async () => {
log.info('Building tray menu')
// get latest config and hass status
const { config } = settings.getAll()
const hassStatus = await hass.status()
// define icon
const trayIcon = config.icon || config.iconTemplate ? PATHS.ICON_PATH_GENERATOR(config.icon || (await hass.render(config.iconTemplate))) : PATHS.MENUBAR_ICONS.DEFAULT
// create the tray if one doesnt exist
if (!tray) tray = new Tray(trayIcon)
// update tray text to reflect that an update is taking place
if (config.icon || config.iconTemplate) tray.setTitle(tray.getTitle().substr(0, 33) + '*')
// open tray on click on windows
isWindows && tray.on('click', () => tray.popUpContextMenu())
tray.on('right-click', openHASSInBrowser)
// set the tray icon to be transparent to indicate reload
if (!config.icon && !config.iconTemplate) tray.setImage(PATHS.MENUBAR_ICONS.TRANSPARENT)
// initialize the menuTemplate and trayTitle
let menuTemplate = []
let trayTitle = ''
// if not connected to hass...
if (!hassStatus.connected) {
log.info('Not connected to hass')
// set tray image to red, and clear title
tray.setImage(PATHS.MENUBAR_ICONS.ERROR)
tray.setTitle('')
// add unable to connect and retry items to window
menuTemplate.push({
label: 'Unable To Connect',
enabled: false,
icon: PATHS.MENUBAR_ICONS.WARNING_ICON
}, {
label: 'Retry',
click: () => { buildTray() }
})
} else {
// otherwise, set the tray title
if (!isWindows && config.titleTemplate) {
trayTitle = await hass.render(config.titleTemplate)
} else if (!isWindows && config.title) {
trayTitle = config.title
}
log.info('Building items')
// build the tray items
menuTemplate = await itemBuilder(config.items, () => { buildTray() })
// set the tray image
tray.setImage(trayIcon)
}
// add constant items
menuTemplate.push(
{ type: 'separator' },
{
label: 'Preferences',
click: (_, __, event) => {
event.shiftKey ? openLoadConfigDialog() : openPreferences()
}
},
{ label: 'Quit', click: () => { app.quit() } }
)
// set the tray context menu
tray.setContextMenu(Menu.buildFromTemplate(menuTemplate))
// set the tray title
// substr makes sure the title is not too long
tray.setTitle(trayTitle.substr(0, 34))
// clear the cache for next time
cache.clear()
}
// when app is opened from file...
app.on('open-file', async (event, path) => {
log.info(`Config file opened: ${path}`)
// prevent default
event.preventDefault()
// load the file
importConfig(path, () => { buildTray(openPreferences) })
// if there is a window open... reload it
if (win) win.reload()
})
// when app is ready...
app.on('ready', async () => {
loadIcons()
log.info('App is ready')
// hide from dock
!isWindows && app.dock.hide()
const autoRebuild = () => {
// build the tray
buildTray()
// get the refresh interval
refreshInterval = settings.get('refreshInterval') * 60 * 1000
// if refreshInterval is not 0, set timeout for refresh
refreshInterval !== 0 && setTimeout(autoRebuild, refreshInterval)
}
// build the tray
autoRebuild()
})
// when the window is closed...
app.on('window-all-closed', () => {
// set the windows to null
win = null
downloader = null
// this also prevents the default behavior of quitting the app
})
// IPC
// when openIconsFolder is recieved
ipcMain.on('openIconsFolder', (_, __) => {
log.info('Opening icons folder')
// open the icons folder
shell.openPath(PATHS.ICONS_FOLDER)
})
// when connect is recieved
ipcMain.on('connect', async (event, data) => {
log.info('Testing HASS connection')
// check the connection and return the results
event.returnValue = await hass.test(data)
})
// when save is recieved
ipcMain.on('save', async (event, data) => {
log.info('Saving config')
// try...
try {
// set the settings
settings.setAll(data)
// set refresh interval
refreshInterval = data.refreshInterval * 60 * 1000
// set whether or not to open on startup
app.setLoginItemSettings({ openAtLogin: data.openOnStart })
// reload home assistant connection
hass.reload()
// build the tray
buildTray(openPreferences)
// if no errors then return that it was successful
event.returnValue = {
success: true
}
} catch (err) {
log.info('Failed to save config')
log.error(err)
// on error... return the error
event.returnValue = {
success: false,
message: err.message
}
}
})
// on save config...
ipcMain.on('exportConfig', (_, __) => {
log.info('Exporting config')
// open the save dialog
const saveDir = dialog.showSaveDialogSync(win, {
filters: [{ name: 'Config', extensions: ['bar'] }],
showsTagField: false,
defaultPath: 'config.bar'
})
log.info(saveDir ? `Saving config to ${saveDir}` : 'No save location specified. Doing nothing.')
// if no save directory was selected... return
if (!saveDir) return
try {
exportConfig(saveDir)
} catch (err) {
log.info('Failed to export config')
log.error(err)
dialog.showErrorBox('Cannot Save Configuration', err.message)
}
})
// on open icon downloader...
ipcMain.on('open-icon-downloader', (_, __) => {
openIconDownloader()
})
// on load from file...
ipcMain.on('importConfig', (_, __) => {
openLoadConfigDialog()
})
// on exit...
ipcMain.on('exit', (_, __) => {
// close the window
if (win) win.close()
if (downloader && !downloader.isDestroyed()) downloader.close()
})