Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(ui): add submenu pointer grace area for diagonal hover

HARRISONe08a59537b27
diff --git a/apps/desktop/src/components/ui/CustomContextMenu.vue b/apps/desktop/src/components/ui/CustomContextMenu.vue
index e3e1fe4e43..64b2395977 100644
--- a/apps/desktop/src/components/ui/CustomContextMenu.vue
+++ b/apps/desktop/src/components/ui/CustomContextMenu.vue
@@ -3,6 +3,7 @@ import { ref, watch, onBeforeUnmount, onMounted, nextTick } from "vue";
import { Check, ChevronRight } from "@lucide/vue";
import { shortcutDisplayKeys } from "@/lib/editor/shortcutDisplay";
import { registerGlobalContextMenu, type ContextMenuRegistration, type ContextMenuItem } from "@/components/ui/customContextMenuRegistry";
+import { buildSubmenuGraceArea, isPointerInGraceArea, type GraceAreaPoint } from "@/lib/ui/pointerGraceArea";
export type { ContextMenuItem };
@@ -34,6 +35,10 @@ const subX = ref(0);
const subY = ref(0);
let subCloseTimer: ReturnType<typeof setTimeout> | null = null;
let subAnchorRect: { left: number; right: number; top: number; bottom: number } | null = null;
+// The wedge between the trigger row and the submenu panel, rebuilt each time the
+// pointer leaves a trigger. While the pointer stays inside it the submenu is
+// treated as hovered, so a diagonal move towards the panel does not close it.
+let subGraceArea: GraceAreaPoint[] | null = null;
let contextMenuRegistration: ContextMenuRegistration | null = null;
let menuRevision = 0;
@@ -42,6 +47,7 @@ function close() {
menuRevision += 1;
activeSubIndex.value = null;
subAnchorRect = null;
+ subGraceArea = null;
activeItems.value = [];
show.value = false;
emit("close");
@@ -94,11 +100,15 @@ watch(show, (val) => {
document.addEventListener("pointerdown", onPointerDownOutside, true);
document.addEventListener("keydown", onKeydown, true);
document.addEventListener("scroll", onScroll, true);
+ // Tracked on the document, in the capture phase, so the trigger-to-submenu
+ // journey is observed even where neither panel is under the cursor.
+ document.addEventListener("pointermove", onDocumentPointerMove, true);
window.addEventListener("resize", onResize);
} else {
document.removeEventListener("pointerdown", onPointerDownOutside, true);
document.removeEventListener("keydown", onKeydown, true);
document.removeEventListener("scroll", onScroll, true);
+ document.removeEventListener("pointermove", onDocumentPointerMove, true);
window.removeEventListener("resize", onResize);
}
});
@@ -180,6 +190,7 @@ function onContextMenu(event: MouseEvent, itemsOverride?: ContextMenuItem[]) {
function onItemMouseEnter(index: number, event: MouseEvent) {
lastMouseX = event.clientX;
lastMouseY = event.clientY;
+ subGraceArea = null;
const item = activeItems.value[index];
if (!item?.children?.length || itemIsDisabled(item)) {
// Moving to an item without children — close submenu immediately, no delay needed
@@ -199,27 +210,62 @@ function onItemMouseEnter(index: number, event: MouseEvent) {
nextTick(() => adjustSubPosition());
}
-function onItemMouseLeave() {
- if (activeSubIndex.value !== null) {
- scheduleSubClose();
- }
+function onItemMouseLeave(event: MouseEvent) {
+ if (activeSubIndex.value === null) return;
+ subGraceArea = buildSubmenuGraceAreaForPointer(event.clientX, event.clientY);
+ scheduleSubClose();
}
-// Last known mouse position (from hover events, without a global mousemove listener)
+// Last known pointer position. Updated by a document-level pointermove while the
+// menu is open; hover events alone are not enough, because the pointer spends the
+// trigger-to-submenu journey in the gap between the two panels, where nothing
+// fires.
let lastMouseX = 0;
let lastMouseY = 0;
+function buildSubmenuGraceAreaForPointer(x: number, y: number): GraceAreaPoint[] | null {
+ const sub = subRef.value?.getBoundingClientRect();
+ if (!sub) return null;
+ // The submenu's edge facing the trigger is its left edge when the panel opens
+ // to the right, and its right edge when it flips to the left. Comparing with
+ // the pointer picks the correct one without tracking the flip separately.
+ const nearEdgeX = x <= sub.left ? sub.left : sub.right;
+ return buildSubmenuGraceArea({
+ origin: { x, y },
+ nearEdgeX,
+ top: sub.top,
+ bottom: sub.bottom,
+ });
+}
+
function isMouseOverSub(): boolean {
if (!subRef.value) return false;
const rect = subRef.value.getBoundingClientRect();
return lastMouseX >= rect.left && lastMouseX <= rect.right && lastMouseY >= rect.top && lastMouseY <= rect.bottom;
}
+function pointerIsHeadingToSubmenu(): boolean {
+ return isMouseOverSub() || isPointerInGraceArea({ x: lastMouseX, y: lastMouseY }, subGraceArea);
+}
+
+function onDocumentPointerMove(event: PointerEvent) {
+ lastMouseX = event.clientX;
+ lastMouseY = event.clientY;
+ // Cancel a pending close as soon as the pointer proves it is moving towards the
+ // submenu, rather than waiting for the timer and re-checking its position.
+ if (subCloseTimer && pointerIsHeadingToSubmenu()) {
+ clearTimeout(subCloseTimer);
+ subCloseTimer = null;
+ }
+}
+
function scheduleSubClose() {
if (subCloseTimer) clearTimeout(subCloseTimer);
subCloseTimer = setTimeout(() => {
- if (!isMouseOverSub()) {
+ subCloseTimer = null;
+ if (!pointerIsHeadingToSubmenu()) {
activeSubIndex.value = null;
+ subGraceArea = null;
}
}, 150);
}
@@ -233,6 +279,7 @@ function onSubMouseEnter() {
function onSubMouseLeave() {
activeSubIndex.value = null;
+ subGraceArea = null;
}
function adjustSubPosition() {
@@ -297,6 +344,7 @@ onBeforeUnmount(() => {
document.removeEventListener("pointerdown", onPointerDownOutside, true);
document.removeEventListener("keydown", onKeydown, true);
document.removeEventListener("scroll", onScroll, true);
+ document.removeEventListener("pointermove", onDocumentPointerMove, true);
window.removeEventListener("resize", onResize);
});
</script>
diff --git a/apps/desktop/src/components/ui/__tests__/CustomContextMenu.spec.ts b/apps/desktop/src/components/ui/__tests__/CustomContextMenu.spec.ts
index 5fd3b06985..63cd725fb0 100644
--- a/apps/desktop/src/components/ui/__tests__/CustomContextMenu.spec.ts
+++ b/apps/desktop/src/components/ui/__tests__/CustomContextMenu.spec.ts
@@ -490,3 +490,99 @@ describe("CustomContextMenu lifecycle", () => {
app.unmount();
});
});
+
+describe("submenu pointer grace area", () => {
+ // The panel sits to the right of the trigger row between these bounds.
+ // happy-dom performs no layout, so the rectangles the component measures are
+ // stubbed on the submenu element directly.
+ const SUBMENU_RECT = { left: 200, right: 300, top: 100, bottom: 200, width: 100, height: 100 } as DOMRect;
+
+ async function mountSubmenuMenu() {
+ const root = defineComponent({
+ setup: () => () => h(CustomContextMenu, { items: [{ label: "Data tools", children: [{ label: "Export" }] }] }, { default: ({ onContextMenu }: { onContextMenu: (event: MouseEvent) => void }) => h("div", { id: "grace-target", onContextmenu: onContextMenu }) }),
+ });
+ const container = document.createElement("div");
+ mountedContainers.push(container);
+ document.body.append(container);
+ const app = createApp(root);
+ app.mount(container);
+ container.querySelector("#grace-target")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+ await nextTick();
+ const trigger = Array.from(document.body.querySelectorAll("button")).find((button) => button.textContent === "Data tools")!;
+ trigger.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true, clientX: 150, clientY: 150 }));
+ await nextTick();
+ const submenu = Array.from(document.body.querySelectorAll("[data-dbx-context-menu]")).at(-1) as HTMLElement;
+ submenu.getBoundingClientRect = () => SUBMENU_RECT;
+ return { app, trigger };
+ }
+
+ function submenuIsOpen(): boolean {
+ return document.body.textContent?.includes("Export") ?? false;
+ }
+
+ function movePointerTo(x: number, y: number) {
+ document.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, clientX: x, clientY: y }));
+ }
+
+ it("keeps the submenu open while the pointer travels diagonally towards it", async () => {
+ vi.useFakeTimers();
+ try {
+ const { app, trigger } = await mountSubmenuMenu();
+ expect(submenuIsOpen()).toBe(true);
+
+ // Leaving the trigger row and crossing the gap between the two panels puts
+ // the pointer inside neither rectangle, which is the movement that used to
+ // close the submenu before it could be reached.
+ trigger.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true, clientX: 150, clientY: 150 }));
+ movePointerTo(190, 150);
+ await nextTick();
+ vi.advanceTimersByTime(400);
+ await nextTick();
+
+ expect(submenuIsOpen()).toBe(true);
+ app.unmount();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("keeps the submenu open when the pointer wobbles mid-travel", async () => {
+ vi.useFakeTimers();
+ try {
+ const { app, trigger } = await mountSubmenuMenu();
+
+ trigger.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true, clientX: 150, clientY: 150 }));
+ movePointerTo(190, 150);
+ await nextTick();
+ vi.advanceTimersByTime(400);
+ // A second move that is still inside the wedge must not re-arm the close.
+ movePointerTo(195, 165);
+ await nextTick();
+ vi.advanceTimersByTime(400);
+ await nextTick();
+
+ expect(submenuIsOpen()).toBe(true);
+ app.unmount();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("still closes the submenu when the pointer moves away from it", async () => {
+ vi.useFakeTimers();
+ try {
+ const { app, trigger } = await mountSubmenuMenu();
+
+ trigger.dispatchEvent(new MouseEvent("mouseleave", { bubbles: true, clientX: 150, clientY: 150 }));
+ movePointerTo(120, 300);
+ await nextTick();
+ vi.advanceTimersByTime(400);
+ await nextTick();
+
+ expect(submenuIsOpen()).toBe(false);
+ app.unmount();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});
diff --git a/apps/desktop/src/lib/ui/__tests__/pointerGraceArea.spec.ts b/apps/desktop/src/lib/ui/__tests__/pointerGraceArea.spec.ts
new file mode 100644
index 0000000000..9c7d6f4eb3
--- /dev/null
+++ b/apps/desktop/src/lib/ui/__tests__/pointerGraceArea.spec.ts
@@ -0,0 +1,101 @@
+import { describe, expect, it } from "vitest";
+import { buildSubmenuGraceArea, isPointInPolygon, isPointerInGraceArea } from "../pointerGraceArea";
+
+describe("isPointInPolygon", () => {
+ const square = [
+ { x: 0, y: 0 },
+ { x: 10, y: 0 },
+ { x: 10, y: 10 },
+ { x: 0, y: 10 },
+ ];
+
+ it("rejects anything that cannot bound an area", () => {
+ expect(isPointInPolygon({ x: 5, y: 5 }, [])).toBe(false);
+ expect(isPointInPolygon({ x: 5, y: 5 }, [{ x: 0, y: 0 }])).toBe(false);
+ expect(
+ isPointInPolygon({ x: 5, y: 5 }, [
+ { x: 0, y: 0 },
+ { x: 10, y: 10 },
+ ]),
+ ).toBe(false);
+ });
+
+ it("separates inside from outside points", () => {
+ expect(isPointInPolygon({ x: 5, y: 5 }, square)).toBe(true);
+ expect(isPointInPolygon({ x: -1, y: 5 }, square)).toBe(false);
+ expect(isPointInPolygon({ x: 11, y: 5 }, square)).toBe(false);
+ expect(isPointInPolygon({ x: 5, y: -1 }, square)).toBe(false);
+ expect(isPointInPolygon({ x: 5, y: 11 }, square)).toBe(false);
+ });
+
+ it("handles a non-convex outline", () => {
+ // An L: the 0..10 square with its top-right quadrant removed, so the
+ // bounding box contains points the outline does not.
+ const lShape = [
+ { x: 0, y: 0 },
+ { x: 10, y: 0 },
+ { x: 10, y: 4 },
+ { x: 4, y: 4 },
+ { x: 4, y: 10 },
+ { x: 0, y: 10 },
+ ];
+ expect(isPointInPolygon({ x: 2, y: 2 }, lShape)).toBe(true);
+ expect(isPointInPolygon({ x: 8, y: 2 }, lShape)).toBe(true);
+ expect(isPointInPolygon({ x: 2, y: 8 }, lShape)).toBe(true);
+ // Inside the bounding box but in the removed quadrant.
+ expect(isPointInPolygon({ x: 8, y: 8 }, lShape)).toBe(false);
+ });
+});
+
+describe("isPointerInGraceArea", () => {
+ it("treats a missing area as no match", () => {
+ expect(isPointerInGraceArea({ x: 1, y: 1 }, null)).toBe(false);
+ expect(isPointerInGraceArea({ x: 1, y: 1 }, undefined)).toBe(false);
+ });
+
+ it("delegates to the polygon test", () => {
+ const area = buildSubmenuGraceArea({ origin: { x: 0, y: 5 }, nearEdgeX: 10, top: 0, bottom: 10 });
+ expect(isPointerInGraceArea({ x: 5, y: 5 }, area)).toBe(true);
+ expect(isPointerInGraceArea({ x: 50, y: 5 }, area)).toBe(false);
+ });
+});
+
+describe("buildSubmenuGraceArea", () => {
+ it("fans out from the departure point across the submenu edge", () => {
+ const area = buildSubmenuGraceArea({ origin: { x: 0, y: 5 }, nearEdgeX: 10, top: 0, bottom: 10 });
+ // The path right after leaving the trigger stays inside.
+ expect(isPointerInGraceArea({ x: 1, y: 5 }, area)).toBe(true);
+ // Both corners of the submenu edge are reachable, which is what survives a
+ // diagonal move: the wedge spans the whole edge, not just the departure row.
+ expect(isPointerInGraceArea({ x: 9, y: 1 }, area)).toBe(true);
+ expect(isPointerInGraceArea({ x: 9, y: 9 }, area)).toBe(true);
+ // Outside the triangle: past the submenu edge, and behind the origin. The
+ // submenu's own rectangle is covered by the hover check instead.
+ expect(isPointerInGraceArea({ x: 11, y: 5 }, area)).toBe(false);
+ expect(isPointerInGraceArea({ x: -1, y: 5 }, area)).toBe(false);
+ });
+
+ it("points leftwards when the submenu sits left of the pointer", () => {
+ const area = buildSubmenuGraceArea({ origin: { x: 60, y: 5 }, nearEdgeX: 50, top: 0, bottom: 10 });
+ expect(isPointerInGraceArea({ x: 55, y: 5 }, area)).toBe(true);
+ expect(isPointerInGraceArea({ x: 65, y: 5 }, area)).toBe(false);
+ });
+
+ it("only admits movement converging on the submenu when the pointer left far above it", () => {
+ const area = buildSubmenuGraceArea({ origin: { x: 0, y: -40 }, nearEdgeX: 10, top: 0, bottom: 10 });
+ // Sliding down towards the submenu's edge is admitted...
+ expect(isPointerInGraceArea({ x: 7, y: -8 }, area)).toBe(true);
+ // ...while moving further away, or drifting sideways, is not.
+ expect(isPointerInGraceArea({ x: 7, y: -20 }, area)).toBe(false);
+ expect(isPointerInGraceArea({ x: 6, y: -8 }, area)).toBe(false);
+ });
+
+ it("keeps the base pinned to the submenu edge", () => {
+ const area = buildSubmenuGraceArea({ origin: { x: 3, y: 4 }, nearEdgeX: 12, top: 2, bottom: 8 });
+ expect(area).toEqual([
+ { x: 3, y: 4 },
+ { x: 12, y: 2 },
+ { x: 12, y: 8 },
+ ]);
+ });
+});
diff --git a/apps/desktop/src/lib/ui/pointerGraceArea.ts b/apps/desktop/src/lib/ui/pointerGraceArea.ts
new file mode 100644
index 0000000000..18e9f12423
--- /dev/null
+++ b/apps/desktop/src/lib/ui/pointerGraceArea.ts
@@ -0,0 +1,57 @@
+export interface GraceAreaPoint {
+ x: number;
+ y: number;
+}
+
+/**
+ * Ray-casting point-in-polygon test.
+ *
+ * This backs the "safe triangle" that keeps a submenu open while the pointer
+ * travels from its trigger row to the submenu panel. The two are separate boxes
+ * with a gap between them, so the pointer always leaves the trigger before it
+ * reaches the submenu, and a diagonal move dips outside both. Treating the wedge
+ * between them as part of the submenu stops the menu from closing mid-travel.
+ */
+export function isPointInPolygon(point: GraceAreaPoint, polygon: readonly GraceAreaPoint[]): boolean {
+ if (polygon.length < 3) return false;
+ let inside = false;
+ for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
+ const { x: xi, y: yi } = polygon[i]!;
+ const { x: xj, y: yj } = polygon[j]!;
+ // The edge (xi, yi)-(xj, yj) crosses the ray cast to the right from `point`
+ // exactly when the point sits between the two y values and lies left of the
+ // intersection x. The opposite-side test also guarantees `yj - yi` is
+ // non-zero, so the division is safe.
+ const crossesRay = yi > point.y !== yj > point.y && point.x < ((xj - xi) * (point.y - yi)) / (yj - yi) + xi;
+ if (crossesRay) inside = !inside;
+ }
+ return inside;
+}
+
+/** Null-safe wrapper: no area means the pointer is not inside it. */
+export function isPointerInGraceArea(point: GraceAreaPoint, area: readonly GraceAreaPoint[] | null | undefined): boolean {
+ if (!area) return false;
+ return isPointInPolygon(point, area);
+}
+
+/**
+ * Builds the safe triangle used while moving from a menu trigger into its
+ * submenu: one vertex where the pointer left the trigger, and a base spanning
+ * the full height of the submenu's edge that faces the trigger.
+ *
+ * A triangle rather than a polygon covering the whole submenu is deliberate.
+ * The pointer has just left the trigger, so it always sits outside the submenu
+ * horizontally, which keeps this a simple, convex shape. A shape that reached
+ * across to the submenu's far edge could instead intersect itself when the
+ * pointer lies between the submenu's two vertical edges, and the ray-casting
+ * test would then give an answer that is not meaningful. The submenu's own
+ * rectangle is covered by the ordinary hover check, so nothing is lost.
+ */
+export function buildSubmenuGraceArea(options: { origin: GraceAreaPoint; nearEdgeX: number; top: number; bottom: number }): GraceAreaPoint[] {
+ const { origin, nearEdgeX, top, bottom } = options;
+ return [
+ { x: origin.x, y: origin.y },
+ { x: nearEdgeX, y: top },
+ { x: nearEdgeX, y: bottom },
+ ];
+}