import { createSignal, For, Match, onCleanup, onMount, Show, Switch, type JSX } from 'solid-js'; import { clamp } from '../model/geometry'; import { EXAMPLES } from '../model/examples'; import { SAMPLES } from '../model/samples'; import { OVERRIDE_LIMITS, PRESETS, resolveAppearance, swatchFor, THEME_IDS, type ThemeId, } from '../model/appearance'; import type { Tool } from '../state/session/editor'; import { commandEnabled, commands, runCommand, type CommandId } from '../state/commands'; import { arrangeOpen, sourceOpen, closeMenu, filesShown, propertiesOpen, menu, objectsOpen, openMenu, type MenuState, } from '../state/ui'; import { tip } from './controls'; import { FileTime } from './FilesPanel'; import { Icon } from './Icon'; import './menus.css'; import { exampleUrl, fileUrl, go, workspace } from '../state/launch'; import { baseName } from './workspace-files'; import { useDiagramSession } from './session-context'; /** The open popover menu, positioned under its anchor. */ export function Menus() { return ( {state => } ); } function Popover(props: { state: MenuState }) { let el!: HTMLDivElement; // The popover is keyed on its state, so the anchor never changes while it is open. // eslint-disable-next-line solid/reactivity const anchor = props.state.anchor; const returnFocus = anchor instanceof HTMLElement ? anchor : null; onMount(() => { const width = el.offsetWidth, height = el.offsetHeight; let left: number, top: number; if (anchor instanceof HTMLElement) { const r = anchor.getBoundingClientRect(); left = anchor.closest('.menubar') ? r.left : r.right - width; top = r.bottom + 4; } else { left = anchor.x; top = anchor.y; } el.style.left = clamp(left, 8, innerWidth - width - 8) + 'px'; top = clamp(top, 8, innerHeight - height - 8); el.style.top = top + 'px'; // A section opened later (the layout test cases) scrolls inside the menu, not off screen. el.style.maxHeight = innerHeight - top - 8 + 'px'; el.querySelector('button,input,select')?.focus({ preventScroll: true }); const outside = (e: PointerEvent) => { const target = e.target as Node; if (!el.contains(target) && !(returnFocus && returnFocus.contains(target))) closeMenu(); }; document.addEventListener('pointerdown', outside, true); onCleanup(() => { document.removeEventListener('pointerdown', outside, true); if (el.contains(document.activeElement)) returnFocus?.focus({ preventScroll: true }); }); }); const onKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeMenu(); return; } const target = e.target as HTMLElement; // Left and right walk the menu bar, as they do in a desktop app. if ((e.key === 'ArrowLeft' || e.key === 'ArrowRight') && anchor instanceof HTMLElement) { const items = [...(anchor.closest('.menubar')?.querySelectorAll('button') ?? [])]; const next = items[ (items.indexOf(anchor as HTMLButtonElement) + (e.key === 'ArrowRight' ? 1 : -1) + items.length) % items.length ]; if (next && items.length) { e.preventDefault(); next.click(); return; } } if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && target.tagName === 'BUTTON') { e.preventDefault(); const items = [...el.querySelectorAll('button:not(:disabled),input,select')], i = items.indexOf(target); items[(i + (e.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length]?.focus(); } }; return ( ); } const MORE_TOOLS: [Tool, string, string][] = [ ['select', 'cursor', 'Select'], ['pan', 'hand', 'Pan'], ['box', 'box', 'Rectangle'], ['diamond', 'diamond', 'Diamond'], ['circle', 'circle', 'Ellipse'], ['text', 'text', 'Text'], ['container', 'group', 'Container'], ]; function ToolsMenu(props: { anchor: MenuState['anchor'] }) { const s = useDiagramSession(); const submenu = (id: MenuState['id']) => () => openMenu(id, props.anchor); return ( <> ); } function MenuItem(props: { command: CommandId; label?: string; note?: string; checked?: boolean }) { const command = () => commands[props.command] as { label: string; keys?: string; icon?: string }; return ( ); } const Heading = (props: { children: JSX.Element }) => ( ); /** Hands over to another menu, anchored where that menu usually opens. */ const openFrom = (id: MenuState['id'], selector: string) => () => { const anchor = document.querySelector(selector); if (anchor) openMenu(id, anchor); }; /** A row that opens a list beside the menu, on hover, click or the right arrow key. */ function Submenu(props: { label: string; icon: string; children: JSX.Element }) { const [rect, setRect] = createSignal(null); let row!: HTMLButtonElement; let timer = 0; const open = () => setRect(row.getBoundingClientRect()); const close = () => { setRect(null); row.focus({ preventScroll: true }); }; onCleanup(() => clearTimeout(timer)); const place = (el: HTMLDivElement, at: DOMRect) => { const width = el.offsetWidth; const right = at.right + 4 + width < innerWidth - 8; el.style.left = `${right ? at.right + 4 : at.left - width - 4}px`; const top = clamp(at.top - 6, 8, innerHeight - el.offsetHeight - 8); el.style.top = `${top}px`; el.style.maxHeight = `${innerHeight - top - 8}px`; el.querySelector('button')?.focus({ preventScroll: true }); }; const onKeyDown = (e: KeyboardEvent) => { const el = e.currentTarget as HTMLElement; e.stopPropagation(); if (e.key === 'Escape' || e.key === 'ArrowLeft') { e.preventDefault(); close(); } else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { e.preventDefault(); const items = [...el.querySelectorAll('button:not(:disabled)')]; const i = items.indexOf(e.target as HTMLElement); items[(i + (e.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length]?.focus(); } }; return (
{ clearTimeout(timer); timer = window.setTimeout(open, 90); }} onPointerLeave={() => { clearTimeout(timer); timer = window.setTimeout(() => setRect(null), 200); }} > {at => (
queueMicrotask(() => place(el, at))} class="popover flyout" role="menu" onKeyDown={onKeyDown} > {props.children}
)}
); } function ExamplesSubmenu() { const s = useDiagramSession(); return ( {example => ( )} ); } /** The newest diagrams in the folder, leaving out the one already open. */ function RecentFiles() { const s = useDiagramSession(); const files = () => [...(workspace()?.files ?? [])] .filter(f => f.path !== s.fileSession()?.file) .sort((a, b) => b.modified - a.modified) .slice(0, 5); return ( Recent in {workspace()?.name} {file => ( )}
); } /** * File shows what works where you are. A served folder creates and moves between its own files, so * it has no file dialog. Without a folder the diagram lives in the browser and comes and goes * through files you pick. */ function FileMenu() { const s = useDiagramSession(); return ( <> } > {info => ( <> )}

); } function EditMenu() { return ( <>


); } function ViewMenu() { return ( <>

); } function LayoutMenu() { const s = useDiagramSession(); return ( <>

); } function HelpMenu() { const s = useDiagramSession(); return ( <>
{sample => ( )} ); } function ExportMenu() { const s = useDiagramSession(); return ( <>
Copy as
); } /** * Diagram theme and diagram-wide padding. Saved with the diagram (in a file, as its `theme` * line); the editor's own light or dark mode is separate. */ function AppearanceMenu() { const s = useDiagramSession(); const look = () => resolveAppearance(s.doc.appearance); const padding = (key: 'paddingX' | 'paddingY', label: string) => ( ); return (
Diagram theme
{id => }

Node padding
{padding('paddingX', 'Horizontal padding')} {padding('paddingY', 'Vertical padding')}
Line corners
{([label, radius]) => ( )}
); } /** One preset with a small drawing in its own colors and title type. */ function ThemeOption(props: { id: ThemeId }) { const s = useDiagramSession(); const preset = () => PRESETS[props.id]; const look = () => resolveAppearance({ theme: props.id, overrides: {}, colors: [] }); const selected = () => s.doc.appearance.theme === props.id; return ( ); }