Compare commits

...
Author SHA1 Message Date
l0ng-ai 4e114163d9 fix(editor): put pinned tabs in the editor tab action slot 2026-11-23 07:09:21 +08:00
8 changed files with 124 additions and 32 deletions

No files matched your search

+37
View File
@@ -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);
});
+24
View File
@@ -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,
}) => {
@@ -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;
}
+3 -9
View File
@@ -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 ? <Icon name="pin" size={11} className={styles.pin} label="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.
+9 -5
View File
@@ -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 = (
<div role="gridcell" className={styles.action}>
{item.modified && <span className={styles.modified} aria-label={tr("Modified")} />}
{close && (
{(unpin || close) && (
<IconButton
icon="close"
title="Close Tab"
icon={unpin ? "pin" : "close"}
title={unpin ? "Unpin Tab" : "Close Tab"}
size="small"
tabIndex={-1}
onMouseDown={(event) => 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);
}}
/>
+11 -1
View File
@@ -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 {
+19
View File
@@ -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(
<Tabs
variant="editor"
items={[{ id: "b", label: "B", text: "B", pinned: true, modified: true }]}
activeId="b"
onSelect={() => {}}
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(
+21 -13
View File
@@ -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<HTMLDivElement>(null);
const tabRefs = useRef(new Map<string, HTMLButtonElement>());
@@ -194,17 +192,17 @@ export function Tabs({
const [dragging, setDragging] = useState<string | null>(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 (
<div
key={item.id}
@@ -476,6 +475,7 @@ export function Tabs({
id={panelIdPrefix ? `${panelIdPrefix}-tab-${item.id}` : undefined}
aria-controls={panelIdPrefix ? `${panelIdPrefix}-panel-${item.id}` : undefined}
aria-selected={selected}
aria-label={pinned && variant === "editor" && item.text ? `${item.text} ${tx("Pinned")}` : undefined}
tabIndex={selected || (!activeId && index === 0) ? 0 : -1}
className={clsx(styles.tabButton, item.dimmed && styles.dimmed)}
style={item.color ? { color: item.color } : undefined}
@@ -500,7 +500,9 @@ export function Tabs({
{typeof item.label === "string" ? <Given>{item.label}</Given> : item.label}
</span>
{item.trailing}
{item.modified && !showClose && <span className={styles.modified} aria-label={tx("Modified")} />}
{item.modified && !showClose && !pinModified && (
<span className={styles.modified} aria-label={tx("Modified")} />
)}
</button>
{pinned && (
<span className={styles.closeSlot} data-tab-close>
@@ -516,6 +518,7 @@ export function Tabs({
onTogglePin!(item.id);
}}
/>
{pinModified && <span className={styles.pinModified} aria-label={tx("Modified")} />}
</span>
)}
{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();