120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
|
|
import type { IEventBuilder } from '@/events/IEventBuilder'
|
|||
|
|
import {
|
|||
|
|
type IWindowFormConfig,
|
|||
|
|
type IWindowFormEvents,
|
|||
|
|
type IWindowFormInstance,
|
|||
|
|
WindowFormDataManager,
|
|||
|
|
EWindowFormState
|
|||
|
|
} from './WindowFormDataManager.ts'
|
|||
|
|
import { WindowFormRenderer } from './WindowFormRenderer'
|
|||
|
|
import { WindowFormEventBinder } from './WindowFormEventBinder'
|
|||
|
|
import { EventBuilderImpl } from '@/events/impl/EventBuilderImpl.ts'
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* WindowFormService
|
|||
|
|
* ----------------------
|
|||
|
|
* 框架入口类,整合 Manager、Renderer、EventBinder 三个子模块,
|
|||
|
|
* 对外提供统一的窗口系统接口(创建、销毁、最小化、最大化、聚焦等)。
|
|||
|
|
*/
|
|||
|
|
export class WindowFormService {
|
|||
|
|
private readonly dataManager: WindowFormDataManager
|
|||
|
|
private renderer: WindowFormRenderer
|
|||
|
|
private eventBinder: WindowFormEventBinder
|
|||
|
|
// 窗口内部事件总线
|
|||
|
|
private readonly wfEventBus: IEventBuilder<IWindowFormEvents>
|
|||
|
|
|
|||
|
|
constructor(eventBus: IEventBuilder<IWindowFormEvents>) {
|
|||
|
|
this.wfEventBus = new EventBuilderImpl<IWindowFormEvents>()
|
|||
|
|
// 1 初始化窗口数据管理器
|
|||
|
|
this.dataManager = new WindowFormDataManager(this.wfEventBus)
|
|||
|
|
// 2 初始化窗口渲染器
|
|||
|
|
this.renderer = new WindowFormRenderer(this.wfEventBus)
|
|||
|
|
// 3 初始化事件绑定模块(拖拽 + 调整大小)
|
|||
|
|
this.eventBinder = new WindowFormEventBinder(this.wfEventBus, this.dataManager)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建一个窗口实例
|
|||
|
|
* @param appId 应用ID
|
|||
|
|
* @param config 应用窗口配置
|
|||
|
|
*/
|
|||
|
|
async createWindow(appId: string, config: IWindowFormConfig): Promise<IWindowFormInstance> {
|
|||
|
|
// 1 创建数据实例
|
|||
|
|
const instance = await this.dataManager.createWindowForm(appId, config)
|
|||
|
|
// 2 加载阶段
|
|||
|
|
this.dataManager.updateState(instance.id, EWindowFormState.LOADING)
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 3 渲染DOM
|
|||
|
|
await this.renderer.createWindowElement(instance)
|
|||
|
|
|
|||
|
|
// 4 绑定交互事件
|
|||
|
|
this.eventBinder.bindWindowEvents(instance)
|
|||
|
|
|
|||
|
|
// 5 模拟加载完成后激活
|
|||
|
|
this.dataManager.updateState(instance.id, EWindowFormState.ACTIVE)
|
|||
|
|
this.dataManager.focus(instance.id)
|
|||
|
|
|
|||
|
|
this.wfEventBus.notify('onLoaded', instance.id)
|
|||
|
|
|
|||
|
|
return instance
|
|||
|
|
} catch (err) {
|
|||
|
|
// 异常状态
|
|||
|
|
this.dataManager.updateState(instance.id, EWindowFormState.ERROR, err as Error)
|
|||
|
|
throw err
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 销毁窗口 */
|
|||
|
|
async destroyWindow(windowId: string): Promise<void> {
|
|||
|
|
const win = this.dataManager.getWindowForm(windowId)
|
|||
|
|
if (!win) return
|
|||
|
|
this.dataManager.updateState(windowId, EWindowFormState.CLOSING)
|
|||
|
|
|
|||
|
|
// 监听 onBeforeClose 的 cancel 回调
|
|||
|
|
let isCanceled = false
|
|||
|
|
this.wfEventBus.notify('onBeforeClose', windowId, () => {
|
|||
|
|
isCanceled = true
|
|||
|
|
})
|
|||
|
|
if (isCanceled) return
|
|||
|
|
|
|||
|
|
// 确保在 DOM 销毁前清理事件
|
|||
|
|
win.subscriptions.forEach(unsub => unsub())
|
|||
|
|
win.subscriptions = []
|
|||
|
|
|
|||
|
|
this.renderer.destroy(win)
|
|||
|
|
|
|||
|
|
this.dataManager.updateState(windowId, EWindowFormState.DESTROYED)
|
|||
|
|
this.dataManager.removeWindowForm(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
getWindow(windowId: string): IWindowFormInstance | undefined {
|
|||
|
|
return this.dataManager.getWindowForm(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 最小化窗口 */
|
|||
|
|
minimize(windowId: string) {
|
|||
|
|
this.dataManager.minimize(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 最大化窗口 */
|
|||
|
|
maximize(windowId: string) {
|
|||
|
|
this.dataManager.maximize(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 还原窗口 */
|
|||
|
|
restore(windowId: string) {
|
|||
|
|
this.dataManager.restore(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 聚焦窗口 */
|
|||
|
|
focus(windowId: string) {
|
|||
|
|
this.dataManager.focus(windowId)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 销毁全局监听器(用于系统卸载时) */
|
|||
|
|
dispose() {
|
|||
|
|
this.eventBinder.dispose()
|
|||
|
|
}
|
|||
|
|
}
|