-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (131 loc) · 3.43 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
// Imports and variable declarations
const { app, BrowserWindow, ipcMain, Tray, nativeTheme, globalShortcut } = require('electron')
const path = require('path')
const updater = require('./updater')
const isDev = !app.isPackaged
let allowQuit = false
let vibrancyOn = true
// Main window
let win = null
const createWindow = () => {
// Create main window
win = new BrowserWindow({
width: 300,
height: 400,
minWidth: 180,
minHeight: 150,
transparent: true,
frame: false,
show: false,
hasShadow: false,
visualEffectState: 'active',
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
// HTML file to load into window
win.loadFile('main.html')
// Show window when ready
win.once('ready-to-show', () => {
win.show()
})
win.webContents.session.setSpellCheckerEnabled(false)
// Open DevTools (dev only)
isDev && win.webContents.openDevTools('detach')
// Set vibrancy to match theme on update
nativeTheme.themeSource = 'system'
nativeTheme.on('updated', () => {
vibrancySet()
})
}
// Tray icon
let tray = null
const createTray = () => {
tray = new Tray(path.join(__dirname, 'iconTemplate@2x.png'))
tray.setToolTip('Temporal')
tray.on('click', () => {
win.isVisible() ? win.hide() : win.show()
})
tray.on('right-click', () => {
app.quit()
})
}
// Set vibrancy
function vibrancySet() {
if (nativeTheme.shouldUseDarkColors) {
win.setVibrancy('dark')
} else {
win.setVibrancy('light')
}
if (!vibrancyOn) {
win.setVibrancy(null)
}
}
// Remove app from dock
app.dock.hide()
// Load electron-reload in dev
if (isDev) {
require('electron-reload')(__dirname, {
electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
})
}
// Instantiate window and tray on app ready
app.whenReady().then(() => {
try {
createWindow()
createTray()
globalShortcut.register('CommandOrControl+Q', () => {
console.log('CommandOrControl+Q was pressed')
})
// Check for updates after 3 seconds
!isDev && setTimeout(updater, 3000)
} catch (err) { console.error(err) }
})
// Create window if one doesn't exist
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// When closing set window size and location
app.on('before-quit', (e) => {
if (!allowQuit) {
e.preventDefault()
let wb = win.getBounds()
const data = {
windowSizeLocation: { x: wb.x, y: wb.y, height: wb.height, width: wb.width }
}
win.webContents.send('save-settings', data)
allowQuit = true
app.quit()
}
globalShortcut.unregister('CommandOrControl+Q')
})
// CLose app if all windows are closed (not Mac)
app.on('window-all-closed', () => {
app.quit()
})
// IPC channel to update window size and location from settings
ipcMain.on('set-window', (e, data) => {
win.setBounds({ x: data.x, y: data.y, height: data.height, width: data.width })
})
// IPC channel for locking app on top
ipcMain.on('ontop-lock', () => {
win.setAlwaysOnTop(true, 'floating')
})
// IPC channel for unlocking app on top
ipcMain.on('ontop-unlock', () => {
win.setAlwaysOnTop(false)
})
// IPC channel for maximizing window
ipcMain.on('win-max', () => {
win.isMaximized() ? win.unmaximize() : win.maximize()
})
// IPC channel for hiding window
ipcMain.on('win-hide', () => {
win.hide()
})
// IPC channel for setting vibrancy
ipcMain.on('trans-set', (e, bool) => {
vibrancyOn = bool
vibrancySet()
})