This commit is contained in:
2025-10-18 19:12:18 +08:00
parent e54bd0a447
commit 7b12efd09c
22 changed files with 119 additions and 2192 deletions

View File

@@ -1,9 +1,11 @@
import { reactive } from 'vue'
import { reactive, ref } from 'vue'
import type { ResourceService } from './ResourceService'
import type { ApplicationSandboxEngine } from './ApplicationSandboxEngine'
import { v4 as uuidv4 } from 'uuid'
import { externalAppDiscovery } from './ExternalAppDiscovery'
import type { WindowFormService } from '@/services/WindowFormService.ts'
import { type AppRegistration, appRegistry } from '@/apps'
import windowManager from '@/ui/components/WindowManager.vue'
/**
* 应用状态枚举
@@ -110,13 +112,20 @@ export interface AppLifecycleEvents {
onStateChanged: (appId: string, newState: AppLifecycleState, oldState: AppLifecycleState) => void
}
interface BuiltInWindowForm {
id: string
appId: string
component: any
props: Record<string, any>
}
/**
* 应用生命周期管理器
*/
export class ApplicationLifecycleManager {
private installedApps = reactive(new Map<string, AppInstance>())
private runningProcesses = reactive(new Map<string, AppInstance>())
private appFiles = new Map<string, Map<string, Blob | string>>() // 应用文件存储
private builtInWindowForms = reactive(new Map<string, BuiltInWindowForm>())
private windowFormService: WindowFormService
private resourceService: ResourceService
@@ -132,10 +141,65 @@ export class ApplicationLifecycleManager {
this.sandboxEngine = sandboxEngine
}
public getBuiltInWindowForms() {
return this.builtInWindowForms
}
/**
* 启动应用
*/
async startApp(appId: string, options: AppStartOptions = {}): Promise<string> {
async startApp(appId: string, options: AppStartOptions = {}) {
// 检查应用是否是内置应用
if (appRegistry.hasApp(appId)) {
await this.startBuiltInApp(appId, options)
} else {
await this.startExternalApp(appId, options)
}
}
private async startBuiltInApp(appId: string, options: AppStartOptions = {}) {
const appRegistration = appRegistry.getApp(appId)
if (!appRegistration) {
console.error(`内置应用 ${appId} 不存在`)
return
}
// 检查是否已在运行
if (this.isAppRunning(appId)) {
console.log(`应用 ${appRegistration.manifest.name} 已在运行`)
return
}
const windowInstance = await this.windowFormService.createWindow(appId, appRegistration.manifest.window)
// 处理异步组件加载
let component = appRegistration.component
if (typeof component === 'function') {
try {
// 如果是函数,调用它来获取组件
component = await component()
} catch (error) {
console.error(`加载应用组件失败: ${appId}`, error)
return
}
}
// 添加窗口
const windowForm: BuiltInWindowForm = {
id: windowInstance.id,
appId,
component,
props: {
windowId: windowInstance.id,
appId
}
}
this.builtInWindowForms.set(windowInstance.id, windowForm)
console.log(this.builtInWindowForms)
}
private async startExternalApp(appId: string, options: AppStartOptions = {}) {
let app = this.installedApps.get(appId)
console.log('-----------------------------')
@@ -344,20 +408,15 @@ export class ApplicationLifecycleManager {
* 检查应用是否正在运行
*/
isAppRunning(appId: string): boolean {
// 首先从已安装应用中查找
let app = this.installedApps.get(appId)
let app;
// 如果未找到,从运行进程列表中查找(可能是外部应用的临时实例)
if (!app) {
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
}
return app?.state === AppLifecycleState.RUNNING || app?.state === AppLifecycleState.SUSPENDED
return app?.state === AppLifecycleState.RUNNING
}
/**