-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
67 lines (57 loc) · 1.59 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
const { app, ipcMain, globalShortcut } = require('electron')
const { createWindow } = require('./src/window')
const { setupTray } = require('./src/tray')
const { setupShortcuts } = require('./src/shortcuts')
const Store = require('electron-store')
const store = new Store()
let mainWindow = null
let tray = null
// Initialize default settings
if (!store.get('hotkey')) store.set('hotkey', 'F24')
if (!store.get('url')) store.set('url', '')
if (!store.get('opacity')) store.set('opacity', 0.5)
// Handle URL changes
ipcMain.on('set-url', (event, newUrl) => {
store.set('url', newUrl)
if (mainWindow) {
mainWindow.loadURL(newUrl)
}
})
// Handle hotkey changes
ipcMain.on('set-hotkey', (event, newHotkey) => {
const currentHotkey = store.get('hotkey')
// Unregister old hotkey
globalShortcut.unregister(currentHotkey)
// Register new hotkey
store.set('hotkey', newHotkey)
globalShortcut.register(newHotkey, () => {
if (mainWindow) {
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show()
}
return true
})
// Update tray - destroy old one first
if (tray) {
tray.destroy()
}
tray = setupTray(mainWindow, store)
})
app.whenReady().then(() => {
mainWindow = createWindow(store)
tray = setupTray(mainWindow, store)
setupShortcuts(mainWindow, store)
})
app.on('will-quit', () => {
if (tray) {
tray.destroy()
}
globalShortcut.unregisterAll()
})
app.on('window-all-closed', (e) => {
e.preventDefault()
})
app.on('activate', () => {
if (require('electron').BrowserWindow.getAllWindows().length === 0) {
mainWindow = createWindow(store)
}
})