-
Notifications
You must be signed in to change notification settings - Fork 456
/
main.development.js
129 lines (101 loc) · 3.64 KB
/
main.development.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
import { app, ipcMain } from 'electron'
import path from 'path'
import createMainWindow from './main/createWindow'
import createBackgroundWindow from './background/createWindow'
import config from './lib/config'
import AppTray from './main/createWindow/AppTray'
import autoStart from './main/createWindow/autoStart'
import initAutoUpdater from './initAutoUpdater'
import {
WINDOW_WIDTH,
} from 'main/constants/ui'
const iconSrc = {
DEFAULT: `${__dirname}/tray_icon.png`,
darwin: `${__dirname}/[email protected]`,
win32: `${__dirname}/tray_icon.ico`
}
const trayIconSrc = iconSrc[process.platform] || iconSrc.DEFAULT
const isDev = () => (process.env.NODE_ENV === 'development' || config.get('developerMode'))
let mainWindow
let backgroundWindow
let tray
const setupEnvVariables = () => {
process.env.CEREBRO_VERSION = app.getVersion()
const isPortableMode = process.argv.some((arg) => arg.toLowerCase() === '-p' || arg.toLowerCase() === '--portable')
// initiate portable mode
// set data directory to ./userdata
if (isPortableMode) {
const userDataPath = path.join(process.cwd(), 'userdata')
app.setPath('userData', userDataPath)
process.env.CEREBRO_DATA_PATH = userDataPath
} else {
process.env.CEREBRO_DATA_PATH = app.getPath('userData')
}
}
app.whenReady().then(() => {
// We cannot require the screen module until the app is ready.
const { screen } = require('electron')
setupEnvVariables()
mainWindow = createMainWindow({
isDev,
src: `file://${__dirname}/main/index.html`, // Main window html
})
mainWindow.on('show', (event) => {
const cursorScreenPoint = screen.getCursorScreenPoint()
const nearestDisplay = screen.getDisplayNearestPoint(cursorScreenPoint)
const goalWidth = WINDOW_WIDTH
const goalX = Math.floor(nearestDisplay.bounds.x + (nearestDisplay.size.width - goalWidth) / 2)
const goalY = nearestDisplay.bounds.y + 200 // "top" is hardcoded now, should get from config or calculate accordingly?
config.set('winPosition', [goalX, goalY])
})
// eslint-disable-next-line global-require
require('@electron/remote/main').initialize()
// eslint-disable-next-line global-require
require('@electron/remote/main').enable(mainWindow.webContents)
backgroundWindow = createBackgroundWindow({
src: `file://${__dirname}/background/index.html`,
})
// eslint-disable-next-line global-require
require('@electron/remote/main').enable(backgroundWindow.webContents)
tray = new AppTray({
src: trayIconSrc,
isDev: isDev(),
mainWindow,
backgroundWindow,
})
// Show tray icon if it is set in configuration
if (config.get('showInTray')) { tray.show() }
autoStart.isEnabled().then((enabled) => {
if (config.get('openAtLogin') !== enabled) {
autoStart.set(config.get('openAtLogin'))
}
})
initAutoUpdater(mainWindow)
app?.dock?.hide()
})
ipcMain.on('message', (event, payload) => {
const toWindow = event.sender === mainWindow.webContents ? backgroundWindow : mainWindow
toWindow.webContents.send('message', payload)
})
ipcMain.on('updateSettings', (event, key, value) => {
mainWindow.settingsChanges.emit(key, value)
// Show or hide menu bar icon when it is changed in setting
if (key === 'showInTray') {
value
? tray.show()
: tray.hide()
}
// Show or hide "development" section in tray menu
if (key === 'developerMode') { tray.setIsDev(isDev()) }
// Enable or disable auto start
if (key === 'openAtLogin') {
autoStart.isEnabled().then((enabled) => {
if (value !== enabled) autoStart.set(value)
})
}
})
ipcMain.on('quit', () => app.quit())
ipcMain.on('reload', () => {
app.relaunch()
app.exit()
})