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

@@ -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> {