Skip to content

Commit

Permalink
fix: fix mac close bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TrumanDu committed Dec 30, 2024
1 parent 819c903 commit 5e09a53
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Toolkit",
"version": "0.1.1",
"version": "0.1.2",
"description": "A toolkit for scalable desktop apps",
"keywords": [
"electron",
Expand Down
6 changes: 3 additions & 3 deletions release/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Toolkit",
"version": "0.1.1",
"version": "0.1.2",
"description": "A toolkit for scalable desktop apps",
"license": "MIT",
"author": {
Expand Down
91 changes: 57 additions & 34 deletions src/main/app_updater.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* eslint-disable class-methods-use-this */
import { dialog, BrowserWindow } from 'electron';
import { dialog, BrowserWindow, shell } from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';

export default class AppUpdater {
private mainWindow: BrowserWindow;

private readonly isMac: boolean;

constructor(window: BrowserWindow) {
this.mainWindow = window;
this.isMac = process.platform === 'darwin';
this.init();
}

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

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

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

if (response === 0) {
// 用户同意更新,开始下载
autoUpdater.downloadUpdate();
if (response === 0) {
// 打开 GitHub release 页面
shell.openExternal(
'https://github.com/TrumanDu/toolkit/releases/latest',
);
}
} else {
// Windows 平台使用热更新
const { response } = await dialog.showMessageBox({
type: 'info',
title: '发现新版本',
message: `发现新版本 ${info.version}\n是否现在更新?`,
detail: info.releaseNotes?.toString() || '暂无更新说明',
buttons: ['现在更新', '暂不更新'],
cancelId: 1,
});

// 显示进度条窗口
this.mainWindow.webContents.send('show-progress-window');
if (response === 0) {
// 用户同意更新,开始下载
autoUpdater.downloadUpdate();
// 显示进度条窗口
this.mainWindow.webContents.send('show-progress-window');
}
}
});

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

// 更新下载进度
// 更新下载进度 (仅 Windows)
autoUpdater.on('download-progress', (progressObj) => {
this.mainWindow.webContents.send('update-progress', {
percent: progressObj.percent,
transferred: progressObj.transferred,
total: progressObj.total,
bytesPerSecond: progressObj.bytesPerSecond,
});
if (!this.isMac) {
this.mainWindow.webContents.send('update-progress', {
percent: progressObj.percent,
transferred: progressObj.transferred,
total: progressObj.total,
bytesPerSecond: progressObj.bytesPerSecond,
});
}
});

// 更新下载完成
// 更新下载完成 (仅 Windows)
autoUpdater.on('update-downloaded', async (info) => {
if (this.isMac) return;

log.info('更新包下载完成:', info);

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

if (response === 0) {
// 用户同意安装,退出并安装
log.info('开始安装更新...');
autoUpdater.quitAndInstall(false, true);
} else {
log.info('下次安装...');
}
});
}

// 检查更新
public checkForUpdates(): void {
log.info('开始检查更新...');
autoUpdater.checkForUpdates().catch((error: Error) => {
public async checkForUpdates(): Promise<void> {
try {
log.info('开始检查更新...');
await autoUpdater.checkForUpdates();
} catch (error) {
log.error('检查更新失败:', error);
});
}
}
}
7 changes: 5 additions & 2 deletions src/main/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ const createDashboardWindow = async () => {

newDashboardWindow.loadURL(resolveHtmlPath('dashboard.html'));
newDashboardWindow.on('close', (event) => {
newDashboardWindow.hide();
event.preventDefault();
if (process.platform === 'darwin') {
event.preventDefault();
newDashboardWindow?.hide();
}
});

// 当窗口准备好时,最大化窗口
newDashboardWindow.webContents.on('did-finish-load', () => {
newDashboardWindow.show();
Expand Down
14 changes: 13 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ const createWindow = async () => {
}

dashboardWindow = await createDashboardWindow();
new AppUpdater(dashboardWindow);

// 等待窗口创建完成后再初始化更新器
dashboardWindow.webContents.on('did-finish-load', () => {
// 初始化自动更新
if (dashboardWindow) {
const appUpdater = new AppUpdater(dashboardWindow);
// 由于checkForUpdatesOnStartup是私有方法,改为调用public方法
appUpdater.checkForUpdates();
}
});

const api = new API(dashboardWindow, initCheck);
api.listen();
// 创建系统托盘图标
Expand Down Expand Up @@ -118,6 +128,8 @@ app
// dock icon is clicked and there are no other windows open.
if (dashboardWindow === null) {
createWindow();
} else {
dashboardWindow.show();
}
});
})
Expand Down

0 comments on commit 5e09a53

Please sign in to comment.