diff --git a/e2e/usagesPopupLayout.spec.ts b/e2e/usagesPopupLayout.spec.ts index 11cbb6379b..3931ed5e0f 100644 --- a/e2e/usagesPopupLayout.spec.ts +++ b/e2e/usagesPopupLayout.spec.ts @@ -1,5 +1,113 @@ import { expect, test } from "./fixtures"; +test.use({ video: "on" }); + +test("Show Usages drags by the title bar, keeps its controls usable and stays inside the viewport", async ({ + workbench: page, +}) => { + await page.evaluate(async () => { + const usages = "/src/features/code-insight/usages.ts"; + const { useUsagesPopup } = await import(/* @vite-ignore */ usages); + const path = "/dev/app/src/Main.ts"; + useUsagesPopup.setState({ + open: true, + anchor: { x: 100, y: 100 }, + symbol: "run", + scope: "project", + groups: [ + { + path, + usages: Array.from({ length: 4 }, (_, line) => ({ path, line, col: 0, endCol: 3, text: "run();" })), + }, + ], + }); + }); + const popup = page.getByRole("dialog", { name: "Usages", exact: true }); + const title = popup.getByText("Usages of run", { exact: true }); + await expect(popup).toBeVisible(); + await popup.evaluate((el) => Promise.all(el.getAnimations().map((a) => a.finished))); + const before = (await popup.boundingBox())!; + await page.screenshot({ path: test.info().outputPath("usages-before-drag.png") }); + + // The empty space after the caption, where the reported drag failed. + const caption = (await title.locator("..").boundingBox())!; + const start = { x: caption.x + caption.width - 4, y: caption.y + caption.height / 2 }; + await page.mouse.move(start.x, start.y); + await page.mouse.down(); + await page.mouse.move(start.x + 120, start.y + 100, { steps: 5 }); + await page.mouse.up(); + await expect.poll(async () => (await popup.boundingBox())!.x).toBeCloseTo(before.x + 120, 0); + await expect.poll(async () => (await popup.boundingBox())!.y).toBeCloseTo(before.y + 100, 0); + const moved = (await popup.boundingBox())!; + expect(moved.width).toBeCloseTo(before.width, 0); + expect(moved.height).toBeCloseTo(before.height, 0); + await page.screenshot({ path: test.info().outputPath("usages-after-drag.png") }); + + await popup.getByRole("button", { name: "Scope", exact: true }).click(); + await expect(page.getByRole("option", { name: "Project and Libraries", exact: true })).toBeVisible(); + await page.keyboard.press("Escape"); + await popup.getByRole("button", { name: "Show Preview", exact: true }).click(); + await expect(popup.getByRole("region", { name: "Preview", exact: true })).toBeVisible(); + await expect.poll(async () => (await popup.boundingBox())!.x).toBeCloseTo(moved.x, 0); + await expect.poll(async () => (await popup.boundingBox())!.y).toBeCloseTo(moved.y, 0); + + const resizeBefore = (await popup.boundingBox())!; + const corner = (await popup.getByRole("separator", { name: "Resize" }).boundingBox())!; + await page.mouse.move(corner.x + 4, corner.y + 4); + await page.mouse.down(); + await page.mouse.move(corner.x + 84, corner.y + 64, { steps: 5 }); + await page.mouse.up(); + await expect.poll(async () => (await popup.boundingBox())!.width).toBeCloseTo(resizeBefore.width + 80, 0); + await expect.poll(async () => (await popup.boundingBox())!.height).toBeCloseTo(resizeBefore.height + 60, 0); + expect((await popup.boundingBox())!.x).toBeCloseTo(moved.x, 0); + expect((await popup.boundingBox())!.y).toBeCloseTo(moved.y, 0); + + // Cancelling a press must release the drag before the next pointer move. + const cancelAt = (await title.boundingBox())!; + await page.mouse.move(cancelAt.x + 5, cancelAt.y + 5); + await page.mouse.down(); + await page.evaluate(() => window.dispatchEvent(new PointerEvent("pointercancel"))); + await page.mouse.move(cancelAt.x + 45, cancelAt.y + 25); + await page.mouse.up(); + expect((await popup.boundingBox())!.x).toBeCloseTo(moved.x, 0); + expect((await popup.boundingBox())!.y).toBeCloseTo(moved.y, 0); + + // A later drag starts at the new position and clamps at both sides of the viewport. + let handle = (await title.boundingBox())!; + await page.mouse.move(handle.x + 5, handle.y + 5); + await page.mouse.down(); + await page.mouse.move(-1000, -1000, { steps: 5 }); + await page.mouse.up(); + await expect.poll(async () => (await popup.boundingBox())!.x).toBeCloseTo(6, 0); + await expect.poll(async () => (await popup.boundingBox())!.y).toBeCloseTo(6, 0); + + handle = (await title.boundingBox())!; + await page.mouse.move(handle.x + 5, handle.y + 5); + await page.mouse.down(); + await page.mouse.move(2400, 1600, { steps: 5 }); + await page.mouse.up(); + const viewport = page.viewportSize()!; + await expect + .poll(async () => { + const box = (await popup.boundingBox())!; + return box.x + box.width; + }) + .toBeCloseTo(viewport.width - 6, 0); + await expect + .poll(async () => { + const box = (await popup.boundingBox())!; + return box.y + box.height; + }) + .toBeCloseTo(viewport.height - 6, 0); + handle = (await title.boundingBox())!; + await page.mouse.move(handle.x + 5, handle.y + 5); + await page.mouse.down(); + await page.keyboard.press("Escape"); + await expect(popup).toHaveCount(0); + await expect(page.locator("[data-drag-cursor-surface]")).toHaveCount(0); + await page.mouse.up(); +}); + test("Show Usages distinguishes same-named files without losing the line or snippet", async ({ workbench: page }) => { await page.setViewportSize({ width: 480, height: 620 }); await page.evaluate(async () => { diff --git a/src/features/code-insight/UsagesPopup.module.css b/src/features/code-insight/UsagesPopup.module.css index 015c0542c7..dc89d477d0 100644 --- a/src/features/code-insight/UsagesPopup.module.css +++ b/src/features/code-insight/UsagesPopup.module.css @@ -5,6 +5,8 @@ overflow: hidden; } .header { + cursor: move; + touch-action: none; flex: none; display: flex; align-items: center; diff --git a/src/features/code-insight/UsagesPopup.tsx b/src/features/code-insight/UsagesPopup.tsx index 8b990f236b..734d80088d 100644 --- a/src/features/code-insight/UsagesPopup.tsx +++ b/src/features/code-insight/UsagesPopup.tsx @@ -188,6 +188,7 @@ function UsagesPopupInner() { onClose={close} aria-label="Usages" className={styles.popup} + draggable resizable="both" minWidth={MIN_WIDTH} minHeight={MIN_HEIGHT} @@ -204,7 +205,7 @@ function UsagesPopupInner() { data-scope={scope ?? undefined} onKeyDown={onKeyDown} > -
+
{symbol ? tx`Usages of ${symbol}` : tx("Usages")}{" "} diff --git a/src/ui/Popup.tsx b/src/ui/Popup.tsx index 49113b800c..2f9209eab4 100644 --- a/src/ui/Popup.tsx +++ b/src/ui/Popup.tsx @@ -1,7 +1,7 @@ /** * Anchored popover: portal + flip/shift positioning, Escape / outside-click / * window-deactivation dismissal (top-most popup only for Escape), optional - * focus trap and focus restore, optional resize from the right and bottom edges. + * focus trap and focus restore, optional title-bar dragging and edge resizing. */ import { createContext, @@ -71,6 +71,8 @@ export interface PopupProps { maxWidthOverAnchor?: number; /** Plain surface without border/shadow styling. */ unstyled?: boolean; + /** Move by descendants marked `data-popup-drag`, except interactive controls. */ + draggable?: boolean; /** * `both`: the right and bottom edges and their corner resize the popup (IDEA's resizable * popups); it keeps its anchored top-left corner. For a popup whose content doesn't scroll @@ -155,6 +157,7 @@ function PopupInner({ matchWidth = false, maxWidthOverAnchor, unstyled = false, + draggable = false, resizable, minWidth = 200, minHeight = 120, @@ -162,17 +165,19 @@ function PopupInner({ onResized, ...aria }: PopupProps) { - const centered = anchor === "center"; - const reference = toReference(anchor); + const [position, setPosition] = useState<{ x: number; y: number } | null>(null); + const moved = position !== null; + const centered = anchor === "center" && !moved; + const reference = toReference(position ?? anchor); const hasInlineMaxHeight = style?.maxHeight != null && style.maxHeight !== ""; const atPoint = !!anchor && typeof anchor === "object" && "x" in anchor && !("getBoundingClientRect" in anchor); const middleware = useMemo( () => [ - offsetMw(offset), + offsetMw(moved ? 0 : offset), hasInlineMaxHeight ? null : measureNaturalHeight, - flip({ padding: 6 }), + moved ? null : flip({ padding: 6 }), // At a point (a context menu) nothing is covered by moving up: fit the screen before scrolling. - shift({ padding: 6, crossAxis: atPoint }), + shift({ padding: 6, crossAxis: atPoint || moved }), sizeMw({ padding: 6, apply({ rects, elements, availableHeight, availableWidth, middlewareData }) { @@ -193,10 +198,10 @@ function PopupInner({ }, }), ], - [hasInlineMaxHeight, offset, matchWidth, maxWidthOverAnchor, atPoint], + [hasInlineMaxHeight, offset, matchWidth, maxWidthOverAnchor, atPoint, moved], ); const { refs, floatingStyles, isPositioned } = useFloating({ - placement, + placement: moved ? "bottom-start" : placement, strategy: "fixed", transform: false, middleware, @@ -214,6 +219,8 @@ function PopupInner({ const outer = useContext(PopupAncestry); const ancestry = useMemo(() => [...outer, layerId], [outer]); const [size, setSize] = useState(() => (resizable && initialSize) || null); + const cancelDrag = useRef<(() => void) | null>(null); + useEffect(() => () => cancelDrag.current?.(), []); useEffect(() => { const layer = openLayer(() => elRef.current, closeOnDeactivate ? () => onCloseRef.current("outside") : undefined); @@ -267,6 +274,42 @@ function PopupInner({ target.focus({ preventScroll: true }); }, [autoFocus]); + const drag = (e: ReactPointerEvent) => { + const el = elRef.current; + const target = e.target; + if (e.button !== 0 || !el || !(target instanceof Element) || !el.contains(target)) return; + if ( + !target.closest("[data-popup-drag]") || + target.closest('button, input, label, a, select, textarea, [contenteditable], [role="button"], [role="combobox"]') + ) + return; + e.preventDefault(); + cancelDrag.current?.(); + const releaseCursor = lockDragCursor("move"); + const start = { x: e.clientX, y: e.clientY }; + const rect = el.getBoundingClientRect(); + const move = (ev: PointerEvent) => { + if (!(ev.buttons & 1)) return up(); + setPosition({ + x: Math.max(6, Math.min(rect.left + ev.clientX - start.x, window.innerWidth - rect.width - 6)), + y: Math.max(6, Math.min(rect.top + ev.clientY - start.y, window.innerHeight - rect.height - 6)), + }); + }; + const up = () => { + window.removeEventListener("pointermove", move); + window.removeEventListener("pointerup", up); + window.removeEventListener("pointercancel", up); + window.removeEventListener("blur", up); + releaseCursor(); + cancelDrag.current = null; + }; + cancelDrag.current = up; + window.addEventListener("pointermove", move); + window.addEventListener("pointerup", up); + window.addEventListener("pointercancel", up); + window.addEventListener("blur", up); + }; + const resize = (edge: ResizeEdge) => (e: ReactPointerEvent) => { const el = elRef.current; if (e.button !== 0 || !el) return; @@ -306,6 +349,7 @@ function PopupInner({ aria-label={aria["aria-label"]} aria-describedby={aria["aria-describedby"]} tabIndex={-1} + onPointerDown={draggable ? drag : undefined} className={clsx(styles.popup, centered && styles.centered, !unstyled && styles.surface, className)} data-positioned={centered || isPositioned || undefined} data-resized={size ? "" : undefined} diff --git a/src/ui/dragCursor.ts b/src/ui/dragCursor.ts index 3ebd0c2675..a398ff0187 100644 --- a/src/ui/dragCursor.ts +++ b/src/ui/dragCursor.ts @@ -1,4 +1,4 @@ -export type DragCursor = "col-resize" | "row-resize" | "ew-resize" | "ns-resize" | "nwse-resize"; +export type DragCursor = "move" | "col-resize" | "row-resize" | "ew-resize" | "ns-resize" | "nwse-resize"; const active = new Set<() => void>(); let listening = false;