Files
esengine/packages/editor-core/src/Services/WindowRegistry.ts

178 lines
3.5 KiB
TypeScript
Raw Normal View History

import { IService } from '@esengine/esengine';
import { ComponentType } from 'react';
/**
*
*/
export interface WindowDescriptor {
/**
*
*/
id: string;
/**
*
*/
component: ComponentType<any>;
/**
*
*/
title?: string;
/**
*
*/
defaultWidth?: number;
/**
*
*/
defaultHeight?: number;
}
/**
*
*/
export interface WindowInstance {
/**
*
*/
descriptor: WindowDescriptor;
/**
*
*/
isOpen: boolean;
/**
*
*/
params?: Record<string, any>;
}
/**
*
*
*
*/
export class WindowRegistry implements IService {
private windows: Map<string, WindowDescriptor> = new Map();
private openWindows: Map<string, WindowInstance> = new Map();
private listeners: Set<() => void> = new Set();
/**
*
*/
registerWindow(descriptor: WindowDescriptor): void {
if (this.windows.has(descriptor.id)) {
console.warn(`Window ${descriptor.id} is already registered`);
return;
}
this.windows.set(descriptor.id, descriptor);
}
/**
*
*/
unregisterWindow(windowId: string): void {
this.windows.delete(windowId);
this.openWindows.delete(windowId);
this.notifyListeners();
}
/**
*
*/
getWindow(windowId: string): WindowDescriptor | undefined {
return this.windows.get(windowId);
}
/**
*
*/
getAllWindows(): WindowDescriptor[] {
return Array.from(this.windows.values());
}
/**
*
*/
openWindow(windowId: string, params?: Record<string, any>): void {
const descriptor = this.windows.get(windowId);
if (!descriptor) {
console.warn(`Window ${windowId} is not registered`);
return;
}
this.openWindows.set(windowId, {
descriptor,
isOpen: true,
params
});
this.notifyListeners();
}
/**
*
*/
closeWindow(windowId: string): void {
this.openWindows.delete(windowId);
this.notifyListeners();
}
/**
*
*/
getOpenWindow(windowId: string): WindowInstance | undefined {
return this.openWindows.get(windowId);
}
/**
*
*/
getAllOpenWindows(): WindowInstance[] {
return Array.from(this.openWindows.values());
}
/**
*
*/
isWindowOpen(windowId: string): boolean {
return this.openWindows.has(windowId);
}
/**
*
*/
addListener(listener: () => void): () => void {
this.listeners.add(listener);
return () => {
this.listeners.delete(listener);
};
}
/**
*
*/
private notifyListeners(): void {
this.listeners.forEach((listener) => listener());
}
/**
*
*/
clear(): void {
this.windows.clear();
this.openWindows.clear();
this.listeners.clear();
}
/**
*
*/
dispose(): void {
this.clear();
}
}