-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
212 lines (180 loc) · 5.15 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const { app, BrowserWindow, ipcMain, dialog, Tray, Menu, nativeImage } = require('electron')
const fs = require('fs')
let mainWindow = null
let tray = null
let config = {}
let authenticated = false
let menuTemplate = [
{
label: 'File',
submenu : [
{label: 'Settings', click() {showSettings()}},
{role: 'quit'},
{type: 'separator'},
{label: 'Logout', click() {logout()}}
]
},
{
label : "View",
submenu : [
{role: 'togglefullscreen'},
{type: 'separator'},
{role: 'reload'},
{role: 'toggledevtools'},
]
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click() { require('electron').shell.openExternal('https://photonix.org') }
}
]
}
]
app.on('ready', () => {
loadConfig()
// Creation of window
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
show: false,
webPreferences: {
nodeIntegration: true,
webviewTag: true
},
icon: 'static/images/logo.png'
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.loadFile('index.html')
// mainWindow.webContents.openDevTools()
let menu = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menu);
// IPC listeners
ipcMain.on('close-main-window', (event, arg) => closeMainWindow())
ipcMain.on('window-visible', (event, arg) => {
event.returnValue = mainWindow.isVisible()
})
ipcMain.on('select-dir', (event, arg) => {
const files = dialog.showOpenDialogSync({
title: 'Select folder',
properties: ['openDirectory']
})
event.returnValue = files
})
ipcMain.on('get-settings', (event) => {
console.log('get-settings')
event.returnValue = config
})
ipcMain.on('set-settings', (event, arg) => {
console.log('set-settings')
let updateContextMenu = false
for (let key in arg) {
console.log(' ' + key + ': ' + arg[key])
config[key] = arg[key]
if (['watchPhotos', 'analyzePhotos'].indexOf(key) > -1) {
updateContextMenu = true
}
}
saveConfig()
if (updateContextMenu) {
setContextMenu()
}
event.returnValue = true
})
})
showMainWindow = () => {
mainWindow.show()
mainWindow.restore()
mainWindow.focus()
}
closeMainWindow = () => {
if (mainWindow.isVisible()) {
mainWindow.hide()
}
else {
mainWindow.destroy()
}
}
quit = () => {
mainWindow.destroy()
}
loadConfig = () => {
let path = app.getPath('userData') + '/config.json'
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
console.log('An error ocurred reading the file: ' + err.message);
return
}
console.log('The file content is: ' + data)
config = JSON.parse(data)
setContextMenu()
})
}
getConfig = () => {
return config
}
saveConfig = () => {
let path = app.getPath('userData') + '/config.json'
let content = JSON.stringify(config)
console.log('Writing to ' + path)
fs.writeFile(path, content, (err) => {
if (err) {
console.log('An error ocurred creating the file: ' + err.message)
}
console.log('The file has been succesfully saved')
})
}
setContextMenu = () => {
// Tray icon amd menu
if (tray === null) {
const icon = nativeImage.createFromDataURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABzklEQVRYw+2XPU7DQBCFPwhQJlIIIEQogB4QSGTTUEMFEjEdHCIH4OcCQQhuAAVbUuYESQURRQ6ATEBCCR2QFDbNWjKWnax/kgaeNJK9Xvs9e97OrOGvYyzifTlgHlgEbMAEWkB7mGJXgHOgoUj94hE4U3MTwyxwDfT6EHujB1wBM3HJi8BrCGJvvAPbUckPge8Y5E58AaUob54EuVuECJPztwTJnWip1TMQ1zoPFELYUkpLSmkJIXRFXOosNS23G4ZhOzAMQ1dAF1h2E457BBwDk0OsJVPAUT8B+yOovntBAnLA6ggErANZ52TCdWEhcE0Wi+Tz+V9jQgjfYwemaVKr1YL6Tx7oeC/sBplHSmnZISGltPqYcccvBfYIu7Dt147XVKfTTkG5XAagUqlQr9d1U+BwPXkHp8NUtoh1wAYstwndKWgHfYGE8eA2oLcO3I9AwH2/QnSjSvGw0ANu3QMpz4QPYA7YGvSkdDpNJpOh2WxSrVYxTVO30d0NmjQTcxcUFC/K6FrYBD4T3pAUwuarpG5Mgvwgqmk2gOeYu6BCXOfm1E6mG4K4C1yEybkOloFTVUh8iVOpVAM4AZaG/WuWVe3baRCmcnmHf4TED6ypmmi/7iVJAAAAAElFTkSuQmCC')
tray = new Tray(icon)
}
const contextMenu = Menu.buildFromTemplate([
{ label: 'Watch for new photos', type: 'checkbox', checked: config.watchPhotos, click() {toggleWatchPhotos()} },
{ label: 'Run analysis on photos', type: 'checkbox', checked: config.analyzePhotos, click() {toggleAnalyzePhotos()} },
{ type: 'separator' },
{ label: 'Show main window', click() {showMainWindow()} },
{ label: 'Close', click() {quit()}},
])
tray.setToolTip('This is my application.')
tray.setContextMenu(contextMenu)
}
login = (server, username, password) => {
console.log(server + ' ' + username + ' ' + password)
authenticated = true
return true
}
logout = () => {
authenticated = false
mainWindow.webContents.executeJavaScript('checkAuthenticated()')
return true
}
isAuthenticated = () => {
return authenticated
}
showSettings = () => {
mainWindow.webContents.send('show-settings')
}
toggleWatchPhotos = () => {
if (!config.watchPhotos) {
config.watchPhotos = true
}
else {
config.watchPhotos = false
}
saveConfig()
}
toggleAnalyzePhotos = () => {
if (!config.analyzePhotos) {
config.analyzePhotos = true
}
else {
config.analyzePhotos = false
}
saveConfig()
}