Files
vue-desktop/src/services/windowForm/WindowFormService.ts

120 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-10-23 12:14:03 +08:00
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
* ----------------------
* ManagerRendererEventBinder
*
*/
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()
}
}