Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Image bar: arrow keys, Home and End step through the thumbnails

Bitti151e436928d9
diff --git a/src/components/workspace/ImageBar.tsx b/src/components/workspace/ImageBar.tsx
index e91bfee..ebfcd9f 100644
--- a/src/components/workspace/ImageBar.tsx
+++ b/src/components/workspace/ImageBar.tsx
@@ -7,8 +7,9 @@ import "../../styles/image-editor.css";
/* The strip of an image project's images along the foot of the Editor and the
Export tab: one thumbnail per image, the selected one ringed in accent, a
draft badged. The mouse wheel scrolls it sideways, and a new image scrolls
- into view at the end. `onMenu` opens a context menu for an image (id) or
- for the bar (null) at a point; without it the bar has no menu. */
+ into view at the end; the arrow keys step through it. `onMenu` opens a
+ context menu for an image (id) or for the bar (null) at a point; without it
+ the bar has no menu. */
export const ImageBar = forwardRef<HTMLDivElement | null, {
images: ProjectAsset[];
selectedId: string | null | undefined;
@@ -39,16 +40,38 @@ export const ImageBar = forwardRef<HTMLDivElement | null, {
return () => element.removeEventListener("wheel", wheel);
}, [images.length]);
+ /* Left and Right step the focus along the thumbnails, Home and End jump to
+ either end, and the bar scrolls to keep the focused one in view. Moving
+ the focus does not open the image: Enter or Space does, as a click. From
+ the bar itself, the first step lands on the selected thumbnail. */
+ const arrowKey = (event: KeyboardEvent<HTMLDivElement>) => {
+ if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key) || event.altKey || event.ctrlKey || event.metaKey) return false;
+ const buttons = [...event.currentTarget.querySelectorAll<HTMLButtonElement>("button[data-image-asset]")];
+ if (buttons.length === 0) return false;
+ event.preventDefault();
+ event.stopPropagation();
+ const at = buttons.indexOf(event.target as HTMLButtonElement);
+ const selected = Math.max(0, buttons.findIndex((button) => button.getAttribute("aria-pressed") === "true"));
+ const next = event.key === "Home" ? 0
+ : event.key === "End" ? buttons.length - 1
+ : at < 0 ? selected
+ : Math.min(buttons.length - 1, Math.max(0, at + (event.key === "ArrowRight" ? 1 : -1)));
+ buttons[next].focus();
+ buttons[next].scrollIntoView?.({ block: "nearest", inline: "nearest" });
+ return true;
+ };
+
const menuKey = (event: KeyboardEvent) => event.key === "ContextMenu" || (event.shiftKey && event.key === "F10");
return <div ref={bar} tabIndex={0} className="image-results" aria-label="Generated images"
onContextMenu={onMenu && ((event) => { event.preventDefault(); onMenu(null, event.clientX, event.clientY); })}
- onKeyDown={onMenu && ((event) => {
- if (event.target !== event.currentTarget || !menuKey(event)) return;
+ onKeyDown={(event) => {
+ if (arrowKey(event)) return;
+ if (!onMenu || event.target !== event.currentTarget || !menuKey(event)) return;
event.preventDefault();
const bounds = event.currentTarget.getBoundingClientRect();
onMenu(null, bounds.left, bounds.top);
- })}>
+ }}>
{images.map((asset) => <button key={asset.id} type="button" data-image-asset={asset.id} data-tooltip={asset.name} aria-label={`View ${asset.name}`} aria-pressed={asset.id === selectedId}
onContextMenu={onMenu && ((event) => { event.preventDefault(); event.stopPropagation(); onMenu(asset.id, event.clientX, event.clientY); })}
onKeyDown={onMenu && ((event) => {
diff --git a/src/components/workspace/ImageExportView.test.tsx b/src/components/workspace/ImageExportView.test.tsx
index f803800..a818318 100644
--- a/src/components/workspace/ImageExportView.test.tsx
+++ b/src/components/workspace/ImageExportView.test.tsx
@@ -75,3 +75,25 @@ it("shows the image bar and exports the image picked in it, leaving the Editor's
await waitFor(() => expect(invoke).toHaveBeenCalledWith("export_generated_image", expect.objectContaining({ relativePath: "media/generated/second.jpg" })));
expect(config.imageScene!.outputAssetId).toBe("out");
});
+
+it("steps through the image bar with the arrow keys without opening an image", () => {
+ vi.spyOn(persistence, "isTauri").mockReturnValue(true);
+ vi.spyOn(persistence, "readMediaFileUrl").mockResolvedValue(null);
+ const config = withOutput({ width: 2048, height: 1152 });
+ for (const name of ["Second", "Third"]) config.assets.push({ id: name, name, kind: "image", relativePath: `media/generated/${name}.jpg`, mimeType: "image/jpeg", width: 1024, height: 576, createdAt: config.createdAt } as ProjectConfig["assets"][number]);
+ render(<ImageExportView config={config} folderPath="D:/Images" />);
+ const bar = screen.getByLabelText("Generated images");
+ const [out, second, third] = ["Out", "Second", "Third"].map((name) => within(bar).getByRole("button", { name: `View ${name}` }));
+ bar.focus();
+ fireEvent.keyDown(bar, { key: "ArrowRight" });
+ expect(out).toHaveFocus();
+ fireEvent.keyDown(out, { key: "ArrowRight" });
+ expect(second).toHaveFocus();
+ fireEvent.keyDown(second, { key: "End" });
+ expect(third).toHaveFocus();
+ fireEvent.keyDown(third, { key: "ArrowRight" });
+ expect(third).toHaveFocus();
+ fireEvent.keyDown(third, { key: "ArrowLeft" });
+ expect(second).toHaveFocus();
+ expect(out).toHaveAttribute("aria-pressed", "true");
+});