-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathindex.ts
More file actions
139 lines (112 loc) · 3.58 KB
/
Copy pathindex.ts
File metadata and controls
139 lines (112 loc) · 3.58 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
import path from 'node:path'
import { electronApp, is, optimizer } from '@electron-toolkit/utils'
import { IpcChannelInvoke, IpcChannelSend } from '@shared/constant/ipc'
import { app, BrowserWindow, ipcMain, nativeImage, shell } from 'electron'
import appIcon from '../../resources/icon.png?asset'
import { killAllProcesses } from './childProcessManager'
import { getCpuInfo, getExtraSRModelList, getGpuInfo, getMemoryInfo } from './getSystemInfo'
import { openDirectory } from './openDirectory'
import { preview, previewFrame } from './previewOutput'
import { PauseCommand, runCommand } from './runCommand'
import { writeSettingsJson } from './writeFile'
function createWindow(): BrowserWindow {
const mainWindow = new BrowserWindow({
width: 1000,
height: 875,
minWidth: 1000,
minHeight: 875,
show: false,
autoHideMenuBar: true,
frame: false,
resizable: true,
icon: nativeImage.createFromPath(appIcon),
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
sandbox: false,
},
})
// ipcMain
ipcMain.on('window:minimize', () => {
mainWindow?.minimize()
})
ipcMain.on('window:maximize', () => {
if (mainWindow?.isMaximized()) {
mainWindow.unmaximize()
}
else {
mainWindow?.maximize()
}
})
ipcMain.on('window:close', () => {
mainWindow?.close()
})
ipcMain.on(IpcChannelSend.EXECUTE_COMMAND, runCommand)
ipcMain.on(IpcChannelSend.PAUSE, PauseCommand)
ipcMain.on(IpcChannelSend.PREVIEW, preview)
ipcMain.on(IpcChannelSend.PREVIEW_FRAME, previewFrame)
ipcMain.on(IpcChannelSend.STOP_ALL_PROCESSES, killAllProcesses)
ipcMain.on(IpcChannelSend.GENERATE_JSON, writeSettingsJson)
ipcMain.handle(IpcChannelInvoke.OPEN_DIRECTORY_DIALOG, openDirectory)
ipcMain.handle(IpcChannelInvoke.GET_GPU_INFO, getGpuInfo)
ipcMain.handle(IpcChannelInvoke.GET_CPU_INFO, getCpuInfo)
ipcMain.handle(IpcChannelInvoke.GET_MEMORY_INFO, getMemoryInfo)
ipcMain.handle(IpcChannelInvoke.GET_EXTRA_SR_MODEL_LIST, getExtraSRModelList)
// mainWindow
mainWindow.on('ready-to-show', () => {
mainWindow?.show()
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
mainWindow.on('close', async () => {
app.quit()
})
if (is.dev && process.env.ELECTRON_RENDERER_URL) {
mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL)
// mainWindow.webContents.openDevTools()
}
else {
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'))
}
return mainWindow
}
// disable hardware acceleration for Compatibility for windows
app.disableHardwareAcceleration()
// ✅ 初始化窗口和主进程监听
app.whenReady().then(() => {
electronApp.setAppUserModelId('com.vset.ai')
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
// ✅ 所有窗口关闭时退出(除 macOS)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
// ✅ 当用户点击任务栏关闭或调用 app.quit() 时,先清理子进程
let isQuitting = false
app.on('before-quit', async (event) => {
if (isQuitting) {
console.log('Quitting...')
return
}
console.log('Killing child process before quitting...')
event.preventDefault()
isQuitting = true
try {
await killAllProcesses()
}
catch (err) {
console.error('❌ killAllProcesses 失败:', err)
}
app.quit()
})