mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(ui): remeasure popup height before choosing a side (#4296)
The shared Popup decided where it fit using the max-height cap from its previous placement, so a popup clipped below its anchor still looked as if it fit there: the agent Composer's @ list stayed clipped after erasing the filter or when file results arrived after it opened. A middleware now clears the old cap before flip, and the size step only caps a popup that is taller than the space available. Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import type { Locator, Page } from "@playwright/test";
|
||||
import type { AgentTask } from "../src/bindings";
|
||||
import { expect, test } from "./fixtures";
|
||||
|
||||
const task: AgentTask = {
|
||||
id: "composer-popup",
|
||||
title: "Composer popup task",
|
||||
provider: "codex",
|
||||
status: "ready",
|
||||
isolation: "local",
|
||||
model: null,
|
||||
cwd: "/dev/acme-api",
|
||||
branch: null,
|
||||
base: null,
|
||||
baseCommit: null,
|
||||
error: null,
|
||||
createdMs: 0,
|
||||
updatedMs: 0,
|
||||
config: [],
|
||||
commands: [],
|
||||
usage: null,
|
||||
queue: [],
|
||||
};
|
||||
|
||||
async function openTask(page: Page, delayFiles = false) {
|
||||
await page.evaluate(
|
||||
async ({ task, delayFiles }) => {
|
||||
// Load the workbench modules in Vite's browser realm, not the Playwright runner.
|
||||
const storePath = "/src/features/agents/store.ts";
|
||||
const ipcPath = "/src/platform/ipc.ts";
|
||||
const store = await import(storePath);
|
||||
const { commands } = await import(ipcPath);
|
||||
const { promise: filesReady, resolve } = Promise.withResolvers<void>();
|
||||
if (delayFiles) window.addEventListener("test:release-composer-files", () => resolve(), { once: true });
|
||||
else resolve();
|
||||
const file = (name: string) => ({ path: `${task.cwd}/${name}`, relPath: name, score: 1, indices: [] });
|
||||
commands.agentChanges = async () => ({ status: "ok", data: [] });
|
||||
commands.findFiles = async (query: string) => {
|
||||
if (!query) await filesReady;
|
||||
return {
|
||||
status: "ok",
|
||||
data: query ? [file("a.ts")] : Array.from({ length: 12 }, (_, i) => file(`file${i}.ts`)),
|
||||
};
|
||||
};
|
||||
commands.findDirs = async () => ({ status: "ok", data: [] });
|
||||
store.useAgents.setState({
|
||||
loaded: true,
|
||||
error: null,
|
||||
providers: [],
|
||||
tasks: new Map([[task.id, task]]),
|
||||
entries: new Map([[task.id, [{ type: "agent", text: "Reply" }]]]),
|
||||
composers: new Map(),
|
||||
unread: new Set(),
|
||||
docked: null,
|
||||
homeOpen: false,
|
||||
sessionLocation: "editor",
|
||||
});
|
||||
store.openTask(task.id);
|
||||
},
|
||||
{ task, delayFiles },
|
||||
);
|
||||
const editor = page.getByRole("textbox", { name: "Message", exact: true });
|
||||
await editor.click();
|
||||
return { editor, list: page.getByRole("listbox", { name: "Files", exact: true }) };
|
||||
}
|
||||
|
||||
function geometry(list: Locator) {
|
||||
return list.evaluate((list) => {
|
||||
const popup = list.closest<HTMLElement>("[data-popup]")!;
|
||||
const rect = popup.getBoundingClientRect();
|
||||
const caret = getSelection()!.getRangeAt(0).getBoundingClientRect();
|
||||
const footer = popup.querySelector("span:last-child > kbd")!.getBoundingClientRect();
|
||||
return {
|
||||
side: rect.bottom <= caret.top ? "above" : rect.top >= caret.bottom ? "below" : "overlap",
|
||||
height: popup.offsetHeight,
|
||||
listHeight: list.clientHeight,
|
||||
inViewport: rect.top >= 0 && rect.bottom <= innerHeight && rect.left >= 0 && rect.right <= innerWidth,
|
||||
footerVisible: footer.height > 0 && footer.top >= rect.top && footer.bottom <= rect.bottom,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const fullList = { side: "above", listHeight: 300, inViewport: true, footerVisible: true };
|
||||
|
||||
test("erasing the @ filter restores the popup's fitting side and height", async ({ workbench: page }) => {
|
||||
const { editor, list } = await openTask(page);
|
||||
await editor.pressSequentially("@");
|
||||
await expect(list.getByText("file0.ts", { exact: true })).toBeVisible();
|
||||
await expect.poll(() => geometry(list)).toMatchObject(fullList);
|
||||
const initial = await geometry(list);
|
||||
|
||||
await editor.pressSequentially("a");
|
||||
await expect(list.getByText("a.ts", { exact: true })).toBeVisible();
|
||||
await expect.poll(() => geometry(list)).toMatchObject({ side: "below", footerVisible: true });
|
||||
await editor.press("Backspace");
|
||||
await expect(list.getByText("file0.ts", { exact: true })).toBeVisible();
|
||||
await expect.poll(() => geometry(list)).toEqual(initial);
|
||||
await expect(editor).toBeFocused();
|
||||
await expect(editor).toHaveText("@");
|
||||
});
|
||||
|
||||
test("first-open @ results can grow and flip after asynchronous file search", async ({ workbench: page }) => {
|
||||
const { editor, list } = await openTask(page, true);
|
||||
for (let i = 0; i < 3; i++) await editor.press("Shift+Enter");
|
||||
await editor.press("ArrowUp");
|
||||
await editor.press("ArrowUp");
|
||||
await editor.press("ArrowUp");
|
||||
await editor.pressSequentially("@");
|
||||
await expect(list.getByRole("option").first()).toBeVisible();
|
||||
await expect.poll(() => geometry(list)).toMatchObject({ side: "below", footerVisible: true });
|
||||
await expect(list.getByText("file0.ts", { exact: true })).toHaveCount(0);
|
||||
|
||||
await page.evaluate(() => window.dispatchEvent(new Event("test:release-composer-files")));
|
||||
await expect(list.getByText("file0.ts", { exact: true })).toBeVisible();
|
||||
await expect.poll(() => geometry(list)).toMatchObject(fullList);
|
||||
await expect(editor).toBeFocused();
|
||||
});
|
||||
+21
-4
@@ -25,6 +25,7 @@ import {
|
||||
shift,
|
||||
size as sizeMw,
|
||||
useFloating,
|
||||
type Middleware,
|
||||
type Placement,
|
||||
type VirtualElement,
|
||||
} from "@floating-ui/react-dom";
|
||||
@@ -78,6 +79,16 @@ export interface PopupProps {
|
||||
type ResizeEdge = "right" | "bottom" | "corner";
|
||||
const EDGE_CURSOR = { right: "ew-resize", bottom: "ns-resize", corner: "nwse-resize" } as const;
|
||||
|
||||
const measureNaturalHeight: Middleware = {
|
||||
name: "measureNaturalHeight",
|
||||
fn({ elements, middlewareData }) {
|
||||
// Keep the new cap through internal flip/size restarts, not through the next update.
|
||||
if (middlewareData.measureNaturalHeight) return {};
|
||||
const previousLimit = elements.floating.style.removeProperty("max-height");
|
||||
return { data: { measured: true }, reset: previousLimit ? { rects: true } : undefined };
|
||||
},
|
||||
};
|
||||
|
||||
function toReference(anchor: PopupAnchor): Element | VirtualElement | null {
|
||||
if (!anchor) return null;
|
||||
if ("current" in anchor) return anchor.current;
|
||||
@@ -117,22 +128,28 @@ function PopupInner({
|
||||
...aria
|
||||
}: PopupProps) {
|
||||
const reference = toReference(anchor);
|
||||
const hasInlineMaxHeight = style?.maxHeight != null && style.maxHeight !== "";
|
||||
const middleware = useMemo(
|
||||
() => [
|
||||
offsetMw(offset),
|
||||
hasInlineMaxHeight ? null : measureNaturalHeight,
|
||||
flip({ padding: 6 }),
|
||||
shift({ padding: 6 }),
|
||||
sizeMw({
|
||||
padding: 6,
|
||||
apply({ rects, elements, availableHeight, availableWidth }) {
|
||||
elements.floating.style.maxHeight = `${Math.max(120, availableHeight)}px`;
|
||||
const floatingStyle = elements.floating.style;
|
||||
const maxHeight = Math.max(120, availableHeight);
|
||||
if (!hasInlineMaxHeight && (rects.floating.height > maxHeight || floatingStyle.maxHeight)) {
|
||||
floatingStyle.maxHeight = `${maxHeight}px`;
|
||||
}
|
||||
const width = Math.max(0, availableWidth);
|
||||
elements.floating.style.maxWidth = `${width}px`;
|
||||
if (matchWidth) elements.floating.style.minWidth = `${Math.min(rects.reference.width, width)}px`;
|
||||
floatingStyle.maxWidth = `${width}px`;
|
||||
if (matchWidth) floatingStyle.minWidth = `${Math.min(rects.reference.width, width)}px`;
|
||||
},
|
||||
}),
|
||||
],
|
||||
[offset, matchWidth],
|
||||
[hasInlineMaxHeight, offset, matchWidth],
|
||||
);
|
||||
const { refs, floatingStyles, isPositioned } = useFloating({
|
||||
placement,
|
||||
|
||||
Reference in New Issue
Block a user