@binblink/wasmnest-plugin-sdk-mobile 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +363 -2
  2. package/README_en.md +353 -0
  3. package/bin/build.js +269 -0
  4. package/bin/cli.js +96 -0
  5. package/bin/lib.js +349 -0
  6. package/bin/manifest-gen.js +324 -0
  7. package/bin/package.js +150 -0
  8. package/bin/scaffold.js +285 -0
  9. package/dev-shell/README.md +151 -0
  10. package/dev-shell/index.html +27 -0
  11. package/dev-shell/package.json +27 -0
  12. package/dev-shell/pnpm-workspace.yaml +9 -0
  13. package/dev-shell/postcss.config.js +6 -0
  14. package/dev-shell/src/App.vue +90 -0
  15. package/dev-shell/src/apps/mock-terminal/MockTerminalSlotCard.vue +51 -0
  16. package/dev-shell/src/apps/mock-terminal/MockTerminalView.vue +168 -0
  17. package/dev-shell/src/apps/mock-terminal/index.ts +50 -0
  18. package/dev-shell/src/components/DevToolbar.vue +100 -0
  19. package/dev-shell/src/components/DialogHost.vue +119 -0
  20. package/dev-shell/src/components/LogPanel.vue +80 -0
  21. package/dev-shell/src/i18n.ts +22 -0
  22. package/dev-shell/src/loader.ts +239 -0
  23. package/dev-shell/src/locale.ts +29 -0
  24. package/dev-shell/src/locales/en-shell.ts +328 -0
  25. package/dev-shell/src/locales/index.ts +70 -0
  26. package/dev-shell/src/locales/shell.ts +328 -0
  27. package/dev-shell/src/main.ts +58 -0
  28. package/dev-shell/src/mock/dialog-service.ts +58 -0
  29. package/dev-shell/src/mock/file-transfer.ts +390 -0
  30. package/dev-shell/src/mock/mobile-api.ts +369 -0
  31. package/dev-shell/src/mock/preset-tasks.ts +186 -0
  32. package/dev-shell/src/mock/session.ts +121 -0
  33. package/dev-shell/src/mock-context.ts +351 -0
  34. package/dev-shell/src/registry.ts +151 -0
  35. package/dev-shell/src/shared-runtime.ts +44 -0
  36. package/dev-shell/src/shell/ShellView.vue +52 -0
  37. package/dev-shell/src/shell/adapters/devAppSource.ts +155 -0
  38. package/dev-shell/src/shell/components/PluginContextHost.vue +40 -0
  39. package/dev-shell/src/shell/components/ShellAppCell.vue +75 -0
  40. package/dev-shell/src/shell/components/ShellAppIcon.vue +111 -0
  41. package/dev-shell/src/shell/components/ShellAppSurface.vue +46 -0
  42. package/dev-shell/src/shell/components/ShellBrandTile.vue +27 -0
  43. package/dev-shell/src/shell/components/ShellCapsuleMenu.vue +123 -0
  44. package/dev-shell/src/shell/components/ShellChip.vue +49 -0
  45. package/dev-shell/src/shell/components/ShellGroupCard.vue +17 -0
  46. package/dev-shell/src/shell/components/ShellHost.vue +75 -0
  47. package/dev-shell/src/shell/components/ShellNotificationShade.vue +496 -0
  48. package/dev-shell/src/shell/components/ShellPermissionPrompt.vue +127 -0
  49. package/dev-shell/src/shell/components/ShellPlaceholderSurface.vue +33 -0
  50. package/dev-shell/src/shell/components/ShellSection.vue +27 -0
  51. package/dev-shell/src/shell/components/ShellSheet.vue +74 -0
  52. package/dev-shell/src/shell/components/ShellStatusbar.vue +84 -0
  53. package/dev-shell/src/shell/components/screens/PluginRoutePage.vue +98 -0
  54. package/dev-shell/src/shell/components/screens/ShellAppDetailScreen.vue +378 -0
  55. package/dev-shell/src/shell/components/screens/ShellAppRunScreen.vue +90 -0
  56. package/dev-shell/src/shell/components/screens/ShellAppsScreen.vue +195 -0
  57. package/dev-shell/src/shell/components/screens/ShellHomeScreen.vue +316 -0
  58. package/dev-shell/src/shell/components/screens/ShellPermissionsScreen.vue +376 -0
  59. package/dev-shell/src/shell/components/screens/ShellSettingsScreen.vue +240 -0
  60. package/dev-shell/src/shell/components/screens/ShellSwitcherScreen.vue +559 -0
  61. package/dev-shell/src/shell/components/ui/ConfirmDialog.vue +137 -0
  62. package/dev-shell/src/shell/components/ui/Toggle.vue +53 -0
  63. package/dev-shell/src/shell/composables/useShellApps.ts +415 -0
  64. package/dev-shell/src/shell/composables/useShellNavigation.ts +197 -0
  65. package/dev-shell/src/shell/composables/useShellNotifications.ts +115 -0
  66. package/dev-shell/src/shell/composables/useShellOpen.ts +35 -0
  67. package/dev-shell/src/shell/composables/useShellOverlays.ts +101 -0
  68. package/dev-shell/src/shell/composables/useShellPreferences.ts +143 -0
  69. package/dev-shell/src/shell/composables/useShellRecent.ts +84 -0
  70. package/dev-shell/src/shell/composables/useToast.ts +33 -0
  71. package/dev-shell/src/shell/index.ts +88 -0
  72. package/dev-shell/src/shell/logger.ts +45 -0
  73. package/dev-shell/src/shell/permissions.ts +145 -0
  74. package/dev-shell/src/shell/registry.ts +220 -0
  75. package/dev-shell/src/shell/styles/shell.css +841 -0
  76. package/dev-shell/src/shell/types.ts +137 -0
  77. package/dev-shell/src/shell/utils.ts +54 -0
  78. package/dev-shell/src/styles/main.css +153 -0
  79. package/dev-shell/src/styles/mobile.css +884 -0
  80. package/dev-shell/src/styles/wallpaper.css +130 -0
  81. package/dev-shell/src/theme.ts +71 -0
  82. package/dev-shell/src/vite-env.d.ts +20 -0
  83. package/dev-shell/tailwind.config.js +101 -0
  84. package/dev-shell/tsconfig.json +16 -0
  85. package/dev-shell/vite.config.ts +130 -0
  86. package/dist/index.d.ts +84 -0
  87. package/dist/index.js +161 -0
  88. package/dist/types.d.ts +637 -0
  89. package/dist/types.js +0 -0
  90. package/dist/vite-plugin.d.ts +16 -0
  91. package/dist/vite-plugin.js +95 -0
  92. package/package.json +80 -3
  93. package/src/constants.ts +6 -0
  94. package/src/global-dialog.ts +168 -0
  95. package/src/index.ts +87 -0
  96. package/src/runtime.ts +44 -0
  97. package/src/types.ts +715 -0
  98. package/src/ui/PluginGlobalDialog.vue +388 -0
  99. package/src/ui/Select.d.ts +37 -0
  100. package/src/ui/Select.vue +307 -0
  101. package/src/ui/plugin-global-dialog.d.ts +11 -0
  102. package/src/ui/useSwipeTabs.d.ts +20 -0
  103. package/src/ui/useSwipeTabs.ts +53 -0
  104. package/src/vite-plugin.ts +132 -0
  105. package/template/.gitignore +4 -0
  106. package/template/README.md +46 -0
  107. package/template/package.json +23 -0
  108. package/template/plugin.json +13 -0
  109. package/template/rust/Cargo.toml +19 -0
  110. package/template/rust/src/lib.rs +33 -0
  111. package/template/src/env.d.ts +24 -0
  112. package/template/src/index.ts +37 -0
  113. package/template/tsconfig.json +17 -0
  114. package/template/vite.config.ts +21 -0
@@ -0,0 +1,137 @@
1
+ <template>
2
+ <Teleport to="body">
3
+ <Transition name="center-modal">
4
+ <div
5
+ v-if="modelValue"
6
+ class="fixed inset-0 z-50 flex items-center justify-center mobile-ui"
7
+ >
8
+ <!-- Backdrop -->
9
+ <div
10
+ class="absolute inset-0 bg-[var(--mobile-overlay)] backdrop-blur-sm"
11
+ @click="handleBackdropClick"
12
+ ></div>
13
+
14
+ <!-- Panel -->
15
+ <div class="relative w-full max-w-sm mx-4 mb-[var(--safe-area-bottom,0px)] bg-[var(--mobile-bg-card)] border border-[var(--mobile-border)] rounded-2xl overflow-hidden shadow-xl modal-panel">
16
+ <!-- Header -->
17
+ <div class="px-6 pt-6 pb-2">
18
+ <div class="flex items-center gap-3 mb-2">
19
+ <!-- Icon -->
20
+ <div
21
+ :class="[
22
+ 'w-10 h-10 rounded-xl flex items-center justify-center flex-shrink-0',
23
+ variant === 'danger' ? 'bg-[var(--mobile-danger-bg)]' :
24
+ variant === 'warning' ? 'bg-[var(--mobile-warning-muted)]' :
25
+ 'bg-[var(--mobile-accent-muted)]'
26
+ ]"
27
+ >
28
+ <svg
29
+ :class="[
30
+ 'w-5 h-5',
31
+ variant === 'danger' ? 'text-[var(--mobile-danger-color)]' :
32
+ variant === 'warning' ? 'text-[var(--mobile-warning)]' :
33
+ 'text-[var(--mobile-accent)]'
34
+ ]"
35
+ fill="none" stroke="currentColor" viewBox="0 0 24 24"
36
+ >
37
+ <path v-if="variant === 'danger'" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z" />
38
+ <path v-else-if="variant === 'warning'" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
39
+ <path v-else stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
40
+ </svg>
41
+ </div>
42
+ <h3 class="text-lg font-semibold text-[var(--mobile-text-primary)]">{{ title }}</h3>
43
+ </div>
44
+ <p class="text-[var(--mobile-text-secondary)] text-sm leading-relaxed">{{ message }}</p>
45
+ </div>
46
+
47
+ <!-- Actions -->
48
+ <div class="flex gap-3 px-6 py-5">
49
+ <button
50
+ class="flex-1 bg-[var(--mobile-input-bg)] text-[var(--mobile-text-secondary)] rounded-xl font-medium active:opacity-80 transition-colors duration-200 confirm-btn-height"
51
+ :disabled="loading"
52
+ @click="handleCancel"
53
+ >
54
+ {{ cancelText }}
55
+ </button>
56
+ <button
57
+ :class="[
58
+ 'flex-1 rounded-xl font-medium active:opacity-80 transition-colors duration-200 confirm-btn-height',
59
+ loading ? 'opacity-50 pointer-events-none' : '',
60
+ variant === 'danger'
61
+ ? 'bg-[var(--mobile-danger-solid-bg)] text-[var(--mobile-text-on-accent)]'
62
+ : 'bg-[var(--mobile-accent)] text-[var(--mobile-text-on-accent)]'
63
+ ]"
64
+ :disabled="loading"
65
+ @click="handleConfirm"
66
+ >
67
+ <span v-if="loading" class="flex items-center justify-center gap-2">
68
+ <span class="w-4 h-4 border-2 border-[color:color-mix(in_srgb,var(--mobile-text-on-accent)_30%,transparent)] border-t-[var(--mobile-text-on-accent)] rounded-full animate-spin"></span>
69
+ {{ confirmText }}
70
+ </span>
71
+ <span v-else>{{ confirmText }}</span>
72
+ </button>
73
+ </div>
74
+ </div>
75
+ </div>
76
+ </Transition>
77
+ </Teleport>
78
+ </template>
79
+
80
+ <script setup lang="ts">
81
+ /**
82
+ * ConfirmDialog — 移动端确认弹窗
83
+ *
84
+ * 迁移自旧公共组件 `src/components/ConfirmDialog.vue`(壳内自足化):支持
85
+ * danger / warning / info 变体与 loading 态,Teleport + z-50 遵循 safe-stack。
86
+ *
87
+ * 与旧实现的一处差异:loading 转圈的 `border-white/30 border-t-white` 改为
88
+ * `--mobile-text-on-accent` 的 color-mix 派生——白/黑是 Tailwind 调色板值,
89
+ * 按 frontend-styles「token-bound」纪律不应出现在壳内(背景色随主题反转时
90
+ * 白边会失去对比)。
91
+ */
92
+ const props = withDefaults(defineProps<{
93
+ modelValue: boolean
94
+ title: string
95
+ message: string
96
+ variant?: 'info' | 'warning' | 'danger'
97
+ confirmText?: string
98
+ cancelText?: string
99
+ loading?: boolean
100
+ closeOnBackdrop?: boolean
101
+ }>(), {
102
+ variant: 'info',
103
+ confirmText: '',
104
+ cancelText: '',
105
+ loading: false,
106
+ closeOnBackdrop: true,
107
+ })
108
+
109
+ const emit = defineEmits<{
110
+ 'update:modelValue': [value: boolean]
111
+ confirm: []
112
+ cancel: []
113
+ }>()
114
+
115
+ function handleConfirm() {
116
+ if (!props.loading) emit('confirm')
117
+ }
118
+
119
+ function handleCancel() {
120
+ if (!props.loading) {
121
+ emit('cancel')
122
+ emit('update:modelValue', false)
123
+ }
124
+ }
125
+
126
+ function handleBackdropClick() {
127
+ if (props.closeOnBackdrop && !props.loading) {
128
+ emit('update:modelValue', false)
129
+ }
130
+ }
131
+ </script>
132
+
133
+ <style scoped>
134
+ .confirm-btn-height {
135
+ height: clamp(2.5rem, 2.75rem, 3rem);
136
+ }
137
+ </style>
@@ -0,0 +1,53 @@
1
+ <template>
2
+ <label class="flex items-center gap-3 cursor-pointer">
3
+ <div class="relative">
4
+ <input
5
+ type="checkbox"
6
+ :checked="modelValue"
7
+ :disabled="disabled"
8
+ class="sr-only"
9
+ @change="$emit('update:modelValue', !modelValue)"
10
+ />
11
+ <!-- 开关样式与桌面端 SettingsView 对齐:方形墨色轨道 + 方形滑块 -->
12
+ <div
13
+ class="w-10 h-5 rounded-[4px] border transition-colors"
14
+ :class="[
15
+ modelValue
16
+ ? 'bg-[var(--mobile-accent)] border-[var(--mobile-accent)]'
17
+ : 'bg-[var(--mobile-bg-primary)] border-[var(--mobile-border-hover)]',
18
+ { 'opacity-50 cursor-not-allowed': disabled }
19
+ ]"
20
+ ></div>
21
+ <div
22
+ class="absolute top-[3px] w-3 h-3 rounded-[2px] transition-all"
23
+ :class="[
24
+ modelValue
25
+ ? 'left-[22px] bg-[var(--mobile-text-on-accent)]'
26
+ : 'left-[3px] bg-[var(--mobile-bg-tertiary)]'
27
+ ]"
28
+ ></div>
29
+ </div>
30
+ <span v-if="label" class="text-sm text-[var(--mobile-text-secondary)]">{{ label }}</span>
31
+ </label>
32
+ </template>
33
+
34
+ <script setup lang="ts">
35
+ /**
36
+ * Toggle — 开关(方形墨色轨道 + 方形滑块)
37
+ *
38
+ * 迁移自旧公共组件 `src/components/Toggle.vue`(壳内自足化),契约与旧实现
39
+ * 逐字一致;壳内消费方(应用详情权限开关)与将来迁移的旧页面共用同一形状。
40
+ */
41
+ interface Props {
42
+ modelValue: boolean
43
+ label?: string
44
+ disabled?: boolean
45
+ }
46
+
47
+ withDefaults(defineProps<Props>(), {
48
+ label: '',
49
+ disabled: false,
50
+ })
51
+
52
+ defineEmits(['update:modelValue'])
53
+ </script>
@@ -0,0 +1,415 @@
1
+ /**
2
+ * Dev Shell 应用视图模型(与宿主 shell/composables/useShellApps.ts 同构)
3
+ * -----------------------------------------------------------------------------
4
+ * 把 ShellRegistry 里的存储模型投影成 UI 需要的形状(排序、统计、派生列表),
5
+ * 并把「启动 / 停止 / 改权限」这些动作收口到一处——组件只调动作、不直接碰数据源。
6
+ *
7
+ * 这样做的收益:数据源从插件系统换成 WASM 应用平台时,组件一行都不用改;
8
+ * 动作的成功/失败口径也只有一份实现(状态回写 + 日志 + 错误暴露)。
9
+ *
10
+ * 停用 / 卸载成功后还会摘掉该应用的运行屏(closeAppRun):运行面由应用注册,
11
+ * 应用一没注册就失效,运行屏留在栈里只会显示空态。收口在这一处而不是分散到各
12
+ * 屏的按钮,是为了「退出即离开运行面」这条不变量只有一个落点。
13
+ */
14
+
15
+ import { computed, ref, type Component, type ComputedRef, type Ref } from 'vue'
16
+ import { i18n } from '../../i18n'
17
+ import { logger } from '../logger'
18
+ import { getShellRegistry } from '../registry'
19
+ import type { ShellApp, ShellAppSource } from '../types'
20
+ import { pushRecent } from './useShellRecent'
21
+ import { useShellNavigation } from './useShellNavigation'
22
+ import { useShellNotifications } from './useShellNotifications'
23
+
24
+ /** 生命周期通知图标(24×24 stroke 视框,与壳内其它 SVG 同规范) */
25
+ const APP_EVENT_ICONS = {
26
+ started: 'M6 4l14 8-14 8z',
27
+ stopped: 'M6 6h12v12H6z',
28
+ approved: 'M20 6L9 17l-5-5',
29
+ installed: 'M12 3v12 M7 10l5 5 5-5 M4 21h16',
30
+ uninstalled: 'M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16',
31
+ failed: 'M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z M12 9v4 M12 17h.01',
32
+ } as const
33
+
34
+ /**
35
+ * 推送一条应用生命周期通知
36
+ *
37
+ * 归属(AGENTS §5.1 B6):壳只对**自身动作的结果**落通知(启动/停止/审批/安装/卸载),
38
+ * 不解释插件业务事件;文案是平台自己的,应用名是注册表事实。
39
+ */
40
+ function notifyAppEvent(
41
+ kind: keyof typeof APP_EVENT_ICONS,
42
+ app: ShellApp,
43
+ extra?: { body?: string; sticky?: boolean },
44
+ ): void {
45
+ const t = i18n.global.t
46
+ useShellNotifications().pushNotification({
47
+ kind: 'event',
48
+ icon: APP_EVENT_ICONS[kind],
49
+ source: t('shell.notify.source.shell'),
50
+ title: t(`shell.notify.app.${kind}.title`, { name: app.name }),
51
+ body: extra?.body ?? t(`shell.notify.app.${kind}.body`),
52
+ appId: app.id,
53
+ sticky: extra?.sticky ?? false,
54
+ })
55
+ }
56
+
57
+ /** 平台统计(应用管理页顶部) */
58
+ export interface ShellStats {
59
+ /** 已安装应用数 */
60
+ installed: number
61
+ /** 运行中应用数 */
62
+ running: number
63
+ /** 应用数据总占用(字节);部分应用未统计时为 undefined,UI 显示「—」而非伪造 0 */
64
+ totalBytes: number | undefined
65
+ }
66
+
67
+ const registry = getShellRegistry()
68
+
69
+ /** 是否正在拉取清单 */
70
+ const loading: Ref<boolean> = ref(false)
71
+ /** 最近一次动作的错误原因(供 UI 展示;成功清空) */
72
+ const lastError: Ref<string | null> = ref(null)
73
+
74
+ /** 应用清单(保持注册/写入顺序) */
75
+ const apps: ComputedRef<ShellApp[]> = computed(() => registry.appsRef.value)
76
+
77
+ /** 展示排序:运行中优先,其次按名称(中文用 localeCompare,避免拼音序错乱) */
78
+ const sortedApps: ComputedRef<ShellApp[]> = computed(() =>
79
+ [...apps.value].sort((a, b) => {
80
+ const aRunning = a.state === 'running' ? 0 : 1
81
+ const bRunning = b.state === 'running' ? 0 : 1
82
+ if (aRunning !== bRunning) return aRunning - bRunning
83
+ return a.name.localeCompare(b.name, 'zh-Hans-CN')
84
+ }),
85
+ )
86
+
87
+ /** 运行中应用(多任务页消费) */
88
+ const runningApps: ComputedRef<ShellApp[]> = computed(() =>
89
+ apps.value.filter((a) => a.state === 'running'),
90
+ )
91
+
92
+ /** 平台统计 */
93
+ const stats: ComputedRef<ShellStats> = computed(() => {
94
+ const measured = apps.value.every((a) => typeof a.sizeBytes === 'number')
95
+ return {
96
+ installed: apps.value.length,
97
+ running: apps.value.filter((a) => a.state === 'running').length,
98
+ totalBytes: measured
99
+ ? apps.value.reduce((sum, a) => sum + (a.sizeBytes ?? 0), 0)
100
+ : undefined,
101
+ }
102
+ })
103
+
104
+ /** 按 id 取应用(含合并后的贡献) */
105
+ function getApp(appId: string): ShellApp | undefined {
106
+ return registry.getApp(appId)
107
+ }
108
+
109
+ /** 从所有数据源重拉应用清单 */
110
+ async function refresh(): Promise<void> {
111
+ loading.value = true
112
+ try {
113
+ const sources = registry.listSources()
114
+ if (sources.length === 0) {
115
+ logger.warn('[Shell] no app source registered; app list stays empty')
116
+ return
117
+ }
118
+ // 各数据源独立失败不影响其它源:一个源挂掉不该让整个首页空白
119
+ await Promise.all(
120
+ sources.map(async (source) => {
121
+ try {
122
+ const list = await source.list()
123
+ registry.upsertApps(source.id, list)
124
+ } catch (e) {
125
+ logger.error(`[Shell] app source list failed: source=${source.id}`, e)
126
+ lastError.value = e instanceof Error ? e.message : String(e)
127
+ }
128
+ }),
129
+ )
130
+ } finally {
131
+ loading.value = false
132
+ }
133
+ }
134
+
135
+ /** 启动应用(幂等:已运行只记一次最近使用) */
136
+ async function launch(appId: string): Promise<boolean> {
137
+ const app = registry.getApp(appId)
138
+ const source = registry.getSourceOf(appId)
139
+ if (!app || !source) {
140
+ logger.error(`[Shell] launch failed: unknown app appId=${appId}`)
141
+ lastError.value = `unknown app: ${appId}`
142
+ return false
143
+ }
144
+ // 不再对「已运行」早退(2026-10-14 修正):dev-shell 里「重开已运行应用」等同
145
+ // 重跑一遍激活与前端状态(didUpdate 语义),早退会让用户点了没反应;数据源的
146
+ // launch 本身幂等,重跑一遍的代价远小于缺状态。
147
+
148
+ // 乐观置为运行中:启动是秒级操作,先动 UI 避免点击后无反馈
149
+ // 通知只在「本就没在跑」时落:重复点已运行的应用(壳允许,用于重挂前端模块)
150
+ // 不该每次都在通知栏叠一条「已启动」
151
+ const wasRunning = app.state === 'running'
152
+ registry.patchApp(appId, { state: 'running', error: undefined })
153
+ try {
154
+ await source.launch(appId)
155
+ pushRecent(appId)
156
+ lastError.value = null
157
+ await refresh()
158
+ if (!wasRunning) notifyAppEvent('started', app)
159
+ return true
160
+ } catch (e) {
161
+ const message = e instanceof Error ? e.message : String(e)
162
+ logger.error(`[Shell] launch failed: appId=${appId}`, e)
163
+ registry.patchApp(appId, { state: 'error', error: message })
164
+ lastError.value = message
165
+ // 失败原因来自数据源,属于「来源方给出的正文」,壳原样呈现不加工
166
+ notifyAppEvent('failed', app, { body: message })
167
+ return false
168
+ }
169
+ }
170
+
171
+ /** 停止应用(保留数据,回收内存与后台任务) */
172
+ async function stop(appId: string): Promise<boolean> {
173
+ const app = registry.getApp(appId)
174
+ const source = registry.getSourceOf(appId)
175
+ if (!app || !source) {
176
+ logger.error(`[Shell] stop failed: unknown app appId=${appId}`)
177
+ return false
178
+ }
179
+
180
+ registry.patchApp(appId, { state: 'stopped', error: undefined })
181
+ try {
182
+ await source.stop(appId)
183
+ lastError.value = null
184
+ await refresh()
185
+ notifyAppEvent('stopped', app)
186
+ // 停用即退出:运行面注册随前端模块一起被 dispose,运行屏留在栈里就只剩
187
+ // 「应用暂无可用界面」空态。这里摘掉该应用的运行屏,退出动作才有落点;
188
+ // 失败时不动栈(运行面多半还在,留用户看错误态)
189
+ useShellNavigation().closeAppRun(appId)
190
+ return true
191
+ } catch (e) {
192
+ const message = e instanceof Error ? e.message : String(e)
193
+ logger.error(`[Shell] stop failed: appId=${appId}`, e)
194
+ registry.patchApp(appId, { state: 'error', error: message })
195
+ lastError.value = message
196
+ return false
197
+ }
198
+ }
199
+
200
+ /**
201
+ * 批准应用的权限申请(整单:全部声明权限 + 内容哈希钉扎)
202
+ *
203
+ * 与「逐项授权」不同:这是后端审批链的入口(`plugin_approve`),批准后应用才可启动。
204
+ * 数据源不支持审批时返回 false,UI 据此不渲染入口(能力诚实原则)。
205
+ */
206
+ async function approveApp(appId: string): Promise<boolean> {
207
+ const app = registry.getApp(appId)
208
+ const source = registry.getSourceOf(appId)
209
+ if (!app || !source?.approve) {
210
+ logger.warn(`[Shell] approve unsupported: appId=${appId}`)
211
+ return false
212
+ }
213
+ try {
214
+ await source.approve(appId)
215
+ lastError.value = null
216
+ await refresh()
217
+ notifyAppEvent('approved', app)
218
+ return true
219
+ } catch (e) {
220
+ const message = e instanceof Error ? e.message : String(e)
221
+ logger.error(`[Shell] approve failed: appId=${appId}`, e)
222
+ lastError.value = message
223
+ return false
224
+ }
225
+ }
226
+
227
+ /** 该应用是否可批准(取决于数据源能力,不是 UI 拍脑袋) */
228
+ function supportsApprove(appId: string): boolean {
229
+ return typeof registry.getSourceOf(appId)?.approve === 'function'
230
+ }
231
+
232
+ /**
233
+ * 逐项变更权限授予
234
+ *
235
+ * @returns true 已生效;false 数据源不支持或调用失败——UI 据此保留原状并提示,
236
+ * 不做「点了就算通过」的假成功。
237
+ */
238
+ async function setPermissionGrant(
239
+ appId: string,
240
+ permissionKey: string,
241
+ granted: boolean,
242
+ ): Promise<boolean> {
243
+ const source = registry.getSourceOf(appId)
244
+ if (!source?.setPermissionGrant) {
245
+ logger.warn(
246
+ `[Shell] permission control unsupported: appId=${appId} permission=${permissionKey}`,
247
+ )
248
+ return false
249
+ }
250
+
251
+ const app = registry.getApp(appId)
252
+ if (!app) return false
253
+
254
+ try {
255
+ const ok = await source.setPermissionGrant(appId, permissionKey, granted)
256
+ if (!ok) return false
257
+ registry.patchApp(appId, {
258
+ permissions: app.permissions.map((p) =>
259
+ p.key === permissionKey ? { ...p, granted } : p,
260
+ ),
261
+ })
262
+ return true
263
+ } catch (e) {
264
+ logger.error(
265
+ `[Shell] set permission failed: appId=${appId} permission=${permissionKey}`,
266
+ e,
267
+ )
268
+ return false
269
+ }
270
+ }
271
+
272
+ /** 该应用的权限是否可逐项控制(取决于数据源能力,不是 UI 拍脑袋) */
273
+ function supportsPermissionControl(appId: string): boolean {
274
+ return typeof registry.getSourceOf(appId)?.setPermissionGrant === 'function'
275
+ }
276
+
277
+ /**
278
+ * 解析应用运行面组件
279
+ *
280
+ * 优先级:应用自带 surface → 数据源延迟解析(如从插件注册表取工具箱视图)。
281
+ * 两级都没有时返回 undefined,由运行屏渲染「该应用尚未提供运行面」空态——
282
+ * 空态要写明原因,不能是空白页。
283
+ */
284
+ function resolveSurface(appId: string): Component | undefined {
285
+ const app = registry.getApp(appId)
286
+ if (!app) return undefined
287
+ return app.contributions.surface?.component ?? registry.getSourceOf(appId)?.resolveSurface?.(appId)
288
+ }
289
+
290
+ /** 应用自带主题色(仅作用于应用内部) */
291
+ function surfaceAccent(appId: string): string | undefined {
292
+ return registry.getApp(appId)?.contributions.surface?.accent
293
+ }
294
+
295
+ /**
296
+ * 是否有数据源支持安装本地包
297
+ *
298
+ * 「添加应用」区按能力渲染:没有安装能力的形态不该出现点了没反应的按钮。
299
+ */
300
+ function supportsInstall(): boolean {
301
+ return registry.listSources().some((s) => typeof s.installFromLocalPackage === 'function')
302
+ }
303
+
304
+ /**
305
+ * 安装本地应用包
306
+ *
307
+ * @returns 安装后的应用 id;null = 用户取消(调用方不弹错误)。
308
+ * 失败由 catch 抛出,错误已由 lastError 暴露。
309
+ */
310
+ async function installFromLocalPackage(): Promise<string | null> {
311
+ const source = registry
312
+ .listSources()
313
+ .find((s): s is ShellAppSource & Required<Pick<ShellAppSource, 'installFromLocalPackage'>> =>
314
+ typeof s.installFromLocalPackage === 'function',
315
+ )
316
+ if (!source) {
317
+ logger.warn('[Shell] install unsupported by every registered source')
318
+ return null
319
+ }
320
+ try {
321
+ const installedId = await source.installFromLocalPackage()
322
+ if (installedId !== null) {
323
+ await refresh()
324
+ // 安装成功的新应用名要到刷新后的清单里取(安装前注册表里还没有它)
325
+ const installed = registry.getApp(installedId)
326
+ if (installed) notifyAppEvent('installed', installed)
327
+ }
328
+ return installedId
329
+ } catch (e) {
330
+ logger.error('[Shell] install local package failed', e)
331
+ lastError.value = e instanceof Error ? e.message : String(e)
332
+ return null
333
+ }
334
+ }
335
+
336
+ /** 该应用是否可卸载(内置应用通常不可) */
337
+ function supportsRemove(appId: string): boolean {
338
+ return typeof registry.getSourceOf(appId)?.remove === 'function'
339
+ }
340
+
341
+ /** 卸载应用并删除数据 */
342
+ async function removeApp(appId: string): Promise<boolean> {
343
+ const source = registry.getSourceOf(appId)
344
+ const app = registry.getApp(appId)
345
+ if (!source?.remove) {
346
+ logger.warn(`[Shell] remove unsupported: appId=${appId}`)
347
+ return false
348
+ }
349
+ try {
350
+ const ok = await source.remove(appId)
351
+ if (ok) {
352
+ registry.clearApp(appId)
353
+ await refresh()
354
+ // 卸载通知需要应用名,但此时应用已从注册表移除——用移除前抓到的快照
355
+ if (app) notifyAppEvent('uninstalled', app)
356
+ // 同停用:应用没了,它的运行屏不能留在栈里(否则返回时只剩「应用不存在」提示)
357
+ useShellNavigation().closeAppRun(appId)
358
+ }
359
+ return ok
360
+ } catch (e) {
361
+ logger.error(`[Shell] remove app failed: appId=${appId}`, e)
362
+ lastError.value = e instanceof Error ? e.message : String(e)
363
+ return false
364
+ }
365
+ }
366
+
367
+ /** 宿主壳应用视图模型 */
368
+ export interface ShellApps {
369
+ apps: ComputedRef<ShellApp[]>
370
+ sortedApps: ComputedRef<ShellApp[]>
371
+ runningApps: ComputedRef<ShellApp[]>
372
+ stats: ComputedRef<ShellStats>
373
+ loading: ComputedRef<boolean>
374
+ lastError: ComputedRef<string | null>
375
+ getApp: typeof getApp
376
+ refresh: typeof refresh
377
+ launch: typeof launch
378
+ stop: typeof stop
379
+ approveApp: typeof approveApp
380
+ supportsApprove: typeof supportsApprove
381
+ setPermissionGrant: typeof setPermissionGrant
382
+ supportsPermissionControl: typeof supportsPermissionControl
383
+ resolveSurface: typeof resolveSurface
384
+ surfaceAccent: typeof surfaceAccent
385
+ supportsInstall: typeof supportsInstall
386
+ installFromLocalPackage: typeof installFromLocalPackage
387
+ supportsRemove: typeof supportsRemove
388
+ removeApp: typeof removeApp
389
+ }
390
+
391
+ /** 获取宿主壳应用视图模型(单例) */
392
+ export function useShellApps(): ShellApps {
393
+ return {
394
+ apps,
395
+ sortedApps,
396
+ runningApps,
397
+ stats,
398
+ loading: computed(() => loading.value),
399
+ lastError: computed(() => lastError.value),
400
+ getApp,
401
+ refresh,
402
+ launch,
403
+ stop,
404
+ approveApp,
405
+ supportsApprove,
406
+ setPermissionGrant,
407
+ supportsPermissionControl,
408
+ resolveSurface,
409
+ surfaceAccent,
410
+ supportsInstall,
411
+ installFromLocalPackage,
412
+ supportsRemove,
413
+ removeApp,
414
+ }
415
+ }