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
refactor(editor): extract a single shared Menu component (dropdowns + context menus)
esengine57dd2f2582c0
diff --git a/desktop/src/components/Menu.tsx b/desktop/src/components/Menu.tsxnew file mode 100644index 000000000..7eb62286f--- /dev/null+++ b/desktop/src/components/Menu.tsx@@ -0,0 +1,92 @@+/**+ * @file Menu.tsx+ * @brief Shared menu primitives — one implementation behind the menu-bar+ * dropdowns and every right-click context menu, so item rendering+ * (labels, shortcuts, checkmarks, disabled state, separators) and the+ * context-menu dismiss behaviour live in a single place.+ */+import { useEffect } from 'react';+import { createPortal } from 'react-dom';+import { Check } from 'lucide-react';++export type MenuItem =+ | { sep: true }+ | { label: string; shortcut?: string; onClick: () => void; disabled?: boolean; checked?: boolean };++/**+ * The contents of a menu — separators and item buttons. Renders inside a+ * positioned `.menu-dropdown` container (the menu-bar provides one; ContextMenu+ * provides its own). Closes the menu via `onSelect` before running the action.+ */+export function MenuItems({ items, onSelect }: { items: MenuItem[]; onSelect: () => void }) {+ return (+ <>+ {items.map((it, i) =>+ 'sep' in it ? (+ <div key={i} className="menu-dropdown__sep" />+ ) : (+ <button+ key={i}+ type="button"+ role="menuitem"+ className="menu-dropdown__item"+ disabled={it.disabled}+ onClick={() => {+ onSelect();+ it.onClick();+ }}+ >+ <span className="menu-dropdown__check">+ {it.checked ? <Check size={13} strokeWidth={2.2} /> : null}+ </span>+ <span className="menu-dropdown__label">{it.label}</span>+ {it.shortcut ? <span className="menu-dropdown__shortcut">{it.shortcut}</span> : null}+ </button>+ ),+ )}+ </>+ );+}++/**+ * A right-click context menu: portaled to the document body, positioned at the+ * cursor, dismissed on an outside press, a scroll, or Escape.+ */+export function ContextMenu({+ x,+ y,+ items,+ onClose,+}: {+ x: number;+ y: number;+ items: MenuItem[];+ onClose: () => void;+}) {+ useEffect(() => {+ const close = () => onClose();+ const onKey = (e: KeyboardEvent) => {+ if (e.key === 'Escape') onClose();+ };+ window.addEventListener('mousedown', close);+ window.addEventListener('scroll', close, true);+ window.addEventListener('keydown', onKey);+ return () => {+ window.removeEventListener('mousedown', close);+ window.removeEventListener('scroll', close, true);+ window.removeEventListener('keydown', onKey);+ };+ }, [onClose]);++ return createPortal(+ <div+ className="menu-dropdown menu-dropdown--ctx"+ role="menu"+ style={{ left: x, top: y }}+ onMouseDown={(e) => e.stopPropagation()}+ >+ <MenuItems items={items} onSelect={onClose} />+ </div>,+ document.body,+ );+}diff --git a/desktop/src/layout/MenuBar.tsx b/desktop/src/layout/MenuBar.tsxindex 5bde8a6bd..221dfe3d2 100644--- a/desktop/src/layout/MenuBar.tsx+++ b/desktop/src/layout/MenuBar.tsx@@ -3,18 +3,14 @@// The window is frameless (titleBarStyle: hiddenInset) so this strip doubles as// the drag region; the menus + dropdowns opt out of dragging.import { useEffect, useRef, useState, useSyncExternalStore } from 'react';-import { Check } from 'lucide-react';import { useEditorStore } from '@/store/editorStore';import { EditorHistory } from '@/engine/EditorHistory';import { SceneCommands } from '@/engine/SceneCommands';import { ProjectStore } from '@/project/ProjectStore';+import { MenuItems, type MenuItem } from '@/components/Menu';const LAYOUT_KEY = 'estella.editor.layout.v1';-type MenuItem =- | { sep: true }- | { label: string; shortcut?: string; onClick: () => void; disabled?: boolean; checked?: boolean };-interface MenuDef {title: string;items: MenuItem[];@@ -63,12 +59,6 @@ export function MenuBar() {};}, [open]);- // Run an item's action, then close the menu.- const run = (fn: () => void) => () => {- setOpen(null);- fn();- };-const save = () => void ProjectStore.save().catch(() => ProjectStore.saveAsViaDialog());const duplicate = () => {if (selectedId == null) return;@@ -170,26 +160,7 @@ export function MenuBar() {</button>{open === m.title && (<div className="menu-dropdown" role="menu">- {m.items.map((it, i) =>- 'sep' in it ? (- <div key={i} className="menu-dropdown__sep" />- ) : (- <button- key={i}- type="button"- role="menuitem"- className="menu-dropdown__item"- disabled={it.disabled}- onClick={run(it.onClick)}- >- <span className="menu-dropdown__check">- {it.checked ? <Check size={13} strokeWidth={2.2} /> : null}- </span>- <span className="menu-dropdown__label">{it.label}</span>- {it.shortcut ? <span className="menu-dropdown__shortcut">{it.shortcut}</span> : null}- </button>- ),- )}+ <MenuItems items={m.items} onSelect={() => setOpen(null)} /></div>)}</div>diff --git a/desktop/src/panels/Outliner.tsx b/desktop/src/panels/Outliner.tsxindex 8f6c789aa..02acdc8f8 100644--- a/desktop/src/panels/Outliner.tsx+++ b/desktop/src/panels/Outliner.tsx@@ -1,5 +1,4 @@import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';-import { createPortal } from 'react-dom';import { ChevronRight, Eye, EyeOff, Lock, Search, Plus } from 'lucide-react';import { useEditorStore } from '@/store/editorStore';import { EngineHost } from '@/engine/EngineHost';@@ -7,6 +6,7 @@ import { SceneStore } from '@/engine/SceneStore';import { SceneQuery } from '@/engine/SceneQuery';import { SceneCommands } from '@/engine/SceneCommands';import { NodeIcon } from '@/components/icons';+import { ContextMenu, type MenuItem } from '@/components/Menu';import type { SceneNode, EntityId } from '@/types';interface RowProps {@@ -111,8 +111,6 @@ function filterNode(node: SceneNode, q: string): SceneNode | null {return kids.length ? { ...node, children: kids } : null;}-type CtxItem = { sep: true } | { label: string; shortcut?: string; onClick: () => void };-export function Outliner() {const engine = useSyncExternalStore(EngineHost.subscribe, EngineHost.getSnapshot);// Rebuild the tree only when the engine pushes a structural change.@@ -153,21 +151,6 @@ export function Outliner() {}}, [tree]);- // Dismiss the context menu on an outside press, scroll, or Escape.- useEffect(() => {- if (!ctx) return;- const close = () => setCtx(null);- const onKey = (e: KeyboardEvent) => e.key === 'Escape' && setCtx(null);- window.addEventListener('mousedown', close);- window.addEventListener('scroll', close, true);- window.addEventListener('keydown', onKey);- return () => {- window.removeEventListener('mousedown', close);- window.removeEventListener('scroll', close, true);- window.removeEventListener('keydown', onKey);- };- }, [ctx]);-const select = (id: EntityId | null) => useEditorStore.getState().select(id);const onContextMenu = (e: React.MouseEvent, id: EntityId) => {@@ -185,7 +168,7 @@ export function Outliner() {if (id != null) select(id);};- const ctxItems: CtxItem[] = ctx+ const ctxItems: MenuItem[] = ctx? [{ label: 'Rename', shortcut: 'F2', onClick: () => setRenaming(ctx.id) },{@@ -253,37 +236,7 @@ export function Outliner() {)}</div>- {ctx &&- createPortal(- <div- className="menu-dropdown menu-dropdown--ctx"- role="menu"- style={{ left: ctx.x, top: ctx.y }}- onMouseDown={(e) => e.stopPropagation()}- >- {ctxItems.map((it, i) =>- 'sep' in it ? (- <div key={i} className="menu-dropdown__sep" />- ) : (- <button- key={i}- type="button"- role="menuitem"- className="menu-dropdown__item"- onClick={() => {- setCtx(null);- it.onClick();- }}- >- <span className="menu-dropdown__check" />- <span className="menu-dropdown__label">{it.label}</span>- {it.shortcut ? <span className="menu-dropdown__shortcut">{it.shortcut}</span> : null}- </button>- ),- )}- </div>,- document.body,- )}+ {ctx && <ContextMenu x={ctx.x} y={ctx.y} items={ctxItems} onClose={() => setCtx(null)} />}</div>);}