From 97a956ffe89c5a9ae08d32465f7a7becd51d7794 Mon Sep 17 00:00:00 2001 From: duanshuwen Date: Tue, 21 Apr 2026 22:37:32 +0800 Subject: [PATCH] feat: remove getCurrentWindowId IPC event and related functionality for cleaner API --- dist-electron/main/main.js | 2 +- dist-electron/preload/preload.js | 3 -- electron/preload/index.ts | 3 -- electron/service/ipc/index.ts | 3 -- global.d.ts | 6 ---- runtime-shared/lib/constants.ts | 1 - src/lib/constants.ts | 1 - src/lib/index.ts | 4 --- src/lib/runtime.ts | 53 +------------------------------- src/stores/settings.ts | 24 ++------------- src/types/runtime.ts | 10 ------ 11 files changed, 5 insertions(+), 105 deletions(-) diff --git a/dist-electron/main/main.js b/dist-electron/main/main.js index a921558..2ffb9f1 100644 --- a/dist-electron/main/main.js +++ b/dist-electron/main/main.js @@ -1,6 +1,6 @@ "use strict"; require("electron"); -require("./main-BjSUL7Zm.js"); +require("./main-BynUR3CU.js"); require("electron-squirrel-startup"); require("electron-log"); require("bytenode"); diff --git a/dist-electron/preload/preload.js b/dist-electron/preload/preload.js index c4f6639..97b1935 100644 --- a/dist-electron/preload/preload.js +++ b/dist-electron/preload/preload.js @@ -21,7 +21,6 @@ var IPC_EVENTS = /* @__PURE__ */ ((IPC_EVENTS2) => { IPC_EVENTS2["APP_QUIT"] = "app:quit"; IPC_EVENTS2["FILE_READ"] = "file:read"; IPC_EVENTS2["FILE_WRITE"] = "file:write"; - IPC_EVENTS2["GET_WINDOW_ID"] = "get-window-id"; IPC_EVENTS2["CUSTOM_EVENT"] = "custom:event"; IPC_EVENTS2["TIME_UPDATE"] = "time:update"; IPC_EVENTS2["RENDERER_IS_READY"] = "renderer-ready"; @@ -92,8 +91,6 @@ const api = { }, // 发送消息到主进程 send: (channel, ...args) => electron.ipcRenderer.send(channel, ...args), - // 获取窗口ID - getCurrentWindowId: () => electron.ipcRenderer.sendSync(IPC_EVENTS.GET_WINDOW_ID), // 发送日志 logger: { debug: (message, ...meta) => electron.ipcRenderer.send(IPC_EVENTS.LOG_DEBUG, message, ...meta), diff --git a/electron/preload/index.ts b/electron/preload/index.ts index e06fcd2..29f2837 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -41,9 +41,6 @@ const api: WindowApi = { // 发送消息到主进程 send: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.send(channel, ...args), - // 获取窗口ID - getCurrentWindowId: () => ipcRenderer.sendSync(IPC_EVENTS.GET_WINDOW_ID), - // 发送日志 logger: { debug: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_DEBUG, message, ...meta), diff --git a/electron/service/ipc/index.ts b/electron/service/ipc/index.ts index b9072cd..12aeeb2 100644 --- a/electron/service/ipc/index.ts +++ b/electron/service/ipc/index.ts @@ -52,9 +52,6 @@ export class IPCManager { } }) - ipcMain.handle('get-window-id',(event: any) => { - event.returnValue = event.sender.id - }) } // 注册同步处理器 diff --git a/global.d.ts b/global.d.ts index 2f896d1..5bbd208 100644 --- a/global.d.ts +++ b/global.d.ts @@ -14,11 +14,6 @@ declare global { params: [window: number] return: {success: boolean, error?: string} } - [IPC_EVENTS.GET_WINDOW_ID]: { - params: [] - return: number - } - // 异步通信 [IPC_EVENTS.FILE_READ]: { params: [filePath: string] @@ -138,7 +133,6 @@ declare global { invokeAsync(channel: T, ...args: IPCTypings[T]['params']): IPCTypings[T]['return'], on(channel: T, callback: (...args: IPCTypings[T]['params']) => void): () => void send(channel: T, ...args: IPCTypings[T]['params']): void, - getCurrentWindowId(): number, versions: NodeJS.ProcessVersions, external: { open: (url: string) => Promise diff --git a/runtime-shared/lib/constants.ts b/runtime-shared/lib/constants.ts index 71b2cef..4bf515c 100644 --- a/runtime-shared/lib/constants.ts +++ b/runtime-shared/lib/constants.ts @@ -19,7 +19,6 @@ export enum IPC_EVENTS { APP_QUIT = 'app:quit', FILE_READ = 'file:read', FILE_WRITE = 'file:write', - GET_WINDOW_ID = 'get-window-id', CUSTOM_EVENT = 'custom:event', TIME_UPDATE = 'time:update', RENDERER_IS_READY = 'renderer-ready', diff --git a/src/lib/constants.ts b/src/lib/constants.ts index d2be6f0..fcaf701 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -9,7 +9,6 @@ export const IPC_EVENTS = { GET_THEME_MODE: 'get-theme-mode', SET_THEME_MODE: 'set-theme-mode', THEME_MODE_UPDATED: 'theme-mode-updated', - GET_WINDOW_ID: 'get-window-id', TASK_PROGRESS: 'task:progress', TASK_STARTED: 'task:started', TASK_COMPLETED: 'task:completed', diff --git a/src/lib/index.ts b/src/lib/index.ts index 288b2af..0a11f4d 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -29,9 +29,7 @@ export { export { detectRuntimePlatform, - detectWindowName, hasIpcBridge, - resolveWindowIdentity, } from './runtime'; export type { @@ -47,6 +45,4 @@ export type { RuntimePlatform, ThemeMode, WindowApiBridge, - WindowIdentity, - WindowName, } from '../types/runtime'; diff --git a/src/lib/runtime.ts b/src/lib/runtime.ts index b12a959..f1d13cb 100644 --- a/src/lib/runtime.ts +++ b/src/lib/runtime.ts @@ -1,6 +1,4 @@ -import { IPC_EVENTS, WINDOW_NAMES } from './constants'; -import { invokeIpc } from './host-api'; -import type { RuntimePlatform, WindowIdentity, WindowName } from '../types/runtime'; +import type { RuntimePlatform } from '../types/runtime'; function normalizePlatform(platform: string | undefined | null): RuntimePlatform { const value = platform?.toLowerCase() ?? ''; @@ -25,52 +23,3 @@ export function detectRuntimePlatform(): RuntimePlatform { } return window.api ? 'unknown' : 'web'; } - -function readWindowIdFromBridge(): string | number | null { - if (typeof window.api?.getCurrentWindowId === 'function') { - return window.api.getCurrentWindowId(); - } - - return null; -} - -export function detectWindowName(windowId?: string | number | null): WindowName { - if (typeof windowId === 'string') { - const normalized = windowId.toLowerCase(); - if (normalized.includes('setting')) return WINDOW_NAMES.SETTING; - if (normalized.includes('dialog')) return WINDOW_NAMES.DIALOG; - if (normalized.includes('loading')) return WINDOW_NAMES.LOADING; - } - return WINDOW_NAMES.MAIN; -} - -export async function resolveWindowIdentity(): Promise { - const platform = detectRuntimePlatform(); - const isElectron = platform !== 'web' && platform !== 'unknown'; - - if (!hasIpcBridge()) { - return { - platform, - windowId: null, - windowName: WINDOW_NAMES.MAIN, - isElectron, - }; - } - - let windowId: string | number | null = null; - try { - windowId = readWindowIdFromBridge(); - if (windowId === null) { - windowId = await invokeIpc(IPC_EVENTS.GET_WINDOW_ID); - } - } catch { - windowId = null; - } - - return { - platform, - windowId, - windowName: detectWindowName(windowId), - isElectron, - }; -} diff --git a/src/stores/settings.ts b/src/stores/settings.ts index 6483aab..e1e1468 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -2,7 +2,7 @@ import { useSyncExternalStore } from 'react'; import { CONFIG_KEYS, DEFAULT_LANGUAGE, DEFAULT_THEME_MODE, IPC_EVENTS } from '../lib/constants'; import { hasHostApiBridge, hostApiFetch, invokeIpc, onIpc } from '../lib/host-api'; import { applyThemeModeToDocument, detectSystemTheme, resolveAppliedTheme, watchSystemTheme } from '../lib/theme'; -import { detectRuntimePlatform, resolveWindowIdentity } from '../lib/runtime'; +import { detectRuntimePlatform } from '../lib/runtime'; import { i18n, setLocale as setI18nLocale } from '../i18n'; import { detectSystemLanguage, resolveSupportedLanguage } from '../i18n/resolver'; import type { @@ -11,15 +11,11 @@ import type { ResolvedThemeMode, RuntimePlatform, ThemeMode, - WindowIdentity, - WindowName, } from '../types/runtime'; export interface SettingsState { initialized: boolean; platform: RuntimePlatform; - windowId: string | number | null; - windowName: WindowName; themeMode: ThemeMode; systemTheme: ResolvedThemeMode; appliedTheme: ResolvedThemeMode; @@ -64,8 +60,6 @@ function createInitialState(): SettingsState { return { initialized: false, platform: detectRuntimePlatform(), - windowId: null, - windowName: 'main', themeMode: DEFAULT_THEME_MODE, systemTheme, appliedTheme: resolveAppliedTheme(DEFAULT_THEME_MODE, systemTheme), @@ -180,16 +174,6 @@ function syncSystemTheme(): void { }); } -async function syncWindowIdentity(): Promise { - const identity = await resolveWindowIdentity(); - patchState({ - platform: identity.platform, - windowId: identity.windowId, - windowName: identity.windowName, - }); - return identity; -} - async function syncThemeEvent(): Promise { if (unsubscribeThemeEvent || typeof window === 'undefined' || !window.api?.on) return; @@ -207,7 +191,7 @@ async function hydrate(): Promise { if (initPromise) return initPromise; initPromise = (async () => { - const identity = await syncWindowIdentity(); + const platform = detectRuntimePlatform(); const systemTheme = detectSystemTheme(); const systemLanguage = detectSystemLanguage(); @@ -228,9 +212,7 @@ async function hydrate(): Promise { patchState({ initialized: true, - platform: identity.platform, - windowId: identity.windowId, - windowName: identity.windowName, + platform, themeMode, systemTheme, appliedTheme, diff --git a/src/types/runtime.ts b/src/types/runtime.ts index 621071d..7d2262e 100644 --- a/src/types/runtime.ts +++ b/src/types/runtime.ts @@ -16,8 +16,6 @@ export type LanguageCode = 'en' | 'zh' | 'th'; export type RuntimePlatform = 'win32' | 'darwin' | 'linux' | 'web' | 'unknown'; -export type WindowName = 'main' | 'setting' | 'dialog' | 'loading'; - export type IpcArgs = readonly unknown[]; export type IpcListener = (...args: unknown[]) => void; @@ -63,14 +61,6 @@ export interface WindowApiBridge { invokeAsync?(channel: string, ...args: IpcArgs): Promise; on?(channel: string, callback: IpcListener): () => void; send?(channel: string, ...args: IpcArgs): void; - getCurrentWindowId?(): string | number; -} - -export interface WindowIdentity { - platform: RuntimePlatform; - windowId: string | number | null; - windowName: WindowName; - isElectron: boolean; } export interface HostApiResult {