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

Русская версия · Three states · Switch

Use ui::checkbox for an independent on/off choice, such as opting into notifications. A Switch can present a setting such as network on/off; both can bind a Boolean value, and an accepted user change updates that bound value, while the application controls side effects and persistence. Use ui::threeState when a mixed value is part of the interaction, or a radio group for one choice among alternatives.

A complete application

This complete program uses a CheckBox with its optional three-state cycle and reports each user change in a label. Space or Enter changes it, and F10 or Ctrl+C exits; runApp reports errors and restores the terminal.

The checkbox example starts unchecked with a status label.
// Copyright (c) 2026 ttldtor.
// SPDX-License-Identifier: BSL-1.0
#include <portals/ui.hpp>
int main() {
using namespace portals;
ui::Ref<CheckBox> selection;
// clang-format off
auto scene = ui::scene(ui::vbox(
ui::checkbox(selection, "Selection").threeState(),
ui::label(status, "Unchecked")));
return scene
| ui::onChange(selection, [status](CheckState value) {
switch (value) {
status.text() = "Unchecked";
break;
status.text() = "Checked";
break;
status.text() = "Mixed";
break;
}
})
| ui::onKey("F10 | Ctrl+C", ui::quit)
| ui::runApp();
// clang-format on
}
Typed Widget observation; explicit composite specializations reuse their existing weak facades.
Definition ui_ref.hpp:761
TextProperty text(TextResolver resolver={}, std::source_location origin=std::source_location::current()) const
Definition ui_ref.hpp:1968
Public Portals API.
Definition action.hpp:8
CheckState
Explicit checkbox value; MIXED describes an aggregate or an opt-in user choice.
Definition controls.hpp:266
@ MIXED
Partial selection, displayed independently of color.
Definition controls.hpp:269
@ CHECKED
Complete selection.
Definition controls.hpp:268
@ UNCHECKED
No selection.
Definition controls.hpp:267

Build portals_docs_check_change with PORTALS_BUILD_TESTS=ON and run it in a terminal. For a Boolean checkbox, the supported recipe is ui::checkbox("Alerts", true); omitting the initial value uses the context default, otherwise unchecked. See build setup and package setup.

State, input and resources

A Boolean model toggles unchecked and checked. CheckState additionally represents MIXED; CheckBoxOptions::cycle defaults to TWO_STATE, where mixed advances to checked. .threeState() selects unchecked → checked → mixed → unchecked. Unmodified Enter, Space and left mouse press activate; release, motion, paste and modified gestures do not. Hidden or disabled ancestry prevents activation. There is no separate read-only checkbox mode.

ui::Ref<CheckBox> observes the mounted widget; ui::onChange(ref, handler) receives successful user changes as in the example. External model updates do not invoke that business callback. A commit is not rolled back when observer notifications are rejected. The scene owns the binding and widget; the Ref does not extend their lifetime. For typed receipts and shared model behavior see binding policy and three-state details.

Resources support CheckBox#enabled { "Enabled"; checked = true }; retrieve it with scene.get<CheckBox>("enabled"). See scene resources.

Styling reference

The natural selector is CheckBox. Normal, focused and disabled paint the entire caption and marker, with disabled first. Checked and mixed values select [x] and [-] ; unchecked uses [ ] . They do not select :selected; header and caret are not painted. Marker strings are fixed and have no separate PSS glyph or color property.

Property Values and effect
foreground, background #RRGGBB; foreground and fill
bold, italic, underline true or false; explicit false overrides a lower layer
width, height Nonnegative cells, auto, fit-content, flex(positive weight) in runtime PSS
min-width, max-width, min-height, max-height Nonnegative cell bounds; only min-width also works in compile-time PSS

Size declarations cannot depend on a state or :read-only; conflicting bounds reject the update and retain the last valid style and geometry. See layout. Manual ControlStyle uses terminal-default colors, normal text, bold focus and italic disabled text. Theme and PSS rules override individual fields; unspecified fields retain lower-layer values. See the cascade.

CheckBox { min-width: 12; }
CheckBox:focused { foreground: #ffffff; background: #245070; }
CheckBox:disabled { italic: true; }

See portals::CheckBox, CheckState, CheckBoxOptions, ui::checkbox and basic styling.