feat(app): show tab shortcut numbers while holding the mod key (#63089)

This commit is contained in:
usrnk1
2026-11-10 12:24:16 +01:00
committed by GitHub
Unverified
parent 8aa00e805e
commit 98e4b5343e
8 changed files with 369 additions and 5 deletions
@@ -9,13 +9,19 @@ import {
// The project avatar never changes while a session works: callers show `ActivityRing` at the row's trailing edge, as
// session tabs do.
export function SessionTabAvatarView(props: { project?: LocalProject; directory: string; unread: boolean }) {
export function SessionTabAvatarView(props: {
project?: LocalProject
directory: string
unread: boolean
label?: string
}) {
return (
<ProjectAvatar
fallback={displayName(props.project ?? { worktree: props.directory })}
src={getProjectAvatarSource(props.project?.id, props.project?.icon)}
variant={getProjectAvatarVariant(props.project?.icon?.color)}
generated={getProjectAvatarGenerated(props.project ?? { worktree: props.directory })}
label={props.label}
unread={props.unread}
/>
)
@@ -0,0 +1,97 @@
import { describe, expect, test } from "bun:test"
import {
INITIAL_TAB_HINTS_STATE,
selectTabHintsShown,
TAB_HINTS_DELAY_MS,
transitionTabHints,
type TabHintsEffect,
type TabHintsEvent,
type TabHintsState,
} from "./tab-hints-machine"
const replay = (events: TabHintsEvent[], state: TabHintsState = INITIAL_TAB_HINTS_STATE) =>
events.reduce<{ state: TabHintsState; effects: TabHintsEffect[] }>(
(current, event) => {
const next = transitionTabHints(current.state, event)
return { state: next.state, effects: [...current.effects, ...next.effects] }
},
{ state, effects: [] },
)
const press = (key: "mod" | "digit" | "other", extra = false): TabHintsEvent => ({
type: "key.down",
key,
mod: true,
extra,
})
const release: TabHintsEvent = { type: "modifiers", mod: false, extra: false }
const elapsed = (attempt: number): TabHintsEvent => ({ type: "delay.elapsed", attempt })
describe("tab hints", () => {
test("holding the mod key alone shows the numbers after the delay, and releasing it hides them", () => {
const pending = replay([press("mod")])
expect(pending.effects).toEqual([{ type: "delay.start", attempt: 1, delayMs: TAB_HINTS_DELAY_MS }])
expect(selectTabHintsShown(pending.state)).toBe(false)
const shown = replay([elapsed(1)], pending.state)
expect(selectTabHintsShown(shown.state)).toBe(true)
expect(selectTabHintsShown(replay([release], shown.state).state)).toBe(false)
})
test("a shortcut pressed before the delay cancels it, so a quick ⌘C never shows numbers", () => {
const result = replay([press("mod"), press("other"), elapsed(1)])
expect(result.effects).toEqual([
{ type: "delay.start", attempt: 1, delayMs: TAB_HINTS_DELAY_MS },
{ type: "delay.clear" },
])
expect(selectTabHintsShown(result.state)).toBe(false)
})
test("digits keep the numbers up while picking tabs, and another key hides them", () => {
const shown = replay([press("mod"), elapsed(1), press("digit"), { type: "modifiers", mod: true, extra: false }])
expect(selectTabHintsShown(shown.state)).toBe(true)
expect(selectTabHintsShown(replay([press("other")], shown.state).state)).toBe(false)
})
test("a digit pressed during the delay cancels it", () => {
expect(selectTabHintsShown(replay([press("mod"), press("digit"), elapsed(1)]).state)).toBe(false)
})
test("another modifier joining in cancels the hints", () => {
expect(selectTabHintsShown(replay([press("mod"), press("other", true), elapsed(1)]).state)).toBe(false)
expect(
selectTabHintsShown(replay([press("mod"), elapsed(1), { type: "modifiers", mod: true, extra: true }]).state),
).toBe(false)
})
test("leaving the window or unmounting cancels a pending delay", () => {
for (const exit of [{ type: "window.left" }, { type: "disposed" }] as const) {
const result = replay([press("mod"), exit, elapsed(1)])
expect(result.effects.at(-1)).toEqual({ type: "delay.clear" })
expect(selectTabHintsShown(result.state)).toBe(false)
}
})
test("a stale delay from an earlier hold does not show the numbers during a later one", () => {
const result = replay([press("mod"), release, press("mod"), elapsed(1)])
expect(selectTabHintsShown(result.state)).toBe(false)
expect(selectTabHintsShown(replay([elapsed(2)], result.state).state)).toBe(true)
})
test("a repeated mod keydown keeps the original delay", () => {
const pending = replay([press("mod")]).state
const next = transitionTabHints(pending, press("mod"))
expect(next.state).toBe(pending)
expect(next.effects).toEqual([])
})
})
@@ -0,0 +1,79 @@
export const TAB_HINTS_DELAY_MS = 400
/** `pending` waits out the delay after the mod key went down alone; `shown` draws the numbers. */
export type TabHintsState =
| { readonly status: "idle"; readonly attempt: number }
| { readonly status: "pending"; readonly attempt: number }
| { readonly status: "shown"; readonly attempt: number }
/**
* `mod` is whether the platform's mod key (⌘ on Mac, Ctrl elsewhere) is held, `extra` whether any other modifier is.
* `key.down` names the key that went down; `modifiers` reports the held state from a key release or a pointer event.
*/
export type TabHintsEvent =
| {
readonly type: "key.down"
readonly key: "mod" | "digit" | "other"
readonly mod: boolean
readonly extra: boolean
}
| { readonly type: "modifiers"; readonly mod: boolean; readonly extra: boolean }
| { readonly type: "delay.elapsed"; readonly attempt: number }
| { readonly type: "window.left" }
| { readonly type: "disposed" }
export type TabHintsEffect =
| { readonly type: "delay.start"; readonly attempt: number; readonly delayMs: number }
| { readonly type: "delay.clear" }
export type TabHintsTransition = {
readonly state: TabHintsState
readonly effects: readonly TabHintsEffect[]
}
export const INITIAL_TAB_HINTS_STATE: TabHintsState = Object.freeze({ status: "idle", attempt: 0 })
const NONE: readonly never[] = []
const CLEAR: readonly TabHintsEffect[] = [{ type: "delay.clear" }]
export function transitionTabHints(state: TabHintsState, event: TabHintsEvent): TabHintsTransition {
if (event.type === "delay.elapsed") {
if (state.status !== "pending" || state.attempt !== event.attempt) return unchanged(state)
return { state: { status: "shown", attempt: state.attempt }, effects: NONE }
}
if (event.type === "window.left" || event.type === "disposed" || !event.mod || event.extra) return reset(state)
if (event.type === "modifiers") return unchanged(state)
if (event.key === "mod") {
if (state.status !== "idle") return unchanged(state)
const attempt = state.attempt + 1
return {
state: { status: "pending", attempt },
effects: [{ type: "delay.start", attempt, delayMs: TAB_HINTS_DELAY_MS }],
}
}
// Once shown, the numbers stay while digits pick tabs; any other shortcut, or a digit pressed early, cancels them.
if (event.key === "digit" && state.status === "shown") return unchanged(state)
return reset(state)
}
export function selectTabHintsShown(state: TabHintsState) {
return state.status === "shown"
}
function reset(state: TabHintsState): TabHintsTransition {
if (state.status === "idle") return unchanged(state)
return { state: { status: "idle", attempt: state.attempt }, effects: state.status === "pending" ? CLEAR : NONE }
}
function unchanged(state: TabHintsState): TabHintsTransition {
return { state, effects: NONE }
}
@@ -462,6 +462,58 @@
}
}
/* Holding the mod key slides each tab's initial out as its shortcut number rises in. */
[data-titlebar-tab]
:is(
[data-slot="project-avatar-initial"],
[data-slot="project-avatar-label"],
[data-slot="tab-icon-face"],
[data-slot="tab-shortcut-face"]
) {
transition:
opacity 160ms ease-out,
transform 160ms ease-out,
filter 160ms ease-out,
visibility 160ms;
}
[data-titlebar-tab] [data-slot="project-avatar-initial"] {
display: block;
}
[data-titlebar-tab] [data-slot="project-avatar-label"] {
opacity: 0;
transform: translateY(6px);
filter: blur(1px);
}
[data-shortcut-hints] [data-titlebar-tab] [data-slot="project-avatar-label"] {
visibility: visible;
opacity: 1;
transform: none;
filter: none;
}
[data-shortcut-hints] [data-titlebar-tab] [data-slot="project-avatar-initial"]:has(~ [data-slot="project-avatar-label"]),
[data-shortcut-hints] [data-titlebar-tab] [data-slot="tab-icon-face"]:has(~ [data-slot="tab-shortcut-face"]) {
opacity: 0;
transform: translateY(-6px);
filter: blur(1px);
}
/* A draft has no avatar, so its neutral tile fades in under the rising number. */
[data-titlebar-tab] [data-slot="tab-shortcut-face"] {
position: absolute;
inset: 0;
display: flex;
opacity: 0;
pointer-events: none;
}
[data-shortcut-hints] [data-titlebar-tab] [data-slot="tab-shortcut-face"] {
opacity: 1;
}
/* A prompt that arrives in a background tab rings it once, like the TUI's prompt pulse. */
[data-titlebar-tab] [data-slot="tab-prompt-pulse"] {
position: absolute;
@@ -487,4 +539,14 @@
[data-titlebar-tab] [data-slot="tab-prompt-pulse"] {
animation: none;
}
[data-titlebar-tab]
:is(
[data-slot="project-avatar-initial"],
[data-slot="project-avatar-label"],
[data-slot="tab-icon-face"],
[data-slot="tab-shortcut-face"]
) {
transition: none;
}
}
+20 -3
View File
@@ -10,7 +10,7 @@ import { Menu } from "@opencode/ui/menu"
import { useServerCtx } from "@/runtime/server/runtime"
import { useLanguage } from "@/runtime/i18n/language"
import { ServerConnection, useServers } from "@/runtime/server/registry"
import { displayName } from "@opencode/ui/project-avatar"
import { displayName, ProjectAvatar } from "@opencode/ui/project-avatar"
import { SessionTabAvatarView } from "@/shell/layout/session-tab-avatar"
import type { SessionInfo } from "@opencode/client/promise"
import { showToast } from "@/shell/notifications/toast"
@@ -71,6 +71,7 @@ export function TabNavItem(
unread: boolean
attention: boolean
loading: boolean
shortcut?: number
fallbackTitle?: string
fallbackDirectory?: string
/** Shows a title at once: the optimistic one, or the one to restore when its save fails. */
@@ -461,7 +462,14 @@ export function TabNavItem(
<span class="block size-4 rounded-[3px] border border-v2-border-border-muted" aria-hidden="true" />
}
>
{(directory) => <SessionTabAvatarView project={project()} directory={directory()} unread={false} />}
{(directory) => (
<SessionTabAvatarView
project={project()}
directory={directory()}
unread={false}
label={props.shortcut?.toString()}
/>
)}
</Show>
<Show when={dotted()}>
<span data-slot="tab-avatar-unread-dot" aria-hidden="true" />
@@ -610,6 +618,7 @@ export function DraftTabItem(
ref?: Ref<HTMLDivElement>
href: string
title: string
shortcut?: number
active?: boolean
onNavigate: () => void
suppressNavigation?: boolean
@@ -684,8 +693,9 @@ export function DraftTabItem(
}}
class="flex h-full min-w-0 flex-1 flex-row items-center gap-1.5 text-[13px] font-medium text-v2-text-text-faint group-data-[active='true']:text-v2-text-text-base [-webkit-user-drag:none]"
>
<span class="flex size-4 shrink-0 items-center justify-center">
<span class="relative flex size-4 shrink-0 items-center justify-center">
<svg
data-slot="tab-icon-face"
class="text-v2-icon-icon-muted group-data-[active='true']:text-v2-icon-icon-base"
width="16"
height="16"
@@ -699,6 +709,13 @@ export function DraftTabItem(
stroke="currentColor"
/>
</svg>
<Show when={props.shortcut}>
{(shortcut) => (
<span data-slot="tab-shortcut-face" aria-hidden="true">
<ProjectAvatar fallback="" label={shortcut().toString()} variant="outline" />
</span>
)}
</Show>
</span>
<span
data-titlebar-tab-title
@@ -45,8 +45,19 @@ import {
type SessionTabView,
} from "./tab-load-machine"
import { setSidebarUnread } from "@/shell/sidebar-peek"
import { createSideFocus } from "@/runtime/plugin/closed-tabs"
import { createEventListener } from "@solid-primitives/event-listener"
import {
INITIAL_TAB_HINTS_STATE,
selectTabHintsShown,
transitionTabHints,
type TabHintsEffect,
type TabHintsEvent,
} from "./tab-hints-machine"
import type { SessionInfo } from "@opencode/client/promise"
const IS_MAC = typeof navigator !== "undefined" && /(Mac|iPod|iPhone|iPad)/.test(navigator.platform)
export const previousSessionCommand = { id: "session.previous", keybind: "alt+arrowup" }
export const nextSessionCommand = { id: "session.next", keybind: "alt+arrowdown" }
@@ -72,6 +83,7 @@ function SessionTabSlot(
unread: boolean
attention: boolean
loading: boolean
shortcut?: number
fallbackTitle?: string
fallbackDirectory?: string
onRememberTitle: (sessionID: string, title: string | undefined) => void
@@ -118,6 +130,7 @@ function SessionTabSlot(
unread={props.unread}
attention={props.attention}
loading={props.loading}
shortcut={props.shortcut}
fallbackTitle={props.fallbackTitle}
fallbackDirectory={props.fallbackDirectory}
onRememberTitle={props.onRememberTitle}
@@ -146,6 +159,7 @@ function SessionTabEntry(
orientation: "horizontal" | "vertical"
serverCtx: ServerCtx | undefined
view: SessionTabView
shortcut?: number
onUnreadChange: (unread: boolean) => void
onNavigate: (element: HTMLDivElement) => void
},
@@ -318,6 +332,7 @@ function SessionTabEntry(
unread={unread()}
attention={status.attention()}
loading={status.loading()}
shortcut={props.shortcut}
fallbackTitle={
{
session: language.t("session.tab.session"),
@@ -348,6 +363,7 @@ function DraftTabSlot(
active: boolean
orientation: "horizontal" | "vertical"
title: string
shortcut?: number
onNavigate: (element: HTMLDivElement) => void
},
) {
@@ -381,6 +397,7 @@ function DraftTabSlot(
}}
href={tabHref(props.tab)}
title={props.title}
shortcut={props.shortcut}
onNavigate={() => props.onNavigate(ref)}
onClose={props.onClose}
onCloseOthers={props.onCloseOthers}
@@ -608,6 +625,63 @@ export function TitlebarTabStrip(props: {
})),
)
// Inside the side panel the same keys pick side tabs, so the numbers stay hidden there.
const [hints, setHints] = createSignal(INITIAL_TAB_HINTS_STATE)
const side = createSideFocus()
let hintTimer: ReturnType<typeof setTimeout> | undefined
const dispatchHints = (event: TabHintsEvent) => {
const next = transitionTabHints(hints(), event)
if (next.state !== hints()) setHints(next.state)
next.effects.forEach(runHintsEffect)
}
function runHintsEffect(effect: TabHintsEffect) {
clearTimeout(hintTimer)
if (effect.type === "delay.clear") return
hintTimer = setTimeout(() => dispatchHints({ type: "delay.elapsed", attempt: effect.attempt }), effect.delayMs)
}
const held = (event: KeyboardEvent | PointerEvent) => ({
mod: IS_MAC ? event.metaKey : event.ctrlKey,
extra: event.shiftKey || event.altKey || (IS_MAC ? event.ctrlKey : event.metaKey),
})
createEventListener(
window,
"keydown",
(event) =>
dispatchHints({
type: "key.down",
key:
event.key === (IS_MAC ? "Meta" : "Control")
? "mod"
: /^Digit[1-9]$/.test(event.code) || /^[1-9]$/.test(event.key)
? "digit"
: "other",
...held(event),
}),
{ capture: true },
)
createEventListener(window, "keyup", (event) => dispatchHints({ type: "modifiers", ...held(event) }), {
capture: true,
})
createEventListener(
window,
["pointermove", "pointerdown"],
(event) => dispatchHints({ type: "modifiers", ...held(event) }),
{ capture: true, passive: true },
)
createEventListener(window, "blur", () => dispatchHints({ type: "window.left" }))
createEventListener(document, "visibilitychange", () => {
if (document.hidden) dispatchHints({ type: "window.left" })
})
onCleanup(() => dispatchHints({ type: "disposed" }))
const shortcut = (index: number) => (index >= 0 && index < 9 ? index + 1 : undefined)
function selectAdjacentTab(offset: -1 | 1, include?: (key: string) => boolean) {
const current = props.currentTab
const key = adjacentTabKey(visibleTabIds(), current ? tabKey(current) : undefined, offset, include)
@@ -620,6 +694,7 @@ export function TitlebarTabStrip(props: {
<div
data-slot={vertical() ? "vertical-tabs" : "titlebar-tabs"}
data-orientation={vertical() ? "vertical" : "horizontal"}
data-shortcut-hints={selectTabHintsShown(hints()) && !side.focused() ? "" : undefined}
class="relative min-w-0"
classList={{ "min-h-0 overflow-hidden": vertical() }}
>
@@ -739,6 +814,7 @@ export function TitlebarTabStrip(props: {
orientation={vertical() ? "vertical" : "horizontal"}
serverCtx={serverCtx()}
view={views().get(id) ?? hiddenTabView}
shortcut={shortcut(visibleIndex())}
onUnreadChange={(value) => setUnread(id, value)}
onNavigate={(element) => {
ref = element
@@ -759,6 +835,7 @@ export function TitlebarTabStrip(props: {
active={props.currentTab === tab && props.highlightActive !== false}
orientation={vertical() ? "vertical" : "horizontal"}
title={language.t("session.tab.session")}
shortcut={shortcut(visibleIndex())}
onNavigate={(element) => {
ref = element
props.onNavigate(tab, element)
@@ -88,6 +88,19 @@
text-shadow: none;
}
[data-slot="project-avatar-surface"] [data-slot="project-avatar-label"] {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
visibility: hidden;
}
[data-slot="project-avatar-surface"]:is([data-has-image], [data-generated]) [data-slot="project-avatar-label"] {
background: var(--project-avatar-bg);
}
[data-slot="project-avatar-surface"][data-generated] {
background: none;
box-shadow: none;
@@ -147,6 +147,8 @@ export interface ProjectAvatarProps extends ComponentProps<"div"> {
unread?: boolean
/** Seeds the generated default avatar shown when there is no image; see getProjectAvatarGenerated. */
generated?: string
/** Text stacked over the avatar, hidden until the caller's CSS reveals `[data-slot="project-avatar-label"]`. */
label?: string
}
export function ProjectAvatar(props: ProjectAvatarProps) {
@@ -156,6 +158,7 @@ export function ProjectAvatar(props: ProjectAvatarProps) {
"variant",
"unread",
"generated",
"label",
"class",
"classList",
"style",
@@ -184,13 +187,23 @@ export function ProjectAvatar(props: ProjectAvatarProps) {
<Show
when={split.src}
fallback={
<Show when={generated()} fallback={first(split.fallback)}>
<Show
when={generated()}
fallback={<span data-slot="project-avatar-initial">{first(split.fallback)}</span>}
>
{(key) => <GeneratedAvatar seed={key()} />}
</Show>
}
>
{(value) => <img src={value()} draggable={false} data-slot="project-avatar-image" />}
</Show>
<Show when={split.label}>
{(label) => (
<span data-slot="project-avatar-label" aria-hidden="true">
{label()}
</span>
)}
</Show>
</div>
<Show when={split.unread}>
<span data-slot="project-avatar-unread-dot" aria-hidden="true" />