From 68bdabf928c9e1f4caf407f1f19c286c5b92a1dc Mon Sep 17 00:00:00 2001 From: Azure <983547216@qq.com> Date: Wed, 17 Sep 2025 12:22:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/process/IProcess.ts | 5 ++ src/core/process/impl/ProcessImpl.ts | 19 ++++- src/core/process/types/ProcessEventTypes.ts | 2 +- .../service/services/WindowFormService.ts | 44 ++++------ src/core/window/IWindowForm.ts | 2 - src/core/window/impl/WindowFormImpl.ts | 83 ++++++++++++------- src/core/window/ui/WindowFormElement.ts | 17 +++- 7 files changed, 105 insertions(+), 67 deletions(-) diff --git a/src/core/process/IProcess.ts b/src/core/process/IProcess.ts index 28f61d0..45f25c0 100644 --- a/src/core/process/IProcess.ts +++ b/src/core/process/IProcess.ts @@ -19,4 +19,9 @@ export interface IProcess { * @param startName 窗体启动名 */ openWindowForm(startName: string): void; + /** + * 关闭窗体 + * @param id 窗体id + */ + closeWindowForm(id: string): void; } diff --git a/src/core/process/impl/ProcessImpl.ts b/src/core/process/impl/ProcessImpl.ts index 64125cc..0a513ee 100644 --- a/src/core/process/impl/ProcessImpl.ts +++ b/src/core/process/impl/ProcessImpl.ts @@ -47,7 +47,7 @@ export default class ProcessImpl implements IProcess { } private initEvent() { - this.event.addEventListener('onProcessWindowFormExit', (id: string) => { + this.event.addEventListener('processWindowFormExit', (id: string) => { this.windowForms.delete(id) if(this.windowForms.size === 0) { processManager.removeProcess(this) @@ -58,7 +58,20 @@ export default class ProcessImpl implements IProcess { public openWindowForm(startName: string) { const info = this._processInfo.windowFormConfigs.find(item => item.name === startName); if (!info) throw new Error(`未找到窗体:${startName}`); - const window = new WindowFormImpl(this, info); - this._windowForms.set(window.id, window); + 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}`); + this.windowForms.delete(id) + if(this.windowForms.size === 0) { + processManager.removeProcess(this) + } + } catch (e) { + console.log('关闭窗体失败', e) + } } } \ No newline at end of file diff --git a/src/core/process/types/ProcessEventTypes.ts b/src/core/process/types/ProcessEventTypes.ts index d6bb3b5..ee98546 100644 --- a/src/core/process/types/ProcessEventTypes.ts +++ b/src/core/process/types/ProcessEventTypes.ts @@ -20,5 +20,5 @@ export interface IProcessEvent extends IEventMap { * 进程的窗体退出 * @param id 窗体id */ - onProcessWindowFormExit: (id: string) => void + processWindowFormExit: (id: string) => void } \ No newline at end of file diff --git a/src/core/service/services/WindowFormService.ts b/src/core/service/services/WindowFormService.ts index 00902c2..0453b25 100644 --- a/src/core/service/services/WindowFormService.ts +++ b/src/core/service/services/WindowFormService.ts @@ -1,4 +1,8 @@ import { AService } from '@/core/service/kernel/AService.ts' +import type { IWindowForm } from '@/core/window/IWindowForm.ts' +import WindowFormImpl from '@/core/window/impl/WindowFormImpl.ts' +import type { IProcess } from '@/core/process/IProcess.ts' +import type { IWindowFormConfig } from '@/core/window/types/IWindowFormConfig.ts' interface IWindow { id: string; @@ -13,67 +17,47 @@ interface IWindow { } export class WindowFormService extends AService { - private windows: Map = new Map(); - private zCounter = 1; + private windows: Map = new Map(); constructor() { - super("WindowForm"); + super("WindowFormService"); console.log('WindowFormService - 服务注册') } - createWindow(title: string, config?: Partial): IWindow { - const id = `win-${Date.now()}-${Math.random()}`; - const win: IWindow = { - id, - title, - x: config?.x ?? 100, - y: config?.y ?? 100, - width: config?.width ?? 400, - height: config?.height ?? 300, - zIndex: this.zCounter++, - minimized: false, - maximized: false - }; - this.windows.set(id, win); - this.sm.broadcast("WindowFrom:created", win); - return win; + public createWindow(proc: IProcess, info: IWindowFormConfig): IWindowForm { + const window = new WindowFormImpl(proc, info); + this.windows.set(window.id, window); + return window; } - closeWindow(id: string) { + public closeWindow(id: string) { if (this.windows.has(id)) { this.windows.delete(id); this.sm.broadcast("WindowFrom:closed", id); } } - focusWindow(id: string) { + public focusWindow(id: string) { const win = this.windows.get(id); if (win) { - win.zIndex = this.zCounter++; this.sm.broadcast("WindowFrom:focused", win); } } - minimizeWindow(id: string) { + public minimizeWindow(id: string) { const win = this.windows.get(id); if (win) { - win.minimized = true; this.sm.broadcast("WindowFrom:minimized", win); } } - maximizeWindow(id: string) { + public maximizeWindow(id: string) { const win = this.windows.get(id); if (win) { - win.maximized = !win.maximized; this.sm.broadcast("WindowFrom:maximized", win); } } - getWindows(): IWindow[] { - return Array.from(this.windows.values()).sort((a, b) => a.zIndex - b.zIndex); - } - onMessage(event: string, data?: any) { console.log(`[WindowService] 收到事件:`, event, data); } diff --git a/src/core/window/IWindowForm.ts b/src/core/window/IWindowForm.ts index 344f7b9..d42b7d3 100644 --- a/src/core/window/IWindowForm.ts +++ b/src/core/window/IWindowForm.ts @@ -10,6 +10,4 @@ export interface IWindowForm { get windowFormEle(): HTMLElement; /** 窗体状态 */ get windowFormState(): TWindowFormState; - /** 关闭窗体 */ - closeWindowForm(): void; } \ No newline at end of file diff --git a/src/core/window/impl/WindowFormImpl.ts b/src/core/window/impl/WindowFormImpl.ts index 08f7aa9..1a67d16 100644 --- a/src/core/window/impl/WindowFormImpl.ts +++ b/src/core/window/impl/WindowFormImpl.ts @@ -3,7 +3,7 @@ import XSystem from '../../XSystem.ts' import type { IProcess } from '@/core/process/IProcess.ts' import type { IWindowForm } from '@/core/window/IWindowForm.ts' import type { IWindowFormConfig } from '@/core/window/types/IWindowFormConfig.ts' -import type { WindowFormPos } from '@/core/window/types/WindowFormTypes.ts' +import type { TWindowFormState, WindowFormPos } from '@/core/window/types/WindowFormTypes.ts' import { processManager } from '@/core/process/ProcessManager.ts' import { DraggableResizableWindow } from '@/core/utils/DraggableResizableWindow.ts' import '../css/window-form.scss' @@ -13,31 +13,43 @@ import { wfem } from '@/core/events/WindowFormEventManager.ts' import type { IObservable } from '@/core/state/IObservable.ts' import { ObservableImpl } from '@/core/state/impl/ObservableImpl.ts' +export interface IWindowFormDataState { + /** 窗体id */ + id: string; + /** 窗体进程id */ + procId: string; + /** 进程名称唯一 */ + name: string; + /** 窗体位置x (左上角) */ + x: number; + /** 窗体位置y (左上角) */ + y: number; + /** 窗体宽度 */ + width: number; + /** 窗体高度 */ + height: number; + /** 窗体状态 'default' | 'minimized' | 'maximized' */ + state: TWindowFormState; + /** 窗体是否已关闭 */ + closed: boolean; +} + export default class WindowFormImpl implements IWindowForm { private readonly _id: string = uuidV4() - private readonly _procId: string + private readonly _proc: IProcess; private dom: HTMLElement private drw: DraggableResizableWindow - private pos: WindowFormPos = { x: 0, y: 0 } - private width: number - private height: number - private _state: IObservable<{ x: number, y: number }> = new ObservableImpl({ - x: 0, - y: 0, - }) + private _data: IObservable; public get id() { return this._id } public get proc() { - return processManager.findProcessById(this._procId) + return this._proc } private get desktopRootDom() { return XSystem.instance.desktopRootDom } - private get sm() { - return serviceManager.getService('WindowForm') - } public get windowFormEle() { return this.dom } @@ -46,27 +58,35 @@ export default class WindowFormImpl implements IWindowForm { } constructor(proc: IProcess, config: IWindowFormConfig) { - this._procId = proc.id + this._proc = proc console.log('WindowForm') - this.pos = { + + this._data = new ObservableImpl({ + id: this.id, + procId: proc.id, + name: proc.processInfo.name, x: config.left ?? 0, y: config.top ?? 0, - } - this.width = config.width ?? 200 - this.height = config.height ?? 100 + width: config.width ?? 200, + height: config.height ?? 100, + state: 'default', + closed: false + }) - this.createWindowFrom() this.initEvent() + this.createWindowFrom() } private initEvent() { - wfem.addEventListener('windowFormClose', this.windowFormCloseFun) - } - - private windowFormCloseFun = (id: string) => { - if (id === this.id) { + this._data.subscribeKey('closed', (state) => { + console.log('closed', state) this.closeWindowForm() - } + this._proc.closeWindowForm(this.id) + }) + + // this._data.subscribeKey(['x', 'y'], (state) => { + // console.log('x,y', state) + // }) } private createWindowFrom() { @@ -96,8 +116,8 @@ export default class WindowFormImpl implements IWindowForm { // this.desktopRootDom.appendChild(this.dom); const wf = document.createElement('window-form-element') - wf.pos = this._state wf.wid = this.id + wf.wfData = this._data wf.dragContainer = document.body wf.snapDistance = 20 wf.taskbarElementId = '#taskbar' @@ -111,21 +131,24 @@ export default class WindowFormImpl implements IWindowForm { console.log('windowForm:stateChange:restore', e) }) wf.addManagedEventListener('windowForm:close', () => { - this.closeWindowForm() + // this.closeWindowForm() }) this.dom = wf this.desktopRootDom.appendChild(this.dom) wfem.notifyEvent('windowFormFocus', this.id) } - public closeWindowForm() { + private closeWindowForm() { // this.drw.destroy(); this.desktopRootDom.removeChild(this.dom) - this.proc?.event.notifyEvent('onProcessWindowFormExit', this.id) - wfem.removeEventListener('windowFormClose', this.windowFormCloseFun) + this._data.dispose() // wfem.notifyEvent('windowFormClose', this.id) } + public minimize() {} + public maximize() {} + public restore() {} + private createWindowFormEle() { const template = document.createElement('template') template.innerHTML = ` diff --git a/src/core/window/ui/WindowFormElement.ts b/src/core/window/ui/WindowFormElement.ts index a818ccd..4987b38 100644 --- a/src/core/window/ui/WindowFormElement.ts +++ b/src/core/window/ui/WindowFormElement.ts @@ -4,6 +4,7 @@ import wfStyle from './wf.scss?inline' import type { TWindowFormState } from '@/core/window/types/WindowFormTypes.ts' import { wfem } from '@/core/events/WindowFormEventManager.ts' import type { IObservable } from '@/core/state/IObservable.ts' +import type { IWindowFormDataState } from '@/core/window/impl/WindowFormImpl.ts' /** 拖拽移动开始的回调 */ type TDragStartCallback = (x: number, y: number) => void; @@ -84,7 +85,7 @@ export class WindowFormElement extends LitElement { @property({ type: Number }) maxHeight?: number @property({ type: Number }) minHeight?: number @property({ type: String }) taskbarElementId?: string - @property({ type: Object }) pos: IObservable<{ x: number, y: number }>; + @property({ type: Object }) wfData: IObservable; private _listeners: Array<{ type: string; handler: EventListener }> = [] @@ -107,6 +108,19 @@ export class WindowFormElement extends LitElement { private animationFrame?: number private resizing = false + // private get x() { + // return this.wfData.state.x + // } + // private set x(value: number) { + // this.wfData.patch({ x: value }) + // } + // private get y() { + // return this.wfData.state.y + // } + // private set y(value: number) { + // this.wfData.patch({ y: value }) + // } + // private windowFormState: TWindowFormState = 'default'; /** 元素信息 */ private targetBounds: IElementRect @@ -687,6 +701,7 @@ export class WindowFormElement extends LitElement { composed: true, }), ) + this.wfData.state.closed = true } /**