Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
Key hints and help

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

Use KeyHints for compact shortcut hints and HelpView for a fuller view of registered commands. The application chooses bindings and opens or closes help through its reference; the example below shows the keys. See KeyHints options and HelpView options.

KeyHints and the open HelpView showing registered commands

Styling reference

KeyHints paints normal, :focused, or :disabled (disabled takes precedence). The full help body has its own HelpView.help-body target and paints its normal style; styling the outer wrapper does not style the body. Their ControlStyle supplies manual defaults beneath the cascade.

Use foreground, background (#RRGGBB), bold, italic, underline (true or false). State-free rules cover every variant. See the cascade and size constraints; min-width requires a state-free selector.

Configuration-only appearance values are listed below; they are owning strings, not PSS properties. KeyHintsOptions::format and HelpViewOptions::format use KeyFormatOptions. Neither background nor text style on the outer HelpView is inherited by its body.

Option Default
KeyHintsOptions::entrySeparator Two spaces
overflowMarker, emptyText, unavailableText +, empty, ?
HelpViewOptions::emptyText, unavailableText No key bindings, Help unavailable
KeyFormatOptions::strokeSeparator, alternativeSeparator then , /
repeatMarker, releaseMarker, unknownMarker [repeat], [release], [?]
maxBytesPerBinding 8192 bytes per complete formatted row
HelpView.help-body { foreground: #d0d8e0; }

A key command's name accompanies its registration. KeyHints and HelpView read those same registrations, so rebinding or disabling a command updates its presentation without maintaining a separate list of shortcuts.

A complete application

// Copyright (c) 2026 ttldtor.
// SPDX-License-Identifier: BSL-1.0
#include <portals/key_hints.hpp>
#include <portals/ui.hpp>
int main() {
using namespace portals;
// clang-format off
return [] {
return ui::scene(ui::vbox(
ui::lineEdit(document, "Document"),
ui::label(status, "Ready"),
ui::button("Disable Save", [save] { return save.setEnabled(false); }),
ui::button("Use Ctrl+Shift+S", [save] { return save.rebind(ui::press("Ctrl+Shift+S")); }),
ui::keyHints(help),
ui::helpView(help)))
| ui::onKey("Ctrl+S", ui::keyInfo("Save").reference(save).owner(document),
[status] { status.text() = "Save requested"; })
| ui::onKey("F1", ui::keyInfo("Help"), [help] { return help.open(); })
| ui::onKey("Escape", ui::keyInfo("Close help").scope(help), [help] { return help.close(); })
| ui::onKey("F10", ui::keyInfo("Quit"), ui::quit);
} | ui::runApp();
// clang-format on
}
Deferred observing slot, published only by a complete successful registration. Construction may alloc...
Definition key_binding.hpp:198
std::expected< void, ManagedError > setEnabled(bool value, std::source_location origin=std::source_location::current()) const
std::expected< void, ManagedError > rebind(const KeySelection &selection, std::source_location origin=std::source_location::current()) const
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
std::expected< void, ManagedError > close(std::source_location origin=std::source_location::current()) const
Definition ui_ref.hpp:1020
TextProperty text(TextResolver resolver={}, std::source_location origin=std::source_location::current()) const
Definition ui_ref.hpp:1968
std::expected< void, ManagedError > open(std::source_location origin=std::source_location::current()) const
Definition ui_ref.hpp:1013
auto reference(ui::Ref< T > value)
Definition ui_static.hpp:572
Public Portals API.
Definition action.hpp:8

The example links portals::core; see package setup. The standalone guide project builds guide_key_help. This is a native application without a replay mode. Its Save command changes a status label; it does not write a file or implement persistence.

F1 opens the help pane, Escape closes it while its scope is active, and F10 exits through the normal runner. These keys are explicitly chosen by the application; neither control installs hidden open, close or quit bindings. The buttons disable Save or change its shortcut. The hints and help reflect both changes.

The help body is registered for style resolution as HelpView.help-body. Its supplied ControlStyle provides manual defaults beneath matching theme and application rules; without a computed style, painting uses its normal manual default. Styling the outer HelpView wrapper does not automatically style the body. KeyHints uses the disabled variant when it or an ancestor is disabled, before choosing focused or normal appearance.

The compact handlers return the checked result from help.open(), help.close() and binding changes. Their failures then reach the existing managed error path. These commands return std::expected, not the automatically collected text-property assignment proxy. Outside a handler, the caller checks the result explicitly.

Metadata, ownership and changes

In a CONFIGURATION resource, HelpView and KeyHints have scoped built-in factories. KeyHints{help="guide"} refers to a HelpView with ID guide in the same resource; without help, exactly one HelpView-capable instance is required. Key registrations remain C++ owned. See resource binding details.

ui::keyInfo(label) adds an owning name to an ordinary ui::onKey registration. Optional .description(text) supplies the longer help explanation. .owner(reference) ties admission to that control and its ancestors; .scope(reference) restricts admission to an existing focus scope. Neither modifier adds a new routing priority: the newest matching admitted registration still wins. A deeper scope does not automatically outrank a newer global binding.

.reference(binding) is useful when the application needs to change that binding later. ui::KeyBindingRef observes the scene-owned command; destroying the observation does not unregister it. Its checked operations are setEnabled, setVisible, setText, rebind, unbind and remove. Unbinding keeps the identity and explanation; removal retires it. Generated identities are local to a scene and are unsuitable as saved configuration keys.

An unpublished or closed observation returns a typed error. Operations are UI-affine; worker threads cannot mutate a binding. Removed controls are never recovered by reusing their slot. Names and descriptions accept owning TextValue expressions. Resource text uses an explicit ui::keyInfo(expression, resolver); resolution occurs during checked metadata normalization, not on every paint. A read snapshot retains neither the resolver nor the callback.

What the two controls display

ui::keyHints(help) reserves one row. It shows named, currently eligible commands and fits complete entries to the available width. Its overflow action opens the supplied help pane through the existing focus and input path. An empty or fully fitting row does not add an invisible focus stop.

ui::helpView(help) starts hidden and contains an ordinary ScrollArea. It is nonmodal: it neither shields the rest of the scene nor traps Tab. Opening enters its existing focus scope; closing returns focus through the existing return stack. Hiding, disabling or removing the pane retires that activation. Reopening creates a new activation; an old return receipt cannot close the new one.

Full help explains unavailable bindings and conditional overlaps. A conflict is not a promise about which command will run in every focus context. Legacy unannotated registrations remain routable and inspectable; compact hints require an explicit command name.

Disabling a named command leaves it visible as unavailable in full help and removes it from compact hints. Setting its visibility to false also disables its routing and removes it from projections. Hidden or disabled owners and ancestors cannot admit the command. Changing only the explanation preserves a pending key sequence; rebinding or changing admission cancels the old pending snapshot.

Terminal support, errors and limits

Terminal phase support is explicit KeyCapabilities information. UNKNOWN remains routable and is marked with [?]; it does not claim native release, repeat or left/right modifier support. UNSUPPORTED excludes affected annotated commands. This metadata does not negotiate a terminal protocol.

The default formatter uses ASCII names, then between sequence strokes, / between alternatives, and explicit repeat/release markers. KeyFormatOptions, KeyHintsOptions and HelpViewOptions customize those strings. Clipping respects grapheme boundaries. A formatted binding is limited to 8192 bytes by default. Scene defaults cap labels at 512 bytes, descriptions at 2048 bytes and aggregate logical projection payload at 1 MiB; existing registration and shortcut limits still apply.

Registration and changes stage owning data before publication. An invalid or over-budget candidate leaves the prior binding and routing state intact. If a fresh presentation cannot be prepared, the visible control shows its unavailable marker rather than claiming that an old projection is current. The error uses the existing diagnostics path; unchanged failed input is not retried every frame. An explicit later change or invalidation permits recovery. A presentation failure does not undo an independently committed binding change.

Hidden projections do not format rows. Static hints and help do not poll, add a second timer or continuously redraw. See input, focus and managed controls for the underlying retained lifecycle.