Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
Declarative UI

Русская версия

Use the declarative API to arrange controls and attach event handlers. ui::scene(...) creates a scene, and ui::runApp() runs the application.

Use data presentation to show loading and result states around ordinary content or a managed virtual list/table.

Run a form

In this program, Add Hello appends text to the editor. Exit and F10 close the application. Add Hello uses a bold style.

Source. To connect Portals and build your application, follow the short library guide.

#include <portals/ui_style.hpp>
int main() {
using namespace portals;
auto scene = ui::scene(ui::vbox(
ui::lineEdit("").id("message"),
ui::button("Add Hello").id("add"),
ui::button("Exit").id("exit")));
auto message = scene.get("message");
ui::Ref<Button> add = scene.get("add");
auto exit = scene.get<Button>("exit");
return scene
| ui::style(add, "* { bold: true; }")
| ui::onClick(add,
[message] {
message.text() += "Hello!";
})
| ui::onClick(exit, ui::quit)
| ui::onKey(key::F10, ui::quit)
| ui::runApp();
}
One-line button; registration is an explicit caller-owned ActionBinding.
Definition controls.hpp:161
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
DynamicRef get(std::string_view name, std::source_location origin=std::source_location::current())
Public Portals API.
Definition action.hpp:8

ui::vbox arranges its children vertically; ui::hbox arranges them horizontally. ui::lineEdit, ui::button and ui::label describe the controls to create. This description is called a recipe. The scene creates the controls when it is prepared for running.

The | chain adds settings and handlers to the scene. Each operation is a UI stage, such as ui::style(...) or ui::onClick(...). The last operation, ui::runApp(), runs the event loop, attempts terminal restoration and scene cleanup, and returns an exit code: 0 on success, 1 on a typed failure. The default error report goes to stderr after cleanup.

Use <portals/ui.hpp> for scenes, controls and handlers. This example includes <portals/ui_style.hpp> because it also uses runtime ui::style(...). See the styling guide for PSS syntax.

Refer to controls

ui::Ref<T> lets a handler refer to a control created by the scene. Pass the reference as the first factory argument, then capture it by value in handlers. For example, message.text() += "Hello!" appends to the editor's text. See text properties for assignment, reads and formatting.

A reference observes the control; the scene owns it. References become usable after the complete recipe has mounted. Once the control is removed or the scene closes, the reference cannot access it. Objects captured by C++ reference must remain alive while their handlers can run.

Look up a named control

Assign an owning ID with .id("message"), then call scene.get("message"). The dynamic observation supports checked text() and Button onClick() capabilities. ui::Ref<Button> add = scene.get("add") and scene.get<Button>("add") require that the mounted widget derives from Button. No call to get prepares the scene. IDs are case sensitive and copied; their input buffers need not survive the call.

Pending requests resolve after the whole recipe mounts and before configure/handler stages. A missing or empty ID, an ambiguous ID, or a wrong type records BIND/INVALID_ARGUMENT with the owning name, expected type and original call site. Startup then skips configuration and terminal acquisition. Legacy style selectors may still match repeated IDs; requesting such an ID is ambiguous. maxConfigurations also bounds the pending lookup queue independently of pending configuration stages.

A lookup after preparation resolves immediately and reports recoverable failures to the existing collector. In a checked setup/configuration scope, a short failure also stops subsequent setup through the existing policy. A published Ref keeps its original NodeId when an ID changes; removal and ID reuse never retarget it. Operations reject foreign threads before reading mutable scene state. A successfully published observation returns CLOSED after cleanup; a removed node returns STALE_NODE. Failed lookup slots retain their original error for inspection. Capture refs by value in callbacks.

Style one reference or read a file

ui::style(add, "* { bold: true; }") applies complete strict PSS v0 rules to exactly the referenced node. It does not include its children. Existing PSS selectors still filter that node, with their original specificity. This scope uses NodeId and survives an ID rename without applying to a replacement node. A foreign scene's Ref returns WRONG_SESSION; an expired or removed target rejects before style publication.

ui::styleFile(add, "button.pss") owns a native path and reads it only when the scene consumes the stage. ui::styleFile("application.pss") is the scene-wide form. For example, a file containing * { bold: true; } can replace the inline stage in the complete program above. Relative paths use the process working directory at consumption. Missing files and parse errors stop startup before terminal activity; diagnostics retain the file operation, source label and PSS position when applicable.

These forms use the existing layer resolver and source metadata. Unnamed stages own independent APPLICATION layers; reusing one stage updates its layer. StyleOptions selects USER or an explicit named upsert using the same precedence and budgets as style layers. Files have no watcher, includes or new expressions. Installed replay consumer verifies the complete handler/error/cleanup path without opening a native terminal.

Handle button activation

ui::onClick(ref, handler) adds a handler after the controls have been created. Handlers for the same button run in registration order. A handler can accept ui::ClickParams& to inspect modifiers and stop the remaining handlers.

This complete program shows Applied after a normal activation. With an admitted Shift-modified activation, it shows Apply with Shift and skips the second handler. F2 changes the label; F10 exits.

Source.

#include <portals/ui.hpp>
int main() {
using namespace portals;
auto scene = ui::scene(ui::vbox(
ui::label(status, "Ready").id("status"),
ui::button(apply, "Apply")));
return scene
| ui::onClick(apply,
[status](ui::ClickParams &params) {
if (params.mods.has(key::SHIFT)) {
status.text() = "Apply with Shift";
params.stop();
}
})
| ui::onClick(apply,
[status] {
status.text() = "Applied";
})
| ui::onKey("F2",
[status] {
status.text() = "F2";
})
| ui::onKey("F10", ui::quit)
| ui::runApp();
}
Owning click snapshot used by pipe callbacks; no widget or scene ownership. The callback borrows this...
Definition controls.hpp:217
void stop() noexcept
Short alias for stopPropagation, affecting the same synchronous chain context.
Definition controls.hpp:230
const InputModifiers mods
Original semantic modifiers, queryable with key tags.
Definition controls.hpp:240
TextProperty text(TextResolver resolver={}, std::source_location origin=std::source_location::current()) const
Definition ui_ref.hpp:1968
constexpr bool has(key::Modifier modifier) const noexcept
Definition input.hpp:43

params.mods.has(key::SHIFT) reads semantic modifiers. params.source distinguishes keyboard, mouse and programmatic activation; params.target identifies the button. Parameters are borrowed for the callback. A copied snapshot retains event data, but calling stop() on that copy cannot stop the original activation. See bindings and events.

Some terminals reserve Shift+mouse for selection and withhold that event. See input for terminal capabilities and modifier reporting.

Register shortcuts

Use ui::onKey("Ctrl+S", handler) for a shortcut, ui::onKey("F10 | Ctrl+C", ui::quit) for alternatives, and ui::onKey("Ctrl+K, K", handler) for successive strokes. The ordinary string shortcut form accepts press events. Use ui::pressRepeat(...) when a command should also run on repeats. See shortcut syntax and event selection for matching rules.

Pass ui::quit directly when the handler only needs to close the current scene. For work that explicitly refers to another scene, use SceneRef; see the scene reference.

Share state between controls

Create a shared state in the scene's recipe factory. The factory receives ManagedScene& and runs during preparation. Its checked result passes state creation failures to the application runner.

The editor, label and button below share the same string state. Accepted editor changes update both captions through managed preparation. Click the button or press F10 to exit.

Source.

#include <portals/ui.hpp>
#include <string>
int main() {
using namespace portals;
auto scene = ui::scene([](ManagedScene &owner) {
return owner.state(std::string("Portals"))
.transform([](auto model) {
return ui::vbox(ui::lineEdit(model),
ui::label(model),
ui::button(model, [] {
ui::quit();
}));
});
});
return scene
| ui::onKey("F10", ui::quit)
| ui::runApp();
}
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
std::expected< ManagedState< T >, ManagedError > state(T initial, Invalidation kind=Invalidation::PAINT, PropertyOptions options={})
Create UI-owned state with guaranteed dirty-frame recovery of missed notifications.
Definition managed.hpp:655

For custom presentation, attach .observe(state, callback) to a control recipe. The scene owns delivery, starting after all references are published; see managed observation for errors, limits and the latest-value contract.

Use owner.derive(input, transform) when several captions need one computed owning value. Its read-only ManagedSource is accepted by the same label, button and observation recipes.

For large datasets, create a model in the same factory and pass its observing handle to ui::virtualList, ui::virtualTable or ui::chart. See models and virtual tables.

Scroll ordinary content

Use ui::scrollArea for a retained form and ui::scrollBar for a standalone or shared axis. The ordinary scroll guide contains a complete native application, bar placement, checked commands and host capture requirements. For datasets, keep using the existing virtual views.

Configure virtual views

Use tableColumn and tableColumns to describe table fields and their formatting. The text and column guide covers syntax. ui::VirtualTableConfig<T>, ui::VirtualListConfig<T> and ui::ChartConfig<T> provide named view settings. Their contracts are in the scene reference.

Continue with a complete application

Fine scene settings

Set scene limits

Set ui::SceneOptions before creating the scene and pass it as the second argument to ui::scene. The options are copied into the scene and checked during preparation. There is no API for changing these limits on an existing scene.

Field in options.managed Meaning Default
maxNodes Retained managed node registrations, including removals awaiting preparation 4096
maxStates Scene-owned properties 256
maxModels Scene-owned models 64
diagnostics.maxErrors Retained diagnostic records 64
diagnostics.maxContexts Causal contexts retained per error 8
diagnostics.maxContextBytes Total label bytes retained per error 1024

All six limits must be greater than zero. Increase them when your form needs more managed objects or more diagnostic detail. maxErrors limits stored history; it does not specify how many errors the application tolerates. On history overflow, the oldest records remain and droppedErrors increases.

#include <portals/ui.hpp>
int main() {
using namespace portals;
options.managed.maxNodes = 8192;
options.managed.maxStates = 512;
options.managed.maxModels = 128;
options.managed.diagnostics.maxErrors = 128;
auto scene = ui::scene(ui::label("F10: quit"), options);
return scene
| ui::onKey("F10", ui::quit)
| ui::runApp();
}
std::size_t maxContextBytes
Total bytes retained across breadcrumb labels in each error.
Definition managed_diagnostics.hpp:117
std::size_t maxContexts
Maximum causal breadcrumbs in each error, in supplied order.
Definition managed_diagnostics.hpp:116
std::size_t maxErrors
Maximum retained errors; the oldest entries survive overflow.
Definition managed_diagnostics.hpp:115
std::size_t maxModels
Maximum scene-owned models; retired row scopes release their slots.
Definition managed.hpp:338
ManagedDiagnosticOptions diagnostics
Positive limits for error history and owning causal traces.
Definition managed.hpp:339
std::size_t maxStates
Maximum scene-owned properties; retired row scopes and private text adapters release their slots.
Definition managed.hpp:336
std::size_t maxNodes
Maximum retained entries, including removals pending next preparation.
Definition managed.hpp:335
Policies for one lazy facade over the existing managed runtime.
Definition ui_scene.hpp:17
ManagedOptions managed
Existing managed ownership and diagnostic budgets.
Definition ui_scene.hpp:19

Set handler limits and sequence timeout

These options are also passed to ui::scene before creation. Change them when a form has many handlers or users need more time between shortcut strokes.

Field Meaning Default
maxConfigurations Queued configure/key/click operations before preparation 64
managed.maxKeyHandlers Owning key registrations 64
managed.maxClickHandlers Pipe click handlers across the scene 256
managed.maxClickHandlersPerNode Pipe click handlers on one button 64
managed.keySequenceTimeout Inactivity between shortcut strokes, milliseconds 1000

Each value must be greater than zero. The example allows two seconds between the strokes of Ctrl+K, K. The comma denotes two successive strokes; see shortcut syntax.

#include <portals/ui.hpp>
int main() {
using namespace portals;
options.maxConfigurations = 128;
options.managed.maxKeyHandlers = 128;
options.managed.maxClickHandlers = 512;
options.managed.keySequenceTimeout = 2000;
auto scene = ui::scene(ui::label("Ctrl+K, K or F10: quit"), options);
return scene
| ui::onKey("Ctrl + K, K", ui::quit)
| ui::onKey("F10", ui::quit)
| ui::runApp();
}
std::size_t maxClickHandlers
Positive maximum retained pipe click callbacks across the scene.
Definition managed.hpp:341
std::size_t maxClickHandlersPerNode
Positive maximum pipe click callbacks for one button.
Definition managed.hpp:342
SchedulerTime keySequenceTimeout
Positive inactivity timeout between sequence strokes, in milliseconds.
Definition managed.hpp:343
std::size_t maxKeyHandlers
Positive maximum owning onKey registrations; onInput resets the count.
Definition managed.hpp:340
std::size_t maxConfigurations
Positive per-queue maximum for deferred configuration callbacks and named lookups.
Definition ui_scene.hpp:21

Compose children from a range

ui::vbox(range) and ui::hbox(range) consume an input range immediately, in iteration order. The range produces one concrete Description<T> type; use the variadic overload for heterogeneous children. A temporary transform view and its captures are destroyed after construction, while the parent owns all produced recipes. The range is never used during mount.

#include <portals/ui.hpp>
#include <array>
int main() {
using namespace portals;
const std::array<std::string_view, 3> captions{"Mercury",
"Venus",
"Earth"};
auto scene = ui::scene(ui::vbox(
captions | std::views::transform([](std::string_view text) {
return ui::label(text);
})));
scene.onKey(key::F10, ui::quit);
return scene.runApp();
}
Scene & onKey(KeySelection selection, Info info, Callback callback, std::source_location origin=std::source_location::current()) &
Definition ui_scene.hpp:531
int runApp(std::stop_token stop={}, std::source_location origin=std::source_location::current())
Run a standalone scene with standard post-restoration error reporting.
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567

The same scene with pipe stages:

#include <portals/ui.hpp>
#include <array>
int main() {
using namespace portals;
const std::array<std::string_view, 3> captions{"Mercury",
"Venus",
"Earth"};
auto scene = ui::scene(ui::vbox(
captions | std::views::transform([](std::string_view text) {
return ui::label(text);
})));
return scene
| ui::onKey(key::F10, ui::quit)
| ui::runApp();
}

Both versions use the same scene, error reporting and cleanup. Existing row/column overloads also accept ranges; vbox(options, range) and hbox(options, range) preserve LinearOptions. Empty ranges create empty layouts. A child mount failure preserves its typed error, rolls back the created subtree and publishes no partial NodeRef or Ref.

Lvalue containers of move-only descriptions require explicit transfer: ui::vbox(children | std::views::as_rvalue). ui::vbox(std::move(children)) also transfers an owning non-view container that does not opt into enable_borrowed_range. User-defined ranges must own their elements when using this implicit-element-transfer overload; that ownership cannot be inferred. A temporary view yielding lvalue descriptions is rejected, since its elements may belong to another owner. Construction and iteration may throw; recipes already transferred from a source are not restored. Borrowed references inside recipes retain their existing lifetime requirements.

Frames, gauges and compact sizing provide standard widgets and consuming methods/pipe stages for a complete small scene.

Text-driven lists

Use ui::virtualList(ui::words(field.draftText())) or ui::virtualList(ui::graphemes(field.draftText())) for an automatically owned text-to-list binding. Complete native application and contracts.