A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(editor): keep Create submenu flyouts inside the window
yhhdf8db21cde3c
diff --git a/desktop/src/components/Menu.tsx b/desktop/src/components/Menu.tsxindex d57116b66..9de2ec661 100644--- a/desktop/src/components/Menu.tsx+++ b/desktop/src/components/Menu.tsx@@ -7,9 +7,10 @@* (labels, shortcuts, checkmarks, disabled state, separators, nested* submenus) and the context-menu dismiss behaviour live in one place.*/-import { useEffect, useLayoutEffect, useRef, useState, type ReactNode } from 'react';+import { useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react';import { createPortal } from 'react-dom';import { Check, ChevronRight } from 'lucide-react';+import { submenuPlacement } from './menuPlacement';export type MenuItem =| { sep: true }@@ -30,6 +31,22 @@ const cls = (v: Variant, ctx: string, bar: string) => (v === 'ctx' ? ctx : bar);/** One menu row: separator, a flyout submenu, or a leaf action. */function MenuNode({ item, variant, close }: { item: MenuItem; variant: Variant; close: () => void }) {const [open, setOpen] = useState(false);+ const rowRef = useRef<HTMLDivElement>(null);+ const flyRef = useRef<HTMLDivElement>(null);+ // Hidden until measured, so the flip/shift is the first thing painted (no flash).+ const [flyStyle, setFlyStyle] = useState<CSSProperties>({ position: 'absolute', left: '100%', top: 0, visibility: 'hidden' });++ // Keep the flyout inside the Electron window: open left when it would overflow the+ // right edge, and shift up when it would run past the bottom (measured before paint).+ useLayoutEffect(() => {+ if (!open) return;+ const row = rowRef.current;+ const fly = flyRef.current;+ if (!row || !fly) return;+ const r = row.getBoundingClientRect();+ const f = fly.getBoundingClientRect();+ setFlyStyle(submenuPlacement(r, f, window.innerWidth, window.innerHeight));+ }, [open]);if ('sep' in item) return <div className={cls(variant, 'ctx-sep', 'menu-dropdown__sep')} />;@@ -41,6 +58,7 @@ function MenuNode({ item, variant, close }: { item: MenuItem; variant: Variant;if ('submenu' in item) {return (<div+ ref={rowRef}style={{ position: 'relative' }}onMouseEnter={() => setOpen(true)}onMouseLeave={() => setOpen(false)}@@ -52,9 +70,10 @@ function MenuNode({ item, variant, close }: { item: MenuItem; variant: Variant;</button>{open ? (<div+ ref={flyRef}className={cls(variant, 'ctx', 'menu-dropdown')}role="menu"- style={{ position: 'absolute', left: '100%', top: 0 }}+ style={flyStyle}onMouseDown={(e) => e.stopPropagation()}>{item.submenu.map((sub, j) => <MenuNode key={j} item={sub} variant={variant} close={close} />)}diff --git a/desktop/src/components/menuPlacement.ts b/desktop/src/components/menuPlacement.tsnew file mode 100644index 000000000..501324dd8--- /dev/null+++ b/desktop/src/components/menuPlacement.ts@@ -0,0 +1,34 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import type { CSSProperties } from 'react';++export interface Box {+ left: number;+ right: number;+ top: number;+ width: number;+ height: number;+}++/**+ * Position a flyout submenu so it stays inside the window: open to the right of+ * its row by default, flip to the left when the right side would overflow (and+ * the left has room), and shift up when it would run past the bottom edge.+ * Pure so the placement is unit-tested without a DOM.+ */+export function submenuPlacement(+ row: Box,+ fly: { width: number; height: number },+ viewportW: number,+ viewportH: number,+ pad = 8,+): CSSProperties {+ const flipLeft = row.right + fly.width > viewportW - pad && row.left - fly.width > pad;+ const overflowY = Math.max(0, row.top + fly.height - (viewportH - pad));+ return {+ position: 'absolute',+ top: overflowY ? -overflowY : 0,+ visibility: 'visible',+ ...(flipLeft ? { right: '100%' } : { left: '100%' }),+ };+}diff --git a/desktop/tests/menu-placement.test.ts b/desktop/tests/menu-placement.test.tsnew file mode 100644index 000000000..94ce9cf95--- /dev/null+++ b/desktop/tests/menu-placement.test.ts@@ -0,0 +1,36 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, it, expect } from 'vitest';+import { submenuPlacement } from '@/components/menuPlacement';++const fly = { width: 180, height: 200 };+const row = (over: Partial<{ left: number; right: number; top: number }>) =>+ ({ left: 100, right: 240, top: 100, width: 140, height: 24, ...over });++describe('submenuPlacement (keeps flyouts inside the window)', () => {+ it('opens to the right when there is room', () => {+ const s = submenuPlacement(row({}), fly, 1200, 800);+ expect(s.left).toBe('100%');+ expect(s.right).toBeUndefined();+ });++ it('flips left when the right edge would overflow', () => {+ const s = submenuPlacement(row({ left: 1000, right: 1150 }), fly, 1200, 800);+ expect(s.right).toBe('100%');+ expect(s.left).toBeUndefined();+ });++ it('stays right when neither side fits (degrades, not flips into worse overflow)', () => {+ const s = submenuPlacement(row({ left: 50, right: 250 }), fly, 300, 800);+ expect(s.left).toBe('100%'); // left has no room (50 - 180 < pad), so keep right+ });++ it('shifts up when it would run past the bottom', () => {+ const s = submenuPlacement(row({ top: 700 }), fly, 1200, 800); // 700 + 200 = 900 > 792+ expect(s.top).toBe(-108);+ });++ it('no vertical shift when it fits', () => {+ expect(submenuPlacement(row({ top: 100 }), fly, 1200, 800).top).toBe(0);+ });+});