adjust the open and close window events.

This commit is contained in:
pompurin404 2024-10-06 00:09:23 +08:00
parent 3c29165028
commit 34b7ba97b7
No known key found for this signature in database
7 changed files with 38 additions and 42 deletions

View File

@ -284,6 +284,14 @@ export async function createWindow(): Promise<void> {
}
}
export function triggerMainWindow(): void {
if (mainWindow?.isVisible()) {
closeMainWindow()
} else {
showMainWindow()
}
}
export function showMainWindow(): void {
if (mainWindow) {
if (quitTimeout) {

View File

@ -2,7 +2,7 @@ import { is } from '@electron-toolkit/utils'
import { BrowserWindow, ipcMain } from 'electron'
import windowStateKeeper from 'electron-window-state'
import { join } from 'path'
import { getAppConfig } from '../config'
import { getAppConfig, patchAppConfig } from '../config'
import { applyTheme } from './theme'
import { buildContextMenu } from './tray'
@ -53,6 +53,7 @@ async function createFloatingWindow(): Promise<void> {
floatingWindow.loadFile(join(__dirname, '../renderer/floating.html'))
}
}
export function showFloatingWindow(): void {
if (floatingWindow) {
floatingWindow.show()
@ -61,6 +62,16 @@ export function showFloatingWindow(): void {
}
}
export async function triggerFloatingWindow(): Promise<void> {
if (floatingWindow?.isVisible()) {
await patchAppConfig({ showFloatingWindow: false })
closeFloatingWindow()
} else {
await patchAppConfig({ showFloatingWindow: true })
showFloatingWindow()
}
}
export function closeFloatingWindow(): void {
if (floatingWindow) {
floatingWindow.close()

View File

@ -1,5 +1,5 @@
import { app, globalShortcut, ipcMain, Notification } from 'electron'
import { mainWindow, showMainWindow } from '..'
import { mainWindow, triggerMainWindow } from '..'
import {
getAppConfig,
getControledMihomoConfig,
@ -9,7 +9,7 @@ import {
import { triggerSysProxy } from '../sys/sysproxy'
import { patchMihomoConfig } from '../core/mihomoApi'
import { quitWithoutCore, restartCore } from '../core/manager'
import { closeFloatingWindow, floatingWindow, showFloatingWindow } from './floatingWindow'
import { floatingWindow, triggerFloatingWindow } from './floatingWindow'
export async function registerShortcut(
oldShortcut: string,
@ -25,22 +25,12 @@ export async function registerShortcut(
switch (action) {
case 'showWindowShortcut': {
return globalShortcut.register(newShortcut, () => {
if (mainWindow?.isVisible()) {
mainWindow?.close()
} else {
showMainWindow()
}
triggerMainWindow()
})
}
case 'showFloatingWindowShortcut': {
return globalShortcut.register(newShortcut, async () => {
if (floatingWindow) {
await patchAppConfig({ showFloatingWindow: false })
closeFloatingWindow()
} else {
await patchAppConfig({ showFloatingWindow: true })
showFloatingWindow()
}
await triggerFloatingWindow()
})
}
case 'triggerSysProxyShortcut': {

View File

@ -15,12 +15,12 @@ import {
mihomoGroups,
patchMihomoConfig
} from '../core/mihomoApi'
import { closeMainWindow, mainWindow, showMainWindow } from '..'
import { mainWindow, showMainWindow, triggerMainWindow } from '..'
import { app, clipboard, ipcMain, Menu, nativeImage, shell, Tray } from 'electron'
import { dataDir, logDir, mihomoCoreDir, mihomoWorkDir } from '../utils/dirs'
import { triggerSysProxy } from '../sys/sysproxy'
import { quitWithoutCore, restartCore } from '../core/manager'
import { closeFloatingWindow, floatingWindow, showFloatingWindow } from './floatingWindow'
import { floatingWindow, triggerFloatingWindow } from './floatingWindow'
export let tray: Tray | null = null
@ -98,13 +98,7 @@ export const buildContextMenu = async (): Promise<Menu> => {
label: floatingWindow?.isVisible() ? '关闭悬浮窗' : '显示悬浮窗',
type: 'normal',
click: async (): Promise<void> => {
if (floatingWindow) {
await patchAppConfig({ showFloatingWindow: false })
closeFloatingWindow()
} else {
await patchAppConfig({ showFloatingWindow: true })
showFloatingWindow()
}
await triggerFloatingWindow()
}
},
{
@ -314,11 +308,7 @@ export async function createTray(): Promise<void> {
tray?.setImage(image)
})
tray?.addListener('right-click', async () => {
if (mainWindow?.isVisible()) {
closeMainWindow()
} else {
showMainWindow()
}
triggerMainWindow()
})
tray?.addListener('click', async () => {
await updateTrayMenu()
@ -326,11 +316,7 @@ export async function createTray(): Promise<void> {
}
if (process.platform === 'win32') {
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
closeMainWindow()
} else {
showMainWindow()
}
triggerMainWindow()
})
tray?.addListener('right-click', async () => {
await updateTrayMenu()
@ -338,11 +324,7 @@ export async function createTray(): Promise<void> {
}
if (process.platform === 'linux') {
tray?.addListener('click', () => {
if (mainWindow?.isVisible()) {
closeMainWindow()
} else {
showMainWindow()
}
triggerMainWindow()
})
ipcMain.on('updateTrayMenu', async () => {
await updateTrayMenu()

View File

@ -65,7 +65,7 @@ import { listWebdavBackups, webdavBackup, webdavDelete, webdavRestore } from '..
import { getInterfaces } from '../sys/interface'
import { copyEnv } from '../resolve/tray'
import { registerShortcut } from '../resolve/shortcut'
import { closeMainWindow, mainWindow, showMainWindow } from '..'
import { closeMainWindow, mainWindow, showMainWindow, triggerMainWindow } from '..'
import {
applyTheme,
fetchThemes,
@ -212,6 +212,7 @@ export function registerIpcMainHandlers(): void {
})
ipcMain.handle('showMainWindow', showMainWindow)
ipcMain.handle('closeMainWindow', closeMainWindow)
ipcMain.handle('triggerMainWindow', triggerMainWindow)
ipcMain.handle('showFloatingWindow', showFloatingWindow)
ipcMain.handle('closeFloatingWindow', closeFloatingWindow)
ipcMain.handle('showContextMenu', () => ipcErrorWrapper(showContextMenu)())

View File

@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
import MihomoIcon from './components/base/mihomo-icon'
import { calcTraffic } from './utils/calc'
import { showContextMenu, showMainWindow } from './utils/ipc'
import { showContextMenu, triggerMainWindow } from './utils/ipc'
import { useAppConfig } from './hooks/use-app-config'
import { useControledMihomoConfig } from './hooks/use-controled-mihomo-config'
@ -34,7 +34,7 @@ const FloatingApp: React.FC = () => {
showContextMenu()
}}
onClick={() => {
showMainWindow()
triggerMainWindow()
}}
className={`app-nodrag cursor-pointer floating-thumb ${tunEnabled ? 'bg-secondary' : sysProxyEnabled ? 'bg-primary' : 'bg-default'} hover:opacity-hover rounded-full h-[calc(100vh-14px)] w-[calc(100vh-14px)]`}
>

View File

@ -335,6 +335,10 @@ export async function closeMainWindow(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('closeMainWindow'))
}
export async function triggerMainWindow(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('triggerMainWindow'))
}
export async function showFloatingWindow(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('showFloatingWindow'))
}