diff --git a/e2e/editorTabClose.spec.ts b/e2e/editorTabClose.spec.ts new file mode 100644 index 000000000..928e2e96a --- /dev/null +++ b/e2e/editorTabClose.spec.ts @@ -0,0 +1,90 @@ +import type { Locator, Page } from "@playwright/test"; +import { expect, openFromTree, test } from "./fixtures"; + +async function setTabs(page: Page, tabs: Record) { + await page.evaluate(async (tabs) => { + const projectModule = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ projectModule); + const { settings } = useProject.getState(); + useProject.setState({ + settings: { ...settings, editor: { ...settings?.editor, tabs: { ...settings?.editor?.tabs, ...tabs } } }, + }); + }, tabs); +} + +const box = async (locator: Locator) => (await locator.boundingBox())!; +const closeOf = (tab: Locator) => tab.locator("..").getByRole("button", { name: "Close Tab", exact: true }); + +test("a close button on the right keeps a narrow slot on every tab", async ({ workbench: page }) => { + await openFromTree(page, "README.md"); + await openFromTree(page, "build.gradle.kts"); + const strip = page.getByRole("tablist", { name: "Editor tabs" }); + const idle = strip.getByRole("tab", { name: "README.md" }); + const label = await box(idle.locator(":scope > span")); + const idleTab = await box(idle.locator("..")); + expect(idleTab.x + idleTab.width - (label.x + label.width)).toBeCloseTo(18, 0); + + const selected = strip.getByRole("tab", { name: "build.gradle.kts" }); + const close = await box(closeOf(selected)); + const tab = await box(selected.locator("..")); + const text = await box(selected.locator(":scope > span")); + expect(close.x).toBeGreaterThanOrEqual(text.x + text.width - 0.5); + expect(close.x + close.width).toBeLessThanOrEqual(tab.x + tab.width + 0.5); + expect(tab.x - (idleTab.x + idleTab.width)).toBeCloseTo(4, 0); +}); + +for (const closeButtonPosition of ["right", "left"]) { + test(`a dragged tab's ghost keeps the tab's layout, close button ${closeButtonPosition}`, async ({ + workbench: page, + }) => { + await openFromTree(page, "README.md"); + await openFromTree(page, "build.gradle.kts"); + await setTabs(page, { closeButtonPosition }); + const selected = page.getByRole("tablist", { name: "Editor tabs" }).getByRole("tab", { name: "build.gradle.kts" }); + const tab = await box(selected.locator("..")); + const close = await box(closeOf(selected)); + await page.mouse.move(tab.x + tab.width / 2, tab.y + tab.height / 2); + await page.mouse.down(); + await page.mouse.move(tab.x + tab.width / 2, tab.y + tab.height / 2 + 40, { steps: 4 }); + const ghost = page.locator('[aria-hidden="true"]:has([data-tab-close])'); + const copy = await box(ghost); + const copyClose = await box(ghost.locator("[data-tab-close] button")); + expect(copy.width).toBeCloseTo(tab.width, 0); + expect(copyClose.x - copy.x).toBeCloseTo(close.x - tab.x, 0); + expect(copyClose.x + copyClose.width).toBeLessThanOrEqual(copy.x + copy.width + 0.5); + await page.keyboard.press("Escape"); + await page.mouse.up(); + await expect(ghost).toHaveCount(0); + }); +} + +test("a close button on the left keeps a narrow slot before the icon", async ({ workbench: page }) => { + await openFromTree(page, "README.md"); + await openFromTree(page, "build.gradle.kts"); + await setTabs(page, { closeButtonPosition: "left" }); + const strip = page.getByRole("tablist", { name: "Editor tabs" }); + + // The selected and the hovered tab show their button in an 18px slot before the icon, which stays visible. + const selected = strip.getByRole("tab", { name: "build.gradle.kts" }); + const other = strip.getByRole("tab", { name: "README.md" }); + await other.hover(); + for (const tab of [selected, other]) { + const icon = tab.locator(":scope > :first-child"); + await expect(icon).toHaveCSS("opacity", "1"); + await expect(closeOf(tab)).toHaveCSS("opacity", "1"); + const slot = await box(tab.locator("..")); + const close = await box(closeOf(tab)); + const glyph = await box(icon); + const label = await box(tab.locator(":scope > span")); + expect(close.x + close.width).toBeLessThanOrEqual(glyph.x + 0.5); + expect(glyph.x - slot.x).toBeCloseTo(18, 0); + expect(slot.x + slot.width - (label.x + label.width)).toBeCloseTo(8, 0); + } + + // Without file icons the close button keeps its own place before the label. + await setTabs(page, { showFileIcon: false }); + await expect(selected.locator("svg, img")).toHaveCount(0); + const label = await box(selected.locator(":scope > span")); + const slot = await box(closeOf(selected)); + expect(slot.x + slot.width).toBeLessThanOrEqual(label.x + 0.5); +}); diff --git a/src/ui/Tabs.module.css b/src/ui/Tabs.module.css index 498b2e6e2..5e65917da 100644 --- a/src/ui/Tabs.module.css +++ b/src/ui/Tabs.module.css @@ -277,6 +277,37 @@ padding: 0 var(--tab-inline-padding, 10px) 0 2px; } +/* Editor tabs in a row sit 4px apart; wrapped rows keep their row gap. */ +.editor:not(.vertical) .scroller { + column-gap: var(--space-1); +} +/* Every closable tab keeps a close slot at one edge (IDEA), cut to what the close glyph needs: 18px, a 2px + inset and the 16px button. The label side keeps the 8px inset, so the side doesn't change a tab's width. */ +.editor:not(.vertical) .closeSlot { + position: absolute; + top: 0; + bottom: 0; + right: 2px; + width: auto; + margin: 0; +} +.editor:not(.vertical) .close { + width: 16px; +} +.editor:not(.vertical) .closeSlot .modified { + left: 5px; +} +.editor:not(.vertical, .closeLeft) .closable .tabButton { + padding-right: 18px; +} +.editor.closeLeft:not(.vertical) .closeSlot { + right: auto; + left: 2px; +} +.editor.closeLeft:not(.vertical) .closable .tabButton { + padding-left: 18px; +} + /* drag & drop: the dragged tab is an empty slot the other tabs make room for (IDEA) */ .tab.dragging, .tab.dragging:hover { diff --git a/src/ui/Tabs.test.tsx b/src/ui/Tabs.test.tsx index 2aeb18c8e..298aab8a0 100644 --- a/src/ui/Tabs.test.tsx +++ b/src/ui/Tabs.test.tsx @@ -358,7 +358,10 @@ describe("Tabs drag & drop", () => { hit = strip("Terminal sessions"); pointer(window, "pointermove", 60); expect(dragged).toHaveClass(styles.dragging!); - const ghost = document.body.lastElementChild as HTMLElement; + // The ghost sits in a copy of the strip's classes, so the strip's tab rules still lay it out. + const context = document.body.lastElementChild as HTMLElement; + expect(context.className).toBe(strip("Terminal sessions").parentElement!.className); + const ghost = context.firstElementChild as HTMLElement; expect(ghost).toHaveAttribute("aria-hidden", "true"); expect(ghost.style.position).toBe("fixed"); expect(shown("Terminal sessions")).toEqual(["Local", "Local (2)", "Local (3)"]); @@ -378,7 +381,7 @@ describe("Tabs drag & drop", () => { .map((b) => b.textContent), ).toEqual(["Local (2)", "Local", "Local (3)"]); expect(tab("Terminal sessions", "Local")).not.toHaveClass(styles.dragging!); - expect(document.body.lastElementChild).not.toBe(ghost); + expect(context.isConnected).toBe(false); }); it("puts the slot back when the pointer leaves the strip and drops nothing on Escape", () => { diff --git a/src/ui/Tabs.tsx b/src/ui/Tabs.tsx index 5cffe7a28..fb1b198d8 100644 --- a/src/ui/Tabs.tsx +++ b/src/ui/Tabs.tsx @@ -362,18 +362,16 @@ export function Tabs({ ) : null; const marker = dragging === null ? drop : null; + const stripClass = clsx( + styles.strip, + styles[variant], + vertical ? styles.vertical : layout !== "scroll" && styles[layout], + closePosition === "left" && styles.closeLeft, + className, + ); return ( -
+
{vertical && trailing &&
{trailing}
}
{ draggingRef.current = item.id; setDragging(item.id); diff --git a/src/ui/pointerDrag.test.ts b/src/ui/pointerDrag.test.ts index 4f5b5de4a..d0ff346d2 100644 --- a/src/ui/pointerDrag.test.ts +++ b/src/ui/pointerDrag.test.ts @@ -48,6 +48,37 @@ describe("startPointerDrag with a reordered macOS tap", () => { expect(onEnd).toHaveBeenCalledWith(true, expect.objectContaining({ x: 60 })); }); + it.each([ + ["dropped", () => fire("pointerup", 0, 40, 0)], + ["cancelled with Escape", () => window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }))], + ])("wraps the ghost in its source's context and removes both when %s", (_, end) => { + const source = document.body.appendChild(document.createElement("div")); + startPointerDrag( + { button: 0, clientX: 0, clientY: 0, altKey: false }, + source, + { type: "t", payload: "" }, + { ghostContext: "strip editor" }, + ); + fire("pointermove", 1, 40, 0); + const ghost = document.querySelector("[aria-hidden]")!; + const context = ghost.parentElement!; + expect(context.parentElement).toBe(document.body); + expect([...context.classList]).toEqual(["strip", "editor"]); + expect(context.style.display).toBe("contents"); + expect(ghost.style.position).toBe("fixed"); + end(); + expect(context.isConnected).toBe(false); + expect(document.body.children).toHaveLength(1); + }); + + it("puts the ghost straight into the body without a context", () => { + start(); + fire("pointermove", 1, 40, 0); + expect(document.querySelector("[aria-hidden]")!.parentElement).toBe(document.body); + fire("pointerup", 0, 40, 0); + expect(document.querySelector("[aria-hidden]")).toBeNull(); + }); + it("clears a text selection that began before the drag threshold (#3334)", () => { const text = document.body.appendChild(document.createElement("p")); text.textContent = "dragging a file must not select this"; diff --git a/src/ui/pointerDrag.ts b/src/ui/pointerDrag.ts index 69132955b..e90534f89 100644 --- a/src/ui/pointerDrag.ts +++ b/src/ui/pointerDrag.ts @@ -158,11 +158,17 @@ export function startPointerDrag( onEnd?: (dragged: boolean, drop?: PointerDrop) => void; /** Not subject to "Drag-and-drop with Alt pressed only" (tabs and stripe buttons). */ always?: boolean; + /** + * Classes of the container `source` is styled by (a tab strip's): the ghost is wrapped in a + * `display: contents` element carrying them, so the container's descendant rules still match it. + */ + ghostContext?: string; } = {}, ) { if (e.button !== 0 || (!opts.always && requiresAlt() && !e.altKey)) return; const start = { x: e.clientX, y: e.clientY }; let ghost: HTMLElement | null = null; + let context: HTMLElement | null = null; let endDrag: (() => void) | null = null; const session = dragSession(data); const onMove = (ev: PointerEvent) => { @@ -185,7 +191,13 @@ export function startPointerDrag( width: `${r.width}px`, height: `${r.height}px`, }); - document.body.appendChild(ghost); + if (opts.ghostContext) { + context = document.createElement("div"); + context.className = opts.ghostContext; + context.style.display = "contents"; + context.appendChild(ghost); + } + document.body.appendChild(context ?? ghost); endDrag = beginDrag(); opts.onStart?.(); } @@ -217,7 +229,7 @@ export function startPointerDrag( window.removeEventListener("pointerup", onUp); window.removeEventListener("pointercancel", onCancel); window.removeEventListener("keydown", onKey, true); - ghost?.remove(); + (context ?? ghost)?.remove(); endDrag?.(); }; const onCancel = () => {