auto destroy window

This commit is contained in:
pompurin404 2024-08-14 11:34:37 +08:00
parent fad4926451
commit 1d1e712014
No known key found for this signature in database
2 changed files with 64 additions and 16 deletions

View File

@ -7,7 +7,7 @@ import {
import icoIcon from '../../../resources/icon.ico?asset' import icoIcon from '../../../resources/icon.ico?asset'
import pngIcon from '../../../resources/icon.png?asset' import pngIcon from '../../../resources/icon.png?asset'
import { patchMihomoConfig } from './mihomoApi' import { patchMihomoConfig } from './mihomoApi'
import { mainWindow } from '..' import { createWindow, destroyTimer, mainWindow } from '..'
import { app, ipcMain, Menu, shell, Tray } from 'electron' import { app, ipcMain, Menu, shell, Tray } from 'electron'
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs' import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
import { triggerSysProxy } from '../resolve/sysproxy' import { triggerSysProxy } from '../resolve/sysproxy'
@ -23,9 +23,16 @@ const buildContextMenu = async (): Promise<Menu> => {
label: '显示窗口', label: '显示窗口',
type: 'normal', type: 'normal',
click: (): void => { click: (): void => {
if (!mainWindow) {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
createWindow(true)
} else {
mainWindow?.show() mainWindow?.show()
mainWindow?.focusOnWebView() mainWindow?.focusOnWebView()
} }
}
}, },
{ {
id: 'rule', id: 'rule',
@ -159,7 +166,18 @@ export async function createTray(): Promise<void> {
tray.setToolTip('Another Mihomo GUI.') tray.setToolTip('Another Mihomo GUI.')
tray.setTitle('Mihomo Party') tray.setTitle('Mihomo Party')
tray.addListener('click', () => { tray.addListener('click', () => {
mainWindow?.isVisible() ? mainWindow?.hide() : mainWindow?.show() if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
if (!mainWindow) {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
createWindow(true)
} else {
mainWindow?.show()
}
}
}) })
} }

View File

@ -16,6 +16,7 @@ import {
} from './core/mihomoApi' } from './core/mihomoApi'
export let mainWindow: BrowserWindow | null = null export let mainWindow: BrowserWindow | null = null
export let destroyTimer: NodeJS.Timeout | null = null
const gotTheLock = app.requestSingleInstanceLock() const gotTheLock = app.requestSingleInstanceLock()
@ -25,8 +26,16 @@ if (!gotTheLock) {
const initPromise = init() const initPromise = init()
app.on('second-instance', async (_event, commandline) => { app.on('second-instance', async (_event, commandline) => {
if (!mainWindow) {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
createWindow(true)
} else {
mainWindow?.show() mainWindow?.show()
mainWindow?.focusOnWebView() mainWindow?.focusOnWebView()
}
const url = commandline.pop() const url = commandline.pop()
if (url) { if (url) {
await handleDeepLink(url) await handleDeepLink(url)
@ -34,17 +43,26 @@ app.on('second-instance', async (_event, commandline) => {
}) })
app.on('open-url', async (_event, url) => { app.on('open-url', async (_event, url) => {
if (!mainWindow) {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
createWindow(true)
} else {
mainWindow?.show() mainWindow?.show()
mainWindow?.focusOnWebView() mainWindow?.focusOnWebView()
}
await handleDeepLink(url) await handleDeepLink(url)
}) })
// Quit when all windows are closed, except on macOS. There, it's common // Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits // for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q. // explicitly with Cmd + Q.
app.on('window-all-closed', () => { app.on('window-all-closed', (e) => {
if (process.platform !== 'darwin') { e.preventDefault()
app.quit() // if (process.platform !== 'darwin') {
} // app.quit()
// }
}) })
app.on('before-quit', () => { app.on('before-quit', () => {
@ -78,7 +96,12 @@ app.whenReady().then(async () => {
app.on('activate', function () { app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the // On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open. // dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow() if (BrowserWindow.getAllWindows().length === 0) {
if (destroyTimer) {
clearTimeout(destroyTimer)
}
createWindow(true)
}
}) })
}) })
@ -106,7 +129,7 @@ async function handleDeepLink(url: string): Promise<void> {
} }
} }
function createWindow(): void { export function createWindow(show = false): void {
Menu.setApplicationMenu(null) Menu.setApplicationMenu(null)
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
@ -125,7 +148,7 @@ function createWindow(): void {
}) })
mainWindow.on('ready-to-show', async () => { mainWindow.on('ready-to-show', async () => {
const { silentStart } = await getAppConfig() const { silentStart } = await getAppConfig()
if (!silentStart) { if (!silentStart || show) {
mainWindow?.show() mainWindow?.show()
mainWindow?.focusOnWebView() mainWindow?.focusOnWebView()
} }
@ -145,7 +168,14 @@ function createWindow(): void {
stopMihomoMemory() stopMihomoMemory()
event.preventDefault() event.preventDefault()
mainWindow?.hide() mainWindow?.hide()
mainWindow?.webContents.reload() if (destroyTimer) {
clearTimeout(destroyTimer)
}
destroyTimer = setTimeout(() => {
mainWindow?.destroy()
mainWindow = null
}, 300000)
// mainWindow?.webContents.reload()
}) })
mainWindow.webContents.setWindowOpenHandler((details) => { mainWindow.webContents.setWindowOpenHandler((details) => {