From 2215239ecbf079ca4dfe36bcdf9709e8f33ba564 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 23 Nov 2026 09:23:25 +0800 Subject: [PATCH] fix(editor): put the pin in the editor tab action slot (#5282) - Put the pin in a pinned editor tab's action slot so the filename has more room. The pin follows the left or right placement and can unpin the tab directly. - Use the same Unpin action in Hidden Editor Tabs. A modified pinned tab shows its unsaved marker beside the pin. --- e2e/editorTabClose.spec.ts | 37 ++++++++++++++++++++++ e2e/hiddenTabsLayout.spec.ts | 24 ++++++++++++++ src/editor/core/view/EditorArea.module.css | 4 --- src/editor/core/view/EditorArea.tsx | 12 ++----- src/ui/HiddenTabsPopup.tsx | 14 +++++--- src/ui/Tabs.module.css | 12 ++++++- src/ui/Tabs.test.tsx | 19 +++++++++++ src/ui/Tabs.tsx | 34 ++++++++++++-------- 8 files changed, 124 insertions(+), 32 deletions(-) diff --git a/e2e/editorTabClose.spec.ts b/e2e/editorTabClose.spec.ts index 928e2e96a..3b934d6c6 100644 --- a/e2e/editorTabClose.spec.ts +++ b/e2e/editorTabClose.spec.ts @@ -88,3 +88,40 @@ test("a close button on the left keeps a narrow slot before the icon", async ({ const slot = await box(closeOf(selected)); expect(slot.x + slot.width).toBeLessThanOrEqual(label.x + 0.5); }); + +test("a pinned editor tab uses the close slot and can be unpinned from it", async ({ workbench: page }) => { + await openFromTree(page, "README.md"); + await openFromTree(page, "build.gradle.kts"); + const strip = page.getByRole("tablist", { name: "Editor tabs" }); + const unpinned = strip.getByRole("tab", { name: "build.gradle.kts", exact: true }); + const pinned = strip.getByRole("tab", { name: "build.gradle.kts Pinned", exact: true }); + const menu = page.getByRole("menu", { name: "Context menu" }); + const pin = async () => { + await unpinned.click({ button: "right" }); + await menu.getByRole("menuitem", { name: "Pin Tab", exact: true }).click(); + }; + await pin(); + const unpin = pinned.locator("..").getByRole("button", { name: "Unpin Tab", exact: true }); + + for (const closeButtonPosition of ["right", "left"]) { + await setTabs(page, { closeButtonPosition }); + await expect(closeOf(pinned)).toHaveCount(0); + const tab = await box(pinned.locator("..")); + const action = await box(unpin); + const label = await box(pinned.locator(":scope > span")); + const inset = closeButtonPosition === "left" ? action.x - tab.x : tab.x + tab.width - action.x - action.width; + expect(inset).toBeCloseTo(2, 0); + expect(action.width).toBeCloseTo(16, 0); + if (closeButtonPosition === "left") expect(action.x + action.width).toBeLessThanOrEqual(label.x); + else expect(action.x).toBeGreaterThanOrEqual(label.x + label.width); + } + + await setTabs(page, { showCloseButton: false }); + await unpin.click(); + await expect(unpinned).toHaveAttribute("aria-selected", "true"); + await expect(closeOf(unpinned)).toHaveCount(0); + + await pin(); + await pinned.click({ button: "middle" }); + await expect(pinned).toHaveCount(0); +}); diff --git a/e2e/hiddenTabsLayout.spec.ts b/e2e/hiddenTabsLayout.spec.ts index d1d7fe8fd..1a1e6cdad 100644 --- a/e2e/hiddenTabsLayout.spec.ts +++ b/e2e/hiddenTabsLayout.spec.ts @@ -12,6 +12,30 @@ const names = [ "InvoicePublisher.ts", ]; +test("a pinned editor tab can be unpinned from the hidden tabs list", async ({ workbench: page }) => { + await page.setViewportSize({ width: 700, height: 650 }); + await page.evaluate(async (first) => { + const module = "/src/workbench/contributions.ts"; + const { editorService } = await import(/* @vite-ignore */ module); + await editorService().openFile(`/dev/acme-api/${first}`); + }, names[0]!); + const strip = page.getByRole("tablist", { name: "Editor tabs" }); + const tab = strip.getByRole("tab", { name: names[0], exact: true }); + await tab.click({ button: "right" }); + await page.getByRole("menuitem", { name: "Pin Tab", exact: true }).click(); + await page.evaluate(async (files) => { + const module = "/src/workbench/contributions.ts"; + const { editorService } = await import(/* @vite-ignore */ module); + for (const name of files) await editorService().openFile(`/dev/acme-api/${name}`); + }, names.slice(1)); + await strip.evaluate((element) => (element.scrollLeft = element.scrollWidth)); + await page.getByRole("button", { name: "Show Hidden Tabs" }).click(); + const row = page.getByRole("grid", { name: "Hidden Editor Tabs" }).getByRole("row").filter({ hasText: names[0]! }); + await expect(row.getByRole("button", { name: "Close Tab", exact: true })).toHaveCount(0); + await row.getByRole("button", { name: "Unpin Tab", exact: true }).click(); + await expect(tab).toHaveCount(1); +}); + test("the editor overflow lists clipped tabs and can close or open one without touching visible tabs", async ({ workbench: page, }) => { diff --git a/src/editor/core/view/EditorArea.module.css b/src/editor/core/view/EditorArea.module.css index 0a1118cf1..dde4d863a 100644 --- a/src/editor/core/view/EditorArea.module.css +++ b/src/editor/core/view/EditorArea.module.css @@ -143,10 +143,6 @@ gap: var(--space-0-5); padding: 0 6px 0 8px; } -.pin { - color: var(--fg-muted); - margin-left: -2px; -} .deprecated { text-decoration-line: line-through; } diff --git a/src/editor/core/view/EditorArea.tsx b/src/editor/core/view/EditorArea.tsx index 337d75a6a..616b48973 100644 --- a/src/editor/core/view/EditorArea.tsx +++ b/src/editor/core/view/EditorArea.tsx @@ -38,7 +38,6 @@ import { useFileColors } from "@/workbench/fileColors"; import { EmptyEditor } from "@/workbench/EditorArea"; import { EmptyState, - Icon, IconButton, Menu, SplitGroup, @@ -216,7 +215,6 @@ function PaneView({ paneId }: { paneId: string }) { ? (gitRevisionOf(t.input.path)?.revision.slice(0, 8) ?? basename(dirname(t.input.path))) : null; const kindMark = tabTrailing(t.input, kinds); - const pin = t.pinned ? : null; const file = t.input.kind === "file" && t.input.path ? files.get(t.input.path) : undefined; const title = markModified && dirty[i] ? `*${name}` : name; const prefix = t.input.kind === "file" && dupe ? (dupe.endsWith("/") ? dupe : `${dupe}/`) : null; @@ -242,6 +240,7 @@ function PaneView({ paneId }: { paneId: string }) { : tabIcon(t.input, kinds) : undefined, italic: t.preview, + pinned: t.pinned, error: t.input.kind === "file" && errors[i], modified: dirty[i], // IDEA: hiding the close button is visual only. Keep close gestures @@ -252,13 +251,7 @@ function PaneView({ paneId }: { paneId: string }) { tints[i] || (fileColors && t.input.kind === "file" && t.input.path ? fileColors(t.input.path) : undefined), tintStrength: tints[i] ? undefined : "var(--file-tint-strength)", tooltip: virtualTooltip(t.input) ?? (showFullPathOnHover ? (t.input.path ?? name) : undefined), - trailing: - kindMark || pin ? ( - <> - {kindMark} - {pin} - - ) : undefined, + trailing: kindMark, }; }); }, [ @@ -369,6 +362,7 @@ function PaneView({ paneId }: { paneId: string }) { onSelect={(id) => ctrl.activate(paneId, id)} onClose={(id) => void ctrl.closeTab(paneId, id)} onCloseOthers={(id) => void ctrl.closeOthers(paneId, id)} + onTogglePin={(id) => ctrl.togglePin(paneId, id)} closeOnShiftClick onDoubleClick={(id) => { // IDEA: a preview tab becomes a regular one; any other tab maximizes the editor. diff --git a/src/ui/HiddenTabsPopup.tsx b/src/ui/HiddenTabsPopup.tsx index c2b796033..cec8076c1 100644 --- a/src/ui/HiddenTabsPopup.tsx +++ b/src/ui/HiddenTabsPopup.tsx @@ -22,6 +22,7 @@ interface Props { onSelect: (id: string) => void; onCloseTab?: (id: string) => void; onCloseOthers?: (id: string) => void; + onTogglePin?: (id: string) => void; onDismiss: () => void; } @@ -34,6 +35,7 @@ export function HiddenTabsPopup({ onSelect, onCloseTab, onCloseOthers, + onTogglePin, onDismiss, }: Props) { const tr = useGivenTx(); @@ -131,20 +133,22 @@ export function HiddenTabsPopup({ className={styles.list} renderItem={(item) => { if (!item) return null; - const close = Boolean(onCloseTab && item.closable !== false && item.showCloseButton !== false); + const unpin = item.pinned && onTogglePin; + const close = onCloseTab && item.closable !== false && item.showCloseButton !== false; const action = (
{item.modified && } - {close && ( + {(unpin || close) && ( event.preventDefault()} onClick={(event) => { event.stopPropagation(); - if (event.altKey && onCloseOthers) onCloseOthers(item.id); + if (unpin) unpin(item.id); + else if (event.altKey && onCloseOthers) onCloseOthers(item.id); else onCloseTab!(item.id); }} /> diff --git a/src/ui/Tabs.module.css b/src/ui/Tabs.module.css index bf2150bb2..820595fff 100644 --- a/src/ui/Tabs.module.css +++ b/src/ui/Tabs.module.css @@ -137,6 +137,16 @@ .unpin { color: var(--fg-muted); } +.pinModified { + position: absolute; + top: calc(50% - 7px); + inset-inline-end: 1px; + width: var(--space-1); + height: var(--space-1); + border-radius: 50%; + background: var(--accent); + pointer-events: none; +} .tab:hover .close, .tab:where([data-context-menu-open]) .close, .selected .close, @@ -301,7 +311,7 @@ width: auto; margin: 0; } -.editor:not(.vertical) .close { +.editor:not(.vertical) :is(.close, .unpin) { width: 16px; } .editor:not(.vertical) .closeSlot .modified { diff --git a/src/ui/Tabs.test.tsx b/src/ui/Tabs.test.tsx index 298aab8a0..3455080c0 100644 --- a/src/ui/Tabs.test.tsx +++ b/src/ui/Tabs.test.tsx @@ -199,6 +199,25 @@ describe("Tabs interactions", () => { expect(handles[0]!.togglePinSelected()).toBe(false); }); + it("shows the modified marker in a pinned editor tab's action slot", () => { + const onTogglePin = vi.fn(); + render( + {}} + onClose={() => {}} + onTogglePin={onTogglePin} + />, + ); + const unpin = screen.getByRole("button", { name: "Unpin Tab" }); + expect(within(unpin.parentElement!).getByLabelText("Modified")).toBeVisible(); + expect(screen.getByRole("tab", { name: "B Pinned" })).toBeVisible(); + fireEvent.click(unpin); + expect(onTogglePin).toHaveBeenCalledWith("b"); + }); + it("a vertical strip moves with ↑/↓, shows actions above the tabs and has no overflow dropdown", () => { const onSelect = vi.fn(); render( diff --git a/src/ui/Tabs.tsx b/src/ui/Tabs.tsx index a4822fc4f..9ffc8a9c4 100644 --- a/src/ui/Tabs.tsx +++ b/src/ui/Tabs.tsx @@ -34,10 +34,7 @@ export interface TabItem { text?: string; icon?: IconName | (string & {}); closable?: boolean; - /** - * Pinned (IDEA's content tabs): with `onTogglePin`, an Unpin button takes the close - * button's place, and the strip's close gestures leave the tab alone. - */ + /** With `onTogglePin`, the tab shows Unpin in the action slot. */ pinned?: boolean; /** * Visual only: hide this tab's close button without disabling its close @@ -129,7 +126,7 @@ export interface TabsProps { onClose?: (id: string) => void; /** Alt+click on a close button (IDEA: close all other tabs). */ onCloseOthers?: (id: string) => void; - /** Pin / Unpin Tab on the selected tab, from the container (a tool window's content tabs). */ + /** Toggle the pinned state of a tab. */ onTogglePin?: (id: string) => void; /** Shift+left-click a tab closes it (editor tabs; plain clicks still select). */ closeOnShiftClick?: boolean; @@ -183,6 +180,7 @@ export function Tabs({ ...aria }: TabsProps) { const vertical = orientation === "vertical"; + const protectPinned = variant !== "editor" && !!onTogglePin; const given = useGivenTx(); const scrollRef = useRef(null); const tabRefs = useRef(new Map()); @@ -194,17 +192,17 @@ export function Tabs({ const [dragging, setDragging] = useState(null); const register = useContext(TabStripRegistry); - const latest = useRef({ items, activeId, onClose, onTogglePin }); + const latest = useRef({ items, activeId, onClose, onTogglePin, protectPinned }); useLayoutEffect(() => { - latest.current = { items, activeId, onClose, onTogglePin }; + latest.current = { items, activeId, onClose, onTogglePin, protectPinned }; }); useEffect( () => register?.({ closeSelected: () => { - const { items, activeId, onClose, onTogglePin } = latest.current; + const { items, activeId, onClose, protectPinned } = latest.current; const item = items.find((it) => it.id === activeId); - if (!item || !onClose || item.closable === false || (item.pinned && onTogglePin)) return false; + if (!item || !onClose || !canCloseTab(item, protectPinned)) return false; onClose(item.id); return true; }, @@ -341,7 +339,7 @@ export function Tabs({ break; case "Delete": case "Backspace": - if (onClose && item.closable !== false && !(item.pinned && onTogglePin)) onClose(item.id); + if (onClose && canCloseTab(item, protectPinned)) onClose(item.id); else return; break; case "Tab": @@ -399,8 +397,9 @@ export function Tabs({ const pinned = !!onTogglePin && !!item.pinned; // Whether a tab can be closed at all (gestures and the button). A hidden // close button is visual only: middle click, Shift+click and Delete still work. - const closable = !!onClose && item.closable !== false && !pinned; - const showClose = closable && item.showCloseButton !== false; + const closable = !!onClose && canCloseTab(item, protectPinned); + const showClose = closable && !pinned && item.showCloseButton !== false; + const pinModified = pinned && variant === "editor" && item.modified; return (
{item.label} : item.label} {item.trailing} - {item.modified && !showClose && } + {item.modified && !showClose && !pinModified && ( + + )} {pinned && ( @@ -516,6 +518,7 @@ export function Tabs({ onTogglePin!(item.id); }} /> + {pinModified && } )} {showClose && ( @@ -564,6 +567,7 @@ export function Tabs({ onSelect={onSelect} onCloseTab={onClose} onCloseOthers={onCloseOthers} + onTogglePin={onTogglePin} onDismiss={closeMenu} /> ) @@ -584,6 +588,10 @@ export function Tabs({ ); } +function canCloseTab(item: TabItem, protectPinned: boolean): boolean { + return item.closable !== false && !(protectPinned && item.pinned); +} + /** Scrolls `scroller` horizontally just enough to show all of `child`. */ export function revealHorizontally(scroller: HTMLElement, child: HTMLElement) { const s = scroller.getBoundingClientRect();