Files
vue-desktop/src/core/process/impl/ProcessImpl.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-08-19 14:56:38 +08:00
import { v4 as uuidV4 } from 'uuid';
2025-08-28 08:05:16 +08:00
import WindowFormImpl from '../../window/impl/WindowFormImpl.ts'
2025-08-26 10:33:41 +08:00
import type { IProcess } from '@/core/process/IProcess.ts'
import type { IProcessInfo } from '@/core/process/IProcessInfo.ts'
2025-08-28 08:05:16 +08:00
import type { IWindowForm } from '@/core/window/IWindowForm.ts'
2025-08-29 14:22:20 +08:00
import { processManager } from '@/core/process/ProcessManager.ts'
2025-09-09 12:03:09 +08:00
import { EventBuilderImpl } from '@/core/events/impl/EventBuilderImpl.ts'
import type { IEventBuilder } from '@/core/events/IEventBuilder.ts'
2025-09-16 14:32:57 +08:00
import type { IProcessEvent } from '@/core/process/types/ProcessEventTypes.ts'
2025-08-19 14:56:38 +08:00
/**
*
*/
2025-08-26 10:33:41 +08:00
export default class ProcessImpl implements IProcess {
2025-08-19 14:56:38 +08:00
private readonly _id: string = uuidV4();
2025-08-26 10:33:41 +08:00
private readonly _processInfo: IProcessInfo;
2025-08-19 14:56:38 +08:00
// 当前进程的窗体集合
2025-08-28 08:05:16 +08:00
private _windowForms: Map<string, IWindowForm> = new Map();
2025-09-09 12:03:09 +08:00
private _event: IEventBuilder<IProcessEvent> = new EventBuilderImpl<IProcessEvent>()
2025-08-19 14:56:38 +08:00
public get id() {
return this._id;
}
public get processInfo() {
return this._processInfo;
}
public get windowForms() {
return this._windowForms;
}
2025-09-09 12:03:09 +08:00
public get event() {
return this._event;
}
2025-08-19 14:56:38 +08:00
2025-08-26 10:33:41 +08:00
constructor(info: IProcessInfo) {
2025-08-19 14:56:38 +08:00
console.log(`AppProcess: ${info.name}`)
this._processInfo = info;
const startName = info.startName;
2025-09-09 12:03:09 +08:00
this.initEvent();
2025-08-29 14:22:20 +08:00
processManager.registerProcess(this);
2025-08-19 14:56:38 +08:00
// 通过设置 isJustProcess 为 true则不会创建窗体
if (!info.isJustProcess) {
this.openWindowForm(startName)
}
}
2025-09-09 12:03:09 +08:00
private initEvent() {
2025-09-17 12:22:28 +08:00
this.event.addEventListener('processWindowFormExit', (id: string) => {
2025-09-09 12:03:09 +08:00
this.windowForms.delete(id)
if(this.windowForms.size === 0) {
processManager.removeProcess(this)
}
})
}
2025-08-19 14:56:38 +08:00
public openWindowForm(startName: string) {
2025-08-28 08:05:16 +08:00
const info = this._processInfo.windowFormConfigs.find(item => item.name === startName);
if (!info) throw new Error(`未找到窗体:${startName}`);
2025-09-17 12:22:28 +08:00
const wf = new WindowFormImpl(this, info);
this._windowForms.set(wf.id, wf);
}
public closeWindowForm(id: string) {
try {
const wf = this._windowForms.get(id);
if (!wf) throw new Error(`未找到窗体:${id}`);
2025-09-22 13:23:12 +08:00
wf.destroy();
2025-09-17 12:22:28 +08:00
this.windowForms.delete(id)
if(this.windowForms.size === 0) {
2025-09-22 13:23:12 +08:00
this.destroy()
2025-09-17 12:22:28 +08:00
processManager.removeProcess(this)
}
} catch (e) {
console.log('关闭窗体失败', e)
}
2025-08-19 14:56:38 +08:00
}
2025-09-22 13:23:12 +08:00
public destroy() {
this._event.destroy()
}
2025-08-19 14:56:38 +08:00
}