diff --git a/packages/app/src/shell/layout/session-tab-avatar.tsx b/packages/app/src/shell/layout/session-tab-avatar.tsx index 1178ed8d626..7eb0c7af574 100644 --- a/packages/app/src/shell/layout/session-tab-avatar.tsx +++ b/packages/app/src/shell/layout/session-tab-avatar.tsx @@ -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 ( ) diff --git a/packages/app/src/shell/titlebar/tab-hints-machine.test.ts b/packages/app/src/shell/titlebar/tab-hints-machine.test.ts new file mode 100644 index 00000000000..7d7cd57418b --- /dev/null +++ b/packages/app/src/shell/titlebar/tab-hints-machine.test.ts @@ -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([]) + }) +}) diff --git a/packages/app/src/shell/titlebar/tab-hints-machine.ts b/packages/app/src/shell/titlebar/tab-hints-machine.ts new file mode 100644 index 00000000000..fda934aeb9b --- /dev/null +++ b/packages/app/src/shell/titlebar/tab-hints-machine.ts @@ -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 } +} diff --git a/packages/app/src/shell/titlebar/tab-nav.css b/packages/app/src/shell/titlebar/tab-nav.css index 0dda7bfb083..d7afd5867f3 100644 --- a/packages/app/src/shell/titlebar/tab-nav.css +++ b/packages/app/src/shell/titlebar/tab-nav.css @@ -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; + } } diff --git a/packages/app/src/shell/titlebar/tab-nav.tsx b/packages/app/src/shell/titlebar/tab-nav.tsx index 0610beda6ef..ea42f57b443 100644 --- a/packages/app/src/shell/titlebar/tab-nav.tsx +++ b/packages/app/src/shell/titlebar/tab-nav.tsx @@ -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(