From 34b7ba97b79b7a35983056edb952ddfe06b1c137 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sun, 6 Oct 2024 00:09:23 +0800 Subject: [PATCH] adjust the open and close window events. --- src/main/index.ts | 8 ++++++++ src/main/resolve/floatingWindow.ts | 13 ++++++++++++- src/main/resolve/shortcut.ts | 18 ++++-------------- src/main/resolve/tray.ts | 30 ++++++------------------------ src/main/utils/ipc.ts | 3 ++- src/renderer/src/FloatingApp.tsx | 4 ++-- src/renderer/src/utils/ipc.ts | 4 ++++ 7 files changed, 38 insertions(+), 42 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 275a149..b6cb95f 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -284,6 +284,14 @@ export async function createWindow(): Promise { } } +export function triggerMainWindow(): void { + if (mainWindow?.isVisible()) { + closeMainWindow() + } else { + showMainWindow() + } +} + export function showMainWindow(): void { if (mainWindow) { if (quitTimeout) { diff --git a/src/main/resolve/floatingWindow.ts b/src/main/resolve/floatingWindow.ts index 3fd56a9..ed1513f 100644 --- a/src/main/resolve/floatingWindow.ts +++ b/src/main/resolve/floatingWindow.ts @@ -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 { 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 { + if (floatingWindow?.isVisible()) { + await patchAppConfig({ showFloatingWindow: false }) + closeFloatingWindow() + } else { + await patchAppConfig({ showFloatingWindow: true }) + showFloatingWindow() + } +} + export function closeFloatingWindow(): void { if (floatingWindow) { floatingWindow.close() diff --git a/src/main/resolve/shortcut.ts b/src/main/resolve/shortcut.ts index 0529fb4..489ab60 100644 --- a/src/main/resolve/shortcut.ts +++ b/src/main/resolve/shortcut.ts @@ -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': { diff --git a/src/main/resolve/tray.ts b/src/main/resolve/tray.ts index 3cb3d3e..0d4e2e3 100644 --- a/src/main/resolve/tray.ts +++ b/src/main/resolve/tray.ts @@ -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 => { label: floatingWindow?.isVisible() ? '关闭悬浮窗' : '显示悬浮窗', type: 'normal', click: async (): Promise => { - if (floatingWindow) { - await patchAppConfig({ showFloatingWindow: false }) - closeFloatingWindow() - } else { - await patchAppConfig({ showFloatingWindow: true }) - showFloatingWindow() - } + await triggerFloatingWindow() } }, { @@ -314,11 +308,7 @@ export async function createTray(): Promise { 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 { } 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 { } if (process.platform === 'linux') { tray?.addListener('click', () => { - if (mainWindow?.isVisible()) { - closeMainWindow() - } else { - showMainWindow() - } + triggerMainWindow() }) ipcMain.on('updateTrayMenu', async () => { await updateTrayMenu() diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 1a30563..54882e8 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -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)()) diff --git a/src/renderer/src/FloatingApp.tsx b/src/renderer/src/FloatingApp.tsx index 97129d6..c4e2580 100644 --- a/src/renderer/src/FloatingApp.tsx +++ b/src/renderer/src/FloatingApp.tsx @@ -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)]`} > diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index 23a0df9..4dc979c 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -335,6 +335,10 @@ export async function closeMainWindow(): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('closeMainWindow')) } +export async function triggerMainWindow(): Promise { + return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('triggerMainWindow')) +} + export async function showFloatingWindow(): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('showFloatingWindow')) }