-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
303 lines (265 loc) · 8.39 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
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
// --------------------------------------------
// THIS SCRIPT ONLY RUNS ON SERVER SIDE.
// --------------------------------------------
// Import electron
const { app, BrowserWindow, ipcMain, Tray, Menu, Notification, powerMonitor, nativeImage, autoUpdater, dialog, shell } = require('electron')
const path = require('path')
const package = require('./package.json')
const imageManager = require('./image');
// Disable hardware acceleration
// app.disableHardwareAcceleration();
// --------------------------------------------
// LOGS Setup
// --------------------------------------------
const { Log } = require('./log')
const log = new Log(app.getPath('logs'))
const moment = require('moment')
/* ----------------------------------------------------
WINDOWS
Run this as early in the main process as possible
----------------------------------------------------- */
if (require('electron-squirrel-startup')) app.quit();
// --------------------------------------------
// Setup development env
// --------------------------------------------
const env = process.env.NODE_ENV || 'production';
let devtool = false
if (env === 'development') {
devtool = true // open devtool
require('electron-reload')(__dirname, {
electron: path.join(__dirname, 'node_modules', '.bin', 'electron'),
hardResetMethod: 'exit'
});
}
// --------------------------------------------
// Auto update the app
// --------------------------------------------
if (env !== 'development') {
const server = 'https://update.electronjs.org'
const url = `${server}/${package.author}/${package.name}/${process.platform}-${process.arch}/${app.getVersion()}`
autoUpdater.setFeedURL({ url })
setInterval(() => {
autoUpdater.checkForUpdates()
}, (60 * 1000)) // check for 60 seconds/1 minutes
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) {
app.isQuiting = true
autoUpdater.quitAndInstall()
}
})
})
autoUpdater.on('error', (message) => {
console.error('There was a problem updating the application')
console.error(message)
})
}
/* ---------------------------------------------
WINDOW - Main window
index.html, renderer.js, preload.js,
---------------------------------------------- */
let tray;
const defaultWindowSetting = {
width: env === 'development' ? 800 : 500,
height: 625,
webPreferences: {
devTools: devtool,
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
},
center: true,
resizable: env === 'development' ? true : false, // true dev
maximizable: env === 'development' ? true : false, // true dev
fullscreenable: env === 'development' ? true : false, // true dev
icon: path.join(__dirname, './assets/images/icon.ico'),
autoHideMenuBar: true //use alt/option to show
}
async function createWindow() {
const win = await new BrowserWindow(defaultWindowSetting)
// Get Image - Call before html file loads
ipcMain.on("get-background-image", async () => {
try {
const imagePath = await imageManager.getBackgroundImage();
if (imagePath) {
win.webContents.send('background-image', imagePath);
}
} catch (error) {
console.error('Error getting background image:', error);
}
});
// Load HTML file
await win.loadFile('index.html')
// On minimize
win.on('minimize', function (event) {
event.preventDefault();
win.hide();
});
// On close
win.on('close', function (event) {
if (!app.isQuiting && process.platform === 'win32') {
event.preventDefault();
win.hide();
}
return false;
});
// Screen - lock
powerMonitor.on('lock-screen', async () => {
win.webContents.send('activity-read-logs', JSON.stringify(await log.create('System locked')))
})
// Screen - unlock
powerMonitor.on('unlock-screen', async () => {
win.webContents.send('activity-read-logs', JSON.stringify(await log.create('System unlocked')))
})
// Screen - shutdown
powerMonitor.on('shutdown', async () => {
win.webContents.send('activity-read-logs', JSON.stringify(await log.create('System shutdown')))
})
// Call notification
ipcMain.on('send-notification', async (event, object) => {
const notification = new Notification({
title: object.title,
body: object.body
})
// Show notification
notification.show();
// Listen for the click event on the notification
notification.on('click', () => {
notification.close();
});
})
// Set Custom Log
ipcMain.on('set-log', async (event, eventType) => {
win.webContents.send('activity-read-logs', JSON.stringify(
await log.create(eventType)
))
})
// Set Config
ipcMain.on('config', async (event, cfg) => {
// Update today time if time is being set
if (cfg.name === 'manual-time') {
await log.config({
name: 'today',
value: moment().format('YYYY-MM-DD HH:mm:ss')
})
}
await log.config(cfg)
})
// Open external link
ipcMain.on('open-external-link', async (event, link) => {
await shell.openExternal(link);
})
// Open folder
ipcMain.on('open-logs-folder', async () => {
await shell.showItemInFolder(app.getPath('logs'));
})
// Get Process
ipcMain.handle('get-process-info', async () => {
return {
// crash: process.crash(),
// hang: process.hang(),
getCreationTime: process.getCreationTime(),
// getHeapStatistics: process.getHeapStatistics(),
// getBlinkMemoryInfo: process.getBlinkMemoryInfo(),
getProcessMemoryInfo: await process.getProcessMemoryInfo(),
getSystemMemoryInfo: process.getSystemMemoryInfo(),
getSystemVersion: process.getSystemVersion(),
getCPUUsage: process.getCPUUsage(),
// getIOCounters: process.getIOCounters(),
uptime: process.uptime(),
// argv: process.argv,
// execPath: process.execPath,
// env: process.env,
pid: process.pid,
arch: process.arch,
platform: process.platform,
// sandboxed: process.sandboxed,
// contextIsolated: process.contextIsolated,
type: process.type,
version: process.version,
versions: process.versions,
// mas: process.mas,
// windowsStore: process.windowsStore,
// contextId: process.contextId,
}
})
// Upload image
ipcMain.on('change-background-image', async (event, image) => {
imageManager.changeBackgroundImage(event, image);
});
// Set BG Image by URL
ipcMain.on('change-background-image-by-url', async (event, url) => {
imageManager.changeBackgroundImageByUrl(event, url);
});
// Delete background image
ipcMain.on('delete-background-image', async (event) => {
imageManager.deleteBackgroundImage(event);
});
// open devtools
if (devtool) {
win.webContents.openDevTools()
}
return win
}
/* ---------------------------------------------------
WINDOW - Ready
When all windows are ready invoke it
---------------------------------------------------- */
app.whenReady().then(async () => {
// Invoke: First time read log from file
ipcMain.handle('activity-logs', async () => await log.history())
// Invoke: History logs
ipcMain.handle('history-logs', async (event, date) => await log.history(date))
// Get Config
ipcMain.handle('get-config', async () => await log.config())
// Set Dock Image
if (process.platform === 'darwin') app.dock.setIcon(nativeImage.createFromPath('./assets/images/icon.png'))
// Create window
const win = await createWindow()
// Create Tray Icon
const trayIconPath = path.join(__dirname, './assets/images/iconTemplate.png')
tray = new Tray(nativeImage.createFromPath(trayIconPath))
tray.setToolTip('Timeo');
const trayContextMenu = Menu.buildFromTemplate([
{
label: 'Open Timeo',
click: () => win.show()
},
{
label: 'About',
role: 'about'
},
{
label: 'Quit',
click: () => {
app.isQuiting = true
app.quit()
}
},
])
tray.setContextMenu(trayContextMenu)
// On Tray icon click
tray.on('click', async () => {
win.show()
})
// Mac: Activate window again on closed.
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
// win.show()
})
})
/* ---------------------------------------------------
WINDOW - Minimize window
Minimize window when clicked on close
---------------------------------------------------- */
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})