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

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 (122) hide show
  1. package/README.md +439 -2
  2. package/README_en.md +415 -0
  3. package/bin/build.js +271 -0
  4. package/bin/cli.js +96 -0
  5. package/bin/lib.js +349 -0
  6. package/bin/manifest-gen.js +327 -0
  7. package/bin/package.js +151 -0
  8. package/bin/scaffold.js +299 -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 +43 -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 +332 -0
  25. package/dev-shell/src/locales/index.ts +70 -0
  26. package/dev-shell/src/locales/shell.ts +331 -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 +374 -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 +144 -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 +501 -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 +359 -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 +357 -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 +417 -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 +44 -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/useSlotCarousel.ts +238 -0
  71. package/dev-shell/src/shell/composables/useToast.ts +33 -0
  72. package/dev-shell/src/shell/index.ts +88 -0
  73. package/dev-shell/src/shell/logger.ts +45 -0
  74. package/dev-shell/src/shell/permissions.ts +145 -0
  75. package/dev-shell/src/shell/registry.ts +220 -0
  76. package/dev-shell/src/shell/slotCards.ts +47 -0
  77. package/dev-shell/src/shell/styles/shell.css +1155 -0
  78. package/dev-shell/src/shell/surfaceIntent.ts +107 -0
  79. package/dev-shell/src/shell/types.ts +152 -0
  80. package/dev-shell/src/shell/utils.ts +54 -0
  81. package/dev-shell/src/styles/main.css +153 -0
  82. package/dev-shell/src/styles/mobile.css +981 -0
  83. package/dev-shell/src/styles/wallpaper.css +130 -0
  84. package/dev-shell/src/theme.ts +71 -0
  85. package/dev-shell/src/vite-env.d.ts +20 -0
  86. package/dev-shell/tailwind.config.js +101 -0
  87. package/dev-shell/tsconfig.json +16 -0
  88. package/dev-shell/vite.config.ts +130 -0
  89. package/dist/index.d.ts +124 -0
  90. package/dist/index.js +229 -0
  91. package/dist/types.d.ts +738 -0
  92. package/dist/types.js +0 -0
  93. package/dist/vite-plugin.d.ts +16 -0
  94. package/dist/vite-plugin.js +95 -0
  95. package/package.json +88 -3
  96. package/src/constants.ts +6 -0
  97. package/src/global-dialog.ts +168 -0
  98. package/src/index.ts +96 -0
  99. package/src/runtime.ts +44 -0
  100. package/src/types.ts +822 -0
  101. package/src/ui/AppUpdateSection.d.ts +44 -0
  102. package/src/ui/AppUpdateSection.vue +253 -0
  103. package/src/ui/PluginGlobalDialog.vue +388 -0
  104. package/src/ui/QuickCard.d.ts +33 -0
  105. package/src/ui/QuickCard.vue +167 -0
  106. package/src/ui/Select.d.ts +37 -0
  107. package/src/ui/Select.vue +307 -0
  108. package/src/ui/plugin-global-dialog.d.ts +11 -0
  109. package/src/ui/useSwipeTabs.d.ts +20 -0
  110. package/src/ui/useSwipeTabs.ts +53 -0
  111. package/src/update.ts +123 -0
  112. package/src/vite-plugin.ts +132 -0
  113. package/template/.gitignore +4 -0
  114. package/template/README.md +46 -0
  115. package/template/package.json +23 -0
  116. package/template/plugin.json +13 -0
  117. package/template/rust/Cargo.toml +19 -0
  118. package/template/rust/src/lib.rs +33 -0
  119. package/template/src/env.d.ts +24 -0
  120. package/template/src/index.ts +37 -0
  121. package/template/tsconfig.json +17 -0
  122. package/template/vite.config.ts +21 -0
@@ -0,0 +1,559 @@
1
+ <template>
2
+ <div class="flex flex-col h-full min-h-0">
3
+ <!-- ==================== 页头(返回 + 标题 + 数量 + 全部关闭) ==================== -->
4
+ <header class="flex items-center gap-2 px-2 pt-2 pb-1 flex-shrink-0">
5
+ <button
6
+ type="button"
7
+ class="flex items-center justify-center rounded-[10px] min-w-[var(--mobile-touch-target-min)] min-h-[var(--mobile-touch-target-min)] transition-colors duration-200 active:bg-[var(--mobile-accent-muted)]"
8
+ :aria-label="t('shell.switcher.back')"
9
+ @click="nav.back()"
10
+ >
11
+ <svg width="21" height="21" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
12
+ <path d="M15 6l-6 6 6 6" />
13
+ </svg>
14
+ </button>
15
+ <div class="flex items-baseline gap-1.5 flex-1 min-w-0">
16
+ <h1 class="text-[var(--font-size-xl)] font-semibold text-[var(--mobile-text-primary)] truncate">
17
+ {{ t('shell.switcher.title') }}
18
+ </h1>
19
+ <span
20
+ v-if="cards.length > 0"
21
+ class="flex-shrink-0 text-[var(--font-size-xs)] text-[var(--mobile-text-secondary)] tabular-nums"
22
+ >
23
+ {{ cards.length }} {{ t('shell.switcher.countUnit') }}
24
+ </span>
25
+ </div>
26
+ <ShellChip
27
+ v-if="cards.length > 0"
28
+ :label="t('shell.switcher.closeAll')"
29
+ tone="danger"
30
+ clickable
31
+ @click="closeAll"
32
+ />
33
+ </header>
34
+
35
+ <!-- ==================== 卡片区(与桌面同形的多行卡片布局) ====================
36
+ 桌面多任务是「信息条在上 + 缩略区在下」的 3/4 竖版卡片,按行居中排布;
37
+ 移动端沿用同一形态与同一信息层级,只把卡片缩到移动端尺寸:每行 2 张
38
+ (宽屏 3 张),整组上下左右居中,超出时可纵向滚动。
39
+
40
+ 缩略区仍不伪造应用截图——平台拿不到应用画面,放大应用图标 + 版本 /
41
+ 权限数就是平台掌握的全部事实(AGENTS §5.1 B4)。
42
+
43
+ 手势保留:横向拖动切换当前卡(网格轻微跟手后吸附),上滑关闭单张;
44
+ 拖拽不是唯一路径——每张卡都可直接点按进入,右上角另有独立关闭键
45
+ (WCAG 2.2 Dragging Movements)。网格内容溢出时纵向上滑让位给原生
46
+ 滚动,否则列表滚不动;此时关闭同样走卡片上的 ×。两种状态都保留
47
+ pinch-zoom,不吞掉系统的双指缩放。 -->
48
+ <template v-if="cards.length > 0">
49
+ <div
50
+ ref="gridEl"
51
+ class="shell-recents-grid flex-1 min-h-0"
52
+ :class="{
53
+ 'shell-recents-grid--dragging': dragAxis !== null,
54
+ 'shell-recents-grid--scrollable': scrollable,
55
+ }"
56
+ role="list"
57
+ :aria-label="t('shell.switcher.title')"
58
+ :style="{ touchAction: scrollable ? 'pan-y pinch-zoom' : 'pinch-zoom' }"
59
+ @pointerdown="onPointerDown"
60
+ @pointermove="onPointerMove"
61
+ @pointerup="onPointerUp"
62
+ @pointercancel="resetDrag"
63
+ >
64
+ <div class="shell-recents-grid-inner" :style="gridStyle()">
65
+ <article
66
+ v-for="(card, index) in cards"
67
+ :key="card.app.id"
68
+ class="shell-recents-card"
69
+ :class="{ active: index === activeIndex }"
70
+ :style="cardStyle(index)"
71
+ :data-app-id="card.app.id"
72
+ role="listitem"
73
+ >
74
+ <!-- 信息条在卡片顶部(与桌面一致):图标打头 + 名称;
75
+ 非运行 / 异常状态才追加一行状态文字,运行中不重复播报默认事实。 -->
76
+ <div class="shell-recents-meta" aria-hidden="true">
77
+ <ShellAppIcon :app="card.app" :size="28" />
78
+ <span class="flex-1 min-w-0">
79
+ <span class="shell-recents-name">{{ card.app.name }}</span>
80
+ <span
81
+ v-if="stateText(card)"
82
+ class="shell-recents-status"
83
+ :class="stateClass(card)"
84
+ >
85
+ {{ stateText(card) }}
86
+ </span>
87
+ </span>
88
+ </div>
89
+
90
+ <div
91
+ class="shell-recents-thumb"
92
+ :class="{ 'is-running': card.running }"
93
+ aria-hidden="true"
94
+ >
95
+ <ShellAppIcon :app="card.app" :size="64" />
96
+ <span v-if="thumbMeta(card)" class="shell-recents-thumb-meta">
97
+ {{ thumbMeta(card) }}
98
+ </span>
99
+ </div>
100
+
101
+ <!-- 唯一打开动作:覆盖整张卡的空按钮(视觉层 pointer-events:none)。
102
+ 预览面 / 信息条可能含应用自绘或不可交互内容,不再嵌进 button。 -->
103
+ <button
104
+ type="button"
105
+ class="shell-recents-open"
106
+ :aria-label="t(card.running ? 'shell.switcher.enterApp' : 'shell.switcher.launchApp', { name: card.app.name })"
107
+ :aria-current="index === activeIndex ? 'true' : undefined"
108
+ @click.stop="onCardClick(card)"
109
+ ></button>
110
+
111
+ <!-- 关闭:每张卡都有(桌面同形),独立按钮压在整卡按钮之上,
112
+ 并标出 data-card-ignore-drag —— 从它起手不算卡片手势 -->
113
+ <button
114
+ type="button"
115
+ data-card-ignore-drag
116
+ class="shell-recents-close"
117
+ :aria-label="t('shell.switcher.closeCard', { name: card.app.name })"
118
+ @click.stop="dismiss(card.app.id)"
119
+ >
120
+ <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true">
121
+ <path d="M6 6l12 12M18 6L6 18" />
122
+ </svg>
123
+ </button>
124
+ </article>
125
+ </div>
126
+ </div>
127
+ </template>
128
+
129
+ <!-- ==================== 空态 ==================== -->
130
+ <div v-else class="flex-1 flex flex-col items-center justify-center px-6 text-center">
131
+ <span
132
+ class="flex items-center justify-center w-14 h-14 rounded-[18px]"
133
+ :style="{ background: 'var(--mobile-bg-elevated)', color: 'var(--mobile-text-secondary)' }"
134
+ aria-hidden="true"
135
+ >
136
+ <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">
137
+ <rect x="3" y="4" width="18" height="16" rx="3" />
138
+ <path d="M3 9h18" />
139
+ </svg>
140
+ </span>
141
+ <p class="mt-4 text-[var(--font-size-base)] font-medium text-[var(--mobile-text-primary)]">
142
+ {{ t('shell.switcher.empty') }}
143
+ </p>
144
+ <p class="mt-1 text-[var(--font-size-sm)] text-[var(--mobile-text-secondary)]">
145
+ {{ t('shell.switcher.emptyHint') }}
146
+ </p>
147
+ <!-- 空态给主动作,不把用户堵在只有页头返回键的空屏里 -->
148
+ <button
149
+ type="button"
150
+ class="mt-5 flex items-center justify-center rounded-[10px] px-5 min-h-[var(--mobile-touch-target-min)] text-[var(--font-size-base)] font-medium transition-opacity duration-200 active:opacity-80"
151
+ :style="{ background: 'var(--mobile-accent)', color: 'var(--mobile-text-on-accent)' }"
152
+ @click="nav.goHome()"
153
+ >
154
+ {{ t('shell.switcher.goHome') }}
155
+ </button>
156
+ </div>
157
+ </div>
158
+ </template>
159
+
160
+ <script setup lang="ts">
161
+ /**
162
+ * 多任务(与桌面同形的卡片布局)
163
+ * -----------------------------------------------------------------------------
164
+ * 形态:卡片按行居中排布(移动端每行 2 张,宽屏 3 张),信息条在上、缩略区在下,
165
+ * 比例 3/4。桌面用 420×560 的大卡,这里只是同一形态的移动端尺寸,几何真源在
166
+ * `styles/shell.css`(.shell-recents-*),组件只给手势位移与序号。
167
+ *
168
+ * 四条硬约束:
169
+ * ① 预览位不伪造内容——平台不持有应用内部快照,放大应用图标 + 真实元信息即
170
+ * 平台掌握的全部事实(伪造截图属于 B4 业务投影越线);
171
+ * ② 拖拽**不是唯一路径**(WCAG 2.2 Dragging Movements):每张卡都可直接点按
172
+ * 进入,关闭键常驻卡片右上角,页头返回负责退出;
173
+ * ③ 手势不互相抢:pointermove 首次越过轴锁阈值即锁定本次手势方向——横向
174
+ * 切卡、纵向上滑关闭;网格内容溢出时纵向手势让位给原生滚动(否则列表
175
+ * 滚不动),贴屏幕左右边缘起手则让位给系统返回手势;
176
+ * ④ 点哪张进哪张、上滑关哪张;拖拽抬手后浏览器补的那次 click 必须吞掉,
177
+ * 否则上滑关闭会顺手把应用打开。
178
+ *
179
+ * 卡片集合 = 运行中应用(MRU 优先)+ 最近使用但未运行的应用。关闭运行中的卡片 =
180
+ * 停用实例并移出最近列表(不然它会以「未运行」身份原地复活,看起来像关不掉)。
181
+ *
182
+ * 已退役:底部「上一个 / 下一个 + 圆点 + 页码」切换条(用户 2026-10-21 裁决)。
183
+ * 手势与整卡点按已覆盖全部切换路径,底部常驻控件只会压缩卡片可视高度。
184
+ */
185
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
186
+ import { useI18n } from 'vue-i18n'
187
+ import { logger } from '../../logger'
188
+ import { useShellApps } from '../../composables/useShellApps'
189
+ import { useShellNavigation } from '../../composables/useShellNavigation'
190
+ import { useShellOpen } from '../../composables/useShellOpen'
191
+ import { useShellRecent } from '../../composables/useShellRecent'
192
+ import type { ShellApp } from '../../types'
193
+ import ShellAppIcon from '../ShellAppIcon.vue'
194
+ import ShellChip from '../ShellChip.vue'
195
+
196
+ const { t } = useI18n()
197
+ const nav = useShellNavigation()
198
+ const { apps, stop } = useShellApps()
199
+ const { recentIds, removeRecent, clearRecent } = useShellRecent()
200
+ const { open } = useShellOpen()
201
+
202
+ interface RecentCard {
203
+ app: ShellApp
204
+ running: boolean
205
+ }
206
+
207
+ /** 最近使用排序:recentIds 有记录的按记录顺序,其余运行中应用按名称兜底 */
208
+ function mruRank(app: ShellApp): number {
209
+ const index = recentIds.value.indexOf(app.id)
210
+ return index === -1 ? Number.MAX_SAFE_INTEGER : index
211
+ }
212
+
213
+ const cards = computed<RecentCard[]>(() => {
214
+ const running = apps.value
215
+ .filter((a) => a.state === 'running')
216
+ .sort((a, b) => {
217
+ const diff = mruRank(a) - mruRank(b)
218
+ return diff !== 0 ? diff : a.name.localeCompare(b.name, 'zh-Hans-CN')
219
+ })
220
+ const runningIds = new Set(running.map((a) => a.id))
221
+ // 最近使用但未运行:卡片保留在列表里(Android recents 的同一语义),点它即启动
222
+ const dormant = recentIds.value
223
+ .map((id) => apps.value.find((a) => a.id === id))
224
+ .filter((app): app is ShellApp => app !== undefined && !runningIds.has(app.id))
225
+ return [
226
+ ...running.map((app) => ({ app, running: true })),
227
+ ...dormant.map((app) => ({ app, running: false })),
228
+ ]
229
+ })
230
+
231
+ // ==================== 前层选择 ====================
232
+
233
+ /** 前层卡片 id;null = 取列表第一张(卡片集合变化时的兜底) */
234
+ const activeId = ref<string | null>(null)
235
+
236
+ const activeIndex = computed(() => {
237
+ const index = cards.value.findIndex((c) => c.app.id === activeId.value)
238
+ return index >= 0 ? index : 0
239
+ })
240
+
241
+ /** 卡片集合变化(启动 / 关闭 / 刷新)后修正前层:原前层消失就落到相邻张 */
242
+ watch(cards, (list) => {
243
+ if (list.length === 0) {
244
+ activeId.value = null
245
+ return
246
+ }
247
+ if (!list.some((c) => c.app.id === activeId.value)) {
248
+ activeId.value = list[Math.min(activeIndex.value, list.length - 1)]?.app.id ?? null
249
+ }
250
+ })
251
+
252
+ /** 上一个 / 下一个(越界即无操作;横向手势的唯一消费者) */
253
+ function step(delta: number): void {
254
+ const target = cards.value[activeIndex.value + delta]
255
+ if (target) activeId.value = target.app.id
256
+ }
257
+
258
+ // ==================== 卡片内容 ====================
259
+
260
+ /** 状态行:运行中不重复播报默认事实,只给未运行 / 待审批 / 停用 / 失败 */
261
+ function stateText(card: RecentCard): string {
262
+ switch (card.app.state) {
263
+ case 'running':
264
+ return ''
265
+ case 'approval':
266
+ return t('shell.common.approval')
267
+ case 'disabled':
268
+ return t('shell.common.disabled')
269
+ case 'error':
270
+ return t('shell.common.error')
271
+ default:
272
+ return t('shell.switcher.dormant')
273
+ }
274
+ }
275
+
276
+ /** 状态行的语义色:异常态才偏离次级文字色(颜色之外另有文字,非纯色传达) */
277
+ function stateClass(card: RecentCard): string {
278
+ if (card.app.state === 'approval') return 'is-warn'
279
+ if (card.app.state === 'error') return 'is-danger'
280
+ return ''
281
+ }
282
+
283
+ /** 缩略区元信息:版本 + 权限数,取不到就不留空行 */
284
+ function thumbMeta(card: RecentCard): string {
285
+ const parts: string[] = []
286
+ if (card.app.version) parts.push(`v${card.app.version}`)
287
+ if (card.app.permissions.length > 0) {
288
+ parts.push(t('shell.apps.permissionCount', { count: card.app.permissions.length }))
289
+ }
290
+ return parts.join(' · ')
291
+ }
292
+
293
+ // ==================== 手势几何 ====================
294
+
295
+ /** 轴锁阈值:小于该位移视为点击,避免手抖把点击判成拖拽 */
296
+ const AXIS_LOCK = 10
297
+ /** 切卡位移阈值 */
298
+ const SWIPE_STEP = 56
299
+ /** 上滑关闭阈值(负值方向) */
300
+ const DISMISS_THRESHOLD = 72
301
+ /**
302
+ * 屏幕左右边缘让位带(px)
303
+ *
304
+ * ui-ux-pro-max `ux / Gesture Conflicts`:自定义手势不得覆盖系统手势。从这条带里
305
+ * 起手不接管卡片手势,交给系统的返回手势——否则贴着屏幕边缘划动会既切卡又返回。
306
+ */
307
+ const EDGE_GUARD = 24
308
+ /** 横向拖动时网格整体跟手位移上限(px):给手势反馈,但不把卡片拖出视野 */
309
+ const GRID_NUDGE_LIMIT = 28
310
+ /** 网格整体跟手比例:横向手势是「切当前卡」,卡片本身不离开网格 */
311
+ const GRID_NUDGE_RATIO = 0.14
312
+
313
+ const gridEl = ref<HTMLElement | null>(null)
314
+ /** 网格内容是否溢出(溢出时纵向上滑归原生滚动) */
315
+ const scrollable = ref(false)
316
+
317
+ /**
318
+ * 同步「是否可纵向滚动」
319
+ *
320
+ * 必须在手势开始前确定:溢出时 `touch-action: pan-y` 把纵向手势交给浏览器,
321
+ * 不溢出时用 `touch-action: none` 接管纵向上滑关闭。若等 pointermove 再判断,
322
+ * 浏览器已经启动滚动 / 发出 pointercancel,滑动关闭会时灵时不灵。
323
+ */
324
+ function syncScrollable(): void {
325
+ const el = gridEl.value
326
+ scrollable.value = !!el && el.scrollHeight > el.clientHeight + 1
327
+ }
328
+
329
+ function onViewportResize(): void {
330
+ syncScrollable()
331
+ }
332
+
333
+ let resizeObserver: ResizeObserver | null = null
334
+
335
+ onMounted(() => {
336
+ syncScrollable()
337
+ window.addEventListener('resize', onViewportResize)
338
+ // 视口旋转 / 分屏会改变可用高度;ResizeObserver 不存在的老 WebView 靠 resize 兜底
339
+ if (typeof ResizeObserver !== 'undefined' && gridEl.value) {
340
+ resizeObserver = new ResizeObserver(syncScrollable)
341
+ resizeObserver.observe(gridEl.value)
342
+ }
343
+ })
344
+
345
+ onBeforeUnmount(() => {
346
+ window.removeEventListener('resize', onViewportResize)
347
+ resizeObserver?.disconnect()
348
+ resizeObserver = null
349
+ })
350
+
351
+ watch(cards, async () => {
352
+ await nextTick()
353
+ syncScrollable()
354
+ })
355
+
356
+ /** 网格整体位移:只在横向拖动时轻微跟手,抬手后吸附回原位 */
357
+ function gridStyle(): Record<string, string> {
358
+ if (dragAxis.value !== 'x') return {}
359
+ const raw = Math.max(
360
+ -GRID_NUDGE_LIMIT,
361
+ Math.min(GRID_NUDGE_LIMIT, dragDx.value * GRID_NUDGE_RATIO),
362
+ )
363
+ // 取整到 0.1px:避免浮点误差写出一长串小数(CSS 里也没有亚像素收益)
364
+ const nudge = Math.round(raw * 10) / 10
365
+ return { transform: `translate3d(${nudge}px, 0, 0)` }
366
+ }
367
+
368
+ /** 卡片自身样式:上滑关闭时跟手位移 + 淡出;其余卡片保持原位 */
369
+ function cardStyle(index: number): Record<string, string> {
370
+ const style: Record<string, string> = { '--i': String(index) }
371
+ const dragging = dragAxis.value === 'y' && cards.value[index]?.app.id === dragTargetId
372
+ if (!dragging) return style
373
+ style.opacity = String(Math.max(0.4, 1 + dragDy.value / 360))
374
+ style.transform = `translate3d(0, ${dragDy.value}px, 0)`
375
+ return style
376
+ }
377
+
378
+ // ==================== 手势(横向切卡 / 纵向上滑关闭) ====================
379
+
380
+ const dragAxis = ref<'x' | 'y' | null>(null)
381
+ const dragDx = ref(0)
382
+ const dragDy = ref(0)
383
+ /** 本次手势是否已越过阈值(越过则抬手后的 click 不再触发卡片动作) */
384
+ let dragMoved = false
385
+ /** 本次手势作用的卡片(纵向关闭按手指下那张卡判定) */
386
+ let dragTargetId: string | null = null
387
+ /** 手势起点 */
388
+ let dragOrigin: { x: number; y: number } | null = null
389
+ /** 起手时网格是否可纵向滚动(可滚动则纵向手势让给原生滚动) */
390
+ let scrollAtStart = false
391
+ /** 本次手势的指针 id(捕获指针时要用) */
392
+ let lastPointerId: number | undefined
393
+ /** 已 setPointerCapture 的指针(抬起 / 取消时释放) */
394
+ let capturedPointerId: number | null = null
395
+
396
+ /**
397
+ * 接管本次手势的后续事件
398
+ *
399
+ * 只在**越过轴锁、确认是拖拽之后**才捕获:简单点击不捕获,click 仍落在卡片上
400
+ * (「点卡片进入」依赖它)。捕获后手指滑出网格区(快速上滑)仍能收到
401
+ * move/up,否则手势会停在偏移位上、卡片留在错位状态。
402
+ */
403
+ function capturePointer(): void {
404
+ const el = gridEl.value
405
+ const id = lastPointerId
406
+ // 非 Tauri 运行时 / 老 WebView 可能没有该 API:缺了就退化到「不捕获」,功能不受影响
407
+ if (!el || id === undefined || typeof el.setPointerCapture !== 'function') return
408
+ try {
409
+ el.setPointerCapture(id)
410
+ capturedPointerId = id
411
+ } catch (e) {
412
+ capturedPointerId = null
413
+ // 捕获失败 → 快滑出界时手势收不到事件(卡片会停在偏移位),降级但要说出来
414
+ logger.warn('[ShellSwitcher] setPointerCapture failed; drag may end out of grid', e)
415
+ }
416
+ }
417
+
418
+ function releasePointer(): void {
419
+ const el = gridEl.value
420
+ const id = capturedPointerId
421
+ capturedPointerId = null
422
+ if (!el || id === null || typeof el.releasePointerCapture !== 'function') return
423
+ try {
424
+ el.releasePointerCapture(id)
425
+ } catch (e) {
426
+ // 指针已抬起 / 取消时释放会抛(该指针不再活跃),属清理路径,不影响交互
427
+ logger.debug('[ShellSwitcher] releasePointerCapture skipped (pointer no longer active)', e)
428
+ }
429
+ }
430
+
431
+ function onPointerDown(event: PointerEvent): void {
432
+ // 只响应主指针;鼠标右键 / 中键不参与
433
+ if (event.pointerType === 'mouse' && event.button !== 0) return
434
+ // 只把「关闭」按钮排除在卡片手势之外:整卡覆盖按钮仍是手势起手面
435
+ if ((event.target as HTMLElement | null)?.closest('[data-card-ignore-drag]')) return
436
+ // 贴边起手让位给系统返回手势(EDGE_GUARD)
437
+ if (event.clientX <= EDGE_GUARD || event.clientX >= window.innerWidth - EDGE_GUARD) return
438
+ // 手指导下真正能否滚动:ref 可能滞后于布局(字体 / 内容晚到),以当次测量为准
439
+ const el = gridEl.value
440
+ scrollAtStart = !!el && el.scrollHeight > el.clientHeight + 1
441
+ // 命中测试取手指底下那张卡;落在卡片之间空档时退回当前卡片
442
+ const hitId =
443
+ (event.target as HTMLElement | null)?.closest('[data-app-id]')?.getAttribute('data-app-id') ??
444
+ cards.value[activeIndex.value]?.app.id
445
+ if (!hitId || !cards.value.some((c) => c.app.id === hitId)) return
446
+ dragTargetId = hitId
447
+ dragOrigin = { x: event.clientX, y: event.clientY }
448
+ lastPointerId = event.pointerId
449
+ dragMoved = false
450
+ dragAxis.value = null
451
+ dragDx.value = 0
452
+ dragDy.value = 0
453
+ }
454
+
455
+ function onPointerMove(event: PointerEvent): void {
456
+ if (!dragOrigin) return
457
+ const dx = event.clientX - dragOrigin.x
458
+ const dy = event.clientY - dragOrigin.y
459
+ if (dragAxis.value === null) {
460
+ if (Math.abs(dx) < AXIS_LOCK && Math.abs(dy) < AXIS_LOCK) return
461
+ const horizontal = Math.abs(dx) > Math.abs(dy)
462
+ // 网格可滚动时纵向手势归浏览器:不锁轴、不捕获、不阻止默认滚动
463
+ if (!horizontal && scrollAtStart) {
464
+ dragOrigin = null
465
+ return
466
+ }
467
+ // 首次越过阈值即锁轴:横向归切卡、纵向归关闭,同一次手势内不再改判
468
+ dragAxis.value = horizontal ? 'x' : 'y'
469
+ dragMoved = true
470
+ // 确认是拖拽后才捕获指针:手指滑出网格区仍能收尾
471
+ capturePointer()
472
+ }
473
+ if (dragAxis.value === 'x') {
474
+ dragDx.value = dx
475
+ } else {
476
+ // 只允许上滑(offset ≤ 0):下滑是「取消」,不应该把卡片往下推
477
+ dragDy.value = Math.min(0, dy)
478
+ }
479
+ }
480
+
481
+ function onPointerUp(event: PointerEvent): void {
482
+ const origin = dragOrigin
483
+ const target = dragTargetId
484
+ const axis = dragAxis.value
485
+ const dx = dragDx.value
486
+ resetDrag()
487
+ if (!origin || !target) return
488
+ if (axis === 'x') {
489
+ if (dx <= -SWIPE_STEP) step(1)
490
+ else if (dx >= SWIPE_STEP) step(-1)
491
+ return
492
+ }
493
+ if (axis !== 'y') return
494
+ // 以抬起位置为准(pointermove 可能因快速滑动而稀疏甚至缺失)
495
+ const offset = Math.min(0, event.clientY - origin.y)
496
+ if (offset <= -DISMISS_THRESHOLD) void dismiss(target)
497
+ }
498
+
499
+ function resetDrag(): void {
500
+ releasePointer()
501
+ dragOrigin = null
502
+ dragTargetId = null
503
+ scrollAtStart = false
504
+ dragAxis.value = null
505
+ dragDx.value = 0
506
+ dragDy.value = 0
507
+ }
508
+
509
+ /**
510
+ * 拖拽刚结束时吞掉随后的 click
511
+ *
512
+ * 一次拖拽抬手后浏览器仍会补一次 click(落在抬手处那个元素上)。不吞掉的话,
513
+ * 上滑关闭会顺手把应用打开、横滑切卡会连带进入——拖拽与点击必须分开判。
514
+ *
515
+ * @returns true = 这次 click 属于拖拽余波,调用方不要再执行动作
516
+ */
517
+ function consumeDragClick(): boolean {
518
+ if (!dragMoved) return false
519
+ dragMoved = false
520
+ return true
521
+ }
522
+
523
+ /** 点卡片 = 进入 / 启动该应用(Android recents:点哪张开哪张) */
524
+ function onCardClick(card: RecentCard): void {
525
+ if (consumeDragClick()) return
526
+ void enter(card.app.id)
527
+ }
528
+
529
+ // ==================== 动作 ====================
530
+
531
+ /** 关闭一张卡片:运行中先停用,然后(无论是否在跑)移出最近列表 */
532
+ async function dismiss(appId: string): Promise<void> {
533
+ const index = cards.value.findIndex((c) => c.app.id === appId)
534
+ if (index === -1) return
535
+ // 前层移走后落到相邻张(后一张优先,没有则前一张)
536
+ if (activeId.value === appId || index === activeIndex.value) {
537
+ activeId.value =
538
+ cards.value[index + 1]?.app.id ?? cards.value[index - 1]?.app.id ?? null
539
+ }
540
+ const app = cards.value[index]
541
+ if (app?.running) await stop(appId)
542
+ removeRecent(appId)
543
+ }
544
+
545
+ /** 全部关闭:停止所有运行中实例并清空最近列表(Android「清除全部」的同一语义) */
546
+ async function closeAll(): Promise<void> {
547
+ const running = apps.value.filter((a) => a.state === 'running')
548
+ for (const app of running) {
549
+ await stop(app.id)
550
+ }
551
+ clearRecent()
552
+ }
553
+
554
+ /** 进入应用:先回主屏重置栈,再走统一入口(未运行的会先启动,待审批落详情页) */
555
+ async function enter(appId: string): Promise<void> {
556
+ nav.goHome()
557
+ await open(appId)
558
+ }
559
+ </script>