feat(sidebar): 后台激活的页面不再顶掉读者,改为标记未读红点

维护者在 #817 上批准了「后台自动激活不接管右侧栏」的行为,并要求同批补上
「后台新开了页面」的提示:事件不能丢,但也不能把读者正在看的页面顶走。

行为:
- 被门控跳过的那次激活仍然创建任务管理页,只是不再抢焦点
  (`OpenTabSeed.reveal: false` → 宿主 `revealIfOpened: false`,即
  「放置但不聚焦」;页面类型本来就没有自己的聚焦路径,已经存在的页面
  再开一次也只是一次聚焦,正是门控要拒绝的那件事)。
- 该页在原生右栏芯片与插件底部工作台对应页签上显示一个静态小红点
  (`aria-label` = 新词条 `tabUnread`,颜色取既有 `--dsw-alias-danger`)。
- 读者切到该页即清除标记:底部工作台走 service.activateTab;原生栏的
  点击插件看不见(`ISidebarRight` 只有 `active()` 读取器,没有选中事件流),
  所以由宿主自己的芯片按 `tab.visible` 清除——非可见芯片不清除,
  否则宿主为每个展开页签都画标题,红点刚亮就会被抹掉。
- 标记存进会话状态的 `unread: TabType[]`(随布局持久化,跨挂载/跨会话/
  刷新都在),两个承载面读同一份,不会各自为政。

文案:20 份词典同批更新两条设置项描述(门控行为变了)+ 新增 `tabUnread`
词条(ja 真日文)。

测试:`tests/sidebar-auto-activation.spec.tsx` 的门控两条改为断言
「页面仍创建 + revealIfOpened:false + 标记出现」,并加红点生命周期与
「已在任务页/正常激活不标红」三条;新增 `tests/native-tab-unread.spec.tsx`
钉住原生芯片这一承载面(画点、按 visible 清除、非可见不清除、别的类型
不串味、不破坏可访问名)。判别性用 6 处镜像还原逐条证伪(见报告)。
This commit is contained in:
Menghuan1918 committed 2026-10-05 23:03:40 +08:00
1 parent 9c18f05441
commit de1e76e2f5
33 files changed
+654 -57

No files matched your search

+6
View File
@@ -257,6 +257,12 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
fileAddress(sessionId, cwd, path) {
return fileAddressFor(sessionId, cwd, path)
},
tabTypeOf(sessionId, tabId) {
// Read through the SESSION, like `close` above: one native id names a
// tab in every session, so a bare read can hand back another session's
// record and clear the wrong session's unread mark.
return records.get(sessionId, tabId)?.tab.type
},
close(sessionId, tabId) {
// Read through the SESSION: the same native id names a tab in every
// session, so a bare read can hand back another session's record.
+41 -3
View File
@@ -23,7 +23,7 @@
* Nothing here is a singleton: the registry is created once per client
* activation and handed to every registration.
*/
import { createElement, useMemo, useSyncExternalStore } from 'react'
import { createElement, useEffect, useMemo, useSyncExternalStore } from 'react'
import type { ComponentType, ReactNode } from 'react'
import type { Context } from '../../context-types.ts'
import type { SessionScope } from '../api.ts'
@@ -32,7 +32,8 @@ import { OrphanedTab } from '../OrphanedTab.tsx'
import { referenceInChat } from '../reference-in-chat.ts'
import { useSessionRoot } from '../use-session-root.ts'
import type { BetterSidebarService } from '../service.ts'
import { toggleExpanded } from '../state.ts'
import { t } from '../locales.ts'
import { isUnread, toggleExpanded } from '../state.ts'
import type { SidebarStore, SidebarTab, TabType } from '../state.ts'
import css from '../sidebar.module.css'
@@ -561,16 +562,53 @@ export function NativeTabTitle(props: NativeTitleInjected & NativeBodyFrameworkP
const glyph = icon ?? (typeof descriptor?.icon === 'function'
? descriptor.icon(CHIP_ICON_SIZE)
: descriptor?.icon)
if (glyph === undefined || glyph === null) return title
// "The reader has not looked at this page": the tab type this chip belongs
// to carries a mark (a background activation opened its page without taking
// the column over). The unread store subscription is what re-renders the
// chip when the mark appears or retires — the native record's own version
// does not move for either.
const unread = useSyncExternalStore(
listener => service.subscribeState(listener),
() => isUnread(service.getSnapshot().state, record?.tab.type ?? descriptorId),
)
// …and the chip is also what RETIRES the mark: this tab being visible means
// the reader is looking at its page now. Guarded on BOTH flags, so an
// inactive chip (the host draws every expanded tab's title) clears nothing
// and a chip with no mark never touches the store.
const type = record?.tab.type ?? descriptorId
const visible = nativeTab.visible === true
useEffect(() => {
if (visible && unread) service.clearUnread(type, sessionId)
}, [visible, unread, type, sessionId, service])
if (glyph === undefined || glyph === null) {
return unread ? <>{title}<UnreadDot /></> : title
}
return (
<>
{/* Decorative: the chip's accessible name stays the title. */}
<span className={css.chipIcon} aria-hidden="true">{glyph}</span>
{title}
{unread ? <UnreadDot /> : null}
</>
)
}
/**
* The native chip's unread mark. `role="img"` + a label so the dot is
* announced rather than skipped as decoration (the glyph beside it is the
* decorative one — `aria-hidden` — while this carries the state).
*/
function UnreadDot(): ReactNode {
return (
<span
className={css.chipUnread}
role="img"
aria-label={t('tabUnread')}
title={t('tabUnread')}
/>
)
}
/** The component pair one descriptor contributes to the native surface. */
export interface NativeTabComponents {
readonly body: ComponentType<NativeBodyInjected & NativeBodyFrameworkProps>