Skip to content

Commit 1d1e712

Browse files
author
pompurin404
committed
auto destroy window
1 parent fad4926 commit 1d1e712

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

src/main/core/tray.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import icoIcon from '../../../resources/icon.ico?asset'
88
import pngIcon from '../../../resources/icon.png?asset'
99
import { patchMihomoConfig } from './mihomoApi'
10-
import { mainWindow } from '..'
10+
import { createWindow, destroyTimer, mainWindow } from '..'
1111
import { app, ipcMain, Menu, shell, Tray } from 'electron'
1212
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
1313
import { triggerSysProxy } from '../resolve/sysproxy'
@@ -23,8 +23,15 @@ const buildContextMenu = async (): Promise<Menu> => {
2323
label: '显示窗口',
2424
type: 'normal',
2525
click: (): void => {
26-
mainWindow?.show()
27-
mainWindow?.focusOnWebView()
26+
if (!mainWindow) {
27+
if (destroyTimer) {
28+
clearTimeout(destroyTimer)
29+
}
30+
createWindow(true)
31+
} else {
32+
mainWindow?.show()
33+
mainWindow?.focusOnWebView()
34+
}
2835
}
2936
},
3037
{
@@ -159,7 +166,18 @@ export async function createTray(): Promise<void> {
159166
tray.setToolTip('Another Mihomo GUI.')
160167
tray.setTitle('Mihomo Party')
161168
tray.addListener('click', () => {
162-
mainWindow?.isVisible() ? mainWindow?.hide() : mainWindow?.show()
169+
if (mainWindow?.isVisible()) {
170+
mainWindow?.close()
171+
} else {
172+
if (!mainWindow) {
173+
if (destroyTimer) {
174+
clearTimeout(destroyTimer)
175+
}
176+
createWindow(true)
177+
} else {
178+
mainWindow?.show()
179+
}
180+
}
163181
})
164182
}
165183

src/main/index.ts

+42-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './core/mihomoApi'
1717

1818
export let mainWindow: BrowserWindow | null = null
19+
export let destroyTimer: NodeJS.Timeout | null = null
1920

2021
const gotTheLock = app.requestSingleInstanceLock()
2122

@@ -25,26 +26,43 @@ if (!gotTheLock) {
2526
const initPromise = init()
2627

2728
app.on('second-instance', async (_event, commandline) => {
28-
mainWindow?.show()
29-
mainWindow?.focusOnWebView()
29+
if (!mainWindow) {
30+
if (destroyTimer) {
31+
clearTimeout(destroyTimer)
32+
}
33+
createWindow(true)
34+
} else {
35+
mainWindow?.show()
36+
mainWindow?.focusOnWebView()
37+
}
38+
3039
const url = commandline.pop()
3140
if (url) {
3241
await handleDeepLink(url)
3342
}
3443
})
3544

3645
app.on('open-url', async (_event, url) => {
37-
mainWindow?.show()
38-
mainWindow?.focusOnWebView()
46+
if (!mainWindow) {
47+
if (destroyTimer) {
48+
clearTimeout(destroyTimer)
49+
}
50+
createWindow(true)
51+
} else {
52+
mainWindow?.show()
53+
mainWindow?.focusOnWebView()
54+
}
55+
3956
await handleDeepLink(url)
4057
})
4158
// Quit when all windows are closed, except on macOS. There, it's common
4259
// for applications and their menu bar to stay active until the user quits
4360
// explicitly with Cmd + Q.
44-
app.on('window-all-closed', () => {
45-
if (process.platform !== 'darwin') {
46-
app.quit()
47-
}
61+
app.on('window-all-closed', (e) => {
62+
e.preventDefault()
63+
// if (process.platform !== 'darwin') {
64+
// app.quit()
65+
// }
4866
})
4967

5068
app.on('before-quit', () => {
@@ -78,7 +96,12 @@ app.whenReady().then(async () => {
7896
app.on('activate', function () {
7997
// On macOS it's common to re-create a window in the app when the
8098
// dock icon is clicked and there are no other windows open.
81-
if (BrowserWindow.getAllWindows().length === 0) createWindow()
99+
if (BrowserWindow.getAllWindows().length === 0) {
100+
if (destroyTimer) {
101+
clearTimeout(destroyTimer)
102+
}
103+
createWindow(true)
104+
}
82105
})
83106
})
84107

@@ -106,7 +129,7 @@ async function handleDeepLink(url: string): Promise<void> {
106129
}
107130
}
108131

109-
function createWindow(): void {
132+
export function createWindow(show = false): void {
110133
Menu.setApplicationMenu(null)
111134
// Create the browser window.
112135
mainWindow = new BrowserWindow({
@@ -125,7 +148,7 @@ function createWindow(): void {
125148
})
126149
mainWindow.on('ready-to-show', async () => {
127150
const { silentStart } = await getAppConfig()
128-
if (!silentStart) {
151+
if (!silentStart || show) {
129152
mainWindow?.show()
130153
mainWindow?.focusOnWebView()
131154
}
@@ -145,7 +168,14 @@ function createWindow(): void {
145168
stopMihomoMemory()
146169
event.preventDefault()
147170
mainWindow?.hide()
148-
mainWindow?.webContents.reload()
171+
if (destroyTimer) {
172+
clearTimeout(destroyTimer)
173+
}
174+
destroyTimer = setTimeout(() => {
175+
mainWindow?.destroy()
176+
mainWindow = null
177+
}, 300000)
178+
// mainWindow?.webContents.reload()
149179
})
150180

151181
mainWindow.webContents.setWindowOpenHandler((details) => {

0 commit comments

Comments
 (0)