import { Menu, type MenuEntry, ui, type Widget } from '../ui'; import { POINT_KIND_LABELS } from './PointKindButton'; import { POINT_KINDS, type PointKind } from './polygonEdit'; /** What the menu of a point acts on. */ export interface PointMenuTarget { /** False while the point must stay: an end of a curve, one of a polygon's last three corners. */ removable: boolean; remove(): void; /** The point's type and how to change it – only where points carry handles. */ kind?: PointKind | null; setKind?(kind: PointKind): void; } /** The entries of a point's menu (#288): Delete point, and its type where it has one. */ export function pointMenuEntries(target: PointMenuTarget): MenuEntry[] { const entries: MenuEntry[] = [ { label: 'Delete point', danger: true, disabled: !target.removable, onSelect: () => target.remove() }, ]; const setKind = target.setKind?.bind(target); if (!setKind || target.kind == null) return entries; entries.push({ separator: true }); for (const kind of POINT_KINDS) entries.push({ label: POINT_KIND_LABELS[kind], checked: () => target.kind === kind, onSelect: () => setKind(kind), }); return entries; } /** * Opens it at a global pointer position – where the right-click was. A popup takes the keyboard * away; `keys` gets it back when the menu closes, so Delete still goes to the tab that has the * point picked and not on to the selection. */ export function openPointMenu(target: PointMenuTarget, x: number, y: number, keys?: Widget): Menu { const menu = new Menu(pointMenuEntries(target)); if (keys) menu.onClose = () => ui().focus.focus(keys); menu.open(x, y); return menu; }