-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathindex.ts
More file actions
149 lines (123 loc) · 3.52 KB
/
Copy pathindex.ts
File metadata and controls
149 lines (123 loc) · 3.52 KB
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
import { app, BrowserWindow } from 'electron'
import { join } from 'path'
import { createWindow, getMainWindow, loadUrl, loadFile, openDevTools } from './window/manager'
import { createTrayManager, TrayManager } from './tray/TrayManager'
import { registerIpcHandlers } from './ipc/handlers'
import { UpdaterManager } from './updater'
import { storeManager } from './store/store'
import { logManager } from './logger/manager'
// Prevent uncaught exceptions from crashing the app
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error)
})
process.on('unhandledRejection', (reason) => {
console.error('Unhandled Rejection:', reason)
})
// Workaround for V8 JIT compiler crash on macOS ARM64 (Electron 33 bug)
// Completely disable JIT compilation to prevent EXC_BAD_ACCESS crashes
// This trades some performance for stability
if (process.platform === 'darwin' && process.arch === 'arm64') {
app.commandLine.appendSwitch('js-flags', '--jitless --no-opt')
app.commandLine.appendSwitch('disable-gpu-sandbox')
}
// Automatically add --no-sandbox flag when running as root user
if (process.getuid && process.getuid() === 0) {
console.log('Detected running as root user, sandbox settings have been automatically handled')
}
declare module 'electron' {
interface App {
isQuitting?: boolean
}
}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', () => {
const mainWindow = getMainWindow()
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.show()
mainWindow.focus()
}
})
initializeApp()
}
let trayManager: TrayManager | null = null
async function initializeApp(): Promise<void> {
app.on('ready', async () => {
await setupApp()
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
const mainWindow = getMainWindow()
if (!mainWindow) {
createWindow()
} else {
mainWindow.show()
}
})
app.on('before-quit', () => {
app.isQuitting = true
trayManager?.destroy()
})
app.on('will-quit', () => {
cleanup()
})
}
async function setupApp(): Promise<void> {
const mainWindow = createWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
title: 'Chat2API',
show: false,
})
await registerIpcHandlers(mainWindow)
trayManager = createTrayManager(mainWindow)
await loadAppContent(mainWindow)
if (process.env.NODE_ENV === 'development') {
openDevTools()
}
}
async function loadAppContent(mainWindow: BrowserWindow): Promise<void> {
const isDev = process.env.NODE_ENV === 'development'
if (isDev) {
try {
await loadUrl('http://localhost:5173')
} catch (error) {
console.error('Failed to load development server:', error)
}
} else {
try {
await loadFile(join(__dirname, '../renderer/index.html'))
} catch (error) {
console.error('Failed to load production files:', error)
}
}
}
function cleanup(): void {
console.log('Application is exiting, performing cleanup...')
logManager.flush()
storeManager.flushPendingWrites()
const updaterManager = UpdaterManager.getInstance()
updaterManager.destroy()
}
export function restartApp(): void {
app.relaunch()
app.quit()
}
export function getAppVersion(): string {
return app.getVersion()
}
export function isAppQuitting(): boolean {
return app.isQuitting ?? false
}
export { getMainWindow }