Skip to content

Commit 5e09a53

Browse files
committed
fix: fix mac close bug
1 parent 819c903 commit 5e09a53

File tree

6 files changed

+80
-42
lines changed

6 files changed

+80
-42
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toolkit",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "A toolkit for scalable desktop apps",
55
"keywords": [
66
"electron",

release/app/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

release/app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toolkit",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "A toolkit for scalable desktop apps",
55
"license": "MIT",
66
"author": {

src/main/app_updater.ts

+57-34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/* eslint-disable class-methods-use-this */
2-
import { dialog, BrowserWindow } from 'electron';
2+
import { dialog, BrowserWindow, shell } from 'electron';
33
import { autoUpdater } from 'electron-updater';
44
import log from 'electron-log';
55

66
export default class AppUpdater {
77
private mainWindow: BrowserWindow;
88

9+
private readonly isMac: boolean;
10+
911
constructor(window: BrowserWindow) {
1012
this.mainWindow = window;
13+
this.isMac = process.platform === 'darwin';
1114
this.init();
1215
}
1316

@@ -18,53 +21,70 @@ export default class AppUpdater {
1821
// 检查更新出错
1922
autoUpdater.on('error', (error) => {
2023
log.error(['检查更新失败', error]);
21-
dialog.showErrorBox('更新出错', error.message);
2224
});
2325

2426
// 检测到新版本
2527
autoUpdater.on('update-available', async (info) => {
2628
log.info('检测到新版本:', info);
2729

28-
// 显示更新确认对话框
29-
const { response } = await dialog.showMessageBox({
30-
type: 'info',
31-
title: '发现新版本',
32-
message: `发现新版本 ${info.version}\n是否现在更新?`,
33-
detail: info.releaseNotes?.toString() || '暂无更新说明',
34-
buttons: ['现在更新', '暂不更新'],
35-
cancelId: 1,
36-
});
30+
if (this.isMac) {
31+
// macOS 平台直接打开下载页面
32+
const { response } = await dialog.showMessageBox({
33+
type: 'info',
34+
title: '发现新版本',
35+
message: `发现新版本 ${info.version}\n是否前往下载页面?`,
36+
detail: info.releaseNotes?.toString() || '暂无更新说明',
37+
buttons: ['前往下载', '暂不更新'],
38+
cancelId: 1,
39+
});
3740

38-
if (response === 0) {
39-
// 用户同意更新,开始下载
40-
autoUpdater.downloadUpdate();
41+
if (response === 0) {
42+
// 打开 GitHub release 页面
43+
shell.openExternal(
44+
'https://github.com/TrumanDu/toolkit/releases/latest',
45+
);
46+
}
47+
} else {
48+
// Windows 平台使用热更新
49+
const { response } = await dialog.showMessageBox({
50+
type: 'info',
51+
title: '发现新版本',
52+
message: `发现新版本 ${info.version}\n是否现在更新?`,
53+
detail: info.releaseNotes?.toString() || '暂无更新说明',
54+
buttons: ['现在更新', '暂不更新'],
55+
cancelId: 1,
56+
});
4157

42-
// 显示进度条窗口
43-
this.mainWindow.webContents.send('show-progress-window');
58+
if (response === 0) {
59+
// 用户同意更新,开始下载
60+
autoUpdater.downloadUpdate();
61+
// 显示进度条窗口
62+
this.mainWindow.webContents.send('show-progress-window');
63+
}
4464
}
4565
});
4666

4767
// 没有可用更新
4868
autoUpdater.on('update-not-available', () => {
4969
log.info('当前已是最新版本');
50-
dialog.showMessageBox({
51-
title: '检查更新',
52-
message: '当前已是最新版本',
53-
});
5470
});
5571

56-
// 更新下载进度
72+
// 更新下载进度 (仅 Windows)
5773
autoUpdater.on('download-progress', (progressObj) => {
58-
this.mainWindow.webContents.send('update-progress', {
59-
percent: progressObj.percent,
60-
transferred: progressObj.transferred,
61-
total: progressObj.total,
62-
bytesPerSecond: progressObj.bytesPerSecond,
63-
});
74+
if (!this.isMac) {
75+
this.mainWindow.webContents.send('update-progress', {
76+
percent: progressObj.percent,
77+
transferred: progressObj.transferred,
78+
total: progressObj.total,
79+
bytesPerSecond: progressObj.bytesPerSecond,
80+
});
81+
}
6482
});
6583

66-
// 更新下载完成
84+
// 更新下载完成 (仅 Windows)
6785
autoUpdater.on('update-downloaded', async (info) => {
86+
if (this.isMac) return;
87+
6888
log.info('更新包下载完成:', info);
6989

7090
// 关闭进度条窗口
@@ -76,23 +96,26 @@ export default class AppUpdater {
7696
title: '更新就绪',
7797
message: '新版本已下载完成,是否现在安装?',
7898
detail: '点击确定将重启应用并安装更新',
79-
buttons: ['现在安装', '稍后安装'],
99+
buttons: ['现在安装', '下次安装'],
80100
cancelId: 1,
81101
});
82102

83103
if (response === 0) {
84104
// 用户同意安装,退出并安装
85105
log.info('开始安装更新...');
86106
autoUpdater.quitAndInstall(false, true);
107+
} else {
108+
log.info('下次安装...');
87109
}
88110
});
89111
}
90112

91-
// 检查更新
92-
public checkForUpdates(): void {
93-
log.info('开始检查更新...');
94-
autoUpdater.checkForUpdates().catch((error: Error) => {
113+
public async checkForUpdates(): Promise<void> {
114+
try {
115+
log.info('开始检查更新...');
116+
await autoUpdater.checkForUpdates();
117+
} catch (error) {
95118
log.error('检查更新失败:', error);
96-
});
119+
}
97120
}
98121
}

src/main/dashboard.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ const createDashboardWindow = async () => {
3131

3232
newDashboardWindow.loadURL(resolveHtmlPath('dashboard.html'));
3333
newDashboardWindow.on('close', (event) => {
34-
newDashboardWindow.hide();
35-
event.preventDefault();
34+
if (process.platform === 'darwin') {
35+
event.preventDefault();
36+
newDashboardWindow?.hide();
37+
}
3638
});
39+
3740
// 当窗口准备好时,最大化窗口
3841
newDashboardWindow.webContents.on('did-finish-load', () => {
3942
newDashboardWindow.show();

src/main/main.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,17 @@ const createWindow = async () => {
6262
}
6363

6464
dashboardWindow = await createDashboardWindow();
65-
new AppUpdater(dashboardWindow);
65+
66+
// 等待窗口创建完成后再初始化更新器
67+
dashboardWindow.webContents.on('did-finish-load', () => {
68+
// 初始化自动更新
69+
if (dashboardWindow) {
70+
const appUpdater = new AppUpdater(dashboardWindow);
71+
// 由于checkForUpdatesOnStartup是私有方法,改为调用public方法
72+
appUpdater.checkForUpdates();
73+
}
74+
});
75+
6676
const api = new API(dashboardWindow, initCheck);
6777
api.listen();
6878
// 创建系统托盘图标
@@ -118,6 +128,8 @@ app
118128
// dock icon is clicked and there are no other windows open.
119129
if (dashboardWindow === null) {
120130
createWindow();
131+
} else {
132+
dashboardWindow.show();
121133
}
122134
});
123135
})

0 commit comments

Comments
 (0)