fix(editor): keep image viewer controls and details readable (#5230)

The image viewer groups the image and zoom controls at the start of a compact toolbar. Its dimensions, file size, and zoom appear at the other end in the standard UI text size. In narrow splits the details wrap onto another row, leaving the SVG Open as Text action readable on one line.
This commit is contained in:
l0ng-ai
2026-11-23 09:12:39 +08:00
committed by GitHub
parent dde4b28abd
commit 13b5f9e575
3 changed files with 117 additions and 27 deletions
+48
View File
@@ -0,0 +1,48 @@
import { expect, stubDesktop, test } from "./fixtures";
test("image controls and metadata stay readable in a narrow editor", async ({ workbench: page }) => {
await page.setViewportSize({ width: 1080, height: 700 });
await stubDesktop(page);
await page.evaluate(async () => {
const path = "/dev/acme-api/status.svg";
const image =
'<svg xmlns="http://www.w3.org/2000/svg" width="128" height="96"><rect width="128" height="96"/></svg>';
window.__ipc.handlers.fs_read_image = () => "data:image/svg+xml," + encodeURIComponent(image);
window.__ipc.handlers.fs_probe = () => ({ size: 4096, binary: false, tooLarge: false });
const contributions = "/src/workbench/contributions.ts";
const { editorService } = await import(/* @vite-ignore */ contributions);
await editorService().openFile(path);
});
const toolbars = page.getByRole("toolbar", { name: "Image viewer", exact: true });
const toolbar = toolbars.last();
const zoomIn = toolbar.getByRole("button", { name: "Zoom In", exact: true });
const actualSize = toolbar.getByRole("button", { name: "Actual Size", exact: true });
const chessboard = toolbar.getByRole("button", { name: "Show Chessboard", exact: true });
const showText = toolbar.getByRole("button", { name: "Open as Text", exact: true });
const dimensions = toolbar.getByText(/128\s*×\s*96/);
await expect(page.getByRole("img", { name: "status.svg", exact: true })).toBeVisible();
await expect(dimensions).toBeVisible();
const wideHeight = (await toolbar.boundingBox())!.height;
await page.getByRole("button", { name: "Split Right", exact: true }).click();
await expect(toolbars).toHaveCount(2);
await page.setViewportSize({ width: 640, height: 570 });
expect(await dimensions.evaluate((el) => el.scrollWidth - el.clientWidth)).toBe(0);
expect((await toolbar.boundingBox())!.height).toBeGreaterThan(wideHeight);
expect((await dimensions.boundingBox())!.y).toBeGreaterThan((await zoomIn.boundingBox())!.y);
await expect(dimensions).toHaveCSS("font-size", "13px");
await expect(showText).toHaveCSS("white-space", "nowrap");
for (const item of [zoomIn, actualSize, chessboard, showText, dimensions]) {
await expect(item).toBeInViewport({ ratio: 1 });
}
await expect(toolbar.getByText("4.0 KB", { exact: true })).toBeVisible();
await zoomIn.click();
await expect(toolbar.getByText("125%", { exact: true })).toBeVisible();
await actualSize.click();
await expect(toolbar.getByText("100%", { exact: true })).toBeVisible();
await chessboard.click();
await expect(chessboard).toHaveAttribute("aria-pressed", "false");
await showText.click();
await expect(page.locator(".cm-content")).toBeVisible();
});
+28 -21
View File
@@ -186,9 +186,6 @@ export function ImageViewer({ input }: { input: EditorInput }) {
const z = view?.z ?? 1;
const svg = isSvgPath(path);
const info = [natural && `${natural.w}×${natural.h}`, size !== null && formatSize(size), view && zoomLabel(z)]
.filter(Boolean)
.join(" · ");
return (
<div
@@ -203,24 +200,27 @@ export function ImageViewer({ input }: { input: EditorInput }) {
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setContext("imageViewerFocus", false);
}}
>
<div className={styles.toolbar} role="toolbar" aria-label={tx("Image viewer")}>
<IconButton icon="zoom-in" size="small" action="image.zoomIn" />
<IconButton icon="zoom-out" size="small" action="image.zoomOut" />
<IconButton icon="actual-size" size="small" action="image.actualSize" />
<IconButton icon="maximize" size="small" action="image.fitToWindow" />
<div className={styles.separator} />
<IconButton icon="chessboard" size="small" action="image.toggleChessboard" pressed={chessboard} />
<IconButton
icon="refresh"
size="small"
title="Reload from Disk"
onClick={() => {
setError(null);
setReload((n) => n + 1);
}}
/>
<div className={styles.spacer} />
<span className={styles.info}>{info}</span>
<div className={clsx(styles.toolbar, styles.imageToolbar)} role="toolbar" aria-label={tx("Image viewer")}>
<div className={styles.imageActions}>
<div className={styles.imageActionGroup}>
<IconButton icon="chessboard" size="small" action="image.toggleChessboard" pressed={chessboard} />
<IconButton
icon="refresh"
size="small"
title="Reload from Disk"
onClick={() => {
setError(null);
setReload((n) => n + 1);
}}
/>
</div>
<div className={styles.imageActionGroup}>
<IconButton icon="zoom-in" size="small" action="image.zoomIn" />
<IconButton icon="zoom-out" size="small" action="image.zoomOut" />
<IconButton icon="actual-size" size="small" action="image.actualSize" />
<IconButton icon="maximize" size="small" action="image.fitToWindow" />
</div>
</div>
{svg && (
<button
type="button"
@@ -230,6 +230,13 @@ export function ImageViewer({ input }: { input: EditorInput }) {
<T>Open as Text</T>
</button>
)}
{(natural || size !== null || view) && (
<div className={styles.imageInfo}>
{natural && <span>{`${natural.w} × ${natural.h}`}</span>}
{size !== null && <span>{formatSize(size)}</span>}
{view && <span>{zoomLabel(z)}</span>}
</div>
)}
</div>
{error ? (
<div className={styles.message}>{tx`Cannot show the image: ${error}`}</div>
+41 -6
View File
@@ -20,12 +20,6 @@
font-size: var(--fs-small);
color: var(--fg-secondary);
}
.separator {
width: 1px;
height: 16px;
margin: 0 6px;
background: var(--border-strong);
}
.spacer {
flex: 1;
}
@@ -61,6 +55,47 @@
/* --- image viewer ------------------------------------------------------------ */
.imageToolbar {
height: auto;
min-height: calc(var(--h-control) + var(--space-2));
flex-wrap: wrap;
gap: var(--space-1) var(--space-2);
padding: var(--space-1) var(--space-2);
}
.imageActions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--space-1);
min-width: 0;
}
.imageActionGroup {
display: flex;
gap: var(--space-0-5);
}
.imageActionGroup + .imageActionGroup {
border-inline-start: 1px solid var(--border-strong);
padding-inline-start: var(--space-1);
}
.imageToolbar .textButton {
white-space: nowrap;
}
.imageInfo {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: var(--space-0-5) var(--space-2);
margin-inline-start: auto;
font-size: var(--fs-ui);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.canvas {
flex: 1;
min-height: 0;