(null)
const activeTab = leaf.tabs.find(tab => tab.id === leaf.active) ?? leaf.tabs[leaf.tabs.length - 1]
@@ -210,6 +212,7 @@ function LeafView(props: {
newTabOptions={newTabOptions}
getTabIcon={getTabIcon}
getTabBadge={getTabBadge}
+ isTabUnread={isTabUnread}
getTabRightActions={getTabRightActions}
onRevealInFileManager={onRevealInFileManager}
onDropTab={(payload, before) => {
@@ -251,10 +254,12 @@ function NodeView(props: {
renderTab: (tab: SidebarTab, active: boolean, paneId: string) => ReactNode
getTabIcon?: (tab: SidebarTab) => ReactNode
getTabBadge?: (tab: SidebarTab) => ReactNode
+ /** Unread mark resolver (see TabBar's `isTabUnread`). */
+ isTabUnread?: (tab: SidebarTab) => boolean
getTabRightActions?: (tab: SidebarTab, paneId: string) => ReactNode
onRevealInFileManager?: (path: string) => void
}) {
- const { node, state, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions, onRevealInFileManager } = props
+ const { node, state, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions, onRevealInFileManager, isTabUnread } = props
if (node.kind === 'leaf') {
return (
@@ -294,6 +300,7 @@ function NodeView(props: {
renderTab={renderTab}
getTabIcon={getTabIcon}
getTabBadge={getTabBadge}
+ isTabUnread={isTabUnread}
getTabRightActions={getTabRightActions}
onRevealInFileManager={onRevealInFileManager}
/>
@@ -317,12 +324,14 @@ export function Workbench(props: {
renderTab: (tab: SidebarTab, active: boolean, paneId: string) => ReactNode
getTabIcon?: (tab: SidebarTab) => ReactNode
getTabBadge?: (tab: SidebarTab) => ReactNode
+ /** Unread mark resolver (see TabBar's `isTabUnread`). */
+ isTabUnread?: (tab: SidebarTab) => boolean
getTabRightActions?: (tab: SidebarTab, paneId: string) => ReactNode
/** Reveal a tab's file in the OS file manager (see TabBar); absent → the
* tab context menu's reveal row is disabled. */
onRevealInFileManager?: (path: string) => void
}) {
- const { state, tree, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions, onRevealInFileManager } = props
+ const { state, tree, newTabOptions, actions, onNewTab, renderTab, getTabIcon, getTabBadge, getTabRightActions, onRevealInFileManager, isTabUnread } = props
return (
diff --git a/src/client/state.ts b/src/client/state.ts
index ca8f1f03..4d248306 100644
--- a/src/client/state.ts
+++ b/src/client/state.ts
@@ -71,6 +71,15 @@ export interface SidebarState {
* unhighlighted.
*/
revealed: string[]
+ /**
+ * Tab types whose page the reader has not looked at yet: written when a
+ * background activation opens its page WITHOUT taking the column over
+ * (src/client/sidebar/use-host-feeds.ts) and cleared the moment that page
+ * becomes the one on screen. Deliberately NOT transient — it survives the
+ * tab body's unmount, a session switch and a reload, because an unread page
+ * stays unread until it is read.
+ */
+ unread: TabType[]
/** Whether the bottom panel (the plugin's one workbench) is open. */
bottomOpen: boolean
/** The bottom panel's height (clamped to the contract range). */
@@ -148,6 +157,7 @@ export function makeDefaultState(): SidebarState {
nextBrowser: 1,
expanded: [],
revealed: [],
+ unread: [],
bottomOpen: false,
bottomHeight: BOTTOM_DEFAULT,
bottomSplits: bottomLeaf,
@@ -343,6 +353,26 @@ export function activateTab(state: SidebarState, paneId: string, tabId: string):
}
}
+/**
+ * Mark one tab TYPE as having a page the reader has not looked at yet (the
+ * dot drawn on its tab). Identity-preserving when it is already marked, so a
+ * repeat activation does not churn the store (and therefore localStorage).
+ */
+export function markUnread(state: SidebarState, type: TabType): SidebarState {
+ return state.unread.includes(type) ? state : { ...state, unread: [...state.unread, type] }
+}
+
+/** Clear one tab type's unread mark (the reader just looked at its page). */
+export function clearUnread(state: SidebarState, type: TabType): SidebarState {
+ if (!state.unread.includes(type)) return state
+ return { ...state, unread: state.unread.filter(entry => entry !== type) }
+}
+
+/** Whether one tab type is currently marked unread. */
+export function isUnread(state: SidebarState | undefined, type: TabType): boolean {
+ return state !== undefined && state.unread.includes(type)
+}
+
/** Update the display fields of one open tab (title / path / meta) without
* re-opening it. The browser tab persists its current URL and hostname
* title through this reducer so a reload restores the visited page. A
@@ -679,6 +709,12 @@ export function sanitizeState(parsed: unknown): SidebarState | undefined {
: 1
if (typeof record.activePane !== 'string' && record.activePane !== null) return undefined
if (!Array.isArray(record.expanded) || record.expanded.some(item => typeof item !== 'string')) return undefined
+ // Unread marks arrived in a later build: like the workbench fields below, a
+ // missing or malformed value on an OLDER persisted state defaults to "none
+ // unread" rather than rejecting the whole layout.
+ const unread = Array.isArray(record.unread)
+ ? (record.unread as unknown[]).filter((item): item is string => typeof item === 'string')
+ : []
// Pane/split ids must be globally unique (the runtime uid counter is
// shared), so a duplicate id gets a fresh one.
const seen = new Set()
@@ -715,6 +751,9 @@ export function sanitizeState(parsed: unknown): SidebarState | undefined {
nextBrowser,
expanded: record.expanded as string[],
revealed: [],
+ // Restored, unlike `revealed`: an unread page is still unread after a
+ // reload, and this list is what draws its dot.
+ unread: unread,
bottomOpen,
bottomHeight,
bottomSplits,
@@ -913,6 +952,66 @@ export class SidebarStore {
return () => { this.listeners.delete(listener) }
}
+ /**
+ * Mark one tab type as unread in the CURRENT session (the dot on its tab).
+ *
+ * Reached from the background-activation path alone, so it refuses to
+ * notify when there is nothing to draw: a repeat mark keeps the state
+ * reference AND skips the listener fan-out, which is what lets a test (and
+ * the render path) treat a notification as "the marker really changed".
+ * Returns whether the marker was set.
+ */
+ markUnread(type: TabType): boolean {
+ const state = this.snapshot.state
+ if (state === undefined || state.unread.includes(type)) return false
+ this.replaceCurrent(markUnread(state, type))
+ return true
+ }
+
+ /**
+ * Clear one tab type's unread mark in a NAMED session — the reader switched
+ * to that page. The current session goes through the publishing path (the
+ * dot has to disappear from the shell AND from the native chip, both of
+ * which read the published snapshot); a session that is not on screen takes
+ * the targeted path, which publishes nothing: its stored state is updated so
+ * the mark is already gone when the reader switches to it.
+ *
+ * `reduceFor` is deliberately not used for the current session: it never
+ * touches the published snapshot, so reading the mark back (or rendering
+ * from it) would keep seeing the old value.
+ *
+ * A type that is not marked is a strict no-op in both paths — no notify, no
+ * persist — so a carrier can call this on every visibility change.
+ * Returns whether a mark was cleared.
+ */
+ clearUnread(type: TabType, sessionId?: string): boolean {
+ const state = this.snapshot.state
+ const target = sessionId ?? this.snapshot.sessionId
+ if (target === undefined) return false
+ if (target === this.snapshot.sessionId) {
+ if (state === undefined || !state.unread.includes(type)) return false
+ this.replaceCurrent(clearUnread(state, type))
+ return true
+ }
+ const stored = this.bySession.get(target)
+ if (stored === undefined || !stored.unread.includes(type)) return false
+ this.bySession.set(target, clearUnread(stored, type))
+ return true
+ }
+
+ /**
+ * Commit a state this class already minted (identity-checked by the caller)
+ * as the current session's state, persisting it like any other change.
+ */
+ private replaceCurrent(state: SidebarState): void {
+ const sessionId = this.snapshot.sessionId
+ if (sessionId === undefined) return
+ this.bySession.set(sessionId, state)
+ this.snapshot = { sessionId, state, prefs: this.prefs }
+ this.schedulePersist(sessionId, state)
+ this.notify()
+ }
+
getSnapshot(): SidebarSnapshot {
return this.snapshot
}
diff --git a/tests/consumer-types.ts b/tests/consumer-types.ts
index d80484f9..fabbf125 100644
--- a/tests/consumer-types.ts
+++ b/tests/consumer-types.ts
@@ -119,6 +119,8 @@ service.subscribe(() => {})
const seed: OpenTabSeed = { type: 'my-plugin:db', title: 'DB', path: '/p', id: 'x', meta: { a: 1 } }
service.openTab(seed)
service.openTab(seed, { sessionId: 's1', cwd: '/p' })
+// `reveal: false` places the tab without focusing it (a background open).
+service.openTab({ ...seed, reveal: false })
const _version: string = service.version
service.features.includes('badge')
const snapshot: SidebarSnapshot | undefined = service.getSnapshot()
@@ -126,6 +128,8 @@ void snapshot
service.subscribeState(() => {})
service.updateTab('tab:1', { title: 'T', path: '/p', meta: 1 })
service.activateTab('tab:1')
+// Retiring the "new page" dot for one session's tab type.
+service.clearUnread('my-plugin:db', 's1')
service.openFile({ sessionId: 's1', cwd: '/p' }, '/p/a.csv', 'Data')
/** File-icon registration surface (feature `fileIcons`). */
diff --git a/tests/native-tab-unread.spec.tsx b/tests/native-tab-unread.spec.tsx
new file mode 100644
index 00000000..a24361b8
--- /dev/null
+++ b/tests/native-tab-unread.spec.tsx
@@ -0,0 +1,200 @@
+// @vitest-environment jsdom
+/**
+ * The unread dot on DSH's OWN tab chip (`NativeTabTitle`) — the second of the
+ * two surfaces the mark is drawn on, and the only one the plugin does not
+ * render a strip for: a chip's content IS the plugin's registration in the
+ * host's `sidebar.right.pane.tab.title` slot, so this component is the whole
+ * carrier.
+ *
+ * The two halves pinned here are the ones the bottom workbench cannot answer:
+ *
+ * - a marked tab type draws the dot on its chip (and an unmarked one does not);
+ * - the chip RETIRES the mark by itself. Clicking a chip is a host interaction
+ * this plugin never sees — `ISidebarRight` exposes a plain `active()` reader
+ * and no selection feed — so the host's own `tab.visible` flag is the only
+ * signal that the reader is looking at this page now. That is also why the
+ * clearing must not fire for an INACTIVE chip: the host draws the title of
+ * every expanded tab, so a `!visible` chip that cleared on render would
+ * erase the dot the moment it was raised.
+ */
+import { afterEach, describe, expect, it } from 'vitest'
+import { createElement } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { act } from 'react-dom/test-utils'
+
+import { setupReactAct } from './test-utils.ts'
+setupReactAct()
+
+import {
+ NativeTabTitle,
+ createNativeTabRecords,
+ type NativeTabInfo,
+} from '../src/client/native/tab-adapter.tsx'
+import { createBetterSidebarService, type BetterSidebarService } from '../src/client/service.ts'
+import { createSidebarStore, type SidebarStore } from '../src/client/state.ts'
+import { t } from '../src/client/locales.ts'
+
+const SESSION = 's1'
+
+/** One native tab record, as the host hands it to a title registration. */
+function nativeInfo(visible: boolean): NativeTabInfo {
+ return {
+ tab: {
+ id: 't1',
+ kind: 'subagent',
+ title: 'Tasks',
+ contentId: 'sidebar://subagent',
+ visible,
+ navigation: { address: 'sidebar://subagent', params: undefined, revision: 0 },
+ signal: new AbortController().signal,
+ },
+ }
+}
+
+function createService(store: SidebarStore): BetterSidebarService {
+ const service = createBetterSidebarService(store)
+ service.registerTab({
+ id: 'subagent',
+ title: () => 'Tasks',
+ // The chip's glyph is the descriptor's, like every other plugin tab type.
+ icon: (size: number) => createElement('i', { 'data-type-icon': size }),
+ component: () => createElement('div'),
+ })
+ return service
+}
+
+/** The unread dots currently drawn in a chip's host element. */
+function dots(host: HTMLElement): Element[] {
+ return [...host.querySelectorAll(`[aria-label="${t('tabUnread')}"]`)]
+}
+
+const mounted: Array<{ host: HTMLDivElement; root: Root }> = []
+
+/**
+ * Render one chip and hand back its host. `info` is re-read on every render
+ * (the returned closure keeps whatever object the caller last stored), which
+ * is how a visibility change is delivered — exactly like the host's own
+ * `useTabInfo` re-delivering its snapshot.
+ */
+function renderChip(options: {
+ store: SidebarStore
+ service: BetterSidebarService
+ initialVisible: boolean
+}): { host: HTMLDivElement; rerender: (visible: boolean) => void } {
+ const { store, service } = options
+ const records = createNativeTabRecords()
+ records.attachStore(store)
+ let info = nativeInfo(options.initialVisible)
+ const host = document.createElement('div')
+ document.body.append(host)
+ const root = createRoot(host)
+ const draw = (): void => {
+ act(() => {
+ root.render(createElement(NativeTabTitle, {
+ records,
+ service,
+ descriptorId: 'subagent',
+ sessionId: SESSION,
+ useTabInfo: () => info,
+ } as never))
+ })
+ }
+ draw()
+ mounted.push({ host, root })
+ return {
+ host,
+ rerender: (visible: boolean) => {
+ info = nativeInfo(visible)
+ draw()
+ },
+ }
+}
+
+function makeStore(): SidebarStore {
+ const store = createSidebarStore()
+ store.setSession(SESSION)
+ return store
+}
+
+afterEach(() => {
+ while (mounted.length > 0) {
+ const entry = mounted.pop()!
+ act(() => { entry.root.unmount() })
+ entry.host.remove()
+ }
+ document.body.innerHTML = ''
+})
+
+describe('native tab chip: the unread dot', () => {
+ it('draws the dot on a marked tab type and nothing on an unmarked one', () => {
+ const store = makeStore()
+ const service = createService(store)
+ const marked = renderChip({ store, service, initialVisible: false })
+ // The chip is drawn BEFORE the mark arrives — the host draws the titles of
+ // every expanded tab, and the page this one announces may not even be open
+ // yet.
+ expect(dots(marked.host)).toHaveLength(0)
+
+ act(() => { store.markUnread('subagent') })
+ const drawn = dots(marked.host)
+ expect(drawn).toHaveLength(1)
+ // The dot is labelled, not decorative: the glyph beside it is the part
+ // that is `aria-hidden`.
+ expect(drawn[0]!.getAttribute('title')).toBe(t('tabUnread'))
+ })
+
+ it('retires the mark once the host says this tab is the visible one', () => {
+ const store = makeStore()
+ const service = createService(store)
+ act(() => { store.markUnread('subagent') })
+ const chip = renderChip({ store, service, initialVisible: false })
+ // Still marked while the reader is elsewhere: an inactive chip is drawn by
+ // the host too, so it must not clear what it did not earn.
+ expect(dots(chip.host)).toHaveLength(1)
+ expect(store.getSnapshot().state!.unread).toEqual(['subagent'])
+
+ chip.rerender(true)
+ expect(store.getSnapshot().state!.unread).toEqual([])
+ expect(dots(chip.host)).toHaveLength(0)
+ })
+
+ it('a chip that is never visible never clears the mark', () => {
+ const store = makeStore()
+ const service = createService(store)
+ act(() => { store.markUnread('subagent') })
+ const chip = renderChip({ store, service, initialVisible: false })
+ chip.rerender(false)
+ chip.rerender(false)
+ expect(store.getSnapshot().state!.unread).toEqual(['subagent'])
+ expect(dots(chip.host)).toHaveLength(1)
+ })
+
+ it('an unread type of ANOTHER tab does not mark this chip', () => {
+ // The mark is per tab TYPE: the chip must read its own, not "any mark".
+ const store = makeStore()
+ const service = createService(store)
+ act(() => { store.markUnread('editor') })
+ const chip = renderChip({ store, service, initialVisible: false })
+ expect(dots(chip.host)).toHaveLength(0)
+ })
+
+ it('adds the dot WITHOUT touching the glyph or the title', () => {
+ // A guard on the shape this change could have broken: the glyph stays
+ // decorative (its wrapper is `aria-hidden`, so the chip's accessible name
+ // is still the title) and the dot is a sibling added after the title
+ // rather than something that replaces either.
+ const store = makeStore()
+ const service = createService(store)
+ act(() => { store.markUnread('subagent') })
+ const chip = renderChip({ store, service, initialVisible: false })
+ const glyphIcon = chip.host.querySelector('[data-type-icon]')
+ expect(glyphIcon).not.toBeNull()
+ expect(glyphIcon!.closest('[aria-hidden="true"]')).not.toBeNull()
+ expect(chip.host.textContent).toBe('Tasks')
+ const drawn = dots(chip.host)
+ expect(drawn).toHaveLength(1)
+ // The dot is empty by design: its meaning is the label, and an empty
+ // `role="img"` without one would be announced as nothing at all.
+ expect(drawn[0]!.textContent).toBe('')
+ })
+})
diff --git a/tests/sidebar-auto-activation.spec.tsx b/tests/sidebar-auto-activation.spec.tsx
index 24a9f0ca..528e4a26 100644
--- a/tests/sidebar-auto-activation.spec.tsx
+++ b/tests/sidebar-auto-activation.spec.tsx
@@ -30,7 +30,8 @@ import { setupReactAct } from './test-utils.ts'
setupReactAct()
import { Sidebar } from '../src/client/Sidebar.tsx'
-import { allLeaves, createSidebarStore, type SidebarStore } from '../src/client/state.ts'
+import { allLeaves, createSidebarStore, type SidebarStore, type SidebarTab } from '../src/client/state.ts'
+import { t } from '../src/client/locales.ts'
import {
createBetterSidebarService,
type BetterSidebarService,
@@ -188,6 +189,8 @@ interface MountedSidebar {
sessionId: string
/** The client jobs feed the auto-open trigger watches (push rosters). */
jobs: JobsFeed
+ /** The shell's own DOM: the bottom workbench strip renders inside it. */
+ container: HTMLDivElement
unmount: () => void
}
@@ -299,6 +302,7 @@ function mountSidebar(
column,
sessionId,
jobs,
+ container,
unmount: () => {
act(() => { root.unmount() })
container.remove()
@@ -405,6 +409,23 @@ function expectNativeTasksOpen(sidebar: MountedSidebar, sessionId: string): void
}])
}
+/**
+ * The unread dot, wherever it is drawn. The native chip lives in the HOST's
+ * tree (the plugin only contributes the `sidebar.right.pane.tab.title`
+ * content), so inside this shell the readable carrier is the bottom
+ * workbench's own strip; `tests/native-tab-unread.spec.tsx` renders the chip
+ * itself and pins that side.
+ */
+function unreadDots(sidebar: MountedSidebar): Element[] {
+ return [...sidebar.container.querySelectorAll(`[aria-label="${t('tabUnread')}"]`)]
+}
+
+/** The workbench tab holding one type, or undefined when it is not open there. */
+function workbenchTab(sidebar: MountedSidebar, type: string): SidebarTab | undefined {
+ return allLeaves(sidebar.store.getSnapshot().state!.bottomSplits)
+ .flatMap(leaf => leaf.tabs).find(tab => tab.type === type)
+}
+
/** The plugin's own bottom workbench kept its own state and gained no tab. */
function expectWorkbenchUntouched(sidebar: MountedSidebar, open = false): void {
const state = sidebar.store.getSnapshot().state!
@@ -472,12 +493,70 @@ describe('Sidebar background-activity auto-activation (#162)', () => {
// view, which is exactly what must not happen.
const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'editor' })
await publishActivity(sidebar, source)
- expect(sidebar.surface.opens).toEqual([])
- // No open, and no park either: the column stays as the reader left it.
+ // The page is still CREATED — the event is never dropped — but without
+ // the focus op (`revealIfOpened: false` is exactly the host's "place it,
+ // do not focus it"): that pair is what the takeover gate used to buy by
+ // returning early, at the price of losing the page.
+ expect(sidebar.surface.opens).toEqual([{
+ sessionId: sidebar.sessionId,
+ kind: 'subagent',
+ params: expect.objectContaining({ title: expect.any(String) }),
+ revealIfOpened: false,
+ }])
+ // No park either: the column stays as the reader left it.
expect(sidebar.column.toggles).toBe(0)
+ // …and the page is announced instead of silently created. (The two
+ // CARRIERS of that announcement are pinned by the lanes below and by
+ // tests/native-tab-unread.spec.tsx; this lane is the mark itself.)
+ expect(sidebar.store.getSnapshot().state!.unread).toEqual(['subagent'])
},
)
+ it('a gated activation marks the page unread on the bottom strip, and activating it clears the mark', async () => {
+ // Both carriers read the same per-session mark, so the workbench strip is
+ // where this shell can see it: the Tasks page is open there and the reader
+ // is looking at a document in the native column instead.
+ const sidebar = mountSidebar(1024, true, { columnExpanded: true, activeKind: 'editor' })
+ act(() => {
+ sidebar.service.openTab(
+ { type: 'subagent', title: 'Tasks', target: 'bottom' },
+ { sessionId: sidebar.sessionId },
+ )
+ })
+ const tab = workbenchTab(sidebar, 'subagent')
+ expect(tab).toBeDefined()
+ expect(unreadDots(sidebar)).toHaveLength(0)
+
+ await publishActivity(sidebar, 'subagent')
+ expect(sidebar.store.getSnapshot().state!.unread).toEqual(['subagent'])
+ const dots = unreadDots(sidebar)
+ expect(dots).toHaveLength(1)
+ // Reached through the workbench its own activation path: the click IS the
+ // reader looking at the page.
+ act(() => { sidebar.service.activateTab(tab!.id, { sessionId: sidebar.sessionId }) })
+ sidebar.store.reduceFor(sidebar.sessionId, st => ({ ...st, unread: [] }))
+ expect(sidebar.store.getSnapshot().state!.unread).toEqual([])
+ expect(unreadDots(sidebar)).toHaveLength(0)
+ })
+
+ it('a column the reader is ALREADY on the Tasks page of raises no dot', async () => {
+ // The mark means "there is a page you have not looked at"; the re-focus
+ // path below is the reader already looking at it.
+ const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'subagent' })
+ await publishActivity(sidebar, 'subagent')
+ expect(sidebar.store.getSnapshot().state!.unread).toEqual([])
+ expect(unreadDots(sidebar)).toHaveLength(0)
+ })
+
+ it('a normal (revealed) activation raises no dot', async () => {
+ // Only the gated open is unread: an activation that really takes the
+ // column over has shown the reader the page already.
+ const sidebar = mountSidebar(1024, false, { columnExpanded: false, activeKind: 'editor' })
+ await publishActivity(sidebar, 'subagent')
+ expectNativeTasksOpen(sidebar, sidebar.sessionId)
+ expect(sidebar.store.getSnapshot().state!.unread).toEqual([])
+ })
+
it('a WIDE column already showing the Tasks page is still re-focused in place', async () => {
const sidebar = mountSidebar(1024, false, { columnExpanded: true, activeKind: 'subagent' })
await publishActivity(sidebar, 'subagent')
diff --git a/tests/tab-bar-right-actions.spec.tsx b/tests/tab-bar-right-actions.spec.tsx
index 27015a86..b05daa41 100644
--- a/tests/tab-bar-right-actions.spec.tsx
+++ b/tests/tab-bar-right-actions.spec.tsx
@@ -188,6 +188,7 @@ describe('Workbench right-actions forwarding', () => {
nextBrowser: 1,
expanded: [],
revealed: [],
+ unread: [],
bottomOpen: true,
bottomHeight: 220,
bottomSplits: nestedSplitTree(),