Files
vue-desktop/src/core/desktop/DesktopProcess.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-08-26 10:33:41 +08:00
import ProcessImpl from '@/core/process/impl/ProcessImpl.ts'
import type { ProcessInfoImpl } from '@/core/process/impl/ProcessInfoImpl.ts'
2025-08-19 14:56:38 +08:00
import XSystem from '@/core/XSystem.ts'
import { BasicSystemProcess } from '@/core/system/BasicSystemProcess.ts'
import { createApp, h } from 'vue'
import DesktopComponent from '@/core/desktop/ui/DesktopComponent.vue'
import { naiveUi } from '@/core/common/naive-ui/components.ts'
import { DesktopEventEnum } from '@/core/events/EventTypes.ts'
import { debounce } from 'lodash'
2025-08-26 17:04:31 +08:00
import type { IProcessInfo } from '@/core/process/IProcessInfo.ts'
2025-08-19 14:56:38 +08:00
2025-08-26 10:33:41 +08:00
export class DesktopProcess extends ProcessImpl {
2025-08-19 14:56:38 +08:00
private _desktopRootDom: HTMLElement;
private _isMounted: boolean = false;
private _width: number = 0;
private _height: number = 0;
private _pendingResize: boolean = false;
public get desktopRootDom() {
return this._desktopRootDom;
}
public get isMounted() {
return this._isMounted;
}
public get basicSystemProcess() {
2025-08-26 10:33:41 +08:00
return XSystem.instance.processManage.findProcessByName<BasicSystemProcess>('basic-system')
2025-08-19 14:56:38 +08:00
}
public get width() {
return this._width;
}
public set width(value: number) {
if (this._height === value) return;
if (!this._isMounted) return;
this._width = value;
this._desktopRootDom.style.width = value >= 0 ? `${value}px` : '100%';
this.scheduleResizeEvent()
}
public get height() {
return this._height;
}
public set height(value: number) {
if (this._height === value) return;
if (!this._isMounted) return;
this._height = value;
this._desktopRootDom.style.height = value >= 0 ? `${value}px` : '100%';
this.scheduleResizeEvent()
}
private scheduleResizeEvent() {
if (this._pendingResize) return;
this._pendingResize = true;
Promise.resolve().then(() => {
if (this._pendingResize) {
this._pendingResize = false;
console.log('onDesktopRootDomResize')
this.eventManages.notifyEvent(DesktopEventEnum.onDesktopRootDomResize, this._width, this._height);
}
});
}
private get eventManages() {
2025-08-26 10:33:41 +08:00
return XSystem.instance.eventManage;
2025-08-19 14:56:38 +08:00
}
2025-08-26 17:04:31 +08:00
constructor(info: IProcessInfo) {
2025-08-19 14:56:38 +08:00
super(info)
console.log('DesktopProcess')
}
public mount(dom: HTMLDivElement) {
2025-08-19 16:59:58 +08:00
console.log('DesktopProcess: start mount')
if (this._isMounted) return
2025-08-19 14:56:38 +08:00
this._width = window.innerWidth
this._height = window.innerHeight
window.addEventListener(
'resize',
debounce(() => {
this.width = window.innerWidth
this.height = window.innerHeight
2025-08-19 16:59:58 +08:00
}, 300)
2025-08-19 14:56:38 +08:00
)
dom.style.zIndex = '0';
dom.style.width = `${this._width}px`
dom.style.height = `${this._height}px`
2025-08-19 16:59:58 +08:00
this._desktopRootDom = dom
2025-08-19 14:56:38 +08:00
const app = createApp(DesktopComponent, { process: this })
app.use(naiveUi)
app.mount(dom)
2025-08-19 16:59:58 +08:00
this._isMounted = true
2025-08-19 14:56:38 +08:00
}
}