The UI window system
Why immediate-mode: there is no retained widget graph to fall out of sync with your state, no create/destroy/diff to manage. The tree is a pure function of your state; render it, and what you see is exactly what you declared. The cost is that geometry exists only after a render pass — see "The runtime model".
A screen is a widget tree
A node is a table with a type, a props table (its data and behaviour), a
style table (its looks and layout), and children:
ui.registerScreen("hud", {
type = "vertical",
style = { gap = 8, padding = 16, background = "#0e1116" },
children = {
{ type = "label", props = { text = "Health 100" }, style = { fontSize = 28, color = "#e6e6e6" } },
{ type = "button", props = { text = "Inventory", onClick = "open_inv" } },
},
}, 10) -- optional layer: a higher number draws on top
ui.showScreen("hud")
ui.updateScreen("hud", buildTree()) -- swap the whole tree to re-render
ui.hideScreen("hud")
propscarries the widget's data and behaviour: a label'stext, a slider'svalue/min/max, an interactive widget's callback ids (onClick,onChange),visible,wrap.stylecarries everything visual and geometric — the whole CSS surface in the styling guide. A visual key placed inpropsdoes nothing and logs a warning, and anidor a callback placed instyledoes nothing and logs an error; keep the two straight.childrennests the tree;horizontalandverticalare the flex row and column that lay them out.id— a top-level key on any node — names that element. It is the name every addressing surface answers to:ui.getLayoutInfo(id), theguitoolbox's bounds, clickElement, hoverElement, dragElement and captureElement tools, and the node's entry in elementTree. Without one the engine generates<screen>/<type>@<path>, which addresses the node just as well but moves when the tree's shape changes — so name the nodes you intend to drive or capture.
Stacking — what layer decides
layer sorts every screen against every other one, whatever their root widgets
are, and it resolves in bands: below 0 sits behind everything, 0–99 is the
ordinary app depth, 100–999 is always-on-top chrome (a taskbar, a persistent
overlay), and 1000+ is menu and popup depth. A screen in a higher band covers
a screen in a lower one, so a card at layer = 800 composites over a radar
panel at layer = 20 whether either is rooted in a vertical, a docked
topPanel, an area or a window.
Inside one band the root type finishes the order. A floating root — an area, a
window, and the anchored overlays a screen builds — floats over the ordinary
content of its own band, which is what makes a window sit on the page it belongs
to. Ordinary content in one band draws in layer order; floating roots in one
band order by interaction, where clicking one raises it and
ui.bringAreaToFront(id) does the same from code.
A modal root takes the top of the stack and dims what it covers whatever its
screen's layer says — that is what makes it modal.
Which boxes take the pointer
A press goes to the topmost box under it that takes the pointer, across every
shown screen. A widget you can point at — a button, a label, a slider, a text
field, an image — takes it over its own box. A vertical, horizontal,
grid or panel takes it over its own box when it is something to point at:
it fills its box (a visible background, a gradient, a backgroundImage, a
backgroundShader), it answers the pointer (onClick and the other pointer
callbacks, a hover or press style, holdsPointer = true), or it scrolls. A
container that only lays out its children takes none, and neither does a
spacer: the children take the pointer over their boxes, and the space
between them belongs to whatever is beneath, in the same screen or in a screen
on a lower layer. That holds for a container an anchor places as well. So a
HUD, a subtitle strip or a wallet laid out across the whole frame in a
transparent vertical leaves the screens under it clickable everywhere it
paints nothing.
style.pointerEvents states it outright, the CSS way, on a vertical,
horizontal, grid, panel, spacer or canvas (ui.getWidgetProps lists
it for each of them): "none" hands the widget's own box
to what is beneath it even where it fills it (an overlay plate a press should
pass through), and its own pointer callbacks and hover style take no pointer
either; "auto" makes the whole box a target even where it paints nothing (a
click shield over a screen). It applies to the widget's own box; each child
decides for its own. On any other kind the key has no effect, and
ui.lastValidation names it there as pointer-events-not-read.
ui.hitTest(x, y) and the takesPointer field of ui.diagnose report the
same decision the engine acts on.
The pointer: a screen that takes clicks while the game steers with the mouse
A game that steers with the mouse holds a pointer lock, and a locked cursor
points at no widget. A shown screen holding an open modal holds the pointer,
and so does one with a shown widget stating holdsPointer = true, the prop
for a pause menu or an inventory built from containers. While such a screen
is up the lock stands down: the cursor shows, every click lands on the screen,
and mouse look stops turning the camera. Hiding the screen brings the lock back
as the game asked, on the next click on the window. A menu whose visible
follows a value (visible = values.bind(game, "paused")) holds the pointer
while the value shows it, with no updateScreen. A HUD holds nothing unless
one of its widgets says so, and a modal stating holdsPointer = false leaves
the cursor to the game. Zin.pointer.status().uiHolders names the screens
holding the pointer; the input guide's "The cursor" section covers the rest.
A screen holding the pointer is also a menu a pad drives with no code: the
d-pad and left stick move focus to the next row or column of widgets in the
direction pressed, south presses it and east dismisses, and it opens focused on its
first widget in reading order when a pad or keyboard was used last. navUp /
navDown / navLeft / navRight on a widget name where a direction goes
from it, and navGroup = true on a container keeps directional moves inside
it. The input guide's "Menus on a pad" section covers it.
Space — the camera's picture or the window
A screen is part of one of two surfaces. Content UI lives in the camera's picture: it lays out over the target the viewport camera renders into, on a canvas 1280 points wide whatever the target's pixel size (narrower on a phone, where a point covers at least the display's density), and is drawn into that target — so in the editor it sits inside the Scene panel, scales with it, and is in every capture of the camera, the way a player sees it. The editor's own chrome lives on the window: it lays out over the whole surface whatever presents the scene. Where the scene presents on the window itself — play mode full screen, a runtime build — the two coincide.
The space follows the render layer: a screen on the EditorUI layer is chrome
and lives on the window, any other screen is content and lives in the camera's
picture. ui.setScreenSpace(name, "window") moves a content screen onto the
window — an overlay that has to cover the editor too — and "camera" puts one
back. ui.getLayoutInfo answers in window points either way, so a click aimed
by a rect lands wherever the screen is drawn, and a text field in the picture
takes the keys while it holds the caret.
Do not memorise the widget set — query it. The gui toolbox's
widgetTypes tool lists every type, and widgetProps lists one type's
props with descriptions. There
are containers (vertical, horizontal, grid, scrollArea, the screen-edge
panels), text and media leaves (label, svg, image), interactive widgets
(button, slider, checkbox, input), and windowing types (area, modal,
popup). Read a working screen from the public UI Style Gallery world
before building from nothing: ten complete CSS-parity screens in ten styles (a
game title menu, a sci-fi HUD, an RPG inventory, neubrutalism, neumorphism,
glassmorphism, a dashboard and more), each showing the real structure and style
in use, a gallery scene that shows them all, and four app templates. Find it
with ZeroMind search (UI Style Gallery), read a screen's source in the shell
with zm show @<guid> (the guid of the screen's module, from the search hit;
zm ls-tree @<guid> lists its files), or install one into your world with the
zeromind toolbox's installAsset and edit it there.
A long list goes in a virtual scroll area. Put the rows straight under a
scrollArea with virtual = true and give each row an id: only the rows in
view are laid out and painted, and the rest are placed at the heights they
took, so a log, a feed or a leaderboard costs the frame what its visible rows
cost rather than what all of them cost. Rows may differ in height; a
rowHeight says they don't.
A widget that follows a value, with no updateScreen
A gauge, a speedometer, an ammo count or a warning light shows a value that
changes every frame. Put values.bind(entity, name, opts) where the widget
takes text, title, header, placeholder, value, min, max,
visible, enabled or checked, and the widget reads the entity's named
value (the topics/values guide) every frame; the screen is registered once
and never updated for it.
ui.registerScreen("dash", { type = "vertical", children = {
{ type = "label", props = { text = values.bind(car, "rpm", { format = "%d rpm" }) } },
{ type = "slider", props = { min = 0, max = 1, value = values.bind(car, "throttle") } },
{ type = "label", props = { text = "BRAKE", visible = values.bind(car, "brake") } },
} })
format is a string.format pattern with one conversion; scale and
offset map a number first (value * scale + offset); fallback is shown
while the entity holds no such value.
Interactivity — a callback needs an owner
An interactive widget carries a string callback id, and onClick works on
any widget, not just a button — put onClick = "id" on an image (a
clickable icon), a vertical / horizontal (a whole clickable row or card), or
a label (a menu item). The engine senses the click over that widget's box; you
never overlay a transparent button.
The id is always a string. A widget tree is data the engine copies, so it
never calls a Lua function placed in it: onClick = function() ... end, an
events = { onClick = fn } table and an onClick (or id) written inside
style are each reported as an error when the screen is registered, naming the
widget and the form that works (codes callback-not-a-string,
value-not-read, widget-key-in-style, logged and kept in
ui.lastValidation(screen)). The screen still registers and draws, and what
the error names is never called or read.
onDrag = "id" works the same way on any widget: a press-drag over its box
calls back every frame with { dx, dy, vx, vy, dragStarted, dragStopped, shift, ctrl, alt }. dx, dy are this frame's movement in points; vx, vy are the
pointer's speed in points per second, and on the frame dragStopped is true
they are the release velocity, which ui.fling(id, { x = vx, y = vy }) throws
the widget with (a sheet dragged and flicked away, a card thrown). Moving the
widget under the finger is ui.setMotion(id, { x, y }); the styling guide's
"Motion the engine steps" section has the whole surface. The engine steps that
motion in edit mode and in play alike.
When the user acts, the engine delivers that id to a top-level function named
onCallback(callbackId, data) — and where that function lives is the part
that trips people up. Callbacks reach a persistent owner:
- a component:
onCallbacktop-level in the component module, besideawake/update, or a local handler its own code hands toui.registerCallbackEnv(key, { onCallback = handler }); - a scene entrypoint:
onCallbacktop-level in the scene script; or - any other code (a module, a tool, a library that puts a screen up)
through
ui.registerCallbackEnv(key, env):env.onCallbackreceives every callback no component instance owns, andui.unregisterCallbackEnv(key)releases it.
-- a module that owns its own screen
local Menu = {}
function Menu.start()
ui.registerScreen("menu", { type = "vertical", children = {
{ type = "button", props = { text = "Play", onClick = "menu_play" } },
} })
ui.registerCallbackEnv("menu", { onCallback = function(id, data)
if id == "menu_play" then Menu.play() end
end })
end
function Menu.stop()
ui.unregisterCallbackEnv("menu")
ui.unregisterScreen("menu")
end
A bare execute/REPL call that registers a screen and nothing else has no
owner, so its clicks go nowhere. This is the rule to internalise: a
standalone interactive UI is authored as a component (or a scene entrypoint,
or code that registers a callback env), never as a loose registerScreen from
the REPL. Register the screens in the component's awake/update and handle
every id in its onCallback.
(ui.click(callbackId) and the gui toolbox's click tool fire one by hand
for testing, by the callback id the widget emits (its onClick / onChange);
the gui toolbox's press tool presses a widget by its element id.)
local function buildTree() ... end -- tree = f(state)
function awake() ui.registerScreen("app", buildTree()) end
function update(dt) ui.updateScreen("app", buildTree()) end -- on state change
function editorUpdate(dt) if engine.paused then update(dt) end end -- tick while the clock is held
function onCallback(id, data) -- every onClick/onChange lands here
if id == "open_inv" then state.invOpen = true end
end
A screen belongs to the component instance whose own code registers it.
"Own code" is the instance's hooks and methods and every closure they create,
such as a listener its awake subscribes to a module or a task it spawns. That
instance's handlers receive the screen's callbacks: its top-level onCallback
and the onCallback of every env its own code registered with
ui.registerCallbackEnv, each handler once per callback. When it holds
neither, a callback on the screen runs nothing: ui.click answers false and
the log warns, naming the callback and the instance. The engine
unregisters the screen when the instance ends: its entity despawns, the
component is removed, or the scene is cleared. The log says so at info level,
naming the screens and the instance. A screen registered by any other code (a
scene entrypoint, a module function, a listener a module or an entrypoint
subscribed) belongs to no instance and stays until something unregisters it.
That holds even when a component's hook is what ran that code: a coin's
update that calls score.add(1) runs the score module's listeners, and the
HUD one of them registers is the module's, so the HUD stays when the coin
despawns. The same rule covers a helper module a component calls to put its
screen up: the helper's ui.registerScreen is the module's, so for the
component to own that screen, have the helper return the tree and call
ui.registerScreen in the component. A UiPanel whose source names a
library does this for the screen the library's mount registers, so that
screen is the panel's. The engine's own library (every @builtin::modules.*
module, the ui namespace itself included) acts for the code that calls it: a
screen one of its functions registers belongs to the component instance whose
own code made that call, as if that code had called ui.registerScreen
itself. Registering a screen that an instance
already owns from such code keeps that owner. /runtime/ui/<name>/meta.json
names a screen's owner (the instance id, or null). A screen unregistered
because its owner ended reads back exactly as one ui.unregisterScreen
removed: ui.listScreens() leaves it out, its meta.json is gone and
ui.getScreenTree returns nil. To give a component's screen a longer life,
register it from code that outlives the component; to end an unowned screen,
call ui.unregisterScreen (from the entrypoint's onUnload, for example).
Interaction — like all gameplay — runs in play, not edit. Wire a handler,
then enter play (the wld toolbox's play tool) and actually use it: a
handler that is never invoked looks identical to a correct one until you drive
it. Hover and press are styled without a callback (see interaction states in the
styling guide); the gui toolbox's state tool reads a node's live
hover/press/focus snapshot.
Building a full-screen app
Anything that owns the viewport — a HUD, a menu, a dashboard — is a
component: its lifecycle functions are top-level (no self), and
module-level locals hold its state. Two presentation facts that are discoverable
but not obvious:
- Give the root an opaque background. Containers are transparent by default
(like a CSS element with no
background), so an unpainted root shows the scene through it. Set an opaquebackgroundon the root, or use acentralPanelroot, which fills the viewport opaquely. - Drive live content from
update/editorUpdate. A clock or a data readout recomputes its label and callsupdateScreen. Put per-frame content in its own small screen and refresh only that — re-registering a whole screen every frame (especially a window whose position the user can drag) is wasted work and easy to get subtly wrong.
Windows — draggable, resizable areas
A floating window is its own screen whose root is an area. The area's
keys — id, pos, movable, resizable, pivot, interactable — sit at the
top level of the node, not inside props. Give it an explicit id:
ui.getAreaPos(id) / ui.setAreaPos(id, x, y) and ui.getAreaSize(id) /
ui.setAreaSize(id, w, h) / ui.resetAreaSize(id) key off it, and without one a
dragged position or size is never recorded.
The declared pos/width/height are re-seat controls: changing the value
moves or resizes the window there, while re-registering with the same value
leaves a window the user dragged or resized exactly where they left it — so a
normal per-frame re-render never yanks a window around. A docked bottomPanel /
topPanel (a taskbar) always draws under a floating window; a maximised window
fills the work area (screen minus the taskbar), so it never fights the
taskbar's z-order.
The screen-edge panels (topPanel, bottomPanel, leftPanel, rightPanel,
centralPanel) are root-only: the renderer docks them from the screen root,
so each must be a screen's root node — nested inside a vertical/horizontal
it renders as [<kind> '<id>' must be screen root, not nested]. To build a
toolbar-body-status shell, use a
full-height vertical of styled rows (a header row, a flex = 1 body, a footer
row) rather than nested panels; reserve the edge panels for their own taskbar
screens.
Drag and drop
Any widget can be a drag source and any widget a drop target — the props sit in
props, next to onClick:
-
dragPayload(a string) makes the widget draggable, carrying that string. Encode what the drop needs in it ("item|sword_01"). -
dragLabel(a string) is what the ghost under the pointer reads while the widget is dragged — the item's name, "3 items". A source without one shows its owntext, and a source with neither shows a translucent outline of itself. -
dragGhost = falseturns that ghost off, for a source that draws its own feedback — an icon, a preview of the item — fromui.dragState(). -
onDrop(a callback id) makes the widget a target: while a payload hovers it the widget outlines, and on releaseonCallback(id, data)fires withdata.value = { payload, u, v }— the payload string and the release point normalised to the target's rect (0..1), so a grid turns it into a cell and a list into an index. The innermost target under the pointer receives the drop. To test a handler without a pointer,ui.click(id, { payload = "..." })delivers the samedata.value. -
On a touch screen, a drag source inside a scroll area that pans on drag (any scroll area on a touch display, and one with
panOnDrag) shares the finger with it: a finger moved along the area's axis scrolls the area, the way a list on a phone scrolls wherever the finger lands, and a drag across that axis, or one the finger rests 0.4 s on the source before making, carries the payload. AnonDragwidget there shares it the same way, and so does aslider: a finger moved along the area scrolls it and leaves the value, while a drag along the track, a drag after the rest and a tap set it. A mouse drag is always the widget's. -
keepsTouchDrag = truemakes every finger drag that starts on the widget its own, whatever its direction, from the press on: the scroll area around it takes none of it, and the finger needs no rest first. It is the engine'stouch-action: none, for a widget that needs every direction of a drag inside a scrolling panel: a joystick, an orbit or map view, a drawing pad, a scrub or a splitter built ononDrag, a hand of cards built ondragPayload, aslider. Set it on the widget that carries the drag prop.{ type = "vertical", id = "stick", style = { width = 160, height = 160 }, props = { onDrag = "stickMoved", keepsTouchDrag = true } } -
ui.dragState()is{ payload, x, y }while a drag is in flight, nil otherwise. Poll it to draw feedback of your own: a slot highlight, a placement preview in the world, or a custom ghost (withdragGhost = falseon the source so the engine's ghost does not draw beside it). Itsx, yare window points, the spaceui.getLayoutInfoanswers in. To place a ghost inside your screen, readui.pointerWidget(): itsx, yare the pointer in the canvas your screen lays out in, which is what anarea'spostakes — the two spaces differ wherever the scene is drawn inside a panel, as in the editor. -
A finger that rests on a drag source or an
onDragwidget, then moves, drags it however long it rested. Past a long press (0.8 s) the drag starts as the finger moves on, the way a touch platform carries an item a long press picked up. A widget that also carriesonContextMenuanswers the hold with its menu instead, and takes no drag from that gesture.
Files from outside the engine
A file dragged from the desktop and an image on the clipboard reach a widget the
same way: as a list of files, each { name, type, size, bytes }, where type is
its media type and bytes a binary-safe string (vfs.write-ready). So one
handler takes both. It works in a native window and in the browser.
onFileDrop(a callback id) makes the widget a target for files dropped from outside the engine:data.valueis the list. The innermost target under the pointer receives the drop, and a drop on no target changes nothing.onFileHoverreports the drag standing over the widget:data.valueis{ hovering = true, count, u, v }as it enters and moves (countfiles, atu, vin 0..1 of the widget's box) and{ hovering = false }as it leaves or lands. While one stands over a target the widget outlines, the way anonDroptarget does; use this callback to show more than that.onFilePastereceives pasted files: Ctrl+V (Cmd+V on macOS) with an image on the clipboard, a screenshot for one, reaches the widget holding the keyboard focus in the list above, aspasted-image.pngon a native engine. A container that declares it receives the paste while it is under the pointer and nothing holds the focus. A clipboard holding text pastes the text into the field as usual.- A file past
ui.maxFileBytes()or one that cannot be read keeps its place in the list witherror("tooLarge"or"unreadable") and nobytes, so a handler can say which file it was, and the other files of the drop arrive as usual.
local function take(files)
for _, file in files do
if file.error ~= nil then
log.warn(file.name .. " was not taken: " .. file.error)
else
vfs.write("/source/uploads/" .. file.name, file.bytes)
end
end
end
edui.vbox{ key = "tray", onFileDrop = take, onFilePaste = take,
onFileHover = function(drag) hovering = drag.hovering end, children = { ... } }
To test a handler without a drag or a clipboard, Zin.test.dragFiles(x, y, count),
Zin.test.dropFiles(x, y, files) and Zin.test.pasteFiles(files) run the same
path a person's drop and paste take. Their x, y are window pixels: multiply
a ui.getLayoutInfo rect by ui.pixelRatio(). On a native engine,
Zin.test.setClipboard({ image = png }) states what the clipboard holds, and
Ctrl+V pressed through Zin.test.pressKey / tapKey with target "ui" then
reads it the way it reads a person's.
A widget can carry both, which is how a list reorders: each row is a source and a target at once.
The ghost is drawn at the live pointer, on the window. A capture re-renders the
frame without the pointer, so it never shows a ghost; a drag is checked by
reading ui.dragState() while it is held — its label is the text the ghost
reads and ghost whether one draws.
Two UIs: the game's and the editor's
Every screen draws in one of two spaces:
"camera"— the game's UI. It draws in the game view's own picture, through the camera: a HUD, a menu, a chat box. In the editor it sits inside the Scene panel; in a build with no editor it is the whole window. A screen fromui.registerScreenorgui.panelis here unless it says otherwise."window"— the editor's UI. It draws in the window around the game view: the dock and its panels (edui), the top bar, an agent's terminal. An editor panel is aneditorPanelasset (guides { path = "types/editorPanel" }).
ui.listScreens() (and gui.screens) reports each screen's space, and
ui.setScreenSpace(name, space) places one. The two carry separate focus: a
field in the game's UI and one in the editor's each hold the keyboard for
their own UI. ui.focusedWidget() returns two values, the focused widget's
id (a string, or nil) and its space (local id, space = ui.focusedWidget()),
ui.blur() reaches both, and Zin.state.uiWantsKeyboard("camera") asks
whether the game's own UI has the keys — the question a game asks before
treating a keystroke as a command. A key typed into either UI never reaches
the scene's controls.
ui.focusedTextField() names the text field that holds the keys, wherever it
is drawn: in the window, in the game view, or on a panel standing in a
headset's world (place and panel), with what it holds, its caret, the
keyboard it takes (kind, multiline) and its box on the picture it is drawn
in (bounds). ui.typeText(text) and ui.typeKey(key, modifiers?) type into
that field the way a keyboard does, and reach the UI alone. They are what an
on-screen keyboard is built from: the engine's keyboard in a headset
(xr_keyboard) follows ui.focusedTextField() to stand below a field on a
panel and types into it with them.
Element ids are shared by both spaces, so give each element an id no other
screen uses. Two screens recording the same id leave ui.getLayoutInfo
answering for whichever painted last: the engine logs it,
ui.idCollisions() lists every shared id with its screens, and the gui
element tools refuse a shared id rather than act on the wrong element.
The runtime model — what to know before you are surprised
-
A screen lays out on its surface's canvas:
ui.screenSize(). Every geometric value a screen states (an area'spos, a widget'swidth/height, anchors) is in logical points on the canvas of the surface it is part of:ui.screenSize()for a camera-space screen (a HUD, a game menu: the default for content UI) andui.screenSize("window")for a window screen (the editor's chrome). The two are one canvas in a running game, and the camera's is fitted to the Scene panel in the editor.ui.getLayoutInforects are recorded in the window's points. Never measure off a capture image: a capture is a downscaled copy of the canvas, so a position read from the screenshot's pixels is wrong by the downscale factor. To place things, compute againstui.screenSize(); to check where something landed, readui.getLayoutInfo(id)or theguitoolbox's bounds and elementTree tools. -
Screens lay out inside the safe area:
ui.safeArea(). On a phone the status bar, the navigation bar or gesture handle, a camera cutout and the on-screen keyboard cover parts of the screen.ui.safeArea()reports what they leave of the camera's canvas, inui.screenSize()space, whether or not a camera-space screen is shown (ui.safeArea("window")is the window's, besideui.screenSize("window")):{ top, right, bottom, left }(how far in from each edge they reach),{ x, y, width, height }(the rect they leave),cutouts(each camera hole or notch as a rect) andrevision(advances on every change: a rotation, the bars shown or hidden, the keyboard opening). A screen's root, its anchors, its top-level%sizes and a window's default position all resolve inside that rect, so a HUD anchored to a corner keeps clear of a cutout with no extra work. An area'sposis still screen space: offset it by the rect'sx/yto keep it inside. A child placed withposition = "absolute"andleft/topis measured from its parent instead, and a root fills the safe rect, so a child of the root atleft = 10, top = 10already sits 10 points inside the rect's corner: add nox/yto it. The area is all zero on a desktop window;ui.simulateSafeArea({ top = 40, bottom = 30, cutouts = { ... } })lays the game's UI out as a phone would on any engine, andui.simulateSafeArea()returns to the platform's own. The simulated area covers the window and, in the editor, the picture the Scene panel presents too, so a HUD in the panel lays out inside it in edit and in play. -
A screen hears its canvas change:
ui.onCanvasChange. The canvas changes while the engine runs: a window is resized, a phone turns, a Scene panel is dragged to a new size, a preview size is set, the keyboard or the system bars come and go. Anchors,%sizes and flex follow it on their own; a layout computed in Luau from the numbers (a HUD that stacks down the side in portrait and runs along the bottom in landscape, boxes placed bypos, a grid sized to the width) is computed again when it changes.ui.canvas(space?)reads everything such a layout needs at once:{ space, width, height, aspect, orientation, pixelRatio, safeArea, revision }, withorientationthe canvas's own shape ("portrait"while taller than wide) andrevisionadvancing on every change of any of them.ui.onCanvasChange(function(canvas, previous) ... end, space?)calls back once for each change, on the frame after it, in every mode (the editor and a paused game included), and returns the function that removes the listener; one a component registers ends with the component. Lay the screen out once fromui.canvas(), then again in the callback:local function layout(c) local tall = c.orientation == "portrait" ui.updateScreen("hud", buildHud(c.safeArea, tall)) end layout(ui.canvas()) local off = ui.onCanvasChange(layout)"camera"(the default) is the picture a HUD lays out on,"window"the window the editor's chrome lays out on. -
See a phone's screen in the editor:
viewport.setSize. With a Scene panel up,tools.use("viewport", "setSize", 390, 844)(orrenderer.setPictureSize) draws the game's picture at an upright phone's 390x844 pixels for the rest of the call: the panel shows it at that shape, scaled to fit, and the game's UI lays out over it the way a 390x844 window does, soui.screenSize(),ui.safeArea(), the touch controls and a capture in the same call all read that phone's screen. Addui.simulateSafeAreafor its notch and bars andui.simulateDisplayScalefor its density, and every screen relays out at the new canvas. Where the scene fills the window (a runtime window, a headless engine) the same call resizes the window itself. -
The on-screen controls are in the safe area reading too:
ui.safeArea().controls. On a touch screen the engine draws its own controls over the picture (the action buttons in the bottom-right, the round Menu button in the top-right).controlslists the rect each covers as{ x, y, width, height, owner }, in the sameui.screenSize()space, andrevisionadvances when one is shown, hidden or moved (a rotation, a scheme adding a button, the player switching to a pad). A box ananchorpins to a corner moves inward clear of them on its own, keeping its margin from the control as it keeps it from the edge. A widget answering a press (a button, a slider, a list that scrolls, a box withonClick) that a box placed bypos, padding or flex puts over an action button (the rows of a full-screen menu, a list scrolled under the column) takes the presses landing there, and the button gives way, drawn nowhere until the widget goes (Zin.touchControls.layout().yielded); a label or a tint a press does nothing to leaves the button standing. The round Menu button holds its place, so a press over it reaches the menu, and a screen keeps its own widgets clear of it through this list. A world that draws on-screen controls of its own states their rects withui.setControlRegions(screenName, rects), so every other screen keeps clear of those as well. -
A phone sizes the UI by its density:
ui.displayScale(). A handheld display (a phone, a tablet, a page in a touch browser) states its density, the physical pixels one density-independent point covers, and a UI point then covers at least that many pixels in either orientation, so 13 point text and a 48 point button keep their physical size on any phone. The canvas is narrower there: about 410 points wide on a phone held upright, so readui.screenSize()rather than assuming 1280. The user's text size setting multiplies every font size the UI typesets (labels, buttons, inputs, editor chrome; canvas text is drawn at the size it names), and on a handheld display a touch reaches an interactive widget anywhere within half the 48 point touch target of it.ui.displayScale()reports{ density, textScale, touchTarget, pixelRatio }(densityis nil on a desktop window, which is sized by its width alone);ui.simulateDisplayScale({ density = 2.625, textScale = 1.3 })sizes the UI as that phone would on any engine, andui.simulateDisplayScale()returns to the platform's own. -
A world plays in the orientation it states:
world.orientation. Set it to"landscape","portrait"or"any"(the default) while editing; in play mode the display is locked to it (both directions of it, so turning the phone over turns the game with it), and in edit mode it follows the device.ui.orientation()reports{ current, lock, enforced, reason, rotation, revision }:currentis the shape of the window the app presents in,lockthe lock in force, andenforcedwhether the platform holds it. Android always does; a browser does once it grants the Screen Orientation lock, which most grant only to a page in fullscreen (reasonthen names the browser's refusal); a desktop window never does and presents in the shape it is given.rotationis the display's turn from its natural orientation in degrees, nil on a desktop window.ui.lockOrientation(lock)changes the lock while the world runs, andui.onOrientationChange(fn)calls back with each new reading of the display. A layout that rearranges itself between the two shapes follows the canvas it lays out on withui.onCanvasChange, whose reading carries the canvas's ownorientation: in the editor that is the Scene panel's picture, which takes a phone's shape on a landscape monitor. A turn also moves the safe area, andui.safeArea()follows it. -
A person can ask for less motion:
ui.reducedMotion(). Android's Remove animations, a browser'sprefers-reduced-motionand the animation settings of Windows, macOS and the Linux desktop all reach the engine while it runs. A style follows it with no Luau through an["@media (prefers-reduced-motion: reduce)"]block (transition = "none",animation = "none"; the styling guide has the whole form);ui.reducedMotion()reports{ reduce, platform, platformSource, setting, source, revision }for motion a script drives, withplatformreading"unknown"where the platform states no such setting;ui.onReducedMotionChange(fn)calls back on each change, andui.setReducedMotion(true | false | nil)states an app's own preference, which wins over the OS setting while it stands. -
A widget moves without its screen being sent again:
ui.spring.ui.spring(id, { x, y, scale, opacity }, { stiffness, damping, onSettle })springs a widget by itsid,ui.fling(id, { x = vx, y = vy })throws it with anonDraghandler's release velocity, andui.setMotions({ [id] = { x, y } })places a whole field a script simulates in one call a frame. The engine steps the motion each frame with no Luau and noupdateScreen; the widget is drawn moved and pressed where it is drawn, its layout box stays put, and reduced motion is honoured on its own. The styling guide's "Motion the engine steps" section has the whole surface. -
A screen is observable the next frame, not the same call.
registerScreen/updateScreenqueue the change; it applies at the frame boundary, and a screen's layout only exists after a render pass. Soui.getLayoutInfo(id)or elementTree called in the sameexecutethat registered the screen read the last published frame and do not see it yet. Register in one step, query in a later one — acaptureor a follow-up call is already frames later. This is the immediate-mode model, not a bug. -
A blank screen is a positive signal. A registered screen that renders nothing is transparent (no
background), sized to zero, off-canvas, or failed validation — not merely "not shown". Check theguitoolbox's validate tool and capturesource = "screen".
Why is my widget not showing, and what did my click hit
ui.getLayoutInfo and the gui toolbox's elementTree tool answer where
layout put a widget.
ui.observe() and ui.diagnose(id) answer what the paint stage did with it.
ui.diagnose("hud-healthbar")
-- { reason = "occluded", detail = "'hud-backdrop' paints later and fills the
-- whole box at full alpha", occludedBy = "hud-backdrop",
-- rect = {...}, clip = {...}, visible = {...}, paintIndex = 41, ... }
reason comes from a closed set — ui.invisibilityReasons() lists it:
| reason | what the engine found |
|---|---|
painted | it reached the frame |
noSuchWidget | no registered screen's element tree carries the id |
hiddenByAuthor | props.visible = false on it or an ancestor kept it out |
screenNotShown | its screen is registered and not shown |
neverLaidOut | its screen is shown and the frame laid out no box |
zeroSize | its own box has no area |
offViewport | its box sits entirely outside the viewport of the pass that drew it (the row's viewport) |
clippedByAncestor | an ancestor's clip removed the whole box |
occluded | a later widget covered it with an opaque fill |
screenNotOnViewport | its box is fine and the camera holding the viewport excludes its screen's render layer, so the frame measured it without drawing it |
Nearest cause wins: a widget both clipped away and covered reports the clip.
The camera's render-layer mask decides the screens in the camera's picture.
With no Scene panel presenting the game viewport, the window is that picture
too, and the mask decides every screen on it. While a Scene panel presents the
game viewport, the window around it is the editor's own picture: a screen
placed on the window (ui.setScreenSpace, and the default for a screen on
EditorUI) paints there whatever the camera's mask holds, and reads painted.
A screen placed in the camera's picture reads screenNotOnViewport under a
camera that excludes its layer, with or without a Scene panel.
In edit the editor's fly-camera holds the viewport and leaves the content ui
layer out. In play the scene's camera holds it. While play sits paused the
fly-camera takes the viewport back over the played world and draws the ui
layer with it, so a screen the game shows there (a pause menu, a HUD) stays
painted and takes the pointer.
Four fields carry the geometry the layout rect alone cannot: clip is the
clip chain the widget painted under, visible is the part of its box that
survived that clip and the viewport, viewport is the region the pass that
drew it paints into, and paintIndex is the order it painted in, which is
what separates two widgets whose rects are identical (ui.paintOrder(a, b)
compares two directly).
Every rect here, and every ui.getLayoutInfo / gui.bounds rect, is in
logical points on the window, wherever the widget's screen draws. A screen on
the window has the whole window as its viewport. A screen in the camera's
picture lays out on the camera's own canvas (the size ui.screenSize()
reports), and while a Scene panel presents that picture its rects are mapped
onto the window through the panel, so its viewport is the panel's rect on
the window. Times ui.pixelRatio() a rect is in the window's physical pixels:
the space Zin.test's mouse primitives and the inputSim toolbox's click /
mouseMove with target "ui" place a pointer in.
For the pointer: ui.hitTest(x, y) names the widget a point belongs to and the
stack under it, in logical points on the window: the space gui.bounds
answers in. Where boxes overlap, the one painting
last takes the point
and the containers holding it follow, so the answer is the widget a click there
lands on rather than the smallest box around the coordinate.
ui.hitTestRegions(regions, opts) asks of whole regions (a rect or a disc
each) which widget a press there does something to, for something drawn over
the UI that needs to know: it names the widget answering a click or a drag
(the answersPress field of ui.diagnose), a label inside a button
answering through the button, and nothing where the press lands on a label
or a filled box that answers none of it. Its takesHold (a field of
ui.diagnose too) says a finger resting on that widget is using it: a slider,
a text field, a scroll area, or a widget with a drag, a pointer-down or -up
callback, onContextMenu, holdsPointer or keepsTouchDrag. opts.space keeps to one surface's
screens.
ui.pointerWidget() is the pointer counterpart of
ui.focusedWidget(): where the pointer sits, whether the UI claimed it, and
which widget the frame reported it over.
The whole reading is ui.observe(), one row per widget, and
/zero/runtime/ui/<screen>/paint.json serves the same rows to a non-Luau
reader. generation advances once per re-rendered frame, so two reads
reporting the same number describe the same frame.
Seeing and driving it
You judge a UI by its pixels and its behaviour, not by its tree. The gui
toolbox is the whole operational surface: capture source="screen" (and
captureElement for one element or a composite) to see it; elementTree,
bounds, validate and state to read it; clickElement, clickAt,
hoverElement, dragElement and key to drive it, and scrollElement
to scroll a specific scrollArea to an offset; show, hide, toggle
and setTheme to manage it.
To use a UI the way its logic sees it — press a button, type into a field,
set a toggle — address the element: press, type and set deliver the
element's own onClick / onChange / onSubmit with no pointer moved and
nobody's keyboard focus touched, on an editor panel ("window" space) or the
game's UI ("camera" space) alike, so an agent can build and exercise a panel
while a person keeps working beside it. They act only on what the frame drew.
The three verbs that aim a pointer (clickElement, hoverElement and
dragElement) are the pointer-level route — hit areas, hover, a drag across
widgets — and share the pointer with anyone at the window. They aim at the
widget's layout rect, and a rect
exists for every widget the layout measured whether the frame drew it or not.
Each one therefore reads ui.diagnose(id) first and sends the pointer only on
a painted verdict; any other verdict comes back as ok = false carrying the
reason and its detail, so a row scrolled past the canvas or a screen on a
render layer the viewport camera excludes reads as the state it is in. On a
painted widget the aim is the middle of the part that reached the frame, so a
row a scroll area cut to a band of itself is pressed inside that band.
clickAt presses a point whatever stands there. Every one of these pointer
verbs speaks logical points, the space gui.bounds and ui.hitTest use, and
converts to window pixels by ui.pixelRatio() itself:
clickAt presses a gui.bounds centre as it reads, and the x, y that
clickElement and hoverElement answer are the point they aimed at, which
clickAt presses again.
A raw pointer aimed from a widget's rect goes through the inputSim toolbox
with target "ui", in window pixels:
local b = tools.use("gui", "bounds", "play-button")
local r = ui.pixelRatio()
tools.use("inputSim", "click", 0, b.centerX * r, b.centerY * r, nil, "ui")
The wld toolbox's play and edit tools flip modes.
UI in the 3D world
For world-space UI — a nameplate, a floating health bar — add a UiPanel
component to an entity with a tree:
entity(id).component.add("UiPanel", { mode = "3d", tree = { type = "label", props = { text = "NPC" } } })
A flat overlay that follows a world point instead (a damage number, a
waypoint marker) is placed each frame from its projection. worldToScreen
answers in the scene viewport's physical pixels, and ui.fromViewport
carries that into the points the screen lays out in:
local s = worldToScreen(target.x, target.y + 2, target.z)
local at = ui.fromViewport(s.x, s.y)
ui.updateScreen("marker", { type = "area", id = "marker", pos = { at.x, at.y },
children = { { type = "label", props = { text = "!" } } } })
ui.toViewport is the way back: a point on the UI as the scene viewport's
pixels screenToRay and Physics.raycastScreen take. A point off screen or
behind the camera answers s.onScreen = false and still points the way to
it. guides { path = "topics/screen-coordinates" } covers both spaces, the
edge arrow for an off-screen point, and the pointer.
UI in a headset
In an XR session the game's HUD (every shown screen of the "camera" space) appears
on a curved panel 2.4 metres ahead with no change to the screens: the controller's
laser is the pointer, the trigger presses what it is on, and the stick scrolls the
scroll area under it. A world receives no pointer while the laser is over a button of
its HUD. xr.panels.create puts any screens on a panel of your own, with rounded
corners unless it states cornerRadius = 0; an image on a panel shows a camera's
texture live, and so does an image that names a file (a thumbnail, an icon, a picture
in the world's files): a panel loads the pictures its screens name whatever the desktop
window shows; see man xr.
When a script errors
In the editor, an uncaught error surfaces as a small card at the bottom centre
of the window (the __task_error_overlay screen), so a failure is visible
rather than a silent freeze. The card shows the script that raised the error
and the error's first line. Every error is also logged, and the logs toolbox's
errors tool has the full stack.
The card stands for one second. An error that arrives while it stands replaces
its text without lengthening its stay. An error is repeating when it arrives
again while the card stands, or when it comes back within a minute of a card
that showed it. Once the card has gone by itself, a repeating error does not
raise it again for a minute, and a card that saw more than one error arrive
leaves three seconds in which no error raises it. An error raised every frame
therefore shows the card once and then appears only in the log, and a one-off
error leaves no quiet period.
ui.unregisterScreen("__task_error_overlay") takes the card down at once and
leaves no quiet period, so the next error raises it straight away. A runtime
profile never shows the card.