feat: remove getCurrentWindowId IPC event and related functionality for cleaner API
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user