dsh-gufa-code 1.3.6 → 1.3.8

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.
package/README.md CHANGED
@@ -73,6 +73,7 @@ dsh plugin add dsh-gufa-code
73
73
  - 把该 App 切到前台(主窗口自动被盖住,等于「隐藏主窗口」);
74
74
  - 以**无边框、半透明(0.55)、永远置顶**的独立悬浮窗出现,并**自动吸附**到该 App 当前窗口:位置、大小一致,窗口移动/缩放时实时跟随。
75
75
  - 读取窗口位置/大小走 macOS **「辅助功能」**权限(不是屏幕录制):系统设置 → 隐私与安全性 → 辅助功能 → 勾选 **DeepSeek Harness**;改完重启一次 DSH 生效。
76
+ - **状态延续**:进入摸鱼时会把主窗口的项目、已打开标签页、界面布局、当前会话一并带进悬浮层,悬浮窗里接着干活而不是从空白开始。
76
77
  - **悬浮层交互**:
77
78
  - 顶部 22px 拖动条移动窗口(右键 = 摸鱼菜单),窗口大小/位置会记住;
78
79
  - 全局快捷键 **⌃⌥⌘G**:切换点击穿透(穿透后鼠标直接操作下面的 App,再按一次切回来);
package/client.js CHANGED
@@ -46,6 +46,30 @@ G.portal = (function createPortalHelper() {
46
46
  })()
47
47
  G.hooks = { useState, useEffect, useRef, useMemo, useCallback, useLayoutEffect }
48
48
 
49
+ /* ── 悬浮层状态:helper 通过 URL 参数 ?gufa-state=<base64url(JSON)> 传主窗口快照 ── */
50
+ G.floatState = (function readFloatState() {
51
+ if (typeof window === 'undefined' || typeof document === 'undefined') return null
52
+ try {
53
+ const raw = new URLSearchParams(window.location.search).get('gufa-state')
54
+ if (raw === null || raw === '') {
55
+ try {
56
+ const stored = localStorage.getItem('gufa-code:float-state')
57
+ return stored === null ? null : JSON.parse(stored)
58
+ } catch {
59
+ return null
60
+ }
61
+ }
62
+ const base64 = raw.replace(/-/g, '+').replace(/_/g, '/')
63
+ const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4)
64
+ const binary = atob(padded)
65
+ const bytes = Uint8Array.from(binary, (chunk) => chunk.charCodeAt(0))
66
+ const text = new TextDecoder().decode(bytes)
67
+ return JSON.parse(text)
68
+ } catch {
69
+ return null
70
+ }
71
+ })()
72
+
49
73
  /* ── 事件总线(零件之间解耦:SSH → 终端 发送命令等) ───────────────────── */
50
74
  G.bus = (function createBus() {
51
75
  const listeners = new Map()
@@ -1081,15 +1105,14 @@ html[data-gufa-float] #root [data-gufa-dock],
1081
1105
  html[data-gufa-float] #root .gufa-overlay-root *,
1082
1106
  html[data-gufa-float] #root [data-gufa-dock] *{visibility:visible!important}
1083
1107
  /* 桌宠提到覆盖层之上:同时兼容 @linxin666/dsh-pet(data-dsh-pet-root)与官方 dsh-pet(.dsh-pet-root) */
1084
- html[data-gufa-ide] .dsh-pet-root{z-index:2147483400!important}
1085
- /* 古法自己的弹窗/菜单打开时:整个覆盖层临时抬到右侧停靠会话(z-index 2147483040)之上 */
1086
- html[data-gufa-popup] .gufa-overlay-root{z-index:2147483300!important}
1087
- /* 外部弹窗(DSH 设置等)打开时:停靠会话与桌宠降到弹窗之下,保证弹窗完整可点可关 */
1108
+ html[data-gufa-ide] .dsh-pet-root{z-index:2147483050!important}
1109
+ /* 外部弹窗(DSH 设置等)打开时:停靠会话、古法 portal、桌宠全部降到官方弹窗之下 */
1088
1110
  html[data-gufa-external-modal] [data-gufa-dock]{z-index:2!important}
1111
+ html[data-gufa-external-modal] #gufa-portal-root{z-index:1!important}
1089
1112
  html[data-gufa-external-modal] .dsh-pet-root{z-index:2!important}
1090
1113
  html[data-gufa-external-modal] div[data-dsh-pet-root]{z-index:2!important}
1091
1114
  /* 桌宠(@linxin666/dsh-pet)在 IDE 模式提到覆盖层之上,避免被古法编程面板遮住 */
1092
- html[data-gufa-ide] div[data-dsh-pet-root]{position:fixed;inset:0;z-index:2147483400;pointer-events:none}
1115
+ html[data-gufa-ide] div[data-dsh-pet-root]{position:fixed;inset:0;z-index:2147483050;pointer-events:none}
1093
1116
  html[data-gufa-ide] div[data-dsh-pet-root] *{pointer-events:auto}
1094
1117
  .gufa-backdrop-page{position:absolute;inset:0;transition:background-color 200ms linear}
1095
1118
  .gufa-backdrop-glow{position:absolute;inset:0;pointer-events:none;
@@ -1151,7 +1174,10 @@ html[data-gufa-ide] [role="dialog"] {
1151
1174
  .gufa-session-group[data-current] .gufa-session-group-head{color:var(--gufa-text)}
1152
1175
  .gufa-session-group .gufa-session-row{margin-left:10px}
1153
1176
  /* 官方同款 body 级 portal 容器(弹窗渲染到这里,天然在停靠会话之上) */
1154
- #gufa-portal-root{position:fixed;inset:0;z-index:2147483350;pointer-events:none}
1177
+ /* 层级契约(见 docs/KNOWN-PITFALLS.md):
1178
+ 1 = 古法覆盖层;2147483040 = 右侧停靠会话;2147483060 = 官方 DSH 菜单/弹窗;
1179
+ 2147483100 = 古法 portal(所有弹窗/菜单);2147483200 = toast。 */
1180
+ #gufa-portal-root{position:fixed;inset:0;z-index:2147483100;pointer-events:none}
1155
1181
  #gufa-portal-root>*{pointer-events:auto}
1156
1182
  /* 摸鱼模式:App 窗口选择器 */
1157
1183
  .gufa-float-backdrop{position:fixed;inset:0;z-index:2147483200;display:flex;align-items:center;justify-content:center;
@@ -6805,7 +6831,7 @@ G.Explorer = function Explorer(props) {
6805
6831
  setMenu(null)
6806
6832
  fn()
6807
6833
  }
6808
- return h(
6834
+ return G.portal(h(
6809
6835
  'div',
6810
6836
  {
6811
6837
  className: 'gufa-menu',
@@ -6838,7 +6864,7 @@ G.Explorer = function Explorer(props) {
6838
6864
  single
6839
6865
  ? h(G.MenuItem, { icon: 'trash', label: t('explorer.delete'), danger: true, onClick: closeAnd(() => props.onDelete?.(menu.path)) })
6840
6866
  : null,
6841
- )
6867
+ ))
6842
6868
  })(),
6843
6869
  )
6844
6870
  }
@@ -7228,7 +7254,7 @@ G.GitPanel = function GitPanel(props) {
7228
7254
  ),
7229
7255
  headMenu === null
7230
7256
  ? null
7231
- : h(
7257
+ : G.portal(h(
7232
7258
  'div',
7233
7259
  {
7234
7260
  className: 'gufa-menu',
@@ -7267,10 +7293,10 @@ G.GitPanel = function GitPanel(props) {
7267
7293
  label: t('graph.open'),
7268
7294
  onClick: menuAction(() => props.onOpenGraph?.()),
7269
7295
  }),
7270
- ),
7296
+ )),
7271
7297
  rowMenu === null
7272
7298
  ? null
7273
- : h(
7299
+ : G.portal(h(
7274
7300
  'div',
7275
7301
  {
7276
7302
  className: 'gufa-menu',
@@ -7298,7 +7324,7 @@ G.GitPanel = function GitPanel(props) {
7298
7324
  })
7299
7325
  : null,
7300
7326
  h(G.MenuItem, { icon: 'files', label: t('git.copyPath'), onClick: menuAction(() => copyText(rowMenu.change.path)) }),
7301
- ),
7327
+ )),
7302
7328
  )
7303
7329
  }
7304
7330
 
@@ -8094,7 +8120,9 @@ G.installDialogLayerWatch = function installDialogLayerWatch(ctx) {
8094
8120
  const isExternalDialog = (el) => {
8095
8121
  if (!(el instanceof Element) || typeof el.closest !== 'function') return false
8096
8122
  if (el.getAttribute('role') !== 'dialog' || el.getAttribute('aria-modal') !== 'true') return false
8097
- return el.closest('.gufa-root') === null
8123
+ if (el.closest('.gufa-root') !== null) return false
8124
+ if (el.closest('#gufa-portal-root') !== null) return false
8125
+ return true
8098
8126
  }
8099
8127
 
8100
8128
  const sync = () => {
@@ -8138,95 +8166,6 @@ G.installDialogLayerWatch = function installDialogLayerWatch(ctx) {
8138
8166
  }
8139
8167
 
8140
8168
 
8141
- /* ─────────────────────── 59-popup-layer.js ─────────────────────── */
8142
- /**
8143
- * 59-popup-layer:弹窗/菜单层级修正。
8144
- *
8145
- * 背景(踩坑记录,见 docs/KNOWN-PITFALLS.md):
8146
- * 古法编程的界面都在 `.gufa-overlay-root`(z-index:1)这个层叠上下文里,而右侧停靠的
8147
- * 官方会话根节点是 body 级 fixed、z-index 2147483040。任何在 root 内部弹出的
8148
- * modal / menu / popover,如果只设自己的 z-index,都会被困在 root 的上下文里,
8149
- * 被停靠会话盖住(表现为「弹窗被右侧聊天挡一半、点不到关闭」)。
8150
- *
8151
- * 修法:检测 root 内是否出现 popup,出现时给 html 加 `data-gufa-popup`,
8152
- * 由 CSS 把整个 overlay root 临时抬到停靠会话之上。
8153
- */
8154
-
8155
- G.installPopupLayerWatch = function installPopupLayerWatch(ctx) {
8156
- if (typeof document === 'undefined') return () => {}
8157
- const ATTR = 'data-gufa-popup'
8158
- const SELECTOR = [
8159
- '[role="menu"]',
8160
- '[role="listbox"]',
8161
- '[role="dialog"]',
8162
- '.gufa-modal',
8163
- '.gufa-float-backdrop',
8164
- '.gufa-branch-dialog-backdrop',
8165
- '.gufa-session-backdrop',
8166
- '.gufa-shortcuts-backdrop',
8167
- ].join(',')
8168
-
8169
- /* 真正需要抬的是 overlay root 的 body 级祖先:它和停靠会话都是 body 的直接子节点,
8170
- 在祖先上设 z-index 才能压过会话的 2147483040;只抬 .gufa-overlay-root 出不了
8171
- 它自己的层叠上下文。 */
8172
- let liftedNodes = []
8173
- const lift = (open) => {
8174
- try {
8175
- if (open) {
8176
- if (liftedNodes.length > 0) return
8177
- let node = document.querySelector('.gufa-overlay-root')
8178
- if (node === null) return
8179
- while (node.parentElement !== null && node.parentElement !== document.body) node = node.parentElement
8180
- if (node.parentElement !== document.body) return
8181
- const saved = { node, zIndex: node.style.zIndex, position: node.style.position }
8182
- node.style.zIndex = '2147483300'
8183
- if (window.getComputedStyle(node).position === 'static') node.style.position = 'relative'
8184
- liftedNodes.push(saved)
8185
- if (node.parentElement !== document.body) return
8186
- } else {
8187
- for (const saved of liftedNodes) {
8188
- saved.node.style.zIndex = saved.zIndex
8189
- saved.node.style.position = saved.position
8190
- }
8191
- liftedNodes = []
8192
- }
8193
- } catch {
8194
- /* ignore */
8195
- }
8196
- }
8197
-
8198
- const sync = () => {
8199
- try {
8200
- const root = document.querySelector('.gufa-root')
8201
- const popup = root === null ? null : root.querySelector(SELECTOR)
8202
- const open = popup !== null
8203
- lift(open)
8204
- if ((document.documentElement.getAttribute(ATTR) === '1') === open) return
8205
- if (open) document.documentElement.setAttribute(ATTR, '1')
8206
- else document.documentElement.removeAttribute(ATTR)
8207
- } catch {
8208
- /* ignore */
8209
- }
8210
- }
8211
-
8212
- let observer = null
8213
- try {
8214
- observer = new MutationObserver(sync)
8215
- observer.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style', 'data-state', 'aria-hidden'] })
8216
- } catch {
8217
- observer = null
8218
- }
8219
- sync()
8220
- const timer = window.setInterval(sync, 400)
8221
- return () => {
8222
- if (observer !== null) observer.disconnect()
8223
- window.clearInterval(timer)
8224
- lift(false)
8225
- document.documentElement.removeAttribute(ATTR)
8226
- }
8227
- }
8228
-
8229
-
8230
8169
  /* ─────────────────────── 60-dock.js ─────────────────────── */
8231
8170
  /**
8232
8171
  * 60-dock:把官方 DSH 会话面板「停靠」进古法编程右侧的 AI 栏。
@@ -9670,7 +9609,9 @@ G.GufaApp = function GufaApp(props) {
9670
9609
  bootedRef.current = true
9671
9610
  let cancelled = false
9672
9611
  void (async () => {
9673
- const saved = readJson(GUFA_LS.project)
9612
+ const saved = floatState !== null && floatState !== undefined && typeof floatState.projectRoot === 'string' && floatState.projectRoot !== ''
9613
+ ? floatState.projectRoot
9614
+ : readJson(GUFA_LS.project)
9674
9615
  if (typeof saved === 'string' && saved !== '') {
9675
9616
  try {
9676
9617
  const value = await G.api('project.open', { path: saved })
@@ -9680,6 +9621,7 @@ G.GufaApp = function GufaApp(props) {
9680
9621
  writeJson(GUFA_LS.project, null)
9681
9622
  }
9682
9623
  }
9624
+ if (G.floatState !== null && G.floatState !== undefined) return
9683
9625
  if (typeof sessionCwd === 'string' && sessionCwd !== '') {
9684
9626
  try {
9685
9627
  const value = await G.api('project.open', { path: sessionCwd })
@@ -10288,12 +10230,23 @@ G.GufaApp = function GufaApp(props) {
10288
10230
  if (floatBusy || target === null || target === undefined) return
10289
10231
  setFloatBusy(true)
10290
10232
  try {
10233
+ const floatState = {
10234
+ projectRoot: project !== null && typeof project.root === 'string' ? project.root : null,
10235
+ activePath: typeof activePath === 'string' ? activePath : null,
10236
+ tabs: tabs
10237
+ .filter((tab) => tab !== null && tab !== undefined && typeof tab.path === 'string' && tab.kind !== 'diff' && tab.kind !== 'graph')
10238
+ .map((tab) => tab.path)
10239
+ .slice(0, 10),
10240
+ ui: readJson(GUFA_LS.ui) ?? {},
10241
+ sessionId: typeof currentSessionId === 'string' ? currentSessionId : null,
10242
+ }
10291
10243
  const value = await G.api('float.start', {
10292
10244
  follow: {
10293
10245
  pid: Number(target.pid) || 0,
10294
10246
  bundleId: typeof target.bundleId === 'string' ? target.bundleId : '',
10295
10247
  title: '',
10296
10248
  },
10249
+ state: floatState,
10297
10250
  })
10298
10251
  setFloatRunning(value?.running === true)
10299
10252
  setFloatPicker(null)
@@ -10333,6 +10286,7 @@ G.GufaApp = function GufaApp(props) {
10333
10286
  const dockSyncRef = useRef('')
10334
10287
  useEffect(() => {
10335
10288
  if (sessionPinnedRef.current === true) return undefined
10289
+ if (G.floatState !== null && G.floatState !== undefined) return undefined
10336
10290
  if (project === null || typeof project.root !== 'string' || project.root === '') return undefined
10337
10291
  if (dockSyncRef.current === project.root) return undefined
10338
10292
  if (typeof sessionCwd === 'string' && sessionCwd !== '' && sessionCwd === project.root) {
@@ -10377,6 +10331,11 @@ G.GufaApp = function GufaApp(props) {
10377
10331
  const entryWorkspaceSyncedRef = useRef(false)
10378
10332
  useEffect(() => {
10379
10333
  if (entryWorkspaceSyncedRef.current) return
10334
+ /* 悬浮层:项目/会话由主窗口状态注入,不做「入口工作区」同步,避免被会话 cwd 覆盖 */
10335
+ if (G.floatState !== null && G.floatState !== undefined) {
10336
+ entryWorkspaceSyncedRef.current = true
10337
+ return
10338
+ }
10380
10339
  if (project === null) return
10381
10340
  const pending = G.pendingEntryWorkspace
10382
10341
  if (pending === null || pending === undefined || typeof pending.key !== 'string') {
@@ -10695,6 +10654,11 @@ G.GufaApp = function GufaApp(props) {
10695
10654
  const lastSessionCwdRef = useRef(null)
10696
10655
  useEffect(() => {
10697
10656
  if (sessionPinnedRef.current === true) return
10657
+ /* 悬浮层里项目由主窗口状态注入,不跟随(Web 版客户端自己的)会话 cwd 切换 */
10658
+ if (G.floatState !== null && G.floatState !== undefined) {
10659
+ lastSessionCwdRef.current = sessionCwd
10660
+ return
10661
+ }
10698
10662
  if (typeof sessionCwd !== 'string' || sessionCwd === '') return
10699
10663
  const previous = lastSessionCwdRef.current
10700
10664
  lastSessionCwdRef.current = sessionCwd
@@ -10889,6 +10853,38 @@ G.GufaApp = function GufaApp(props) {
10889
10853
  })()
10890
10854
  }, [project])
10891
10855
 
10856
+ /* 悬浮窗:恢复主窗口带过来的打开的标签页(只恢复普通文件,diff/graph 跳过) */
10857
+ const floatTabsRestoredRef = useRef(false)
10858
+ useEffect(() => {
10859
+ if (floatTabsRestoredRef.current) return
10860
+ if (project === null) return
10861
+ let state = null
10862
+ state = G.floatState
10863
+ if (state === null || typeof state !== 'object') return
10864
+ floatTabsRestoredRef.current = true
10865
+ const paths = Array.isArray(state.tabs) ? state.tabs.filter((path) => typeof path === 'string' && path !== '') : []
10866
+ const ordered = paths.filter((path) => path !== state.activePath).concat(
10867
+ typeof state.activePath === 'string' && state.activePath !== '' ? [state.activePath] : [],
10868
+ )
10869
+ void (async () => {
10870
+ for (const path of ordered.slice(0, 10)) {
10871
+ try {
10872
+ await openFile(path)
10873
+ } catch {
10874
+ /* 文件可能已被删除,忽略 */
10875
+ }
10876
+ }
10877
+ })()
10878
+ }, [project, openFile])
10879
+
10880
+ /* 悬浮层恢复入口:主窗口把状态带过来后,由 99-main 显式调用(不依赖 App 自己的启动时序) */
10881
+ useEffect(() => {
10882
+ G.floatRestoreRef = { applyProject, openFile }
10883
+ return () => {
10884
+ if (G.floatRestoreRef !== undefined && G.floatRestoreRef !== null && G.floatRestoreRef.applyProject === applyProject) G.floatRestoreRef = null
10885
+ }
10886
+ }, [applyProject, openFile])
10887
+
10892
10888
  const activeFileForChat = useMemo(() => {
10893
10889
  if (activeTab === null || activeTab.kind === 'diff' || activeTab.kind === 'graph') return null
10894
10890
  return { path: activeTab.path, content: activeTab.content ?? '', language: activeTab.language }
@@ -12247,6 +12243,67 @@ const GUFA_PLUGIN = {
12247
12243
  if (document.querySelector('.gufa-overlay-root') !== null || attempts > 40) {
12248
12244
  document.documentElement.removeAttribute('data-gufa-booting')
12249
12245
  window.clearInterval(timer)
12246
+ /* 显式恢复主窗口状态(项目 → 标签页 → 会话),避免依赖 Web 版 App 的启动时序 */
12247
+ try {
12248
+ if (window.__gufaFloatRestored !== true && G.floatState !== null && G.floatState !== undefined) {
12249
+ window.__gufaFloatRestored = true
12250
+ void (async () => {
12251
+ const state = G.floatState
12252
+ let projectValue = null
12253
+ if (typeof state.projectRoot === 'string' && state.projectRoot !== '') {
12254
+ try {
12255
+ projectValue = await G.api('project.open', { path: state.projectRoot })
12256
+ } catch {
12257
+ projectValue = null
12258
+ }
12259
+ }
12260
+ const restore = G.floatRestoreRef
12261
+ if (projectValue !== null && restore !== null && restore !== undefined && typeof restore.applyProject === 'function') {
12262
+ restore.applyProject(projectValue)
12263
+ }
12264
+ await new Promise((resolve) => setTimeout(resolve, 350))
12265
+ const paths = Array.isArray(state.tabs) ? state.tabs.filter((path) => typeof path === 'string' && path !== '') : []
12266
+ const ordered = typeof state.activePath === 'string' && state.activePath !== ''
12267
+ ? paths.filter((path) => path !== state.activePath).concat([state.activePath])
12268
+ : paths
12269
+ const openFile = restore !== null && restore !== undefined && typeof restore.openFile === 'function' ? restore.openFile : null
12270
+ if (openFile !== null) {
12271
+ for (const path of ordered.slice(0, 10)) {
12272
+ try {
12273
+ await openFile(path)
12274
+ } catch {
12275
+ /* ignore */
12276
+ }
12277
+ }
12278
+ }
12279
+ if (typeof state.sessionId === 'string' && state.sessionId !== '') {
12280
+ try {
12281
+ const workspace = G.ctx === null || G.ctx === undefined ? undefined : G.ctx.get('uiWorkspace')
12282
+ if (workspace !== undefined && workspace !== null && typeof workspace.openSession === 'function') workspace.openSession(state.sessionId)
12283
+ } catch {
12284
+ /* ignore */
12285
+ }
12286
+ }
12287
+ })()
12288
+ }
12289
+ } catch {
12290
+ /* ignore */
12291
+ }
12292
+ /* 恢复主窗口带过来的当前会话(悬浮窗是独立客户端实例,需要主动选中同一会话) */
12293
+ try {
12294
+ const parsedState = G.floatState
12295
+ const sessionId = parsedState !== null && typeof parsedState.sessionId === 'string' ? parsedState.sessionId : ''
12296
+ if (sessionId !== '') {
12297
+ const workspace = G.ctx === null || G.ctx === undefined ? undefined : G.ctx.get('uiWorkspace')
12298
+ if (workspace !== undefined && workspace !== null && typeof workspace.openSession === 'function') {
12299
+ window.setTimeout(() => {
12300
+ try { workspace.openSession(sessionId) } catch { /* ignore */ }
12301
+ }, 600)
12302
+ }
12303
+ }
12304
+ } catch {
12305
+ /* ignore */
12306
+ }
12250
12307
  }
12251
12308
  }, 250)
12252
12309
  ctx.effect(() => () => {
@@ -12296,16 +12353,6 @@ const GUFA_PLUGIN = {
12296
12353
  window.setTimeout(cleanStaleSidebarLayouts, 1500)
12297
12354
  window.setTimeout(cleanStaleSidebarLayouts, 5000)
12298
12355
 
12299
- // 弹窗/菜单打开时把覆盖层整体抬到右侧停靠会话之上(否则会被盖住)
12300
- try {
12301
- if (typeof G.installPopupLayerWatch === 'function') {
12302
- const disposePopupWatch = G.installPopupLayerWatch(ctx)
12303
- if (typeof disposePopupWatch === 'function') ctx.effect(() => disposePopupWatch, 'gufa-code: popup layer watch')
12304
- }
12305
- } catch (error) {
12306
- console.error('[gufa-code] 安装弹窗层级监听失败', error)
12307
- }
12308
-
12309
12356
  // 外部弹窗(设置等)打开时降低停靠层,避免遮挡弹窗导致关不掉
12310
12357
  try {
12311
12358
  if (typeof G.installDialogLayerWatch === 'function') {
@@ -244,10 +244,12 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
244
244
  self.window.opaque = NO;
245
245
  self.window.backgroundColor = NSColor.clearColor;
246
246
  self.window.hasShadow = YES;
247
- self.window.level = NSFloatingWindowLevel + 1;
247
+ /* 高于普通浮动窗口(歌词、置顶小工具等),确保真正盖住被跟随的 App */
248
+ self.window.level = NSStatusWindowLevel;
248
249
  self.window.collectionBehavior = (NSWindowCollectionBehaviorCanJoinAllSpaces
249
250
  | NSWindowCollectionBehaviorFullScreenAuxiliary
250
- | NSWindowCollectionBehaviorStationary);
251
+ | NSWindowCollectionBehaviorStationary
252
+ | NSWindowCollectionBehaviorIgnoresCycle);
251
253
  self.window.minSize = NSMakeSize(520, 360);
252
254
  self.window.title = @"古法编程悬浮层";
253
255
 
@@ -257,8 +259,39 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
257
259
 
258
260
  WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
259
261
  config.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
260
- /* URL query 可能被 DSH 的浏览器端路由吞掉,这里在文档开始前写一个标记,客户端据此进入悬浮模式 */
261
- WKUserScript *floatMarker = [[WKUserScript alloc] initWithSource:@"try{localStorage.setItem('gufa-code:float','1')}catch(e){}"
262
+ /* 文档开始前注入:悬浮模式标记 + 主窗口状态(项目/UI 布局/打开的标签页/会话),
263
+ 让悬浮窗里的古法编程「接着主窗口继续干活」,而不是一个空白的新实例。 */
264
+ NSMutableString *bootstrap = [NSMutableString stringWithString:@"try{localStorage.setItem('gufa-code:float','1');"];
265
+ NSDictionary *state = [self.options[@"state"] isKindOfClass:[NSDictionary class]] ? self.options[@"state"] : nil;
266
+ if (state != nil) {
267
+ NSData *stateJson = [NSJSONSerialization dataWithJSONObject:state options:0 error:NULL];
268
+ if (stateJson != nil) {
269
+ NSString *stateText = [[NSString alloc] initWithData:stateJson encoding:NSUTF8StringEncoding];
270
+ NSString *escaped = [stateText stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
271
+ escaped = [escaped stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
272
+ escaped = [escaped stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
273
+ escaped = [escaped stringByReplacingOccurrencesOfString:@"\r" withString:@""];
274
+ [bootstrap appendFormat:@"localStorage.setItem('gufa-code:float-state','%@');", escaped];
275
+ NSString *projectRoot = [state[@"projectRoot"] isKindOfClass:[NSString class]] ? state[@"projectRoot"] : @"";
276
+ if (projectRoot.length > 0) {
277
+ NSString *projectEscaped = [projectRoot stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
278
+ projectEscaped = [projectEscaped stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
279
+ [bootstrap appendFormat:@"localStorage.setItem('gufa-code:project','\"%@\"');", projectEscaped];
280
+ }
281
+ NSDictionary *ui = [state[@"ui"] isKindOfClass:[NSDictionary class]] ? state[@"ui"] : nil;
282
+ if (ui != nil) {
283
+ NSData *uiJson = [NSJSONSerialization dataWithJSONObject:ui options:0 error:NULL];
284
+ if (uiJson != nil) {
285
+ NSString *uiText = [[NSString alloc] initWithData:uiJson encoding:NSUTF8StringEncoding];
286
+ NSString *uiEscaped = [uiText stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
287
+ uiEscaped = [uiEscaped stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
288
+ [bootstrap appendFormat:@"localStorage.setItem('gufa-code:ui:v1','%@');", uiEscaped];
289
+ }
290
+ }
291
+ }
292
+ }
293
+ [bootstrap appendString:@"}catch(e){}"];
294
+ WKUserScript *floatMarker = [[WKUserScript alloc] initWithSource:bootstrap
262
295
  injectionTime:WKUserScriptInjectionTimeAtDocumentStart
263
296
  forMainFrameOnly:YES];
264
297
  [config.userContentController addUserScript:floatMarker];
@@ -317,14 +350,28 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
317
350
  self.navigation.pendingLoad = ^{
318
351
  __strong typeof(weakSelf) self = weakSelf;
319
352
  if (self == nil) return;
320
- NSURLComponents *components = [NSURLComponents componentsWithString:appUrl];
321
- components.queryItems = @[[NSURLQueryItem queryItemWithName:@"gufa-float" value:@"1"]];
322
- NSURL *clean = components.URL;
323
- if (clean != nil && ![clean.absoluteString isEqualToString:self.webView.URL.absoluteString]) {
324
- [self.webView loadRequest:[NSURLRequest requestWithURL:clean]];
353
+ /* 鉴权 cookie 已在 token URL 重定向时写入;这里用 JS 写 localStorage(避开 URL 长度/转义问题),
354
+ 再跳到带 gufa-float 标记的干净地址,客户端据此自动进入摸鱼模式并恢复状态。 */
355
+ NSMutableString *js = [NSMutableString stringWithString:@"try{"];
356
+ NSDictionary *state = [self.options[@"state"] isKindOfClass:[NSDictionary class]] ? self.options[@"state"] : nil;
357
+ if (state != nil) {
358
+ NSData *stateJson = [NSJSONSerialization dataWithJSONObject:state options:0 error:NULL];
359
+ NSString *stateText = stateJson != nil ? [[NSString alloc] initWithData:stateJson encoding:NSUTF8StringEncoding] : @"";
360
+ if (stateText.length > 0) {
361
+ NSData *quoted = [NSJSONSerialization dataWithJSONObject:@[stateText] options:0 error:NULL];
362
+ NSString *arrayLiteral = quoted != nil ? [[NSString alloc] initWithData:quoted encoding:NSUTF8StringEncoding] : @"[]";
363
+ /* arrayLiteral = ["{\"projectRoot\":...}"] */
364
+ if (arrayLiteral.length >= 2) {
365
+ NSString *stateLiteral = [arrayLiteral substringWithRange:NSMakeRange(1, arrayLiteral.length - 2)];
366
+ [js appendFormat:@"localStorage.setItem('gufa-code:float-state', %@);", stateLiteral];
367
+ }
368
+ }
325
369
  }
370
+ [js appendString:@"localStorage.setItem('gufa-code:float','1');location.href='/?gufa-float=1';}catch(e){}"];
371
+ [self.webView evaluateJavaScript:js completionHandler:nil];
326
372
  };
327
373
  [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
374
+
328
375
  }
329
376
 
330
377
  - (void)saveFrame {
@@ -386,8 +433,12 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
386
433
  self.statusItem.button.title = self.clickThrough ? @"古法·穿透" : @"古法";
387
434
  }
388
435
 
436
+ /* 只重启定时器,不动 following/窗口引用(stopFollowTimer 会清状态,不能在这里调) */
389
437
  - (void)startFollowTimer {
390
- [self stopFollowTimer];
438
+ if (self.followTimer != nil) {
439
+ [self.followTimer invalidate];
440
+ self.followTimer = nil;
441
+ }
391
442
  __weak typeof(self) weakSelf = self;
392
443
  self.followTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 repeats:YES block:^(NSTimer *timer) {
393
444
  __strong typeof(weakSelf) self = weakSelf;
@@ -427,7 +478,18 @@ static OSStatus GufaHotKeyHandler(EventHandlerCallRef nextHandler, EventRef even
427
478
  }
428
479
  CGFloat screenHeight = NSScreen.mainScreen.frame.size.height;
429
480
  NSRect frame = NSMakeRect(target.origin.x, screenHeight - target.origin.y - target.size.height, target.size.width, target.size.height);
481
+ static int applyLogCount = 0;
482
+ if (applyLogCount < 6) {
483
+ applyLogCount += 1;
484
+ GufaLog(@"follow-apply target=%.0f,%.0f %.0fx%.0f -> frame=%.0f,%.0f %.0fx%.0f", target.origin.x, target.origin.y, target.size.width, target.size.height, frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
485
+ }
430
486
  [self.window setFrame:frame display:YES animate:NO];
487
+ static int verifyLogCount = 0;
488
+ if (verifyLogCount < 3) {
489
+ verifyLogCount += 1;
490
+ NSRect actual = self.window.frame;
491
+ GufaLog(@"follow-applied actual=%.0f,%.0f %.0fx%.0f", actual.origin.x, actual.origin.y, actual.size.width, actual.size.height);
492
+ }
431
493
  }
432
494
 
433
495
  /* 全局快捷键 ⌃⌥⌘G(四键,避免冲突):穿透后窗口收不到鼠标事件,靠它切回来(Carbon 热键不需要辅助功能权限) */
@@ -2,25 +2,32 @@
2
2
 
3
3
  > 这份清单记录已经犯过、并且容易再犯的错误。加新功能前先扫一遍。
4
4
 
5
- ## 1. 古法编程里的浮层会被右侧停靠会话盖住(反复踩)
6
-
7
- **现象**:弹窗/菜单只显示一半;关闭按钮点不到;列表被右侧「古法 AI」聊天栏压住;点击穿透的弹窗挡在聊天栏下面。
8
-
9
- **原因**:
10
- - 古法编程的所有界面都在 `.gufa-overlay-root` 里,它是 `z-index:1` 的层叠上下文;
11
- - 右侧停靠的官方会话根节点是 **body 级** `position:fixed; z-index:2147483040`;
12
- - 所以 root 内弹窗自己的 `z-index` 写多大都没用,出不了 root 的层叠上下文,必然被会话盖住。
13
-
14
- **正确做法(三选一)**:
15
- 1. 古法自己的弹窗/菜单:打开时把 root 抬起来 —— 现有 `59-popup-layer.js` 会自动检测 root 内出现的
16
- `[role=menu] / [role=dialog] / [role=listbox] / .gufa-modal / .gufa-float-backdrop` 等,
17
- 给 `<html>` 加 `data-gufa-popup`,CSS 把 `.gufa-overlay-root` 抬到 `z-index:2147483300`;
18
- 新增全屏浮层时**必须**把它的类名加进 `59-popup-layer.js` 的 SELECTOR。
19
- 2. 外部(DSH 官方)弹窗:`59-dialog-layer.js` 检测 body 级 `[role="dialog"][aria-modal="true"]`,
20
- 加 `data-gufa-external-modal`,CSS 把停靠会话降到 `z-index:2`。
21
- 3. 任何新浮层提交前:**在有右侧会话打开的情况下**实测它是否完整可见、可关闭。
22
-
23
- **禁止**:只在浮层自身上写一个很高的 `z-index` 就交付。
5
+ ## 1. 弹窗/菜单层级:只有一个正确做法(反复踩过多次,最终定案)
6
+
7
+ **现象**:弹窗被右侧「古法 AI」聊天栏压住、只显示一半、关闭按钮点不到;或者反过来把官方设置/会话弹窗盖住。
8
+
9
+ **根因**:古法编程界面在 `.gufa-overlay-root`(z-index:1)这个层叠上下文里;右侧停靠的官方会话根节点是 body 级
10
+ `position:fixed; z-index:2147483040`。任何渲染在 root 内部的弹窗/菜单,z-index 写多大都出不了 root 的上下文,
11
+ 必然被会话盖住。
12
+
13
+ **定案(不要再发明新写法)**:
14
+
15
+ 1. 所有浮层(modal / 菜单 / 列表 / 确认框)一律用 `G.portal(node)` 渲染到 `#gufa-portal-root`(body 级,官方同款 portal 机制);
16
+ - 新增浮层时**必须**用 `G.portal(...)` 包裹,否则一定踩坑;
17
+ - 模块内联菜单(`50-panels.js` 的 `.gufa-menu`)已经全部 portal,坐标本来就是 client 坐标 + `position:fixed`。
18
+ 2. 层级契约(见 `00-core.js` 注释,按顺序递增):
19
+ - `1`:`.gufa-overlay-root`(IDE 本体)
20
+ - `2147483040`:右侧停靠会话
21
+ - `2147483050`:桌宠
22
+ - `2147483060`:官方 DSH 菜单/弹窗(`html[data-gufa-ide] [role=...]` 规则)
23
+ - `2147483100`:`#gufa-portal-root`(古法所有弹窗/菜单)
24
+ - `2147483200`:toast
25
+ 3. 官方弹窗打开时(`59-dialog-layer.js` 检测 body 级 `[role=dialog][aria-modal=true]`):
26
+ `html[data-gufa-external-modal]` 把停靠会话降到 2、`#gufa-portal-root` 降到 1,保证官方弹窗永远在最上面。
27
+ 4. **禁止**:抬高 `.gufa-overlay-root` 或它的祖先来做层级修正(历史补丁,已删除,最容易造成“弹窗层级混乱”)。
28
+
29
+ **验收方式**:改完浮层后,在有右侧会话、且 IDE 里有打开文件的情况下,逐一点一遍:
30
+ 资源管理器右键菜单 / Git 菜单 / 摸鱼列表 / 切换会话 / 工作区切换 / 设置弹窗,确认都能完整显示、能点、能关。
24
31
 
25
32
  ## 2. macOS 屏幕录制权限 + 悬浮层 helper
26
33
 
package/host/dev.js CHANGED
@@ -45,6 +45,7 @@ export function register(router, env) {
45
45
  const startedAt = new Date().toISOString()
46
46
  const ctx = env?.ctx ?? router.ctx
47
47
 
48
+
48
49
  /* ── 桌面悬浮层 / 摸鱼模式(macOS) ────────────────────────────────
49
50
  * 通过 .app bundle 启动 helper:TCC(屏幕录制)按 bundle 身份授权,
50
51
  * 用户在「系统设置 → 隐私与安全性 → 屏幕录制」里添加「古法编程悬浮层.app」即可。
@@ -212,7 +213,13 @@ export function register(router, env) {
212
213
  }
213
214
  const base = `${webServer.protocol}//127.0.0.1:${String(webServer.port)}`
214
215
  const authUrl = connection.authenticatedUrl(`${base}/`)
215
- const payload = JSON.stringify({ authUrl, appUrl: `${base}/`, follow: follow !== null && follow.pid > 0 ? follow : undefined })
216
+ const state = body !== null && typeof body === 'object' && body.state !== null && typeof body.state === 'object' ? body.state : null
217
+ const payload = JSON.stringify({
218
+ authUrl,
219
+ appUrl: `${base}/`,
220
+ follow: follow !== null && follow.pid > 0 ? follow : undefined,
221
+ state: state ?? undefined,
222
+ })
216
223
  const binary = floatBinaryPath()
217
224
  if (binary === null) throw new ApiError('missing-helper', '悬浮层程序未构建', 500)
218
225
  const child = spawn(binary, [payload], { detached: true, stdio: 'ignore' })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-gufa-code",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "古法编程 · AI 辅助:一键把 DSH 主面板变成 VS Code 风格 IDE(Monaco 编辑器、文件树、Git、SSH、真实终端 + 右侧官方会话);主张亲手写、每个改动心中有数,AI 是助理、你是第一责任人",
5
5
  "icon": "icon.svg",
6
6
  "keywords": [
@@ -38,6 +38,30 @@ G.portal = (function createPortalHelper() {
38
38
  })()
39
39
  G.hooks = { useState, useEffect, useRef, useMemo, useCallback, useLayoutEffect }
40
40
 
41
+ /* ── 悬浮层状态:helper 通过 URL 参数 ?gufa-state=<base64url(JSON)> 传主窗口快照 ── */
42
+ G.floatState = (function readFloatState() {
43
+ if (typeof window === 'undefined' || typeof document === 'undefined') return null
44
+ try {
45
+ const raw = new URLSearchParams(window.location.search).get('gufa-state')
46
+ if (raw === null || raw === '') {
47
+ try {
48
+ const stored = localStorage.getItem('gufa-code:float-state')
49
+ return stored === null ? null : JSON.parse(stored)
50
+ } catch {
51
+ return null
52
+ }
53
+ }
54
+ const base64 = raw.replace(/-/g, '+').replace(/_/g, '/')
55
+ const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4)
56
+ const binary = atob(padded)
57
+ const bytes = Uint8Array.from(binary, (chunk) => chunk.charCodeAt(0))
58
+ const text = new TextDecoder().decode(bytes)
59
+ return JSON.parse(text)
60
+ } catch {
61
+ return null
62
+ }
63
+ })()
64
+
41
65
  /* ── 事件总线(零件之间解耦:SSH → 终端 发送命令等) ───────────────────── */
42
66
  G.bus = (function createBus() {
43
67
  const listeners = new Map()
@@ -1073,15 +1097,14 @@ html[data-gufa-float] #root [data-gufa-dock],
1073
1097
  html[data-gufa-float] #root .gufa-overlay-root *,
1074
1098
  html[data-gufa-float] #root [data-gufa-dock] *{visibility:visible!important}
1075
1099
  /* 桌宠提到覆盖层之上:同时兼容 @linxin666/dsh-pet(data-dsh-pet-root)与官方 dsh-pet(.dsh-pet-root) */
1076
- html[data-gufa-ide] .dsh-pet-root{z-index:2147483400!important}
1077
- /* 古法自己的弹窗/菜单打开时:整个覆盖层临时抬到右侧停靠会话(z-index 2147483040)之上 */
1078
- html[data-gufa-popup] .gufa-overlay-root{z-index:2147483300!important}
1079
- /* 外部弹窗(DSH 设置等)打开时:停靠会话与桌宠降到弹窗之下,保证弹窗完整可点可关 */
1100
+ html[data-gufa-ide] .dsh-pet-root{z-index:2147483050!important}
1101
+ /* 外部弹窗(DSH 设置等)打开时:停靠会话、古法 portal、桌宠全部降到官方弹窗之下 */
1080
1102
  html[data-gufa-external-modal] [data-gufa-dock]{z-index:2!important}
1103
+ html[data-gufa-external-modal] #gufa-portal-root{z-index:1!important}
1081
1104
  html[data-gufa-external-modal] .dsh-pet-root{z-index:2!important}
1082
1105
  html[data-gufa-external-modal] div[data-dsh-pet-root]{z-index:2!important}
1083
1106
  /* 桌宠(@linxin666/dsh-pet)在 IDE 模式提到覆盖层之上,避免被古法编程面板遮住 */
1084
- html[data-gufa-ide] div[data-dsh-pet-root]{position:fixed;inset:0;z-index:2147483400;pointer-events:none}
1107
+ html[data-gufa-ide] div[data-dsh-pet-root]{position:fixed;inset:0;z-index:2147483050;pointer-events:none}
1085
1108
  html[data-gufa-ide] div[data-dsh-pet-root] *{pointer-events:auto}
1086
1109
  .gufa-backdrop-page{position:absolute;inset:0;transition:background-color 200ms linear}
1087
1110
  .gufa-backdrop-glow{position:absolute;inset:0;pointer-events:none;
@@ -1143,7 +1166,10 @@ html[data-gufa-ide] [role="dialog"] {
1143
1166
  .gufa-session-group[data-current] .gufa-session-group-head{color:var(--gufa-text)}
1144
1167
  .gufa-session-group .gufa-session-row{margin-left:10px}
1145
1168
  /* 官方同款 body 级 portal 容器(弹窗渲染到这里,天然在停靠会话之上) */
1146
- #gufa-portal-root{position:fixed;inset:0;z-index:2147483350;pointer-events:none}
1169
+ /* 层级契约(见 docs/KNOWN-PITFALLS.md):
1170
+ 1 = 古法覆盖层;2147483040 = 右侧停靠会话;2147483060 = 官方 DSH 菜单/弹窗;
1171
+ 2147483100 = 古法 portal(所有弹窗/菜单);2147483200 = toast。 */
1172
+ #gufa-portal-root{position:fixed;inset:0;z-index:2147483100;pointer-events:none}
1147
1173
  #gufa-portal-root>*{pointer-events:auto}
1148
1174
  /* 摸鱼模式:App 窗口选择器 */
1149
1175
  .gufa-float-backdrop{position:fixed;inset:0;z-index:2147483200;display:flex;align-items:center;justify-content:center;
@@ -409,7 +409,7 @@ G.Explorer = function Explorer(props) {
409
409
  setMenu(null)
410
410
  fn()
411
411
  }
412
- return h(
412
+ return G.portal(h(
413
413
  'div',
414
414
  {
415
415
  className: 'gufa-menu',
@@ -442,7 +442,7 @@ G.Explorer = function Explorer(props) {
442
442
  single
443
443
  ? h(G.MenuItem, { icon: 'trash', label: t('explorer.delete'), danger: true, onClick: closeAnd(() => props.onDelete?.(menu.path)) })
444
444
  : null,
445
- )
445
+ ))
446
446
  })(),
447
447
  )
448
448
  }
@@ -832,7 +832,7 @@ G.GitPanel = function GitPanel(props) {
832
832
  ),
833
833
  headMenu === null
834
834
  ? null
835
- : h(
835
+ : G.portal(h(
836
836
  'div',
837
837
  {
838
838
  className: 'gufa-menu',
@@ -871,10 +871,10 @@ G.GitPanel = function GitPanel(props) {
871
871
  label: t('graph.open'),
872
872
  onClick: menuAction(() => props.onOpenGraph?.()),
873
873
  }),
874
- ),
874
+ )),
875
875
  rowMenu === null
876
876
  ? null
877
- : h(
877
+ : G.portal(h(
878
878
  'div',
879
879
  {
880
880
  className: 'gufa-menu',
@@ -902,7 +902,7 @@ G.GitPanel = function GitPanel(props) {
902
902
  })
903
903
  : null,
904
904
  h(G.MenuItem, { icon: 'files', label: t('git.copyPath'), onClick: menuAction(() => copyText(rowMenu.change.path)) }),
905
- ),
905
+ )),
906
906
  )
907
907
  }
908
908
 
@@ -13,7 +13,9 @@ G.installDialogLayerWatch = function installDialogLayerWatch(ctx) {
13
13
  const isExternalDialog = (el) => {
14
14
  if (!(el instanceof Element) || typeof el.closest !== 'function') return false
15
15
  if (el.getAttribute('role') !== 'dialog' || el.getAttribute('aria-modal') !== 'true') return false
16
- return el.closest('.gufa-root') === null
16
+ if (el.closest('.gufa-root') !== null) return false
17
+ if (el.closest('#gufa-portal-root') !== null) return false
18
+ return true
17
19
  }
18
20
 
19
21
  const sync = () => {
@@ -253,7 +253,9 @@ G.GufaApp = function GufaApp(props) {
253
253
  bootedRef.current = true
254
254
  let cancelled = false
255
255
  void (async () => {
256
- const saved = readJson(GUFA_LS.project)
256
+ const saved = floatState !== null && floatState !== undefined && typeof floatState.projectRoot === 'string' && floatState.projectRoot !== ''
257
+ ? floatState.projectRoot
258
+ : readJson(GUFA_LS.project)
257
259
  if (typeof saved === 'string' && saved !== '') {
258
260
  try {
259
261
  const value = await G.api('project.open', { path: saved })
@@ -263,6 +265,7 @@ G.GufaApp = function GufaApp(props) {
263
265
  writeJson(GUFA_LS.project, null)
264
266
  }
265
267
  }
268
+ if (G.floatState !== null && G.floatState !== undefined) return
266
269
  if (typeof sessionCwd === 'string' && sessionCwd !== '') {
267
270
  try {
268
271
  const value = await G.api('project.open', { path: sessionCwd })
@@ -871,12 +874,23 @@ G.GufaApp = function GufaApp(props) {
871
874
  if (floatBusy || target === null || target === undefined) return
872
875
  setFloatBusy(true)
873
876
  try {
877
+ const floatState = {
878
+ projectRoot: project !== null && typeof project.root === 'string' ? project.root : null,
879
+ activePath: typeof activePath === 'string' ? activePath : null,
880
+ tabs: tabs
881
+ .filter((tab) => tab !== null && tab !== undefined && typeof tab.path === 'string' && tab.kind !== 'diff' && tab.kind !== 'graph')
882
+ .map((tab) => tab.path)
883
+ .slice(0, 10),
884
+ ui: readJson(GUFA_LS.ui) ?? {},
885
+ sessionId: typeof currentSessionId === 'string' ? currentSessionId : null,
886
+ }
874
887
  const value = await G.api('float.start', {
875
888
  follow: {
876
889
  pid: Number(target.pid) || 0,
877
890
  bundleId: typeof target.bundleId === 'string' ? target.bundleId : '',
878
891
  title: '',
879
892
  },
893
+ state: floatState,
880
894
  })
881
895
  setFloatRunning(value?.running === true)
882
896
  setFloatPicker(null)
@@ -916,6 +930,7 @@ G.GufaApp = function GufaApp(props) {
916
930
  const dockSyncRef = useRef('')
917
931
  useEffect(() => {
918
932
  if (sessionPinnedRef.current === true) return undefined
933
+ if (G.floatState !== null && G.floatState !== undefined) return undefined
919
934
  if (project === null || typeof project.root !== 'string' || project.root === '') return undefined
920
935
  if (dockSyncRef.current === project.root) return undefined
921
936
  if (typeof sessionCwd === 'string' && sessionCwd !== '' && sessionCwd === project.root) {
@@ -960,6 +975,11 @@ G.GufaApp = function GufaApp(props) {
960
975
  const entryWorkspaceSyncedRef = useRef(false)
961
976
  useEffect(() => {
962
977
  if (entryWorkspaceSyncedRef.current) return
978
+ /* 悬浮层:项目/会话由主窗口状态注入,不做「入口工作区」同步,避免被会话 cwd 覆盖 */
979
+ if (G.floatState !== null && G.floatState !== undefined) {
980
+ entryWorkspaceSyncedRef.current = true
981
+ return
982
+ }
963
983
  if (project === null) return
964
984
  const pending = G.pendingEntryWorkspace
965
985
  if (pending === null || pending === undefined || typeof pending.key !== 'string') {
@@ -1278,6 +1298,11 @@ G.GufaApp = function GufaApp(props) {
1278
1298
  const lastSessionCwdRef = useRef(null)
1279
1299
  useEffect(() => {
1280
1300
  if (sessionPinnedRef.current === true) return
1301
+ /* 悬浮层里项目由主窗口状态注入,不跟随(Web 版客户端自己的)会话 cwd 切换 */
1302
+ if (G.floatState !== null && G.floatState !== undefined) {
1303
+ lastSessionCwdRef.current = sessionCwd
1304
+ return
1305
+ }
1281
1306
  if (typeof sessionCwd !== 'string' || sessionCwd === '') return
1282
1307
  const previous = lastSessionCwdRef.current
1283
1308
  lastSessionCwdRef.current = sessionCwd
@@ -1472,6 +1497,38 @@ G.GufaApp = function GufaApp(props) {
1472
1497
  })()
1473
1498
  }, [project])
1474
1499
 
1500
+ /* 悬浮窗:恢复主窗口带过来的打开的标签页(只恢复普通文件,diff/graph 跳过) */
1501
+ const floatTabsRestoredRef = useRef(false)
1502
+ useEffect(() => {
1503
+ if (floatTabsRestoredRef.current) return
1504
+ if (project === null) return
1505
+ let state = null
1506
+ state = G.floatState
1507
+ if (state === null || typeof state !== 'object') return
1508
+ floatTabsRestoredRef.current = true
1509
+ const paths = Array.isArray(state.tabs) ? state.tabs.filter((path) => typeof path === 'string' && path !== '') : []
1510
+ const ordered = paths.filter((path) => path !== state.activePath).concat(
1511
+ typeof state.activePath === 'string' && state.activePath !== '' ? [state.activePath] : [],
1512
+ )
1513
+ void (async () => {
1514
+ for (const path of ordered.slice(0, 10)) {
1515
+ try {
1516
+ await openFile(path)
1517
+ } catch {
1518
+ /* 文件可能已被删除,忽略 */
1519
+ }
1520
+ }
1521
+ })()
1522
+ }, [project, openFile])
1523
+
1524
+ /* 悬浮层恢复入口:主窗口把状态带过来后,由 99-main 显式调用(不依赖 App 自己的启动时序) */
1525
+ useEffect(() => {
1526
+ G.floatRestoreRef = { applyProject, openFile }
1527
+ return () => {
1528
+ if (G.floatRestoreRef !== undefined && G.floatRestoreRef !== null && G.floatRestoreRef.applyProject === applyProject) G.floatRestoreRef = null
1529
+ }
1530
+ }, [applyProject, openFile])
1531
+
1475
1532
  const activeFileForChat = useMemo(() => {
1476
1533
  if (activeTab === null || activeTab.kind === 'diff' || activeTab.kind === 'graph') return null
1477
1534
  return { path: activeTab.path, content: activeTab.content ?? '', language: activeTab.language }
@@ -242,6 +242,67 @@ const GUFA_PLUGIN = {
242
242
  if (document.querySelector('.gufa-overlay-root') !== null || attempts > 40) {
243
243
  document.documentElement.removeAttribute('data-gufa-booting')
244
244
  window.clearInterval(timer)
245
+ /* 显式恢复主窗口状态(项目 → 标签页 → 会话),避免依赖 Web 版 App 的启动时序 */
246
+ try {
247
+ if (window.__gufaFloatRestored !== true && G.floatState !== null && G.floatState !== undefined) {
248
+ window.__gufaFloatRestored = true
249
+ void (async () => {
250
+ const state = G.floatState
251
+ let projectValue = null
252
+ if (typeof state.projectRoot === 'string' && state.projectRoot !== '') {
253
+ try {
254
+ projectValue = await G.api('project.open', { path: state.projectRoot })
255
+ } catch {
256
+ projectValue = null
257
+ }
258
+ }
259
+ const restore = G.floatRestoreRef
260
+ if (projectValue !== null && restore !== null && restore !== undefined && typeof restore.applyProject === 'function') {
261
+ restore.applyProject(projectValue)
262
+ }
263
+ await new Promise((resolve) => setTimeout(resolve, 350))
264
+ const paths = Array.isArray(state.tabs) ? state.tabs.filter((path) => typeof path === 'string' && path !== '') : []
265
+ const ordered = typeof state.activePath === 'string' && state.activePath !== ''
266
+ ? paths.filter((path) => path !== state.activePath).concat([state.activePath])
267
+ : paths
268
+ const openFile = restore !== null && restore !== undefined && typeof restore.openFile === 'function' ? restore.openFile : null
269
+ if (openFile !== null) {
270
+ for (const path of ordered.slice(0, 10)) {
271
+ try {
272
+ await openFile(path)
273
+ } catch {
274
+ /* ignore */
275
+ }
276
+ }
277
+ }
278
+ if (typeof state.sessionId === 'string' && state.sessionId !== '') {
279
+ try {
280
+ const workspace = G.ctx === null || G.ctx === undefined ? undefined : G.ctx.get('uiWorkspace')
281
+ if (workspace !== undefined && workspace !== null && typeof workspace.openSession === 'function') workspace.openSession(state.sessionId)
282
+ } catch {
283
+ /* ignore */
284
+ }
285
+ }
286
+ })()
287
+ }
288
+ } catch {
289
+ /* ignore */
290
+ }
291
+ /* 恢复主窗口带过来的当前会话(悬浮窗是独立客户端实例,需要主动选中同一会话) */
292
+ try {
293
+ const parsedState = G.floatState
294
+ const sessionId = parsedState !== null && typeof parsedState.sessionId === 'string' ? parsedState.sessionId : ''
295
+ if (sessionId !== '') {
296
+ const workspace = G.ctx === null || G.ctx === undefined ? undefined : G.ctx.get('uiWorkspace')
297
+ if (workspace !== undefined && workspace !== null && typeof workspace.openSession === 'function') {
298
+ window.setTimeout(() => {
299
+ try { workspace.openSession(sessionId) } catch { /* ignore */ }
300
+ }, 600)
301
+ }
302
+ }
303
+ } catch {
304
+ /* ignore */
305
+ }
245
306
  }
246
307
  }, 250)
247
308
  ctx.effect(() => () => {
@@ -291,16 +352,6 @@ const GUFA_PLUGIN = {
291
352
  window.setTimeout(cleanStaleSidebarLayouts, 1500)
292
353
  window.setTimeout(cleanStaleSidebarLayouts, 5000)
293
354
 
294
- // 弹窗/菜单打开时把覆盖层整体抬到右侧停靠会话之上(否则会被盖住)
295
- try {
296
- if (typeof G.installPopupLayerWatch === 'function') {
297
- const disposePopupWatch = G.installPopupLayerWatch(ctx)
298
- if (typeof disposePopupWatch === 'function') ctx.effect(() => disposePopupWatch, 'gufa-code: popup layer watch')
299
- }
300
- } catch (error) {
301
- console.error('[gufa-code] 安装弹窗层级监听失败', error)
302
- }
303
-
304
355
  // 外部弹窗(设置等)打开时降低停靠层,避免遮挡弹窗导致关不掉
305
356
  try {
306
357
  if (typeof G.installDialogLayerWatch === 'function') {
@@ -1,86 +0,0 @@
1
- /**
2
- * 59-popup-layer:弹窗/菜单层级修正。
3
- *
4
- * 背景(踩坑记录,见 docs/KNOWN-PITFALLS.md):
5
- * 古法编程的界面都在 `.gufa-overlay-root`(z-index:1)这个层叠上下文里,而右侧停靠的
6
- * 官方会话根节点是 body 级 fixed、z-index 2147483040。任何在 root 内部弹出的
7
- * modal / menu / popover,如果只设自己的 z-index,都会被困在 root 的上下文里,
8
- * 被停靠会话盖住(表现为「弹窗被右侧聊天挡一半、点不到关闭」)。
9
- *
10
- * 修法:检测 root 内是否出现 popup,出现时给 html 加 `data-gufa-popup`,
11
- * 由 CSS 把整个 overlay root 临时抬到停靠会话之上。
12
- */
13
-
14
- G.installPopupLayerWatch = function installPopupLayerWatch(ctx) {
15
- if (typeof document === 'undefined') return () => {}
16
- const ATTR = 'data-gufa-popup'
17
- const SELECTOR = [
18
- '[role="menu"]',
19
- '[role="listbox"]',
20
- '[role="dialog"]',
21
- '.gufa-modal',
22
- '.gufa-float-backdrop',
23
- '.gufa-branch-dialog-backdrop',
24
- '.gufa-session-backdrop',
25
- '.gufa-shortcuts-backdrop',
26
- ].join(',')
27
-
28
- /* 真正需要抬的是 overlay root 的 body 级祖先:它和停靠会话都是 body 的直接子节点,
29
- 在祖先上设 z-index 才能压过会话的 2147483040;只抬 .gufa-overlay-root 出不了
30
- 它自己的层叠上下文。 */
31
- let liftedNodes = []
32
- const lift = (open) => {
33
- try {
34
- if (open) {
35
- if (liftedNodes.length > 0) return
36
- let node = document.querySelector('.gufa-overlay-root')
37
- if (node === null) return
38
- while (node.parentElement !== null && node.parentElement !== document.body) node = node.parentElement
39
- if (node.parentElement !== document.body) return
40
- const saved = { node, zIndex: node.style.zIndex, position: node.style.position }
41
- node.style.zIndex = '2147483300'
42
- if (window.getComputedStyle(node).position === 'static') node.style.position = 'relative'
43
- liftedNodes.push(saved)
44
- if (node.parentElement !== document.body) return
45
- } else {
46
- for (const saved of liftedNodes) {
47
- saved.node.style.zIndex = saved.zIndex
48
- saved.node.style.position = saved.position
49
- }
50
- liftedNodes = []
51
- }
52
- } catch {
53
- /* ignore */
54
- }
55
- }
56
-
57
- const sync = () => {
58
- try {
59
- const root = document.querySelector('.gufa-root')
60
- const popup = root === null ? null : root.querySelector(SELECTOR)
61
- const open = popup !== null
62
- lift(open)
63
- if ((document.documentElement.getAttribute(ATTR) === '1') === open) return
64
- if (open) document.documentElement.setAttribute(ATTR, '1')
65
- else document.documentElement.removeAttribute(ATTR)
66
- } catch {
67
- /* ignore */
68
- }
69
- }
70
-
71
- let observer = null
72
- try {
73
- observer = new MutationObserver(sync)
74
- observer.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style', 'data-state', 'aria-hidden'] })
75
- } catch {
76
- observer = null
77
- }
78
- sync()
79
- const timer = window.setInterval(sync, 400)
80
- return () => {
81
- if (observer !== null) observer.disconnect()
82
- window.clearInterval(timer)
83
- lift(false)
84
- document.documentElement.removeAttribute(ATTR)
85
- }
86
- }