From 570bebc85e06afdb7fc83df62403e87836de3cb3 Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Fri, 9 Oct 2026 23:09:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(dock):=20=E5=8D=A0=E4=BD=8D=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E6=94=B9=E7=94=A8=E5=91=BD=E4=B8=AD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=20+=20=E5=A4=9A=E7=82=B9=E9=87=87=E6=A0=B7=EF=BC=88=E4=BF=AE?= =?UTF-8?q?=E3=80=8Ctab=20=E5=A4=9A=E7=9A=84=E6=97=B6=E5=80=99=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E4=B9=9F=E9=94=99=E8=AF=AF=E3=80=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两个真问题: 1. **页签芯片筛不出来**。原先把「占位」定义成 `button, [role="button"]` 的 rect 相交,而右侧栏的页签芯片不是这两者 —— 页签一多,让位会**停在芯片上**。 改用 `document.elementsFromPoint`:直接问页面「这一格下面有什么」, 与宿主用什么标签、什么类名无关。判定为占位的是**不透明的小控件** (≤300px 宽:页签芯片、图标胶囊);整行的不透明背景板(会话头 titleRow、 面板 tabStrip、面板本体)不算 —— 把它们算进去会让入口一路退到屏幕中间 (实测退到 x=530)。 2. **只测中心点不够**。槽位宽 28px,中心空着不代表右缘不压着邻居: 实测中心判空、box 却与 Split/Fullscreen 相交。现在沿水平方向采 **5 个点** (2 / 8 / 14 / 20 / 26),整格全空才算空;让位步长同时收到 4px。 真机复测(2200px 视口、右侧栏打开、开过页签): 入口落在 `2066`,宿主整簇从 `Split@2094` 起 —— **rect 相交为零**。 判别性:`tests/dock-fallback.spec.tsx` 的芯片用例把命中测试桩成「x > 430 被页签盖住」, 断言入口退到 `404px`;**关掉命中测试即红** (`expected '464px' to be '404px'`,464 正是压在页签上的位置)。全 8 条绿, 全量 189 文件 / 2214 通过,typecheck / lint 干净。 --- .local-many.png | Bin 0 -> 1084 bytes CHANGELOG.md | 2 +- src/client/sidebar/dock-fallback.tsx | 107 ++++++++++++++++++++++----- tests/dock-fallback.spec.tsx | 45 ++++++++++- 4 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 .local-many.png diff --git a/.local-many.png b/.local-many.png new file mode 100644 index 0000000000000000000000000000000000000000..6f39527f18713ec4a29ee35eb1ff217f3fff2949 GIT binary patch literal 1084 zcmeAS@N?(olHy`uVBq!ia0y~yU`ztCO*oin;p<#qoZCa>c%I+~kMv}xJ$W2U}mJijlIyxBjg#{cBaYr^-|uU_uI-{u3$r<0SP z+dNmL>pFeoUfF>nyg<#`LncA+fs)K3=*g-?w_}^P_t|-r5)O<<{Fzpq;NvNCnk&6s@!-t{~82=TD-9G2+fUlw#SeeT|{BP@Fy*DD*%+Gk@K!y z!G{Tc4(gwk)E@Bu+U<7Fj%_b`EUI#{gx@6z34h=@Y%uG2la)o`t1V5pJ2)$x z&u$a`=(y46`i*UC=UklX%y&%2>&V>xn~oND=FhY)e|l$HcTIw#xf>i%7QJAdE4^`%}_3nOg)ls=x< z*ZAVVY5lz$a-)4p-%fwIdFGp)cXk!8u6nN~!11Ex(oJ*2cO`Ma7+;bRDSIyIwg1Hk zlT~IJ7dKhWiM%#{yW#nJim%FFZdC#Xp@li4gM$x)fPgRq%Mlg^Mnjz3gHNBPvVP(E zFLl_U#%_L-_l~dJwnw+UjanPF{`&7 element.closest('[data-dsh-dock-fallback]') === null) } +/** + * 该槽位是否被宿主的东西占着。 + * + * **用命中测试,不猜宿主的 DOM 形状**:早先的版本把「占位」定义成 + * `button, [role="button"]`,而右侧栏的**页签芯片**未必是这两者——于是页签一多, + * 让位会在芯片上停下来(tab 多的时候位置就错)。`elementsFromPoint` 直接把该点下面 + * 的整摞元素交出来,芯片、图标、文字都跑不掉,与宿主用什么标签、什么类名无关。 + * + * 判定为「占着」的条件:这一摞里存在一个**不透明**或**可交互**的元素(我们自己的 + * 外壳、`html`/`body` 除外)。只负责排版的透明容器(会话头的 titleRow、面板的 + * tabStrip 外框等)不算。 + * @param cx - 槽位中心 x(视口坐标)。 + * @param cy - 槽位中心 y(视口坐标)。 + * @returns 被占时为 true。 + */ +function slotOccupied(cx: number, cy: number): boolean { + // jsdom(单测)没有命中测试,退回按元素 rect 判定;真实浏览器一律走命中测试。 + if (typeof document.elementsFromPoint !== 'function') return slotOccupiedByRect(cx, cy) + const stack = document.elementsFromPoint(cx, cy) + return stack.some((element) => { + if (element.closest('[data-dsh-dock-fallback]') !== null) return false + if (element === document.body || element === document.documentElement) return false + if (element.matches('button, a, input, select, textarea, [role="button"], [role="tab"], [role="menuitem"], [role="link"]')) return true + // 不透明**小控件**(页签芯片的底、图标胶囊等)才算占位。整行的容器——会话头 + // 的 titleRow、面板的 tabStrip、面板本体——底也是不透明的,但它们只是背景板: + // 把整行判成占位会让入口一路退到屏幕中间(实测退到 x=530)。 + const box = element.getBoundingClientRect() + if (box.width > MAX_OCCUPANT_W) return false + const background = getComputedStyle(element).backgroundColor + return background !== '' && background !== 'transparent' && background !== 'rgba(0, 0, 0, 0)' + }) +} + +/** + * 命中测试缺席时(jsdom)的占位判定:拿同排宿主按钮的 rect 做相交测试。 + * 真实浏览器不会走这条路径(见 {@link slotOccupied})。 + * @param cx - 槽位中心 x(视口坐标)。 + * @param cy - 槽位中心 y(视口坐标)。 + * @returns 被占时为 true。 + */ +function slotOccupiedByRect(cx: number, cy: number): boolean { + return [...document.querySelectorAll('button, [role="button"]')] + .filter(element => !element.hasAttribute('data-dsh-bottom-toggle')) + .map(element => element.getBoundingClientRect()) + .filter(box => box.width > 4 && box.height > 4) + .some(box => cx > box.left - GAP / 2 && cx < box.right + GAP / 2 + && cy > box.top && cy < box.bottom) +} + /** * 量本按钮应放的 top/left:贴在宿主「右侧栏角落控件」的**左侧空位**上。 * - * 两件事都不能靠猜: + * 三件事都不能靠猜: * * 1. **锚点要认控件的两种形态**——右侧栏关闭时它带 `data-sidebar-right-expand`, * 打开时该属性整个消失、同一座位换成 `data-sidebar-right-toggle`。只认前者会落到 - * 下面的视口兜底坐标(`innerWidth - 44`),而那里正是宿主自己的角落按钮群。 + * 视口兜底坐标(`innerWidth - 44`),而那里正是宿主自己的角落按钮群。 * 2. **让位不能让一个固定偏移**——宿主的角落控件是肩并肩一簇(实测打开态: - * Split / Fullscreen / Collapse 各 28px、间隔 8px)。只往左挪 `GAP + BUTTON_SIZE` - * 正好落在**下一个**按钮上(实测:精确压住 Fullscreen 28x28px)。所以这里逐个槽位 - * 左移,直到该槽位与**任何**宿主按钮都不相交;找不到完全不重叠的位置时(极窄窗口) - * 停在最左,宁可贴边也不叠。 + * `Split@2094 / Fullscreen@2130 / Collapse@2166`,各 28px、间隔 8px)。只往左挪 + * `GAP + BUTTON_SIZE` 正好落在**下一个**按钮上(实测精确压住 Fullscreen 28x28px)。 + * 3. **占位与否要真的去问页面**——见 {@link slotOccupied}:页签芯片会把那一排填满, + * 按角色筛元素是筛不干净的。 * + * 因此这里从锚点左侧起逐像素左移,取**最近的一个真的空着**的槽位;一直退到视口 + * 左缘仍被占时停在最左(宁可贴边,也不叠在宿主控件上)。 * @returns 视口坐标下的 top/left。 */ function measure(): { top: number; left: number } { @@ -54,21 +129,13 @@ function measure(): { top: number; left: number } { if (anchor === null) return { top: 8, left: window.innerWidth - BUTTON_SIZE - GAP - 8 } const rect = anchor.getBoundingClientRect() const top = Math.max(4, rect.top + (rect.height - BUTTON_SIZE) / 2) - // 同排的宿主按钮(本按钮自己除外):只有与它竖直方向真的相交才算占位。 - const occupied = [...document.querySelectorAll('button, [role="button"]')] - .filter(element => !element.hasAttribute('data-dsh-bottom-toggle')) - .map(element => element.getBoundingClientRect()) - .filter(box => box.width > 4 && box.height > 4) - let left = rect.left - GAP - BUTTON_SIZE - for (let step = 0; step < 8; step++) { - const collides = occupied.some(box => left < box.right + GAP / 2 - && left + BUTTON_SIZE > box.left - GAP / 2 - && top < box.bottom - && top + BUTTON_SIZE > box.top) - if (!collides) break - left -= BUTTON_SIZE + GAP + let left = Math.max(4, rect.left - GAP - BUTTON_SIZE) + for (let step = 0; step < 400; step++) { + if (slotFree(left, top)) break + left -= STEP_PX + if (left <= 4) { left = 4; break } } - return { top, left: Math.max(4, left) } + return { top, left } } export function DockFallback({ store }: { store: SidebarStore }): ReactNode { diff --git a/tests/dock-fallback.spec.tsx b/tests/dock-fallback.spec.tsx index 9811162d..3a1b5d8d 100644 --- a/tests/dock-fallback.spec.tsx +++ b/tests/dock-fallback.spec.tsx @@ -165,8 +165,9 @@ describe('dock fallback(空白会话入口)', () => { await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) }) const shell = document.querySelector('[data-dsh-dock-fallback]') expect(shell, '空白会话里备用入口应当在').not.toBeNull() - // 464 被占 → 再让一格到 428;停在 464 就是压在 Fullscreen 上。 - expect(shell!.style.left, '占位时必须继续左移,不能停在 Fullscreen 上').toBe('428px') + // 464 被占 → 4px 步长退到第一个整格都不碰 Fullscreen(464..492) 的槽位 432 + // (右缘采样点 432+26=458 ≤ 460)。停在 464 就是压在 Fullscreen 上。 + expect(shell!.style.left, '占位时必须继续左移,不能停在 Fullscreen 上').toBe('432px') } finally { unmount() anchor.remove() @@ -174,6 +175,46 @@ describe('dock fallback(空白会话入口)', () => { } }) + // 页签芯片**不是** `button` / `[role="button"]`,按角色筛元素筛不到它们 —— 页签一多, + // 「让位」就会停在芯片上(用户报的「tab 多的时候位置也错误」)。真实浏览器走命中测试: + // 该点下面整摞元素只要有不透明/可交互的就算占着。这里把命中测试桩成「前两个槽位被 + // 页签盖住」,断言入口继续左移。 + it('steps past tab chips that are not buttons (hit-test occupancy)', async () => { + phaseIs(true) + const rect = (left: number, top: number) => () => ({ + x: left, y: top, left, top, width: 28, height: 28, right: left + 28, bottom: top + 28, + toJSON: () => ({}), + }) as DOMRect + const anchor = document.createElement('button') + anchor.setAttribute('data-sidebar-right-toggle', 'true') + anchor.getBoundingClientRect = rect(500, 10) + document.body.append(anchor) + // 一个「芯片」:不是 button,但有背景色 → 命中测试必须判它占位。 + const chip = document.createElement('div') + chip.setAttribute('data-test-chip', 'true') + chip.style.backgroundColor = 'rgb(40, 40, 40)' + document.body.append(chip) + const original = document.elementsFromPoint + Object.defineProperty(document, 'elementsFromPoint', { + configurable: true, + writable: true, + value: (x: number) => (x > 430 ? [chip] : []), + }) + const { unmount } = renderDockFallback() + try { + await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) }) + const shell = document.querySelector('[data-dsh-dock-fallback]') + expect(shell, '空白会话里备用入口应当在').not.toBeNull() + // 桩的命中测试说 x > 430 都被芯片盖住 → 右缘采样点(left+26) 必须 ≤ 430,即 left ≤ 404。 + expect(shell!.style.left, '被页签占位时必须继续左移').toBe('404px') + } finally { + unmount() + anchor.remove() + chip.remove() + Object.defineProperty(document, 'elementsFromPoint', { configurable: true, writable: true, value: original }) + } + }) + it('degrades to nothing when the host route is unavailable', async () => { vi.mocked(sessionPhase).mockRejectedValue(new Error('unsupported')) const { unmount } = renderDockFallback()