Compare commits
3 Commits
e3ff2045ea
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f80c1863b9 | |||
| ff4791922e | |||
| 16b4b27352 |
35
README.md
35
README.md
@@ -1,37 +1,2 @@
|
|||||||
# vue-desktop
|
# vue-desktop
|
||||||
|
|
||||||
浏览器:Chrome 84+、Edge 84+、Firefox 79+、Safari 14+
|
|
||||||
|
|
||||||
Node.js:v14+
|
|
||||||
|
|
||||||
不支持IE
|
|
||||||
|
|
||||||
## Recommended IDE Setup
|
|
||||||
|
|
||||||
[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur).
|
|
||||||
|
|
||||||
## Type Support for `.vue` Imports in TS
|
|
||||||
|
|
||||||
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) to make the TypeScript language service aware of `.vue` types.
|
|
||||||
|
|
||||||
## Customize configuration
|
|
||||||
|
|
||||||
See [Vite Configuration Reference](https://vite.dev/config/).
|
|
||||||
|
|
||||||
## Project Setup
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pnpm install
|
|
||||||
```
|
|
||||||
|
|
||||||
### Compile and Hot-Reload for Development
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pnpm dev
|
|
||||||
```
|
|
||||||
|
|
||||||
### Type-Check, Compile and Minify for Production
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pnpm build
|
|
||||||
```
|
|
||||||
|
|||||||
@@ -1,23 +1,14 @@
|
|||||||
import { BasicSystemProcess } from '@/core/system/BasicSystemProcess.ts'
|
import { BasicSystemProcess } from '@/core/system/BasicSystemProcess.ts'
|
||||||
import { DesktopProcess } from '@/core/desktop/DesktopProcess.ts'
|
import { DesktopProcess } from '@/core/desktop/DesktopProcess.ts'
|
||||||
import { ObservableWeakRefImpl } from '@/core/state/impl/ObservableWeakRefImpl.ts'
|
|
||||||
import type { IObservable } from '@/core/state/IObservable.ts'
|
|
||||||
import { NotificationService } from '@/core/service/services/NotificationService.ts'
|
import { NotificationService } from '@/core/service/services/NotificationService.ts'
|
||||||
import { SettingsService } from '@/core/service/services/SettingsService.ts'
|
import { SettingsService } from '@/core/service/services/SettingsService.ts'
|
||||||
import { WindowFormService } from '@/core/service/services/WindowFormService.ts'
|
import { WindowFormService } from '@/core/service/services/WindowFormService.ts'
|
||||||
import { UserService } from '@/core/service/services/UserService.ts'
|
import { UserService } from '@/core/service/services/UserService.ts'
|
||||||
import { processManager } from '@/core/process/ProcessManager.ts'
|
import { processManager } from '@/core/process/ProcessManager.ts'
|
||||||
|
|
||||||
interface IGlobalState {
|
|
||||||
isLogin: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class XSystem {
|
export default class XSystem {
|
||||||
private static _instance: XSystem = new XSystem()
|
private static _instance: XSystem = new XSystem()
|
||||||
|
|
||||||
private _globalState: IObservable<IGlobalState> = new ObservableWeakRefImpl<IGlobalState>({
|
|
||||||
isLogin: false
|
|
||||||
})
|
|
||||||
private _desktopRootDom: HTMLElement;
|
private _desktopRootDom: HTMLElement;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -31,19 +22,13 @@ export default class XSystem {
|
|||||||
public static get instance() {
|
public static get instance() {
|
||||||
return this._instance
|
return this._instance
|
||||||
}
|
}
|
||||||
public get globalState() {
|
|
||||||
return this._globalState
|
|
||||||
}
|
|
||||||
public get desktopRootDom() {
|
public get desktopRootDom() {
|
||||||
return this._desktopRootDom
|
return this._desktopRootDom
|
||||||
}
|
}
|
||||||
|
|
||||||
public initialization(dom: HTMLDivElement) {
|
public async initialization(dom: HTMLDivElement) {
|
||||||
this._desktopRootDom = dom
|
this._desktopRootDom = dom
|
||||||
processManager.runProcess('basic-system', BasicSystemProcess).then(() => {
|
await processManager.runProcess('basic-system', BasicSystemProcess)
|
||||||
processManager.runProcess('desktop', DesktopProcess).then((proc) => {
|
await processManager.runProcess('desktop', DesktopProcess, dom)
|
||||||
proc.mount(dom)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/core/common/types/IDestroyable.ts
Normal file
8
src/core/common/types/IDestroyable.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* 可销毁接口
|
||||||
|
* 销毁实例,清理副作用,让内存可以被回收
|
||||||
|
*/
|
||||||
|
export interface IDestroyable {
|
||||||
|
/** 销毁实例,清理副作用,让内存可以被回收 */
|
||||||
|
destroy(): void
|
||||||
|
}
|
||||||
@@ -5,95 +5,68 @@ import DesktopComponent from '@/core/desktop/ui/DesktopComponent.vue'
|
|||||||
import { naiveUi } from '@/core/common/naive-ui/components.ts'
|
import { naiveUi } from '@/core/common/naive-ui/components.ts'
|
||||||
import { debounce } from 'lodash'
|
import { debounce } from 'lodash'
|
||||||
import type { IProcessInfo } from '@/core/process/IProcessInfo.ts'
|
import type { IProcessInfo } from '@/core/process/IProcessInfo.ts'
|
||||||
import { eventManager } from '@/core/events/EventManager.ts'
|
|
||||||
import { processManager } from '@/core/process/ProcessManager.ts'
|
import { processManager } from '@/core/process/ProcessManager.ts'
|
||||||
import './ui/DesktopElement.ts'
|
import './ui/DesktopElement.ts'
|
||||||
|
import { ObservableImpl } from '@/core/state/impl/ObservableImpl.ts'
|
||||||
|
|
||||||
|
interface IDesktopDataState {
|
||||||
|
/** 显示器宽度 */
|
||||||
|
monitorWidth: number;
|
||||||
|
/** 显示器高度 */
|
||||||
|
monitorHeight: number;
|
||||||
|
}
|
||||||
|
|
||||||
export class DesktopProcess extends ProcessImpl {
|
export class DesktopProcess extends ProcessImpl {
|
||||||
private _desktopRootDom: HTMLElement;
|
/** 桌面根dom,类似显示器 */
|
||||||
private _isMounted: boolean = false;
|
private readonly _monitorDom: HTMLElement
|
||||||
private _width: number = 0;
|
private _isMounted: boolean = false
|
||||||
private _height: number = 0;
|
private _data = new ObservableImpl<IDesktopDataState>({
|
||||||
private _pendingResize: boolean = false;
|
monitorWidth: 0,
|
||||||
|
monitorHeight: 0,
|
||||||
|
})
|
||||||
|
|
||||||
public get desktopRootDom() {
|
public get monitorDom() {
|
||||||
return this._desktopRootDom;
|
return this._monitorDom
|
||||||
}
|
}
|
||||||
public get isMounted() {
|
public get isMounted() {
|
||||||
return this._isMounted;
|
return this._isMounted
|
||||||
}
|
}
|
||||||
public get basicSystemProcess() {
|
public get basicSystemProcess() {
|
||||||
return processManager.findProcessByName<BasicSystemProcess>('basic-system')
|
return processManager.findProcessByName<BasicSystemProcess>('basic-system')
|
||||||
}
|
}
|
||||||
|
|
||||||
public get width() {
|
get data() {
|
||||||
return this._width;
|
return this._data
|
||||||
}
|
|
||||||
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() {
|
constructor(info: IProcessInfo, dom: HTMLDivElement) {
|
||||||
if (this._pendingResize) return;
|
|
||||||
|
|
||||||
this._pendingResize = true;
|
|
||||||
|
|
||||||
Promise.resolve().then(() => {
|
|
||||||
if (this._pendingResize) {
|
|
||||||
this._pendingResize = false;
|
|
||||||
console.log('onDesktopRootDomResize')
|
|
||||||
eventManager.notifyEvent('onDesktopRootDomResize', this._width, this._height);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(info: IProcessInfo) {
|
|
||||||
super(info)
|
super(info)
|
||||||
console.log('DesktopProcess')
|
console.log('DesktopProcess')
|
||||||
}
|
|
||||||
|
|
||||||
public mount(dom: HTMLDivElement) {
|
|
||||||
console.log('DesktopProcess: start mount')
|
|
||||||
if (this._isMounted) return
|
|
||||||
this._width = window.innerWidth
|
|
||||||
this._height = window.innerHeight
|
|
||||||
window.addEventListener(
|
|
||||||
'resize',
|
|
||||||
debounce(() => {
|
|
||||||
this.width = window.innerWidth
|
|
||||||
this.height = window.innerHeight
|
|
||||||
}, 300)
|
|
||||||
)
|
|
||||||
|
|
||||||
dom.style.zIndex = '0';
|
|
||||||
dom.style.width = `${this._width}px`
|
|
||||||
dom.style.height = `${this._height}px`
|
|
||||||
dom.style.position = 'relative'
|
dom.style.position = 'relative'
|
||||||
dom.style.overflow = 'hidden'
|
dom.style.overflow = 'hidden'
|
||||||
this._desktopRootDom = dom
|
dom.style.width = `${window.innerWidth}px`
|
||||||
|
dom.style.height = `${window.innerHeight}px`
|
||||||
|
|
||||||
|
this._monitorDom = dom
|
||||||
|
this._data.state.monitorWidth = window.innerWidth
|
||||||
|
this._data.state.monitorHeight = window.innerHeight
|
||||||
|
window.addEventListener('resize', this.onResize)
|
||||||
|
|
||||||
|
this.createDesktopUI()
|
||||||
|
}
|
||||||
|
|
||||||
|
private onResize = debounce(() => {
|
||||||
|
this._monitorDom.style.width = `${window.innerWidth}px`
|
||||||
|
this._monitorDom.style.height = `${window.innerHeight}px`
|
||||||
|
this._data.state.monitorWidth = window.innerWidth
|
||||||
|
this._data.state.monitorHeight = window.innerHeight
|
||||||
|
}, 300)
|
||||||
|
|
||||||
|
private createDesktopUI() {
|
||||||
|
if (this._isMounted) return
|
||||||
const app = createApp(DesktopComponent, { process: this })
|
const app = createApp(DesktopComponent, { process: this })
|
||||||
app.use(naiveUi)
|
app.use(naiveUi)
|
||||||
app.mount(dom)
|
app.mount(this._monitorDom)
|
||||||
|
|
||||||
// this.initDesktop(dom)
|
|
||||||
|
|
||||||
this._isMounted = true
|
this._isMounted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,4 +74,9 @@ export class DesktopProcess extends ProcessImpl {
|
|||||||
const d = document.createElement('desktop-element')
|
const d = document.createElement('desktop-element')
|
||||||
dom.appendChild(d)
|
dom.appendChild(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override destroy() {
|
||||||
|
super.destroy()
|
||||||
|
window.removeEventListener('resize', this.onResize)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,16 +5,20 @@
|
|||||||
>
|
>
|
||||||
<div class="desktop-root" @contextmenu="onContextMenu">
|
<div class="desktop-root" @contextmenu="onContextMenu">
|
||||||
<div class="desktop-bg">
|
<div class="desktop-bg">
|
||||||
<div class="desktop-icons-container"
|
<div class="desktop-icons-container" :style="gridStyle">
|
||||||
:style="gridStyle">
|
<AppIcon
|
||||||
<AppIcon v-for="(appIcon, i) in appIconsRef" :key="i"
|
v-for="(appIcon, i) in appIconsRef"
|
||||||
:iconInfo="appIcon" :gridTemplate="gridTemplate"
|
:key="i"
|
||||||
|
:iconInfo="appIcon"
|
||||||
|
:gridTemplate="gridTemplate"
|
||||||
@dblclick="runApp(appIcon)"
|
@dblclick="runApp(appIcon)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="task-bar">
|
<div class="task-bar">
|
||||||
<div id="taskbar" class="w-[80px] h-full flex justify-center items-center bg-blue">测试</div>
|
<div id="taskbar" class="w-[80px] h-full flex justify-center items-center bg-blue">
|
||||||
|
测试
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</n-config-provider>
|
</n-config-provider>
|
||||||
@@ -32,18 +36,29 @@ import { processManager } from '@/core/process/ProcessManager.ts'
|
|||||||
|
|
||||||
const props = defineProps<{ process: DesktopProcess }>()
|
const props = defineProps<{ process: DesktopProcess }>()
|
||||||
|
|
||||||
const { appIconsRef, gridStyle, gridTemplate } = useDesktopInit('.desktop-icons-container')
|
props.process.data.subscribeKey(['monitorWidth', 'monitorHeight'], ({monitorWidth, monitorHeight}) => {
|
||||||
|
console.log('onDesktopRootDomResize', monitorWidth, monitorHeight)
|
||||||
eventManager.addEventListener('onDesktopRootDomResize',
|
|
||||||
(width, height) => {
|
|
||||||
console.log(width, height)
|
|
||||||
notificationApi.create({
|
notificationApi.create({
|
||||||
title: '桌面通知',
|
title: '桌面通知',
|
||||||
content: `桌面尺寸变化${width}x${height}}`,
|
content: `桌面尺寸变化${monitorWidth}x${monitorHeight}}`,
|
||||||
duration: 2000,
|
duration: 2000,
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
)
|
|
||||||
|
// props.process.data.subscribe((data) => {
|
||||||
|
// console.log('desktopData', data.monitorWidth)
|
||||||
|
// })
|
||||||
|
|
||||||
|
const { appIconsRef, gridStyle, gridTemplate } = useDesktopInit('.desktop-icons-container')
|
||||||
|
|
||||||
|
// eventManager.addEventListener('onDesktopRootDomResize', (width, height) => {
|
||||||
|
// console.log(width, height)
|
||||||
|
// notificationApi.create({
|
||||||
|
// title: '桌面通知',
|
||||||
|
// content: `桌面尺寸变化${width}x${height}}`,
|
||||||
|
// duration: 2000,
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
const onContextMenu = (e: MouseEvent) => {
|
const onContextMenu = (e: MouseEvent) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
@@ -61,7 +76,7 @@ $taskBarHeight: 40px;
|
|||||||
|
|
||||||
.desktop-bg {
|
.desktop-bg {
|
||||||
@apply w-full h-full flex-1 p-2 pos-relative;
|
@apply w-full h-full flex-1 p-2 pos-relative;
|
||||||
background-image: url("imgs/desktop-bg-2.jpeg");
|
background-image: url('imgs/desktop-bg-2.jpeg');
|
||||||
background-repeat: no-repeat;
|
background-repeat: no-repeat;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
height: calc(100% - #{$taskBarHeight});
|
height: calc(100% - #{$taskBarHeight});
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { IDestroyable } from '@/core/common/types/IDestroyable.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 事件定义
|
* 事件定义
|
||||||
* @interface IEventMap 事件定义 键是事件名称,值是事件处理函数
|
* @interface IEventMap 事件定义 键是事件名称,值是事件处理函数
|
||||||
@@ -9,7 +11,7 @@ export interface IEventMap {
|
|||||||
/**
|
/**
|
||||||
* 事件管理器接口定义
|
* 事件管理器接口定义
|
||||||
*/
|
*/
|
||||||
export interface IEventBuilder<Events extends IEventMap> {
|
export interface IEventBuilder<Events extends IEventMap> extends IDestroyable {
|
||||||
/**
|
/**
|
||||||
* 添加事件监听
|
* 添加事件监听
|
||||||
* @param eventName 事件名称
|
* @param eventName 事件名称
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ interface HandlerWrapper<T extends (...args: any[]) => any> {
|
|||||||
once: boolean
|
once: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class EventBuilderImpl<Events extends IEventMap>
|
export class EventBuilderImpl<Events extends IEventMap> implements IEventBuilder<Events> {
|
||||||
implements IEventBuilder<Events>
|
|
||||||
{
|
|
||||||
private _eventHandlers = new Map<keyof Events, Set<HandlerWrapper<Events[keyof Events]>>>()
|
private _eventHandlers = new Map<keyof Events, Set<HandlerWrapper<Events[keyof Events]>>>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,9 +22,9 @@ export class EventBuilderImpl<Events extends IEventMap>
|
|||||||
eventName: E,
|
eventName: E,
|
||||||
handler: F,
|
handler: F,
|
||||||
options?: {
|
options?: {
|
||||||
immediate?: boolean;
|
immediate?: boolean
|
||||||
immediateArgs?: Parameters<F>;
|
immediateArgs?: Parameters<F>
|
||||||
once?: boolean;
|
once?: boolean
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
if (!handler) return
|
if (!handler) return
|
||||||
@@ -91,4 +89,8 @@ export class EventBuilderImpl<Events extends IEventMap>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy() {
|
||||||
|
this._eventHandlers.clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,12 @@ import type { IProcessInfo } from '@/core/process/IProcessInfo.ts'
|
|||||||
import type { IWindowForm } from '@/core/window/IWindowForm.ts'
|
import type { IWindowForm } from '@/core/window/IWindowForm.ts'
|
||||||
import type { IEventBuilder } from '@/core/events/IEventBuilder.ts'
|
import type { IEventBuilder } from '@/core/events/IEventBuilder.ts'
|
||||||
import type { IProcessEvent } from '@/core/process/types/ProcessEventTypes.ts'
|
import type { IProcessEvent } from '@/core/process/types/ProcessEventTypes.ts'
|
||||||
|
import type { IDestroyable } from '@/core/common/types/IDestroyable.ts'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进程接口
|
* 进程接口
|
||||||
*/
|
*/
|
||||||
export interface IProcess {
|
export interface IProcess extends IDestroyable {
|
||||||
/** 进程id */
|
/** 进程id */
|
||||||
get id(): string;
|
get id(): string;
|
||||||
/** 进程信息 */
|
/** 进程信息 */
|
||||||
|
|||||||
@@ -66,12 +66,18 @@ export default class ProcessImpl implements IProcess {
|
|||||||
try {
|
try {
|
||||||
const wf = this._windowForms.get(id);
|
const wf = this._windowForms.get(id);
|
||||||
if (!wf) throw new Error(`未找到窗体:${id}`);
|
if (!wf) throw new Error(`未找到窗体:${id}`);
|
||||||
|
wf.destroy();
|
||||||
this.windowForms.delete(id)
|
this.windowForms.delete(id)
|
||||||
if(this.windowForms.size === 0) {
|
if(this.windowForms.size === 0) {
|
||||||
|
this.destroy()
|
||||||
processManager.removeProcess(this)
|
processManager.removeProcess(this)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('关闭窗体失败', e)
|
console.log('关闭窗体失败', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public destroy() {
|
||||||
|
this._event.destroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -37,9 +37,10 @@ export default class ProcessManagerImpl implements IProcessManager {
|
|||||||
this._processInfos.push(...internalProcessInfos)
|
this._processInfos.push(...internalProcessInfos)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async runProcess<T extends IProcess = IProcess>(
|
public async runProcess<T extends IProcess = IProcess, A extends any[] = any[]>(
|
||||||
proc: string | IProcessInfo,
|
proc: string | IProcessInfo,
|
||||||
constructor?: new (info: IProcessInfo) => T,
|
constructor?: new (info: IProcessInfo, ...args: A) => T,
|
||||||
|
...args: A
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
let info = typeof proc === 'string' ? this.findProcessInfoByName(proc) : proc
|
let info = typeof proc === 'string' ? this.findProcessInfoByName(proc) : proc
|
||||||
if (isUndefined(info)) {
|
if (isUndefined(info)) {
|
||||||
@@ -55,7 +56,7 @@ export default class ProcessManagerImpl implements IProcessManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建进程
|
// 创建进程
|
||||||
let process = isUndefined(constructor) ? new ProcessImpl(info) : new constructor(info)
|
let process = isUndefined(constructor) ? new ProcessImpl(info) : new constructor(info, ...args)
|
||||||
|
|
||||||
return process as T
|
return process as T
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,8 +81,13 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
/** 全量订阅函数集合 */
|
/** 全量订阅函数集合 */
|
||||||
private listeners: Set<TObservableListener<T>> = new Set()
|
private listeners: Set<TObservableListener<T>> = new Set()
|
||||||
|
|
||||||
/** 字段订阅函数集合 */
|
/**
|
||||||
private keyListeners: Map<keyof T, Set<TObservableKeyListener<T, keyof T>>> = new Map()
|
* 字段订阅函数集合
|
||||||
|
* 新结构:
|
||||||
|
* Map<TObservableKeyListener, Array<keyof T>>
|
||||||
|
* 记录每个回调订阅的字段数组,保证多字段订阅 always 返回所有订阅字段值
|
||||||
|
*/
|
||||||
|
private keyListeners: Map<TObservableKeyListener<T, keyof T>, Array<keyof T>> = new Map()
|
||||||
|
|
||||||
/** 待通知的字段集合 */
|
/** 待通知的字段集合 */
|
||||||
private pendingKeys: Set<keyof T> = new Set()
|
private pendingKeys: Set<keyof T> = new Set()
|
||||||
@@ -161,7 +166,7 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
|
|
||||||
/** 安排下一次通知(微任务合并) */
|
/** 安排下一次通知(微任务合并) */
|
||||||
private scheduleNotify(): void {
|
private scheduleNotify(): void {
|
||||||
if (!this.notifyScheduled && !this.disposed) {
|
if (!this.notifyScheduled && !this.disposed && this.pendingKeys.size > 0) {
|
||||||
this.notifyScheduled = true
|
this.notifyScheduled = true
|
||||||
Promise.resolve().then(() => this.flushNotify())
|
Promise.resolve().then(() => this.flushNotify())
|
||||||
}
|
}
|
||||||
@@ -171,7 +176,6 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
private flushNotify(): void {
|
private flushNotify(): void {
|
||||||
if (this.disposed) return
|
if (this.disposed) return
|
||||||
|
|
||||||
const keys = Array.from(this.pendingKeys)
|
|
||||||
this.pendingKeys.clear()
|
this.pendingKeys.clear()
|
||||||
this.notifyScheduled = false
|
this.notifyScheduled = false
|
||||||
|
|
||||||
@@ -180,30 +184,13 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
try {
|
try {
|
||||||
fn(this.state as unknown as T)
|
fn(this.state as unknown as T)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// 可以根据需要把错误上报或自定义处理
|
|
||||||
// 这里简单打印以便调试
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error("Observable listener error:", err)
|
console.error("Observable listener error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字段订阅:把同一个回调的多个 key 聚合到一次调用里
|
// ================== 字段订阅 ==================
|
||||||
const fnMap: Map<TObservableKeyListener<T, keyof T>, Array<keyof T>> = new Map()
|
// 遍历所有回调,每个回调都返回它订阅的字段(即使只有部分字段变化)
|
||||||
for (const key of keys) {
|
this.keyListeners.forEach((subKeys, fn) => {
|
||||||
const set = this.keyListeners.get(key)
|
|
||||||
if (!set) continue
|
|
||||||
for (const fn of set) {
|
|
||||||
const existing = fnMap.get(fn)
|
|
||||||
if (existing === undefined) {
|
|
||||||
fnMap.set(fn, [key])
|
|
||||||
} else {
|
|
||||||
existing.push(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 调用每个字段订阅回调
|
|
||||||
fnMap.forEach((subKeys, fn) => {
|
|
||||||
try {
|
try {
|
||||||
// 构造 Pick<T, K> 风格的结果对象:结果类型为 Pick<T, (typeof subKeys)[number]>
|
// 构造 Pick<T, K> 风格的结果对象:结果类型为 Pick<T, (typeof subKeys)[number]>
|
||||||
const result = {} as Pick<T, (typeof subKeys)[number]>
|
const result = {} as Pick<T, (typeof subKeys)[number]>
|
||||||
@@ -214,7 +201,6 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
// 调用时类型上兼容 TObservableKeyListener<T, K>,因为我们传的是对应 key 的 Pick
|
// 调用时类型上兼容 TObservableKeyListener<T, K>,因为我们传的是对应 key 的 Pick
|
||||||
fn(result as Pick<T, (typeof subKeys)[number]>)
|
fn(result as Pick<T, (typeof subKeys)[number]>)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error("Observable keyListener error:", err)
|
console.error("Observable keyListener error:", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -227,7 +213,6 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
try {
|
try {
|
||||||
fn(this.state as unknown as T)
|
fn(this.state as unknown as T)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error("Observable subscribe immediate error:", err)
|
console.error("Observable subscribe immediate error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,19 +221,18 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 订阅指定字段变化 */
|
/** 订阅指定字段变化(多字段订阅 always 返回所有字段值) */
|
||||||
public subscribeKey<K extends keyof T>(
|
public subscribeKey<K extends keyof T>(
|
||||||
keys: K | K[],
|
keys: K | K[],
|
||||||
fn: TObservableKeyListener<T, K>,
|
fn: TObservableKeyListener<T, K>,
|
||||||
options: { immediate?: boolean } = {}
|
options: { immediate?: boolean } = {}
|
||||||
): () => void {
|
): () => void {
|
||||||
const keyArray = Array.isArray(keys) ? keys : [keys]
|
const keyArray = Array.isArray(keys) ? keys : [keys]
|
||||||
for (const key of keyArray) {
|
|
||||||
if (!this.keyListeners.has(key)) this.keyListeners.set(key, new Set())
|
|
||||||
// 存储为 Set<TObservableKeyListener<T, keyof T>>
|
|
||||||
this.keyListeners.get(key)!.add(fn as TObservableKeyListener<T, keyof T>)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ================== 存储回调和它订阅的字段数组 ==================
|
||||||
|
this.keyListeners.set(fn as TObservableKeyListener<T, keyof T>, keyArray as (keyof T)[])
|
||||||
|
|
||||||
|
// ================== 立即调用 ==================
|
||||||
if (options.immediate) {
|
if (options.immediate) {
|
||||||
const result = {} as Pick<T, K>
|
const result = {} as Pick<T, K>
|
||||||
keyArray.forEach(k => {
|
keyArray.forEach(k => {
|
||||||
@@ -257,30 +241,26 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
try {
|
try {
|
||||||
fn(result)
|
fn(result)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.error("Observable subscribeKey immediate error:", err)
|
console.error("Observable subscribeKey immediate error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================== 返回取消订阅函数 ==================
|
||||||
return () => {
|
return () => {
|
||||||
for (const key of keyArray) {
|
this.keyListeners.delete(fn as TObservableKeyListener<T, keyof T>)
|
||||||
this.keyListeners.get(key)?.delete(fn as TObservableKeyListener<T, keyof T>)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 批量更新状态(避免重复 schedule) */
|
/** 批量更新状态(避免重复 schedule) */
|
||||||
public patch(values: Partial<T>): void {
|
public patch(values: Partial<T>): void {
|
||||||
let changed = false
|
let changed = false
|
||||||
// 用 for..in 保持和对象字面量兼容(跳过原型链)
|
|
||||||
for (const key in values) {
|
for (const key in values) {
|
||||||
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
if (Object.prototype.hasOwnProperty.call(values, key)) {
|
||||||
const typedKey = key as keyof T
|
const typedKey = key as keyof T
|
||||||
const oldValue = (this.state as Record<keyof T, unknown>)[typedKey]
|
const oldValue = (this.state as Record<keyof T, unknown>)[typedKey]
|
||||||
const newValue = values[typedKey] as unknown
|
const newValue = values[typedKey] as unknown
|
||||||
if (oldValue !== newValue) {
|
if (oldValue !== newValue) {
|
||||||
// 直接写入 state,会被 Proxy 的 set 捕获并安排通知
|
(this.state as Record<keyof T, unknown>)[typedKey] = newValue
|
||||||
;(this.state as Record<keyof T, unknown>)[typedKey] = newValue
|
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -322,3 +302,4 @@ export class ObservableImpl<T extends TNonFunctionProperties<T>> implements IObs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { ObservableImpl } from '@/core/state/impl/ObservableImpl.ts'
|
||||||
|
|
||||||
|
interface IGlobalStoreParams {
|
||||||
|
/** 桌面根dom ID,类似显示器 */
|
||||||
|
monitorDomId: string;
|
||||||
|
monitorWidth: number;
|
||||||
|
monitorHeight: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const globalStore = new ObservableImpl<IGlobalStoreParams>({
|
||||||
|
monitorDomId: '#app',
|
||||||
|
monitorWidth: 0,
|
||||||
|
monitorHeight: 0
|
||||||
|
})
|
||||||
|
|||||||
@@ -734,6 +734,7 @@ export class DraggableResizableWindow {
|
|||||||
* 销毁实例
|
* 销毁实例
|
||||||
*/
|
*/
|
||||||
public destroy() {
|
public destroy() {
|
||||||
|
try {
|
||||||
if (this.handle) this.handle.removeEventListener('mousedown', this.onMouseDownDrag);
|
if (this.handle) this.handle.removeEventListener('mousedown', this.onMouseDownDrag);
|
||||||
this.target.removeEventListener('mousedown', this.onMouseDownResize);
|
this.target.removeEventListener('mousedown', this.onMouseDownResize);
|
||||||
document.removeEventListener('mousemove', this.onMouseMoveDragRAF);
|
document.removeEventListener('mousemove', this.onMouseMoveDragRAF);
|
||||||
@@ -746,5 +747,6 @@ export class DraggableResizableWindow {
|
|||||||
this.resizeObserver?.disconnect();
|
this.resizeObserver?.disconnect();
|
||||||
this.mutationObserver.disconnect();
|
this.mutationObserver.disconnect();
|
||||||
cancelAnimationFrame(this.animationFrame ?? 0);
|
cancelAnimationFrame(this.animationFrame ?? 0);
|
||||||
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import type { IProcess } from '@/core/process/IProcess.ts'
|
import type { IProcess } from '@/core/process/IProcess.ts'
|
||||||
import type { TWindowFormState } from '@/core/window/types/WindowFormTypes.ts'
|
import type { TWindowFormState } from '@/core/window/types/WindowFormTypes.ts'
|
||||||
|
import type { IDestroyable } from '@/core/common/types/IDestroyable.ts'
|
||||||
|
|
||||||
export interface IWindowForm {
|
export interface IWindowForm extends IDestroyable {
|
||||||
/** 窗体id */
|
/** 窗体id */
|
||||||
get id(): string;
|
get id(): string;
|
||||||
/** 窗体所属的进程 */
|
/** 窗体所属的进程 */
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ export interface IWindowFormDataState {
|
|||||||
|
|
||||||
export default class WindowFormImpl implements IWindowForm {
|
export default class WindowFormImpl implements IWindowForm {
|
||||||
private readonly _id: string = uuidV4()
|
private readonly _id: string = uuidV4()
|
||||||
private readonly _proc: IProcess;
|
private readonly _proc: IProcess
|
||||||
|
private readonly _data: IObservable<IWindowFormDataState>
|
||||||
private dom: HTMLElement
|
private dom: HTMLElement
|
||||||
private drw: DraggableResizableWindow
|
private drw: DraggableResizableWindow
|
||||||
private _data: IObservable<IWindowFormDataState>;
|
|
||||||
|
|
||||||
public get id() {
|
public get id() {
|
||||||
return this._id
|
return this._id
|
||||||
@@ -70,7 +70,7 @@ export default class WindowFormImpl implements IWindowForm {
|
|||||||
width: config.width ?? 200,
|
width: config.width ?? 200,
|
||||||
height: config.height ?? 100,
|
height: config.height ?? 100,
|
||||||
state: 'default',
|
state: 'default',
|
||||||
closed: false
|
closed: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.initEvent()
|
this.initEvent()
|
||||||
@@ -109,4 +109,6 @@ export default class WindowFormImpl implements IWindowForm {
|
|||||||
public minimize() {}
|
public minimize() {}
|
||||||
public maximize() {}
|
public maximize() {}
|
||||||
public restore() {}
|
public restore() {}
|
||||||
|
|
||||||
|
public destroy() {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user