feat: remove getCurrentWindowId IPC event and related functionality for cleaner API

This commit is contained in:
duanshuwen
2026-04-21 22:37:32 +08:00
parent f9c331315b
commit 97a956ffe8
11 changed files with 5 additions and 105 deletions

View File

@@ -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");

View File

@@ -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),

View File

@@ -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),

View File

@@ -52,9 +52,6 @@ export class IPCManager {
}
})
ipcMain.handle('get-window-id',(event: any) => {
event.returnValue = event.sender.id
})
}
// 注册同步处理器

6
global.d.ts vendored
View File

@@ -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<T extends keyof IPCTypings>(channel: T, ...args: IPCTypings[T]['params']): IPCTypings[T]['return'],
on<T extends keyof IPCTypings>(channel: T, callback: (...args: IPCTypings[T]['params']) => void): () => void
send<T extends keyof IPCTypings>(channel: T, ...args: IPCTypings[T]['params']): void,
getCurrentWindowId(): number,
versions: NodeJS.ProcessVersions,
external: {
open: (url: string) => Promise<void>

View File

@@ -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',

View File

@@ -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',

View File

@@ -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';

View File

@@ -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<WindowIdentity> {
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<string | number | null>(IPC_EVENTS.GET_WINDOW_ID);
}
} catch {
windowId = null;
}
return {
platform,
windowId,
windowName: detectWindowName(windowId),
isElectron,
};
}

View File

@@ -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<WindowIdentity> {
const identity = await resolveWindowIdentity();
patchState({
platform: identity.platform,
windowId: identity.windowId,
windowName: identity.windowName,
});
return identity;
}
async function syncThemeEvent(): Promise<void> {
if (unsubscribeThemeEvent || typeof window === 'undefined' || !window.api?.on) return;
@@ -207,7 +191,7 @@ async function hydrate(): Promise<SettingsState> {
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<SettingsState> {
patchState({
initialized: true,
platform: identity.platform,
windowId: identity.windowId,
windowName: identity.windowName,
platform,
themeMode,
systemTheme,
appliedTheme,

View File

@@ -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?<T = unknown>(channel: string, ...args: IpcArgs): Promise<T>;
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<T = unknown> {