Files
feuerwerk/src/editor/graph/NodePalette.ts
T
adminandClaude Opus 5 b9f1a9d319
game-maker / suite (push) Successful in 6m13s
A graph that grew past one screen needs somewhere to say what a part of it is for (#38)
The graph could hold any number of nodes and said nothing about which of them
belonged together. Groups and comments are two separate things, as decided in
the issue: a group is a titled box that carries the nodes it encloses, a
comment is free text that carries nothing.

Both live as optional arrays on Graph, not as registry nodes. A comment that
were a real node would run through the interpreter, show up in the drop
palettes and be compiled into a shader — the wrong place for a piece of text.
As arrays on Graph they exist in all four graph kinds at once, and a document
without them means exactly "no groups", so no format bump.

Membership is computed when the box is grabbed, never stored: the center of a
node inside the box travels with it, and a node dragged out is simply out. A
box inside a box travels with the outer one, its own nodes included. A group
is grabbed by its title bar only — its body stays transparent to the pointer,
or a rubber band could never be drawn across the nodes it holds. Deleting a
box deletes the box; the nodes stay where they are, because a box that
silently took ten nodes with it would be the most expensive surprise in the
feature.

The color is a free picker, and the title color follows from it: relative
luminance per WCAG, threshold at 0.179 where the two contrast ratios meet —
not 0.5, which would pick white on mid grey where black reads better.

Resizing snaps the far edge to the grid, not the width, so the corner that is
not being dragged stays put. The minimum size rises in whole steps for that
reason: 80 is no multiple of 24, so a box squeezed to the stop would have sat
off-grid in exactly the mode meant to keep it on.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 11:24:34 +02:00

147 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { NodeDef } from '../../core/graph';
import { ListView, Popup, ScrollContainer, TextField, Theme, ui } from '../../ui';
import { type PaletteEntry, rankedEntries } from './paletteEntries';
let current: NodePalette | null = null;
function setCurrent(palette: NodePalette): void {
current = palette;
}
/** The palette standing open, for the test hook. */
export function currentNodePalette(): NodePalette | null {
return current;
}
/** Below this the list would jump in height with every keystroke while typing a query. */
const MIN_LIST_HEIGHT = 260;
/**
* Something the palette offers that is not a node: adding a group or a comment. They belong here
* because "right-click where it should go" is the one gesture the graph has for putting something
* on the canvas – a second gesture for two entries would be a second thing to know.
*/
export interface PaletteAction {
id: string;
label: string;
hint?: string;
/**
* What the search matches instead of the label. The label starts with "Add", and a query of
* "add" is one somebody types to find the `Add` node – two entries jumping the list for it
* would cost more than they help.
*/
match: string;
onSelect: () => void;
}
/** Searchable list of node types; right-click menu of the graph editor. */
export class NodePalette extends Popup {
private readonly search: TextField;
private readonly list = new ListView();
private readonly scroll = new ScrollContainer();
private filtered: PaletteEntry[] = [];
/** Guards against a second pick per opening (e.g. Enter and a row click racing). */
private picked = false;
/**
* @param accepts restricts the list, e.g. to the node types an edge dropped into empty space
* could actually connect to.
*/
constructor(
private readonly entries: PaletteEntry[],
private readonly onPick: (entry: PaletteEntry) => void,
private readonly accepts: (def: NodeDef) => boolean = () => true,
/** Listed above the nodes. An edge dropped into empty space passes none: it wants a node. */
private readonly actions: readonly PaletteAction[] = [],
) {
super({ padding: 4, gap: 4 });
this.minWidth = 240;
this.search = new TextField('', {
placeholder: 'Search nodes…',
onInput: () => this.refresh(),
// Enter only. NOT onChange: that also fires when the search field loses focus because
// the user clicked a list row, which would add the first match *and* the clicked one.
onSubmit: () => {
const action = this.matchingActions()[0];
if (action) {
this.run(() => action.onSelect());
return;
}
const first = this.filtered[0];
if (first) this.pick(first);
},
});
this.content.add(this.search);
this.scroll.minHeight = MIN_LIST_HEIGHT;
this.scroll.setContent(this.list);
this.content.add(this.scroll, 1);
this.list.rowHeight = Theme.rowHeight - 2;
this.list.emptyText = 'No matching node';
this.list.onSelect = (id) => {
const action = this.matchingActions().find((a) => a.id === id);
if (action) {
this.run(() => action.onSelect());
return;
}
const entry = this.filtered.find((e) => e.id === id);
if (entry) this.pick(entry);
};
this.refresh();
}
override open(globalX: number, globalY: number): void {
super.open(globalX, globalY);
setCurrent(this);
this.picked = false;
ui().focus.focus(this.search);
}
override close(): void {
if (current === this) current = null;
super.close();
}
/** What the palette offers, in the order it is listed – actions first, then the best node (test hook). */
rowsDrawn(): { label: string; type: string }[] {
return [
...this.matchingActions().map((a) => ({ label: a.label, type: a.id })),
...this.filtered.map((entry) => ({ label: entry.def.label, type: entry.def.type })),
];
}
/** Two entries need no ranking, only a yes or no. */
private matchingActions(): readonly PaletteAction[] {
const query = this.search.value.trim().toLowerCase();
return this.actions.filter((a) => a.match.toLowerCase().includes(query));
}
private refresh(): void {
this.filtered = rankedEntries(
this.entries.filter((e) => this.accepts(e.def)),
this.search.value,
);
this.list.setItems([
...this.matchingActions().map((a) => ({ id: a.id, label: a.label, hint: a.hint })),
...this.filtered.map((e) => ({ id: e.id, label: e.def.label, hint: e.hint })),
]);
this.scroll.minHeight = Math.min(
Math.max(this.list.measure().h, MIN_LIST_HEIGHT),
this.roomFor(this.scroll),
);
this.refreshLayout();
this.scroll.relayoutContent();
}
private pick(entry: PaletteEntry): void {
this.run(() => this.onPick(entry));
}
/** One pick per opening, whatever was picked: Enter and a row click can race. */
private run(chosen: () => void): void {
if (this.picked) return;
this.picked = true;
this.close();
chosen();
}
}