diff --git a/e2e/gitLogScrollbar.spec.ts b/e2e/gitLogScrollbar.spec.ts index e70fca87b..00e67db82 100644 --- a/e2e/gitLogScrollbar.spec.ts +++ b/e2e/gitLogScrollbar.spec.ts @@ -76,6 +76,35 @@ test.beforeEach(async ({ workbench: page }) => { }); }); +test("keeps the last Log mounted and prevents focus while another Git tab is shown", async ({ workbench: page }) => { + const list = page.locator('[data-git-log] [role="listbox"]'); + const original = await list.elementHandle(); + await page.getByRole("tab", { name: "Console", exact: true }).click(); + await expect(page.getByRole("tab", { name: "Console", exact: true })).toHaveAttribute("aria-selected", "true"); + expect(await list.evaluate((element, previous) => element === previous, original)).toBe(true); + + expect( + await list.evaluate((element) => { + // WebKit briefly paints a parked log while clearing its text selection. + const host = element.closest("[data-git-log]")!.parentElement!; + host.setAttribute("data-selection", ""); + try { + element.focus(); + return host.contains(document.activeElement); + } finally { + host.removeAttribute("data-selection"); + } + }), + ).toBe(false); + + await page.getByRole("tab", { name: "Worktrees", exact: true }).click(); + await page.getByRole("tab", { name: "Log", exact: true }).click(); + await expect(page.getByRole("listbox", { name: "Commits" })).toBeVisible(); + expect(await list.evaluate((element, previous) => element === previous, original)).toBe(true); + await list.focus(); + await expect(list).toBeFocused(); +}); + test("fits a wide graph and saved columns without horizontal scrolling", async ({ workbench: page }) => { await page.evaluate(async () => { const module = "/src/features/vcs/logOptions.ts"; diff --git a/src/features/vcs/GitLog.module.css b/src/features/vcs/GitLog.module.css index 2221b1928..eb5c56a85 100644 --- a/src/features/vcs/GitLog.module.css +++ b/src/features/vcs/GitLog.module.css @@ -1,9 +1,26 @@ .root { + position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; } +/* Absolute in both states, so hiding and showing the log doesn't lay it out again. */ +.logHost { + position: absolute; + inset: 0; + display: flex; + flex-direction: column; +} +.logHidden { + content-visibility: hidden; + z-index: -1; +} +/* WebKit leaves a stale selection highlight on a subtree whose rendering was skipped when the selection cleared. */ +.logHidden[data-selection] { + content-visibility: visible; + opacity: 0; +} .headerTabs { min-width: 0; border: 0; diff --git a/src/features/vcs/GitLog.tsx b/src/features/vcs/GitLog.tsx index e1e271e51..e0dd69169 100644 --- a/src/features/vcs/GitLog.tsx +++ b/src/features/vcs/GitLog.tsx @@ -14,6 +14,7 @@ import { useFrozenWhileWindowResizes, } from "@/ui"; import { copySelectionOnKeyDown } from "@/ui/selectableText"; +import { useSelectionVisibility } from "@/ui/useSelectionBoundary"; import { spanStyle } from "@/ui/ansi"; import clsx from "clsx"; import { useContext, useEffect, useLayoutEffect, useRef, useState } from "react"; @@ -34,6 +35,7 @@ import { LogScope } from "./LogScope"; import { closeLogTab, initLogBranchFilter, + isLogTab, logState, refsSignature, reloadLog, @@ -85,6 +87,15 @@ export function GitToolWindow() { // window drag; they follow the window once it stops. const root = useRef(null); useFrozenWhileWindowResizes(root); + // The last log tab shown stays mounted (hidden, keeping its layout) while another tab is shown, + // so switching back to it neither rebuilds its rows, graph and branch tree nor reloads it. + const logTabs = useGitLog((s) => s.logTabs); + const logShown = isLogTab(shown, logTabs); + const [lastLog, setLastLog] = useState(null); + if (logShown && lastLog !== shown) setLastLog(shown); + const keptLog = logShown ? shown : lastLog && isLogTab(lastLog, logTabs) ? lastLog : null; + const logHost = useRef(null); + useSelectionVisibility(logHost, !!keptLog && (available || external)); if (!available && !external) return ( @@ -107,11 +118,14 @@ export function GitToolWindow() { ) : shown === "worktrees" ? ( - ) : ( - // Each log tab has its own view (its filters and commits are swapped into the store). - - - + ) : null} + {keptLog && ( +
+ {/* Each log tab has its own view (its filters and commits are swapped into the store). */} + + + +
)} );