0521. Frames as screens: draw into, drop into, labels, presets, layers drag and drop
Status: Accepted (2026-11-28, ED1).
Context
Frames are the containers people design screens in. They must behave like Figma’s frames on the canvas and in the layers panel, through the session (multiplayer, per-user undo). They must also stay cheap on the reference scenes: the 4,000-page website has 4,000 top-level frames, and the 5M-shape page has 5M root children.
Decision
- Frame index.
frames.ts keeps the page’s top-level frames as one Int32Array of handles.
- It is rebuilt when the structure changes, at most every 300 ms on pages with more than 200K root children.
- Bounds are read from the doc columns on demand.
frameAt(x, y, skip) returns the deepest visible frame under a point, checking top-level frames top-down and then nested frames; skipped subtrees are left out. Cost: O(top-level frames + children of the hit frames).
- Drawing.
- Shapes, pencil strokes and text are created in the frame under the start point, in that frame’s coordinate space (page numbers on the root, so parity holds).
- The frame tool (F / A) draws white frames with clip content on, named “Frame N”. At the end of the draw the frame adopts the layers of its parent it encloses completely, in the same gesture and undo step. Candidates come from the engine’s rect query, capped at 20K.
- Paste with one page-aligned frame selected pastes into it, at the copied page position (+10 per paste). The exception: when the frame is one of the copied layers or inside them, the paste goes beside it on the page.
- Dropping. A move drag tracks its drop target: the deepest frame under the pointer, outside the selection.
- The overlay outlines the target when the drop would change a parent.
- On release, the dragged top-level layers whose parent changes get
SET {PARENT_INDEX, x, y, rotation} with their page placement kept. The rest get the usual transform ops. Both go in one commit.
- Children of groups keep their group, and a node never moves into its own subtree.
- At most 1,000 roots are reparented; bigger selections only move.
geometry.ts reparentOps / reparentPlacement do the math (rotated parents included). The layers panel uses the same code.
- Selection rules (Figma).
- The name label of a top-level frame selects and drags the frame.
- The background of an unselected top-level frame that has layers starts a marquee instead of selecting the frame.
- A marquee selects a frame only when it encloses it completely, and then not its layers separately. Otherwise it selects the frame’s layers it touches.
- Labels. The overlay draws top-level frame names above their top-left corner, rotated with the frame and blue when selected. Cost: one pass over the index per rendered frame, plus one
fillText per frame that is on screen and wider than 24 px. Label rects are kept for clicks. Name widths are cached by name.
- Resizing.
- Resizing one page-aligned frame by its handles is a live gesture on the frame’s box (Figma: content is not scaled). Up to 2,000 direct children are shifted back so they keep their page position when the left or top edge moves.
- Multi-selections, rotated frames and groups keep the GPU-preview resize, which scales content.
- W/H typed in the panel resize a frame’s box only. Presets (phone, tablet, desktop) resize selected frames, or add a new screen right of the existing ones when the frame tool is active with nothing selected.
- Clip content. The editor writes
CLIP_CONTENT (new frames: on; checkbox in the panel). The engine clips rendering and hit testing (EN2: a scissor for page-aligned frames, a discard variant otherwise). Until that lands, CLIP_CONTENT renders unclipped.
- Layers panel drag and drop.
- Dragging a row moves the selection (top-most nodes) when the row is selected, else that row.
- Drop zones: the upper or lower part of a row places the layers as siblings above or below it. Below an expanded container means its top child. The middle of a frame or group row means inside it, on top of its children.
- One commit of
reparentOps (fractional keys between the target’s neighbours, skipping the dragged nodes; page placement kept). Drops into a dragged node’s own subtree are refused.
- Only the row under the pointer is computed, so the list stays virtualized. The list auto-scrolls near its edges.
- Top-level frames show as screens: bold name, frame icon.
Alternatives
- Scanning the whole page to find frames under the pointer: O(nodes) per pointer move on a 5M-shape page.
- Keeping the GPU-preview resize for single frames: cheap, but it squashes the screen’s content. Figma resizes the box only.
- Reparenting in the layers panel without keeping placement, as a pure tree move: layers would jump across the canvas.
Consequences
- Moving a layer into a frame elsewhere in the paint order changes
PARENT_INDEX. The engine handles that as a tail layout from the earlier position; EN2’s chunk insertion makes it cheap.
- Canvas drops reparent at most 1,000 roots per drag. Layers-panel drops have no cap.
- Frame resize by handles keeps children in place only for page-aligned frames with up to 2,000 direct children.
- Tested:
test/reparent.test.ts (placement under rotated frames, key order, ancestry), plus browser checks for draw-into, adopt, label click, background marquee, drop in and out with undo, presets, clip, the marquee rule, content-preserving resize, and layers inside and reorder drops.