diff --git a/src/features/profiler/ProfilerView.test.tsx b/src/features/profiler/ProfilerView.test.tsx index 508137c901..4a56c006d6 100644 --- a/src/features/profiler/ProfilerView.test.tsx +++ b/src/features/profiler/ProfilerView.test.tsx @@ -1,8 +1,10 @@ -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; +import { registerAction } from "@/platform/actions"; import type { Profile, ProfileMode } from "@/platform/ipc"; import { useI18n } from "@/platform/i18n"; +import { installKeyboardDispatcher } from "@/platform/keymap"; import { ProfilerTabs, ProfilerView } from "./ProfilerView"; import { addSnapshot, resetProfiler, useProfiler } from "./store"; @@ -99,6 +101,37 @@ describe("ProfilerView", () => { expect(screen.getByText("Zoomed into Main.run")).toBeInTheDocument(); }); + it("reserves Escape ahead of the workbench only when the zoomed graph has focus", async () => { + addSnapshot("App", profile("cpu", 120)); + const focusEditor = vi.fn(); + const unregister = registerAction({ id: "toolwindow.focusEditor", title: "Focus Editor", run: focusEditor }); + const uninstall = installKeyboardDispatcher(); + try { + render(); + const user = userEvent.setup(); + const graph = screen.getByRole("tree", { name: "Flame graph" }); + graph.focus(); + await user.keyboard("{ArrowDown}{Enter}"); + expect(screen.getByText("Zoomed into Main.run")).toBeInTheDocument(); + + screen.getByRole("textbox", { name: "Search frames" }).focus(); + await user.keyboard("{Escape}"); + expect(focusEditor).toHaveBeenCalledOnce(); + expect(screen.getByText("Zoomed into Main.run")).toBeInTheDocument(); + + graph.focus(); + await user.keyboard("{Escape}"); + expect(screen.queryByText("Zoomed into Main.run")).not.toBeInTheDocument(); + expect(focusEditor).toHaveBeenCalledOnce(); + expect(graph).toHaveFocus(); + await user.keyboard("{Escape}"); + expect(focusEditor).toHaveBeenCalledTimes(2); + } finally { + uninstall(); + unregister(); + } + }); + it("allocation profile shows bytes and marks type frames", () => { addSnapshot("App", profile("allocations", 640_000_000)); useProfiler.setState({ tab: "tree" }); diff --git a/src/features/profiler/ProfilerView.tsx b/src/features/profiler/ProfilerView.tsx index fd07a99bc2..8fd458f1f9 100644 --- a/src/features/profiler/ProfilerView.tsx +++ b/src/features/profiler/ProfilerView.tsx @@ -6,6 +6,7 @@ import { useCallback, useId, useLayoutEffect, useMemo, useRef, useState, type KeyboardEvent } from "react"; import clsx from "clsx"; import type { Profile } from "@/platform/ipc"; +import { registerKeyClaim } from "@/platform/keymap"; import { EmptyState, Icon, IconButton, Input, Tabs, Tree, T } from "@/ui"; import { editorService } from "@/workbench/contributions"; import { colorSlot, flameLayout, formatWeight, frameLocation, matchingFrames, parentIndex, share } from "./model"; @@ -113,6 +114,10 @@ function FlameGraph({ profile, query }: { profile: Profile; query: string }) { } }, [activeId]); useLayoutEffect(scrollToActive, [scrollToActive, layout.depth]); + useLayoutEffect(() => { + if (zoom === 0) return; + return registerKeyClaim((event, stroke) => stroke === "Escape" && event.target === graphRef.current); + }, [zoom]); const zoomTo = (node: number) => { setZoom(node);