Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
radio_group.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/collection.hpp>
6#include <portals/controls.hpp>
7#include <portals/text_value.hpp>
8
9namespace portals {
13 bool enabled{true};
14 bool visible{true};
15
17 RadioOption(std::string_view caption, bool enabled = true, bool visible = true)
18 : text(caption), enabled(enabled), visible(visible) {
19 }
20
22 RadioOption(TextValue caption, bool enabled = true, bool visible = true)
23 : text(std::move(caption)), enabled(enabled), visible(visible) {
24 }
25
27 ~RadioOption() = default;
28};
29
31struct RadioItem {
33 bool enabled{true};
34 bool visible{true};
36 bool operator==(const RadioItem &) const = default;
37};
38
44public:
46 RadioGroupValue() = default;
48 ~RadioGroupValue() = default;
53 static std::expected<RadioGroupValue, TreeError> from(RowCollection<RadioItem> rows,
54 std::optional<RowId> selected = {});
56 std::expected<RadioGroupValue, TreeError> withSelection(std::optional<RowId> selected) const;
58 std::span<const ModelRow<RadioItem>> rows() const noexcept;
60 std::optional<RowId> selectedId() const noexcept;
62 std::string_view selectedText() const noexcept;
64 std::optional<RowId> firstEligible() const noexcept;
67 bool operator==(const RadioGroupValue &other) const;
68
69private:
70 friend class RadioGroup;
71 friend class ManagedScene;
72 friend class RadioGroupBinding;
73 struct Data;
74 std::shared_ptr<const Data> data_;
75 std::optional<RowId> selected_;
76 bool shorthand_{};
77};
78
84
86class RadioGroup : public SizedWidget {
87public:
93 explicit RadioGroup(ControlStyle style = {}, SizePolicy policy = DEFAULT_CONTENT_POLICY, bool readOnly = false,
94 WidthPolicy widthPolicy = {},
97 RadioGroup(const RadioGroup &) = delete;
99 RadioGroup &operator=(const RadioGroup &) = delete;
101 RadioGroup(RadioGroup &&) = delete;
107 std::expected<CellSize, TreeError> measure(CellSize available) override;
109 std::expected<void, TreeError> arrange(CellRect local) override;
114 std::expected<void, TreeError> paint(PaintContext &context) override;
116 ComputedStyle styleDefaults() const override;
117
119 bool styleReadOnly() const noexcept override;
122
123private:
124 friend class RadioGroupBinding;
125 friend std::expected<void, TreeError> syncRadioGroup(WidgetTree &, Node<RadioGroup>, Node<Widget>);
126 std::expected<NodeGeometry, TreeError> geometry(bool painting = false) const;
127 WidgetTree *tree_{};
128 Node<Widget> node_;
129 ControlStyle style_;
130 WidthPolicy widthPolicy_;
131 CellRect bounds_;
132 RadioGroupValue value_;
133 std::optional<RowId> active_;
134 bool readOnly_{};
135 bool enabled_{true};
136 bool focused_{};
138};
139
141using RadioGroupResult = std::expected<PropertyUpdate<RadioGroupValue>, TreeError>;
142
145public:
157 std::expected<void, TreeError> sync();
159 std::expected<RadioGroupValue, TreeError> value() const;
161 RadioGroupResult select(std::optional<RowId> id);
162
163private:
164 friend std::expected<RadioGroupBinding, TreeError> bindRadioGroup(WidgetTree &, EventRouter &, Node<RadioGroup>,
165 std::shared_ptr<Property<RadioGroupValue>>,
166 std::function<void(RadioGroupResult)>);
167 struct State;
168 RadioGroupBinding(std::shared_ptr<State>, ActionBinding, Subscription);
169 std::shared_ptr<State> state_;
170 ActionBinding action_;
171 Subscription subscription_;
172};
173
179std::expected<RadioGroupBinding, TreeError> bindRadioGroup(WidgetTree &tree, EventRouter &router, Node<RadioGroup> node,
180 std::shared_ptr<Property<RadioGroupValue>> model,
181 std::function<void(RadioGroupResult)> changed);
185std::expected<void, TreeError> syncRadioGroup(WidgetTree &tree, Node<RadioGroup> node, Node<Widget> focused);
186} // namespace portals
Move-only scoped disconnect token; never owns a tree or widget. Disconnect/destruction may run on any...
Definition action.hpp:96
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
UI-owned focus and synchronous bubbling over an existing WidgetTree. Tree and Scheduler must outlive ...
Definition action.hpp:169
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Borrowed synchronous command sink supplied by the frame owner. submit must consume commands immediate...
Definition widget_types.hpp:66
UI-owned typed value with node-scoped observers and explicit partial delivery. WidgetTree and its Sch...
Definition property.hpp:62
RadioGroupBinding(RadioGroupBinding &&other) noexcept
~RadioGroupBinding()
Disconnect this registration on its creating UI thread.
RadioGroupBinding & operator=(const RadioGroupBinding &)=delete
Copying cannot duplicate a live input registration.
RadioGroupBinding(const RadioGroupBinding &)=delete
Registrations have one owner.
RadioGroupBinding & operator=(RadioGroupBinding &&other) noexcept
std::expected< RadioGroupValue, TreeError > value() const
friend std::expected< RadioGroupBinding, TreeError > bindRadioGroup(WidgetTree &, EventRouter &, Node< RadioGroup >, std::shared_ptr< Property< RadioGroupValue > >, std::function< void(RadioGroupResult)>)
RadioGroupResult select(std::optional< RowId > id)
std::expected< void, TreeError > sync()
Coherent immutable resident options and optional selection; copies share all row storage....
Definition radio_group.hpp:43
std::span< const ModelRow< RadioItem > > rows() const noexcept
~RadioGroupValue()=default
Release shared rows; rvalue transfers preserve a valid shared-owner source, like RowCollection.
std::optional< RowId > selectedId() const noexcept
static std::expected< RadioGroupValue, TreeError > from(RowCollection< RadioItem > rows, std::optional< RowId > selected={})
std::string_view selectedText() const noexcept
RadioGroupValue()=default
Construct an empty value without allocating rows or selecting an item.
std::expected< RadioGroupValue, TreeError > withSelection(std::optional< RowId > selected) const
std::optional< RowId > firstEligible() const noexcept
ComputedStyle styleDefaults() const override
RadioGroup(const RadioGroup &)=delete
Tree-owned widgets cannot copy their live geometry context.
bool styleReadOnly() const noexcept override
RadioGroup & operator=(RadioGroup &&)=delete
A bound widget cannot transfer its tree context.
friend std::expected< void, TreeError > syncRadioGroup(WidgetTree &, Node< RadioGroup >, Node< Widget >)
RadioGroup(ControlStyle style={}, SizePolicy policy=DEFAULT_CONTENT_POLICY, bool readOnly=false, WidthPolicy widthPolicy={}, RadioSelectionMode selectionMode=RadioSelectionMode::ON_ACTIVATION)
std::expected< void, TreeError > arrange(CellRect local) override
std::expected< CellSize, TreeError > measure(CellSize available) override
RadioGroup(RadioGroup &&)=delete
A bound widget's address and tree identity remain fixed.
RadioGroup & operator=(const RadioGroup &)=delete
Tree-owned widgets cannot be assigned.
std::expected< void, TreeError > paint(PaintContext &context) override
RadioGroupValue value() const
Immutable owning rows with explicit provider identity and bounded identity lookup.
Definition collection.hpp:19
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
UI-owned move-only cancellation token. Release before Scheduler. Disconnect is immediate logically; a...
Definition scheduler.hpp:121
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
UI-owned extension object. Hooks borrow their arguments for one call. Models are separate explicit ow...
Definition widget.hpp:32
Public Portals API.
Definition action.hpp:8
std::expected< RadioGroupBinding, TreeError > bindRadioGroup(WidgetTree &tree, EventRouter &router, Node< RadioGroup > node, std::shared_ptr< Property< RadioGroupValue > > model, std::function< void(RadioGroupResult)> changed)
RadioSelectionMode
User selection publication policy; active navigation remains local to the group.
Definition radio_group.hpp:80
@ ON_ACTIVATION
Arrows move active; Space/Enter selects.
Definition radio_group.hpp:81
std::expected< PropertyUpdate< RadioGroupValue >, TreeError > RadioGroupResult
Exact selection commit and observer-publication receipt, or a precommit TreeError.
Definition radio_group.hpp:141
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
std::expected< void, TreeError > syncRadioGroup(WidgetTree &tree, Node< RadioGroup > node, Node< Widget > focused)
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ IMMEDIATE
Publish each semantic-valid text change; invalid intermediate text remains visible.
Definition line_edit.hpp:28
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
Owning paint values; no theme engine or behavior is encoded here.
Definition controls.hpp:24
Owning arranged geometry; invalid geometry must never be used for hit-testing.
Definition widget_types.hpp:48
One immutable, validated single-line radio caption and its input eligibility.
Definition radio_group.hpp:31
GraphemeText text
Owning text measured under the receiving scene's policy.
Definition radio_group.hpp:32
bool operator==(const RadioItem &) const =default
bool visible
Hidden items occupy no row and reject input.
Definition radio_group.hpp:34
bool enabled
Disabled items retain their selected value but reject input.
Definition radio_group.hpp:33
bool enabled
User input eligibility; selection is preserved when disabled.
Definition radio_group.hpp:13
RadioOption(TextValue caption, bool enabled=true, bool visible=true)
Definition radio_group.hpp:22
TextValue text
Literal or resource key, resolved with the receiving scene policy.
Definition radio_group.hpp:12
bool visible
Whether the option occupies a row.
Definition radio_group.hpp:14
RadioOption(std::string_view caption, bool enabled=true, bool visible=true)
Definition radio_group.hpp:17
~RadioOption()=default
Release the cold owning expression; copies retain ordinary fallible value semantics.
Provider-assigned row identity, independent of position; increment generation on reuse.
Definition row_id.hpp:9
Owning independent policies for width and height.
Definition layout.hpp:35
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42