feat(tabs): add Ctrl+Tab MRU switcher

This commit is contained in:
Allen authored and GitHub committed 2026-08-20 22:12:52 +08:00
1 parent 81872165da
commit 7ce992b8f8
18 files changed
+615 -1

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
/**
* Ordering and selection helpers for the JetBrains-style Ctrl+Tab switcher.
* Pure functions so the behavior is unit-testable without mounting the UI.
*/
/**
* Orders tabs by most-recent use: tabs that appear in `mruIds` (oldest first,
* as recorded on activation) come first in reverse-chronological order; tabs
* never visited (e.g. restored from a previous session) follow in tab-bar
* order. Ids that no longer have a tab are dropped.
*/
export function tabSwitcherOrder<T extends { id: string }>(tabs: readonly T[], mruIds: readonly string[]): T[] {
const tabById = new Map(tabs.map((tab) => [tab.id, tab]));
const ordered: T[] = [];
const seen = new Set<string>();
for (let i = mruIds.length - 1; i >= 0; i--) {
const id = mruIds[i]!;
if (seen.has(id)) continue;
seen.add(id);
const tab = tabById.get(id);
if (tab) ordered.push(tab);
}
for (const tab of tabs) {
if (!seen.has(tab.id)) ordered.push(tab);
}
return ordered;
}
/** Initial highlight when the switcher opens: the previous tab, so a quick tap toggles between the two most recent tabs. */
export function initialTabSwitcherSelection(count: number): number {
return count > 1 ? 1 : 0;
}
/** Moves the highlight with wrap-around; returns -1 when there is nothing to select. */
export function moveTabSwitcherSelection(current: number, delta: -1 | 1, count: number): number {
if (count <= 0) return -1;
if (current < 0) return delta > 0 ? 0 : count - 1;
return (current + delta + count) % count;
}
@@ -0,0 +1,124 @@
import { tabSwitcherDirectionFromShortcut, type ShortcutLikeEvent } from "@/lib/editor/keyboardShortcuts";
import { isMacShortcutPlatform, parseShortcutParts } from "@/lib/editor/shortcutDisplay";
export type TabSwitcherKeyboardAction = { type: "move"; direction: -1 | 1 } | { type: "commit" } | { type: "cancel" };
export interface TabSwitcherKeyboardControllerOptions {
isOpen: () => boolean;
shortcut: () => string;
move: (direction: -1 | 1) => void;
commit: () => void;
cancel: () => void;
}
function normalizeReleaseKey(key: string): string {
if (key === "Ctrl" || key === "Control") return "Control";
if (key === "Cmd" || key === "Meta") return "Meta";
if (key === "Space") return " ";
if (key === "Plus") return "+";
return key.length === 1 ? key.toLowerCase() : key;
}
export function tabSwitcherReleaseKeys(shortcut: string, event: ShortcutLikeEvent, platform = globalThis.navigator?.platform || ""): ReadonlySet<string> {
const parts = parseShortcutParts(shortcut);
const key = parts[parts.length - 1];
const modifiers = new Set(parts.slice(0, -1));
const releaseKeys = new Set<string>();
if (modifiers.has("Mod")) {
if (isMacShortcutPlatform(platform)) {
releaseKeys.add("Meta");
} else {
if (event.ctrlKey) releaseKeys.add("Control");
if (event.metaKey) releaseKeys.add("Meta");
}
}
if (modifiers.has("Ctrl") || modifiers.has("Control")) releaseKeys.add("Control");
if (modifiers.has("Meta") || modifiers.has("Cmd")) releaseKeys.add("Meta");
if (modifiers.has("Alt")) releaseKeys.add("Alt");
if (modifiers.has("Shift")) releaseKeys.add("Shift");
if (!releaseKeys.size && key) releaseKeys.add(normalizeReleaseKey(key));
return releaseKeys;
}
export function tabSwitcherKeydownAction(event: ShortcutLikeEvent, shortcut: string, platform = globalThis.navigator?.platform || ""): TabSwitcherKeyboardAction | null {
const direction = tabSwitcherDirectionFromShortcut(event, { tabSwitcher: shortcut }, platform);
if (direction) return { type: "move", direction };
if (event.key === "ArrowDown") return { type: "move", direction: 1 };
if (event.key === "ArrowUp") return { type: "move", direction: -1 };
if (event.key === "Enter") return { type: "commit" };
if (event.key === "Escape") return { type: "cancel" };
return null;
}
export function captureTabSwitcherKeydown(event: KeyboardEvent, shortcut: string, onAction: (action: TabSwitcherKeyboardAction) => void, platform = globalThis.navigator?.platform || ""): boolean {
const action = tabSwitcherKeydownAction(event, shortcut, platform);
if (!action) return false;
event.preventDefault();
event.stopPropagation();
onAction(action);
return true;
}
export function createTabSwitcherKeyboardController(options: TabSwitcherKeyboardControllerOptions, platform = globalThis.navigator?.platform || "") {
let releaseKeys: ReadonlySet<string> = new Set();
function reset() {
releaseKeys = new Set();
}
function rememberOpeningEvent(event: ShortcutLikeEvent) {
releaseKeys = tabSwitcherReleaseKeys(options.shortcut(), event, platform);
}
function handleKeydownCapture(event: KeyboardEvent): boolean {
if (!options.isOpen()) return false;
return captureTabSwitcherKeydown(
event,
options.shortcut(),
(action) => {
if (action.type === "move") {
options.move(action.direction);
} else if (action.type === "commit") {
reset();
options.commit();
} else {
reset();
options.cancel();
}
},
platform,
);
}
function handleKeyup(event: KeyboardEvent): boolean {
if (!options.isOpen() || !releaseKeys.has(normalizeReleaseKey(event.key))) return false;
reset();
options.commit();
return true;
}
function cancelForDeactivation(): boolean {
if (!options.isOpen()) {
reset();
return false;
}
reset();
options.cancel();
return true;
}
function handleVisibilityChange(visibilityState: DocumentVisibilityState): boolean {
return visibilityState === "visible" ? false : cancelForDeactivation();
}
return {
reset,
rememberOpeningEvent,
handleKeydownCapture,
handleKeyup,
handleWindowBlur: cancelForDeactivation,
handleVisibilityChange,
};
}