diff --git a/e2e/toolwindows.spec.ts b/e2e/toolwindows.spec.ts index 5b7c41131f..e76a9cc1f2 100644 --- a/e2e/toolwindows.spec.ts +++ b/e2e/toolwindows.spec.ts @@ -168,6 +168,24 @@ test("⌘⇧F12 hides all tool windows and restores them", async ({ workbench: p await expect(page.getByRole("region", { name: "Problems" })).toBeVisible(); }); +test("a tool window resize keeps its cursor over other controls until release", async ({ workbench: page }) => { + const gap = page.locator('[data-gap="left"]'); + const box = (await gap.boundingBox())!; + const hide = page.getByRole("region", { name: "Project" }).getByRole("button", { name: "Hide", exact: true }); + const cursor = () => hide.evaluate((el) => getComputedStyle(el).cursor); + + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); + await page.mouse.down(); + await page.mouse.move(box.x + 80, box.y + box.height / 2, { steps: 4 }); + // The pointer is over the Project tree now; the button's own arrow cursor must not win. + await expect.poll(cursor).toBe("ew-resize"); + expect(await page.evaluate(() => document.documentElement.hasAttribute("data-drag-cursor"))).toBe(true); + + await page.mouse.up(); + await expect.poll(cursor).toBe("default"); + expect(await page.evaluate(() => document.documentElement.hasAttribute("data-drag-cursor"))).toBe(false); +}); + test("the Terminal sends typed commands to the shell and shows its output", async ({ workbench: page }) => { // The shell is stubbed at the IPC layer: it echoes input and answers `echo`. await stubDesktop(page); diff --git a/src/features/database/Database.module.css b/src/features/database/Database.module.css index 34638a18a4..25149c79ef 100644 --- a/src/features/database/Database.module.css +++ b/src/features/database/Database.module.css @@ -67,7 +67,7 @@ right: -3px; width: 6px; height: 100%; - cursor: col-resize; + cursor: ew-resize; } .rowNumber { display: flex; diff --git a/src/features/database/Grid.tsx b/src/features/database/Grid.tsx index 479006099e..695e998f93 100644 --- a/src/features/database/Grid.tsx +++ b/src/features/database/Grid.tsx @@ -17,7 +17,7 @@ import { } from "react"; import { useVirtualizer } from "@tanstack/react-virtual"; import clsx from "clsx"; -import { Checkbox, Dialog, Icon, Popup, SelectListbox, showContextMenu, type MenuItem } from "@/ui"; +import { Checkbox, Dialog, Icon, lockDragCursor, Popup, SelectListbox, showContextMenu, type MenuItem } from "@/ui"; import { parseDelimited, toCsv, toOneRow, toTsv, type Cell } from "./model"; import { initialJsonText, isJsonColumn, isJsonValue } from "./jsonValue"; import { JsonValueWindow } from "./JsonValueEditor"; @@ -520,6 +520,7 @@ export function Grid(props: GridProps) { const startResize = (e: MouseEvent, col: number) => { e.preventDefault(); e.stopPropagation(); + const releaseCursor = lockDragCursor("ew-resize"); const startX = e.clientX; const start = widths[col] ?? 100; const move = (ev: globalThis.MouseEvent) => { @@ -529,6 +530,7 @@ export function Grid(props: GridProps) { const up = () => { window.removeEventListener("mousemove", move); window.removeEventListener("mouseup", up); + releaseCursor(); }; window.addEventListener("mousemove", move); window.addEventListener("mouseup", up); diff --git a/src/features/vcs/GitLog.module.css b/src/features/vcs/GitLog.module.css index c543dd8f83..d979c681b2 100644 --- a/src/features/vcs/GitLog.module.css +++ b/src/features/vcs/GitLog.module.css @@ -224,7 +224,7 @@ inset-block: 0; inset-inline-end: 0; width: 6px; - cursor: col-resize; + cursor: ew-resize; touch-action: none; } .columnResize:focus-visible { diff --git a/src/features/vcs/GitLog.tsx b/src/features/vcs/GitLog.tsx index c412bf76ae..40179ad261 100644 --- a/src/features/vcs/GitLog.tsx +++ b/src/features/vcs/GitLog.tsx @@ -47,6 +47,7 @@ import { copyText, hideNativeScrollbar, lazyComponent, + lockDragCursor, Scrollbar, } from "@/ui"; import { hasPrimaryModifier } from "@/platform/keymap"; @@ -1894,10 +1895,17 @@ function LogColumnHeader({ element: HTMLElement; pointerId: number; } | null>(null); - const resizeDrag = useRef<{ x: number; width: number; element: HTMLElement; pointerId: number } | null>(null); + const resizeDrag = useRef<{ + x: number; + width: number; + element: HTMLElement; + pointerId: number; + releaseCursor: () => void; + } | null>(null); const cancel = useCallback(() => { const captures = [press.current, resizeDrag.current]; press.current = null; + resizeDrag.current?.releaseCursor(); resizeDrag.current = null; for (const capture of captures) if (capture?.element.hasPointerCapture?.(capture.pointerId)) @@ -1999,7 +2007,13 @@ function LogColumnHeader({ if (e.button !== 0) return; e.preventDefault(); e.stopPropagation(); - resizeDrag.current = { x: e.clientX, width, element: e.currentTarget, pointerId: e.pointerId }; + resizeDrag.current = { + x: e.clientX, + width, + element: e.currentTarget, + pointerId: e.pointerId, + releaseCursor: lockDragCursor("ew-resize"), + }; e.currentTarget.setPointerCapture(e.pointerId); }} onPointerMove={(e) => { @@ -2007,13 +2021,16 @@ function LogColumnHeader({ resize(resizeDrag.current.width + e.clientX - resizeDrag.current.x); }} onPointerUp={(e) => { + resizeDrag.current?.releaseCursor(); resizeDrag.current = null; if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId); }} onPointerCancel={() => { + resizeDrag.current?.releaseCursor(); resizeDrag.current = null; }} onLostPointerCapture={() => { + resizeDrag.current?.releaseCursor(); resizeDrag.current = null; }} onKeyDown={(e) => { diff --git a/src/styles/global.css b/src/styles/global.css index a3617e3cd4..acae1dd175 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -38,6 +38,14 @@ body { cursor: default; } +/* A resize drag owns the cursor until release, even over the editor or a button. */ +html[data-drag-cursor], +html[data-drag-cursor] *, +html[data-drag-cursor] *::before, +html[data-drag-cursor] *::after { + cursor: var(--drag-cursor) !important; +} + html[data-selection-lock] *:not([data-selection-scope], [data-selection-scope] *) { -webkit-user-select: none !important; user-select: none !important; diff --git a/src/ui/Dialog.tsx b/src/ui/Dialog.tsx index 4ed77ba3df..cc13a0656e 100644 --- a/src/ui/Dialog.tsx +++ b/src/ui/Dialog.tsx @@ -10,6 +10,7 @@ import { selectableText } from "./selectableText"; import appIcon from "../../src-tauri/icons/128x128.png"; import styles from "./Dialog.module.css"; import { isImeKey } from "@/platform/ime"; +import { lockDragCursor } from "./dragCursor"; /** * `default`: filled accent button, triggered by Enter. `cancel`: Esc / close @@ -370,6 +371,7 @@ function DialogInner({ const el = ref.current; if (!el) return; e.preventDefault(); + const releaseCursor = lockDragCursor("nwse-resize"); const start = { x: e.clientX, y: e.clientY }; const rect = el.getBoundingClientRect(); const minW = minWidth ?? (typeof width === "number" ? width : 320); @@ -395,6 +397,7 @@ function DialogInner({ const up = () => { window.removeEventListener("pointermove", move); window.removeEventListener("pointerup", up); + releaseCursor(); if (last) onResized?.(last); }; window.addEventListener("pointermove", move); diff --git a/src/ui/Splitter.module.css b/src/ui/Splitter.module.css index 0445ddae31..130a47f03b 100644 --- a/src/ui/Splitter.module.css +++ b/src/ui/Splitter.module.css @@ -18,11 +18,11 @@ } .handle[aria-orientation="vertical"] { width: 1px; - cursor: col-resize; + cursor: ew-resize; } .handle[aria-orientation="horizontal"] { height: 1px; - cursor: row-resize; + cursor: ns-resize; } .handle::after { content: ""; diff --git a/src/ui/dragCursor.test.ts b/src/ui/dragCursor.test.ts new file mode 100644 index 0000000000..f326a04f52 --- /dev/null +++ b/src/ui/dragCursor.test.ts @@ -0,0 +1,48 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { lockDragCursor } from "./dragCursor"; + +const html = document.documentElement; + +afterEach(() => { + html.removeAttribute("data-drag-cursor"); + html.style.removeProperty("--drag-cursor"); +}); + +describe("lockDragCursor", () => { + it("holds the cursor until the drag ends", () => { + const release = lockDragCursor("ew-resize"); + expect(html.getAttribute("data-drag-cursor")).toBe(""); + expect(html.style.getPropertyValue("--drag-cursor")).toBe("ew-resize"); + + release(); + expect(html.hasAttribute("data-drag-cursor")).toBe(false); + expect(html.style.getPropertyValue("--drag-cursor")).toBe(""); + }); + + it("ignores a repeated release", () => { + const release = lockDragCursor("ns-resize"); + release(); + release(); + expect(html.hasAttribute("data-drag-cursor")).toBe(false); + }); + + it("restores an outer drag after a nested one ends", () => { + const outer = lockDragCursor("ew-resize"); + const inner = lockDragCursor("nwse-resize"); + expect(html.style.getPropertyValue("--drag-cursor")).toBe("nwse-resize"); + + inner(); + expect(html.getAttribute("data-drag-cursor")).toBe(""); + expect(html.style.getPropertyValue("--drag-cursor")).toBe("ew-resize"); + + outer(); + expect(html.hasAttribute("data-drag-cursor")).toBe(false); + }); + + it("releases when the pointer goes up outside the handle", () => { + lockDragCursor("ew-resize"); + document.dispatchEvent(new Event("pointerup", { bubbles: true })); + expect(html.hasAttribute("data-drag-cursor")).toBe(false); + expect(html.style.getPropertyValue("--drag-cursor")).toBe(""); + }); +}); diff --git a/src/ui/dragCursor.ts b/src/ui/dragCursor.ts new file mode 100644 index 0000000000..da97572f4f --- /dev/null +++ b/src/ui/dragCursor.ts @@ -0,0 +1,50 @@ +/** + * Keeps one cursor over the whole window while a resize drag runs. + * + * `document.body.style.cursor` is not enough: the pointer soon leaves the + * handle and elements with a cursor of their own (the editor's I-beam, a + * button's arrow) take over, so the resize cursor appears to fall back to the + * default one mid-drag. `global.css` keys off `data-drag-cursor` instead and + * wins over those cursors with `!important`. + */ +export type DragCursor = "ew-resize" | "ns-resize" | "nwse-resize"; + +const active = new Set<() => void>(); +let listening = false; + +/** Safety net: a drag that never reports its end (unmounted handle, lost capture) must not hold the cursor. */ +function releaseAll() { + // Last lock first: a nested one restores the outer cursor before the outer one clears it. + for (const release of [...active].reverse()) release(); +} + +function listenForEnd() { + if (listening) return; + listening = true; + document.addEventListener("pointerup", releaseAll, true); + document.addEventListener("pointercancel", releaseAll, true); + window.addEventListener("blur", releaseAll); +} + +/** Returns the release function; call it when the drag ends or is cancelled. */ +export function lockDragCursor(cursor: DragCursor): () => void { + const html = document.documentElement; + const previous = html.style.getPropertyValue("--drag-cursor"); + const wasLocked = html.hasAttribute("data-drag-cursor"); + html.style.setProperty("--drag-cursor", cursor); + html.setAttribute("data-drag-cursor", ""); + listenForEnd(); + let released = false; + const release = () => { + if (released) return; + released = true; + active.delete(release); + if (wasLocked) html.style.setProperty("--drag-cursor", previous); + else { + html.removeAttribute("data-drag-cursor"); + html.style.removeProperty("--drag-cursor"); + } + }; + active.add(release); + return release; +} diff --git a/src/ui/index.ts b/src/ui/index.ts index 3247a18a57..60167b9795 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -37,6 +37,7 @@ export { lazyComponent, lazyWhen, prefetchLazyComponents } from "./lazy"; export { Given, T } from "./T"; export { selectableText, copyMenuFor, copyText } from "./selectableText"; export { useSelectionBoundary } from "./useSelectionBoundary"; +export { lockDragCursor, type DragCursor } from "./dragCursor"; export { useSanitizedField } from "./useSanitizedField"; export * from "./rowSelection"; export * from "./LogPane"; diff --git a/src/workbench/toolwindows/ToolWindows.module.css b/src/workbench/toolwindows/ToolWindows.module.css index 32db496a24..a419fc4029 100644 --- a/src/workbench/toolwindows/ToolWindows.module.css +++ b/src/workbench/toolwindows/ToolWindows.module.css @@ -133,10 +133,10 @@ grid-area: gap-bottom; } .gap[aria-orientation="vertical"] { - cursor: col-resize; + cursor: ew-resize; } .gap[aria-orientation="horizontal"] { - cursor: row-resize; + cursor: ns-resize; } /* A slightly wider hit area than the gap itself. */ .gap::before { @@ -209,7 +209,7 @@ flex: none; width: 0; outline: none; - cursor: col-resize; + cursor: ew-resize; } .stripeHandle::before { content: ""; diff --git a/src/workbench/toolwindows/ToolWindows.tsx b/src/workbench/toolwindows/ToolWindows.tsx index c29d09ee7c..6bc9b7eecf 100644 --- a/src/workbench/toolwindows/ToolWindows.tsx +++ b/src/workbench/toolwindows/ToolWindows.tsx @@ -17,7 +17,16 @@ import { create } from "zustand"; import { call, commands } from "@/platform/ipc"; import { shortcutLabel } from "@/platform/keymap"; import { useProject } from "@/platform/project"; -import { dragRequiresAlt, Icon, IconButton, Menu, showContextMenu, tooltipAttrs, type MenuItem } from "@/ui"; +import { + dragRequiresAlt, + Icon, + IconButton, + lockDragCursor, + Menu, + showContextMenu, + tooltipAttrs, + type MenuItem, +} from "@/ui"; import { FOCUSABLE } from "@/ui/popupState"; import { TabStripRegistry } from "@/ui/Tabs"; import { @@ -229,8 +238,7 @@ function StripeHandle({ const origin = e.clientX; const icons = parseFloat(getComputedStyle(stripe).getPropertyValue("--stripe-icon-button")) || 30; const start = names ? width : icons; - const cursor = document.body.style.cursor; - document.body.style.cursor = "col-resize"; + const releaseCursor = lockDragCursor("ew-resize"); let next = { names, width, px: start }; const move = (ev: globalThis.PointerEvent) => { if (!(ev.buttons & 1)) return end(); @@ -243,7 +251,7 @@ function StripeHandle({ el.removeEventListener("pointermove", move); el.removeEventListener("pointerup", end); el.removeEventListener("pointercancel", end); - document.body.style.cursor = cursor; + releaseCursor(); stripe.style.removeProperty("--stripe-drag"); onCommit(next.names, next.width); }; @@ -885,8 +893,7 @@ function SplitHandle({ anchor, hidden }: { anchor: ToolWindowAnchor; hidden: boo e.preventDefault(); el.setPointerCapture(e.pointerId); const origin = stacked ? e.clientY : e.clientX; - const cursor = document.body.style.cursor; - document.body.style.cursor = stacked ? "row-resize" : "col-resize"; + const releaseCursor = lockDragCursor(stacked ? "ns-resize" : "ew-resize"); let ratio: number | null = null; const move = (ev: globalThis.PointerEvent) => { if (!(ev.buttons & 1)) return end(); @@ -897,7 +904,7 @@ function SplitHandle({ anchor, hidden }: { anchor: ToolWindowAnchor; hidden: boo el.removeEventListener("pointermove", move); el.removeEventListener("pointerup", end); el.removeEventListener("pointercancel", end); - document.body.style.cursor = cursor; + releaseCursor(); if (ratio !== null) setToolWindowSplit(anchor, ratio); // Back to the store's value (unchanged when the ratio was clamped to it). apply(m.areas, useToolWindowLayout.getState().splits[anchor]); @@ -1042,8 +1049,7 @@ function GapHandle({ const el = e.currentTarget; el.setPointerCapture(e.pointerId); const origin = vertical ? e.clientX : e.clientY; - const cursor = document.body.style.cursor; - document.body.style.cursor = vertical ? "col-resize" : "row-resize"; + const releaseCursor = lockDragCursor(vertical ? "ew-resize" : "ns-resize"); let next = m.size; const move = (ev: globalThis.PointerEvent) => { if (!(ev.buttons & 1)) return end(); @@ -1056,7 +1062,7 @@ function GapHandle({ el.removeEventListener("pointermove", move); el.removeEventListener("pointerup", end); el.removeEventListener("pointercancel", end); - document.body.style.cursor = cursor; + releaseCursor(); if (next === 0) visibleIds.forEach(hideToolWindow); else if (next !== m.size) commit(next); else sync();