Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/client/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,13 @@ export function BottomDockToggle(props: { store: SidebarStore }) {
useCallback((callback: () => void) => store.subscribe(callback), [store]),
useCallback(() => store.getSnapshot(), [store]),
)
// 宿主右侧面板展开时不出现:那个状态下它右上角是一簇自己的控件
// (Split / Fullscreen / Collapse right sidebar),入口挤进去只会跟它们打架。
if (useRightPanelOpen()) return null
// 与右侧栏**共存**:本入口注册在宿主的 session header utilities 里(见
// sidebar/bottom-toggle.tsx),那一列在宿主角落控件(Split / Fullscreen /
// Collapse right sidebar)的左侧,本来就是流式排布、不会压住它们。此前这里
// 一句 `if (useRightPanelOpen()) return null` 把按钮整个藏掉,于是「先展开右侧
// 栏、再想开底部面板」这条路走不通——现在只在展开态打一个 `data-right-open`
// 标记(样式可据此让位),按钮本身照常渲染、照常可点。
const rightOpen = useRightPanelOpen()
const open = snapshot.state?.bottomOpen === true
const label = open ? t('collapseBottomPanel') : t('expandBottomPanel')
return (
Expand All @@ -795,6 +799,7 @@ export function BottomDockToggle(props: { store: SidebarStore }) {
type="button"
className={css.toggleButton}
data-dsh-bottom-toggle
data-right-open={rightOpen ? 'true' : undefined}
data-active={open ? 'true' : undefined}
aria-label={label}
aria-pressed={open}
Expand Down
7 changes: 7 additions & 0 deletions src/client/sidebar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
color var(--ds-transition-duration-slow) var(--ds-ease-in-out);
}

.toggleButton[data-right-open='true'] {
/* 与右侧栏共存时让位:宿主的角落控件(Split / Fullscreen / Collapse right sidebar)
在展开态比收起态多出两颗,把本按钮压到「贴住右边那一簇」的位置只会互相遮挡。
这里只做视觉上的让位(留出更宽的右侧间隔),点击目标本身不变。 */
margin-right: 40px;
}

.toggleButton:hover:not(:disabled):not([aria-disabled='true']) {
background: var(--dsw-alias-interactive-bg-hover);
color: var(--dsw-alias-label-primary);
Expand Down
11 changes: 4 additions & 7 deletions src/client/sidebar/dock-fallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { createPortal } from 'react-dom'
import type { SidebarStore } from '../state.ts'
import { BottomDockToggle } from '../Sidebar.tsx'
import { useSessionPhase } from '../session-phase.ts'
import { useRightPanelOpen } from './right-panel-open.ts'
import css from '../sidebar.module.css'

/** 按钮尺寸与宿主展开按钮一致(28px),间距 8px。 */
Expand Down Expand Up @@ -158,11 +157,11 @@ export function DockFallback({ store }: { store: SidebarStore }): ReactNode {
useCallback(() => store.getSnapshot(), [store]),
)
const phase = useSessionPhase(snapshot.sessionId)
const rightOpen = useRightPanelOpen()
const [pos, setPos] = useState(measure)
const [headerEntry, setHeaderEntry] = useState(headerEntryPresent)
// 渲染条件(顺序即下面早退的顺序):相位已读到 blank、会话头那套入口不在场、右侧面板收起。
const rendered = phase !== undefined && phase.blank && !headerEntry && !rightOpen
// 渲染条件(顺序即下面早退的顺序):相位已读到 blank、会话头那套入口不在场。
// 「是否渲染」不再看右侧栏:按钮与它共存(展开态靠 measure() 让位)。
const rendered = phase !== undefined && phase.blank && !headerEntry
const onResize = useCallback((): void => {
// 页面不可见时什么都不做:看不到的版面不值得测量,state 也不需要写。
if (document.hidden) return
Expand Down Expand Up @@ -197,9 +196,7 @@ export function DockFallback({ store }: { store: SidebarStore }): ReactNode {
if (phase === undefined || !phase.blank) return null
// 兜底:相位说 blank、但会话头的入口确实在 DOM 里 —— 让位,绝不叠出第二套。
if (headerEntry) return null
// 宿主右侧面板展开时不渲染(里面那个按钮本身也会返回 null,这里连空壳一起省掉):
// 那个状态下右上角是宿主自己的一簇控件,没有本入口的容身之处。
if (rightOpen) return null

// portal 到 body:面板宿主(z 20-30)的层叠上下文压不过会话头,按钮会被 titleRow
// 拦截点击。40 高于 AppFrame(20) 与会话头、低于 DSH 浮层(100+)。
return createPortal(
Expand Down
79 changes: 73 additions & 6 deletions src/client/sidebar/right-panel-open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
* (= 不隐藏),按钮照旧出现、位置由 {@link ./dock-fallback.tsx} 的让位逻辑兜住;
* 而不是读成「永远展开」把按钮永久藏掉。
*
* **全模块只有一份答案、一条表**(v0.25 的稳态开销治理):早先每个消费者各建一条
* 500ms 的表、各做两趟全文档 `querySelectorAll`(本文件 + `dock-fallback.tsx` 共两条),
* 与挂载了多少个消费者成正比。现在订阅者只是引用计数——0 → 1 才起表并立刻读一次,
* 1 → 0 就停表;消费者再多也只有一条表。
* **全模块只有一份答案、一条表**(v0.25 的稳态开销治理):订阅者只是引用计数——
* 0 → 1 才建观察者与兜底表并立刻读一次,1 → 0 全拆掉;消费者再多也只有一份。
*
* **以事件为主、采样为辅**(本次修复):宿主切换那两个属性、整块换控件、动画收尾
* 都会立刻触发重读(见 {@link startObserving}),所以答案落在变化的那一帧,而不是
* 落在下一次采样的拍点上——此前 500ms 轮询让底部面板按钮在展开动画结束后才消失、
* 收起时又先闪回来一拍。兜底表降到 {@link POLL_MS}(1.5s),只服务「观察者拿不到
* 信号」(宿主改名/搬家)这一种情形。
*
* **页面不可见时表本身不存在**(不是「tick 进来再早退」):隐藏的页面没有要服务的
* 版面,`visibilitychange` 回到可见时才立刻读一次并重新起表。
*/
import { useEffect, useState } from 'react'

/** 复核间隔:与备用入口的位置对齐同一节拍(500ms)。 */
const POLL_MS = 500
/**
* 兜底复核间隔。事件驱动为主(见 {@link observe}),这条表只在观察者拿不到
* 变化时兜底:宿主换了属性名、把控件搬到另一个容器、或移除后又挂回来。
*/
const POLL_MS = 1_500

/** 共享的展开态答案;所有消费者读同一份。 */
let open = false
Expand All @@ -34,6 +41,9 @@ const listeners = new Set<(next: boolean) => void>()
/** 全模块**唯一**的定时器:消费者再多也只有这一条。 */
let timer: ReturnType<typeof setInterval> | undefined

/** 宿主角落控件的观察者(0 → 1 个订阅者时建、1 → 0 时拆)。 */
let observer: MutationObserver | undefined

/** 真实浏览器里的 document;SSR / 极端环境缺席。 */
function pageDocument(): Document | undefined {
return typeof document === 'undefined' ? undefined : document
Expand Down Expand Up @@ -88,7 +98,63 @@ function publish(): void {
for (const listener of listeners) listener(next)
}

/** 宿主角落控件:同一座位在收起/展开两态之间换的是这两个属性之一。 */
const CORNER_ATTRS = ['data-sidebar-right-expand', 'data-sidebar-right-toggle', 'style', 'class', 'hidden']

/** 动画结束的监听器(挂在 document 的 capture 段,动画属于宿主的元素)。 */
const settleListeners = new Set<() => void>()

function reRead(): void {
if (!pageVisible()) return
publish()
}

/**
* 事件驱动的复核(本模块此前只有 500ms 采样)。
*
* 采样只能「碰巧」读到翻转:展开动画进行中读到的一直是旧值,于是底部面板按钮
* 要等动画结束后的下一拍才消失;收起时又可能在动画中途读成「已收起」,按钮
* 提前闪回来一拍再消失。观察宿主真正改动的那几个属性,答案就在变化的当帧落地:
*
* - `attributes` + `attributeFilter`:宿主切换 `data-sidebar-right-expand` /
* `data-sidebar-right-toggle` 时就通知,`style`/`class`/`hidden` 覆盖用 CSS
* 隐藏的那一路;
* - `childList` + `subtree`:宿主整块换掉角落控件时通知;
* - 捕获阶段的 `transitionend` / `animationend`:动画收尾再读一次,覆盖「属性
* 早就翻转、可见性到动画结束才变」的情形。
*
* 兜底仍是 {@link POLL_MS} 那条表——观察者拿不到信号时(改名、搬家)靠它。
*/
function startObserving(): void {
if (observer !== undefined) return
const doc = pageDocument()
if (doc === undefined || typeof MutationObserver === 'undefined') return
observer = new MutationObserver(reRead)
observer.observe(doc.body ?? doc.documentElement, {
attributes: true,
attributeFilter: CORNER_ATTRS,
childList: true,
subtree: true,
})
doc.addEventListener('transitionend', reRead, true)
doc.addEventListener('animationend', reRead, true)
settleListeners.add(reRead)
}

function stopObserving(): void {
observer?.disconnect()
observer = undefined
const doc = pageDocument()
if (doc === undefined || settleListeners.size === 0) return
for (const listener of settleListeners) {
doc.removeEventListener('transitionend', listener, true)
doc.removeEventListener('animationend', listener, true)
}
settleListeners.clear()
}

function stopTimer(): void {
stopObserving()
if (timer === undefined) return
clearInterval(timer)
timer = undefined
Expand All @@ -98,6 +164,7 @@ function stopTimer(): void {
function startTimer(): void {
if (timer !== undefined) return
if (!pageVisible()) return
startObserving()
timer = setInterval(publish, POLL_MS)
}

Expand Down
15 changes: 8 additions & 7 deletions tests/dock-fallback.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,9 @@ describe('dock fallback(空白会话入口)', () => {
}
})

// 用户定的规则:**宿主右侧面板展开时不显示这个按钮**。那个状态下宿主右上角
// 本来就是自己的一簇控件(Split / Fullscreen / Collapse right sidebar),入口挤进去
// 只会跟它们打架 —— 干脆不出现。这条规则同时把「展开态该把入口摆哪儿」整个问题去掉,
// 让位/命中测试那套只需要服务收起态。
it('renders no entry while the host right panel is expanded', async () => {
// owner 定的规则(#992 后续):**宿主右侧面板展开时按钮照旧存在**,只是让位到
// 那一簇控件的左侧——否则「先展开右侧栏、再想开底部面板」这条路是断的。
it('keeps the entry while the host right panel is expanded', async () => {
phaseIs(true)
// 可见的 Collapse 控件(rect 非零)= 右侧栏展开;收起态的 Open 控件不可见。
const hostToggle = document.createElement('button')
Expand All @@ -278,8 +276,11 @@ describe('dock fallback(空白会话入口)', () => {
const { unmount } = renderDockFallback()
try {
await act(async () => { await new Promise(resolve => { setTimeout(resolve, 0) }) })
expect(document.querySelector('[data-dsh-dock-fallback]'), '右侧栏展开时不该有备用入口').toBeNull()
expect(document.querySelector('[data-dsh-bottom-toggle]'), '右侧栏展开时不该有底部入口').toBeNull()
expect(document.querySelector('[data-dsh-dock-fallback]'), '右侧栏展开时仍要有备用入口').not.toBeNull()
const toggle = document.querySelector('[data-dsh-bottom-toggle]')
expect(toggle, '右侧栏展开时仍要有底部入口').not.toBeNull()
// 与右侧栏共存:按钮带 data-right-open 标记(样式据此让位),且不被藏掉。
expect(toggle?.getAttribute('data-right-open')).toBe('true')
} finally {
unmount()
hostToggle.remove()
Expand Down
36 changes: 36 additions & 0 deletions tests/right-panel-open.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,42 @@ describe('useRightPanelOpen(共享 poller)', () => {
expect(documentQueries - queriesBefore, '一个 tick 只扫一趟文档').toBe(2)
})

// 事件驱动(本次修复):宿主角落控件的属性一变就该**当帧**重读,不再等 500ms 采样。
// 这是「底部按钮在展开动画结束后才消失 / 收起时闪一拍」的根因修复点。
it('re-reads on the host corner attribute change without waiting for the fallback tick', async () => {
const view = mount(createElement(Consumer, { label: 'header' }))
expect(openOf(view.container, 'header'), '宿主没有展开态控件时读成收起').toBe('false')
const queriesBefore = documentQueries
// 宿主把座位从 expand 换成 toggle:MutationObserver 应当立刻通知。
await act(async () => {
appendExpandedToggle()
await Promise.resolve()
})
expect(openOf(view.container, 'header'), '属性变化后不等 tick 就更新').toBe('true')
// 这次更新来自观察者,不是兜底表(表一次都没跑)。
expect(documentQueries).toBeGreaterThan(queriesBefore)
})

// transitionend / animationend 是第二重信号:宿主在动画收尾才让旧控件不可见时,
// 属性早就翻转了,事件补一次读能把答案对齐到动画结束的那一刻。
it('re-reads when the host animation settles', async () => {
const view = mount(createElement(Consumer, { label: 'header' }))
expect(openOf(view.container, 'header')).toBe('false')
await act(async () => {
appendExpandedToggle()
await Promise.resolve()
})
expect(openOf(view.container, 'header')).toBe('true')
// 反过来:把展开态控件藏掉(rect 归零)再发一次 transitionend,应回到收起。
const toggle = document.querySelector('[data-sidebar-right-toggle]') as HTMLElement
toggle.getBoundingClientRect = () => ({ width: 0, height: 0, x: 0, y: 0, left: 0, top: 0, right: 0, bottom: 0, toJSON: () => ({}) }) as DOMRect
await act(async () => {
document.dispatchEvent(new Event('transitionend'))
await Promise.resolve()
})
expect(openOf(view.container, 'header'), '动画结束事件触发重读').toBe('false')
})

it('holds no interval and reads nothing while hidden, then reads once on show', () => {
// 先让共享缓存在可见状态下定成「收起」:宿主此刻没有展开态控件。
const priming = mount(createElement(Consumer, { label: 'prime' }))
Expand Down
Loading