feat(web): add shortcuts to the surface dropdown (#7318)
This commit is contained in:
1 parent
5ea5a80a83
commit
3b8e7bbbe0
2 files changed
+149
-58
No files matched your search
@@ -2,7 +2,22 @@ import type { DesktopPreviewFavicon, PreviewSessionSnapshot } from "@t3tools/con
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { RightPanelTabs, tabMuteMenuItem } from "./RightPanelTabs";
|
||||
import { RightPanelTabs, surfaceShortcutActionForKey, tabMuteMenuItem } from "./RightPanelTabs";
|
||||
|
||||
function shortcutEvent(
|
||||
key: string,
|
||||
overrides: Partial<Parameters<typeof surfaceShortcutActionForKey>[1]> = {},
|
||||
): Parameters<typeof surfaceShortcutActionForKey>[1] {
|
||||
return {
|
||||
key,
|
||||
altKey: false,
|
||||
ctrlKey: false,
|
||||
defaultPrevented: false,
|
||||
isComposing: false,
|
||||
metaKey: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
const previewSurface = {
|
||||
id: "browser:tab-1" as const,
|
||||
@@ -125,6 +140,32 @@ describe("RightPanelTabs preview favicon", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("surface shortcuts", () => {
|
||||
const actions = [
|
||||
{ shortcut: "B", available: true, label: "Browser" },
|
||||
{ shortcut: "D", available: false, label: "Diff" },
|
||||
] as const;
|
||||
|
||||
it("matches available surface shortcuts case-insensitively", () => {
|
||||
expect(surfaceShortcutActionForKey(actions, shortcutEvent("b"))).toBe(actions[0]);
|
||||
expect(surfaceShortcutActionForKey(actions, shortcutEvent("B"))).toBe(actions[0]);
|
||||
});
|
||||
|
||||
it("does not activate unavailable surfaces", () => {
|
||||
expect(surfaceShortcutActionForKey(actions, shortcutEvent("d"))).toBeNull();
|
||||
});
|
||||
|
||||
it("leaves modified, composing, and already-handled key events alone", () => {
|
||||
expect(surfaceShortcutActionForKey(actions, shortcutEvent("b", { metaKey: true }))).toBeNull();
|
||||
expect(
|
||||
surfaceShortcutActionForKey(actions, shortcutEvent("b", { isComposing: true })),
|
||||
).toBeNull();
|
||||
expect(
|
||||
surfaceShortcutActionForKey(actions, shortcutEvent("b", { defaultPrevented: true })),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("RightPanelTabs audio indicator", () => {
|
||||
// A muted tab only shows the indicator while it is actually making sound:
|
||||
// arming mute on a quiet tab is deliberate and stays invisible until there
|
||||
|
||||
@@ -31,7 +31,7 @@ import { readLocalApi } from "~/localApi";
|
||||
import { Button } from "~/components/ui/button";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
|
||||
import { Kbd } from "~/components/ui/kbd";
|
||||
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
|
||||
import { Menu, MenuItem, MenuPopup, MenuShortcut, MenuTrigger } from "~/components/ui/menu";
|
||||
import { ScrollArea } from "~/components/ui/scroll-area";
|
||||
import { faviconUrlForOrigin } from "~/lib/favicon";
|
||||
import { useTheme } from "~/hooks/useTheme";
|
||||
@@ -173,6 +173,23 @@ function tabAudioState(overlay: DesktopPreviewOverlay | null): TabAudioState {
|
||||
return overlay.audioMuted ? "muted" : "audible";
|
||||
}
|
||||
|
||||
type SurfaceShortcutEvent = Pick<
|
||||
KeyboardEvent,
|
||||
"altKey" | "ctrlKey" | "defaultPrevented" | "isComposing" | "key" | "metaKey"
|
||||
>;
|
||||
|
||||
export function surfaceShortcutActionForKey<
|
||||
const Action extends { available: boolean; shortcut: string },
|
||||
>(actions: readonly Action[], event: SurfaceShortcutEvent): Action | null {
|
||||
if (event.defaultPrevented || event.isComposing) return null;
|
||||
if (event.metaKey || event.ctrlKey || event.altKey) return null;
|
||||
return (
|
||||
actions.find(
|
||||
(action) => action.available && action.shortcut.toLowerCase() === event.key.toLowerCase(),
|
||||
) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement }) {
|
||||
return (
|
||||
<Tooltip>
|
||||
@@ -185,6 +202,7 @@ function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement })
|
||||
function SurfaceMenuItem(props: {
|
||||
available: boolean;
|
||||
disabledReason?: string;
|
||||
shortcut: string;
|
||||
onClick: () => void;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
@@ -193,8 +211,10 @@ function SurfaceMenuItem(props: {
|
||||
className={!props.available ? "data-disabled:pointer-events-auto" : undefined}
|
||||
onClick={props.onClick}
|
||||
disabled={!props.available}
|
||||
aria-keyshortcuts={props.shortcut}
|
||||
>
|
||||
{props.children}
|
||||
<MenuShortcut>{props.shortcut}</MenuShortcut>
|
||||
</MenuItem>
|
||||
);
|
||||
if (props.available || !props.disabledReason) return item;
|
||||
@@ -305,8 +325,8 @@ function RightPanelEmptyState(props: {
|
||||
});
|
||||
useEffect(() => {
|
||||
const handler = (event: KeyboardEvent) => {
|
||||
if (event.defaultPrevented || event.isComposing) return;
|
||||
if (event.metaKey || event.ctrlKey || event.altKey) return;
|
||||
const action = surfaceShortcutActionForKey(shortcutActionsRef.current, event);
|
||||
if (!action) return;
|
||||
if (document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS)) return;
|
||||
const target = event.target;
|
||||
if (target instanceof HTMLElement) {
|
||||
@@ -316,10 +336,6 @@ function RightPanelEmptyState(props: {
|
||||
const editable = target.isContentEditable ? target : target.closest("[contenteditable]");
|
||||
if (editable && (editable.textContent ?? "").trim().length > 0) return;
|
||||
}
|
||||
const action = shortcutActionsRef.current.find(
|
||||
(candidate) => candidate.shortcut.toLowerCase() === event.key.toLowerCase(),
|
||||
);
|
||||
if (!action) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
action.onClick();
|
||||
@@ -573,6 +589,67 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
|
||||
const ownsDesktopTitleBar = isElectron && props.mode === "inline";
|
||||
const { resolvedTheme } = useTheme();
|
||||
const tabListRef = useRef<HTMLDivElement>(null);
|
||||
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
|
||||
|
||||
const addSurfaceActions = [
|
||||
{
|
||||
label: "Browser",
|
||||
icon: Globe2,
|
||||
shortcut: "B",
|
||||
available: props.browserAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.browser,
|
||||
onClick: props.onAddBrowser,
|
||||
},
|
||||
{
|
||||
label: "Terminal",
|
||||
icon: TerminalSquare,
|
||||
shortcut: "T",
|
||||
available: props.terminalAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.terminal,
|
||||
onClick: props.onAddTerminal,
|
||||
},
|
||||
{
|
||||
label: "Files",
|
||||
icon: Files,
|
||||
shortcut: "F",
|
||||
available: props.filesAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.files,
|
||||
onClick: props.onAddFiles,
|
||||
},
|
||||
{
|
||||
label: "Diff",
|
||||
icon: FileDiff,
|
||||
shortcut: "D",
|
||||
available: props.diffAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.diff,
|
||||
onClick: props.onAddDiff,
|
||||
},
|
||||
{
|
||||
label: "Pull request",
|
||||
icon: GitPullRequest,
|
||||
shortcut: "P",
|
||||
available: props.pullRequestAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.pullRequest,
|
||||
onClick: props.onAddPullRequest,
|
||||
},
|
||||
{
|
||||
label: "Agents",
|
||||
icon: Bot,
|
||||
shortcut: "A",
|
||||
available: props.agentsAvailable,
|
||||
disabledReason: SURFACE_DISABLED_REASONS.agents,
|
||||
onClick: props.onAddAgents,
|
||||
},
|
||||
] as const;
|
||||
|
||||
const handleAddSurfaceMenuKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
|
||||
const action = surfaceShortcutActionForKey(addSurfaceActions, event.nativeEvent);
|
||||
if (!action) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
setAddSurfaceMenuOpen(false);
|
||||
action.onClick();
|
||||
};
|
||||
|
||||
const handleTabContextMenu = useCallback(
|
||||
async (event: ReactMouseEvent, surface: RightPanelSurface) => {
|
||||
@@ -804,7 +881,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
|
||||
);
|
||||
})}
|
||||
{props.surfaces.length > 0 ? (
|
||||
<Menu>
|
||||
<Menu open={addSurfaceMenuOpen} onOpenChange={setAddSurfaceMenuOpen}>
|
||||
<MenuTrigger
|
||||
render={
|
||||
<Button
|
||||
@@ -817,55 +894,28 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
|
||||
>
|
||||
<Plus className="size-3.5" />
|
||||
</MenuTrigger>
|
||||
<MenuPopup align="start" side="bottom" sideOffset={6} className="min-w-44">
|
||||
<SurfaceMenuItem
|
||||
available={props.browserAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.browser}
|
||||
onClick={props.onAddBrowser}
|
||||
>
|
||||
<Globe2 />
|
||||
Browser
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.terminalAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.terminal}
|
||||
onClick={props.onAddTerminal}
|
||||
>
|
||||
<TerminalSquare />
|
||||
Terminal
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.filesAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.files}
|
||||
onClick={props.onAddFiles}
|
||||
>
|
||||
<Files />
|
||||
Files
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.diffAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.diff}
|
||||
onClick={props.onAddDiff}
|
||||
>
|
||||
<FileDiff />
|
||||
Diff
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.pullRequestAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.pullRequest}
|
||||
onClick={props.onAddPullRequest}
|
||||
>
|
||||
<GitPullRequest />
|
||||
Pull request
|
||||
</SurfaceMenuItem>
|
||||
<SurfaceMenuItem
|
||||
available={props.agentsAvailable}
|
||||
disabledReason={SURFACE_DISABLED_REASONS.agents}
|
||||
onClick={props.onAddAgents}
|
||||
>
|
||||
<Bot />
|
||||
Agents
|
||||
</SurfaceMenuItem>
|
||||
<MenuPopup
|
||||
align="start"
|
||||
side="bottom"
|
||||
sideOffset={6}
|
||||
className="min-w-44"
|
||||
onKeyDownCapture={handleAddSurfaceMenuKeyDown}
|
||||
>
|
||||
{addSurfaceActions.map((action) => {
|
||||
const Icon = action.icon;
|
||||
return (
|
||||
<SurfaceMenuItem
|
||||
key={action.label}
|
||||
available={action.available}
|
||||
disabledReason={action.disabledReason}
|
||||
shortcut={action.shortcut}
|
||||
onClick={action.onClick}
|
||||
>
|
||||
<Icon />
|
||||
{action.label}
|
||||
</SurfaceMenuItem>
|
||||
);
|
||||
})}
|
||||
</MenuPopup>
|
||||
</Menu>
|
||||
) : null}
|
||||
|
||||
Reference in new issue
Block a user