Skip to content

Commit 1923b8c

Browse files
committed
refactor: split window index.ts
1 parent 1c02a55 commit 1923b8c

4 files changed

Lines changed: 232 additions & 215 deletions

File tree

src/main/autoUpdater.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { BrowserWindow } from 'electron'
2+
import { autoUpdater } from 'electron-updater'
3+
import { is } from '@electron-toolkit/utils'
4+
import { join } from 'path'
5+
6+
export function setupAutoUpdater(): void {
7+
// 设置自动下载为false,我们想手动控制下载过程
8+
autoUpdater.autoDownload = false
9+
autoUpdater.autoInstallOnAppQuit = true
10+
11+
// 在开发环境中启用自动更新,使用开发配置
12+
if (is.dev) {
13+
autoUpdater.updateConfigPath = join(__dirname, '../../dev-app-update.yml')
14+
// 强制启用开发环境的更新检查
15+
autoUpdater.forceDevUpdateConfig = true
16+
// 开发环境下跳过签名验证
17+
autoUpdater.allowDowngrade = true
18+
}
19+
20+
// 更新可用时的处理
21+
autoUpdater.on('update-available', (info) => {
22+
console.log('Update available:', info)
23+
// 通知渲染进程有更新可用
24+
BrowserWindow.getAllWindows().forEach((window) => {
25+
window.webContents.send('update-available', info)
26+
})
27+
})
28+
29+
// 没有更新时的处理
30+
autoUpdater.on('update-not-available', (info) => {
31+
console.log('Update not available:', info)
32+
BrowserWindow.getAllWindows().forEach((window) => {
33+
window.webContents.send('update-not-available', info)
34+
})
35+
})
36+
37+
// 更新下载进度
38+
autoUpdater.on('download-progress', (progressObj) => {
39+
console.log('Download progress:', progressObj)
40+
BrowserWindow.getAllWindows().forEach((window) => {
41+
window.webContents.send('download-progress', progressObj)
42+
})
43+
})
44+
45+
// 更新下载完成
46+
autoUpdater.on('update-downloaded', (info) => {
47+
console.log('Update downloaded:', info)
48+
BrowserWindow.getAllWindows().forEach((window) => {
49+
window.webContents.send('update-downloaded', info)
50+
})
51+
})
52+
53+
// 更新错误处理
54+
autoUpdater.on('error', (error) => {
55+
console.error('Update error:', error)
56+
BrowserWindow.getAllWindows().forEach((window) => {
57+
window.webContents.send('update-error', error.message)
58+
})
59+
})
60+
}
61+
62+
export { autoUpdater }

src/main/index.ts

Lines changed: 7 additions & 215 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,9 @@
1-
import { app, shell, BrowserWindow, ipcMain, dialog } from 'electron'
2-
import { join } from 'path'
3-
import { writeFile } from 'fs/promises'
4-
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
5-
import windowStateKeeper from 'electron-window-state'
6-
import { autoUpdater } from 'electron-updater'
7-
import icon from '../../resources/icon.png?asset'
1+
import { app, BrowserWindow } from 'electron'
2+
import { electronApp, optimizer } from '@electron-toolkit/utils'
83
import { aiHandler } from './aiHandler'
9-
10-
// 配置自动更新
11-
function setupAutoUpdater(): void {
12-
// 设置自动下载为false,我们想手动控制下载过程
13-
autoUpdater.autoDownload = false
14-
autoUpdater.autoInstallOnAppQuit = true
15-
16-
// 在开发环境中启用自动更新,使用开发配置
17-
if (is.dev) {
18-
autoUpdater.updateConfigPath = join(__dirname, '../../dev-app-update.yml')
19-
// 强制启用开发环境的更新检查
20-
autoUpdater.forceDevUpdateConfig = true
21-
// 开发环境下跳过签名验证
22-
autoUpdater.allowDowngrade = true
23-
}
24-
25-
// 更新可用时的处理
26-
autoUpdater.on('update-available', (info) => {
27-
console.log('Update available:', info)
28-
// 通知渲染进程有更新可用
29-
BrowserWindow.getAllWindows().forEach((window) => {
30-
window.webContents.send('update-available', info)
31-
})
32-
})
33-
34-
// 没有更新时的处理
35-
autoUpdater.on('update-not-available', (info) => {
36-
console.log('Update not available:', info)
37-
BrowserWindow.getAllWindows().forEach((window) => {
38-
window.webContents.send('update-not-available', info)
39-
})
40-
})
41-
42-
// 更新下载进度
43-
autoUpdater.on('download-progress', (progressObj) => {
44-
console.log('Download progress:', progressObj)
45-
BrowserWindow.getAllWindows().forEach((window) => {
46-
window.webContents.send('download-progress', progressObj)
47-
})
48-
})
49-
50-
// 更新下载完成
51-
autoUpdater.on('update-downloaded', (info) => {
52-
console.log('Update downloaded:', info)
53-
BrowserWindow.getAllWindows().forEach((window) => {
54-
window.webContents.send('update-downloaded', info)
55-
})
56-
})
57-
58-
// 更新错误处理
59-
autoUpdater.on('error', (error) => {
60-
console.error('Update error:', error)
61-
BrowserWindow.getAllWindows().forEach((window) => {
62-
window.webContents.send('update-error', error.message)
63-
})
64-
})
65-
}
66-
67-
function createWindow(): void {
68-
const mainWindowState = windowStateKeeper({
69-
defaultWidth: 960,
70-
defaultHeight: 640,
71-
fullScreen: false,
72-
maximize: false
73-
})
74-
// Create the browser window.
75-
const mainWindow = new BrowserWindow({
76-
x: mainWindowState.x,
77-
y: mainWindowState.y,
78-
width: mainWindowState.width,
79-
height: mainWindowState.height,
80-
minWidth: 480,
81-
minHeight: 320,
82-
show: false,
83-
autoHideMenuBar: true,
84-
frame: false, // 禁用默认边框,启用自定义标题栏
85-
titleBarStyle: 'hidden', // 隐藏标题栏
86-
...(process.platform === 'linux' ? { icon } : {}),
87-
webPreferences: {
88-
preload: join(__dirname, '../preload/index.js'),
89-
sandbox: false
90-
}
91-
})
92-
93-
mainWindowState.manage(mainWindow)
94-
if (mainWindowState.isMaximized) {
95-
mainWindow.maximize()
96-
}
97-
98-
mainWindow.on('ready-to-show', () => {
99-
mainWindow.show()
100-
})
101-
102-
mainWindow.webContents.setWindowOpenHandler((details) => {
103-
shell.openExternal(details.url)
104-
return { action: 'deny' }
105-
})
106-
107-
// HMR for renderer base on electron-vite cli.
108-
// Load the remote URL for development or the local html file for production.
109-
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
110-
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
111-
} else {
112-
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
113-
}
114-
}
4+
import { setupAutoUpdater } from './autoUpdater'
5+
import { setupIpcHandlers } from './ipcHandlers'
6+
import { createWindow } from './window'
1157

1168
// This method will be called when Electron has finished
1179
// initialization and is ready to create browser windows.
@@ -133,108 +25,8 @@ app.whenReady().then(() => {
13325
// Setup auto updater
13426
setupAutoUpdater()
13527

136-
// 更新相关的IPC处理程序
137-
ipcMain.handle('check-for-updates', async () => {
138-
try {
139-
console.log('IPC: 开始检查更新')
140-
141-
// 在开发环境中,如果没有强制配置,给出明确的提示
142-
if (is.dev && !autoUpdater.forceDevUpdateConfig) {
143-
console.log('开发环境中跳过更新检查,需要设置 forceDevUpdateConfig')
144-
throw new Error('开发环境中的更新检查已被跳过')
145-
}
146-
147-
const result = await autoUpdater.checkForUpdates()
148-
console.log('IPC: 更新检查完成', result)
149-
return result
150-
} catch (error) {
151-
console.error('IPC: Check for updates error:', error)
152-
// 确保错误被正确传递到渲染进程
153-
throw error
154-
}
155-
})
156-
157-
ipcMain.handle('download-update', async () => {
158-
try {
159-
console.log('IPC: 开始下载更新')
160-
const result = await autoUpdater.downloadUpdate()
161-
console.log('IPC: 更新下载开始', result)
162-
return result
163-
} catch (error) {
164-
console.error('IPC: Download update error:', error)
165-
throw error
166-
}
167-
})
168-
169-
ipcMain.handle('quit-and-install', () => {
170-
console.log('IPC: 退出并安装更新')
171-
autoUpdater.quitAndInstall()
172-
})
173-
174-
ipcMain.handle('get-app-version', () => {
175-
const version = app.getVersion()
176-
console.log('IPC: 获取应用版本', version)
177-
return version
178-
})
179-
180-
// IPC test
181-
ipcMain.on('ping', () => console.log('pong'))
182-
183-
// 窗口控制IPC处理程序
184-
ipcMain.handle('window-minimize', (event) => {
185-
const window = BrowserWindow.fromWebContents(event.sender)
186-
if (window) window.minimize()
187-
})
188-
189-
ipcMain.handle('window-maximize', (event) => {
190-
const window = BrowserWindow.fromWebContents(event.sender)
191-
if (window) {
192-
if (window.isMaximized()) {
193-
window.unmaximize()
194-
} else {
195-
window.maximize()
196-
}
197-
}
198-
})
199-
200-
ipcMain.handle('window-close', (event) => {
201-
const window = BrowserWindow.fromWebContents(event.sender)
202-
if (window) window.close()
203-
})
204-
205-
ipcMain.handle('window-is-maximized', (event) => {
206-
const window = BrowserWindow.fromWebContents(event.sender)
207-
return window ? window.isMaximized() : false
208-
})
209-
210-
// Handle save file
211-
ipcMain.handle('save-file', async (event, { content, defaultPath, filters }) => {
212-
try {
213-
const result = await dialog.showSaveDialog({
214-
title: '保存文件',
215-
defaultPath: defaultPath,
216-
filters: filters || [
217-
{ name: 'Text Files', extensions: ['txt'] },
218-
{ name: 'All Files', extensions: ['*'] }
219-
]
220-
})
221-
222-
if (result.canceled) {
223-
return { success: false, cancelled: true }
224-
}
225-
226-
// Handle both string and Uint8Array/Buffer content
227-
if (typeof content === 'string') {
228-
await writeFile(result.filePath!, content, 'utf8')
229-
} else {
230-
await writeFile(result.filePath!, Buffer.from(content))
231-
}
232-
return { success: true, filePath: result.filePath }
233-
} catch (error) {
234-
console.error('Save file error:', error)
235-
return { success: false, error: error instanceof Error ? error.message : '未知错误' }
236-
}
237-
})
28+
// Setup IPC handlers
29+
setupIpcHandlers()
23830

23931
createWindow()
24032

0 commit comments

Comments
 (0)