Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
key_binding.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <initializer_list>
6#include <portals/managed_diagnostics.hpp>
7#include <portals/shortcut.hpp>
8#include <portals/text_value.hpp>
9#include <portals/widget.hpp>
10#include <thread>
11
12namespace portals {
13class ManagedScene;
14class KeyBinding;
15
16namespace ui {
17class KeyInfo;
18class KeyBindingRef;
19} // namespace ui
20
21namespace detail {
22struct KeyBindingSlot;
23struct ActionCancellation;
24} // namespace detail
25
28public:
30 KeyBindingId() = default;
32 bool operator==(const KeyBindingId &other) const = default;
33
34private:
35 friend class ManagedScene;
36 NodeId scene_{};
37 std::uint64_t serial_{};
38};
39
46
55
57using KeyTextResolver = std::function<std::expected<std::string, TextResourceError>(std::string_view)>;
58
60struct KeyInfo {
62 explicit KeyInfo(TextValue value) : label(std::move(value)) {
63 }
64
66 TextValue description{std::string_view{}};
70 bool enabled{true};
71 bool visible{true};
72private:
73 friend class ManagedScene;
74 friend class ui::KeyInfo;
75 std::shared_ptr<detail::KeyBindingSlot> publication_;
76 std::weak_ptr<const void> scopeIdentity_;
77 std::shared_ptr<detail::ActionCancellation> ownerLifetime_;
78};
79
91
97
101 ~KeyConflict() = default;
102 std::size_t alternative{};
103 std::size_t otherOrdinal{};
104 std::optional<KeyBindingId> otherId{};
105 std::size_t otherAlternative{};
107 std::size_t sharedStrokes{};
108 bool thisIsNewer{};
111 std::vector<KeyAction> overlapPhases = std::vector<KeyAction>(0);
112};
113
118 : label(std::move(caption)), description(std::move(detail)) {
119 }
120
122 ~KeyBindingView() = default;
123 std::optional<KeyBindingId> id{};
124 std::size_t ordinal{};
127 std::optional<KeyEvent> legacy{};
128 std::optional<Shortcut> shortcut{};
129 std::vector<KeyAction> actions = std::vector<KeyAction>(0);
133 std::vector<KeyConflict> conflicts = std::vector<KeyConflict>(0);
134};
135
140 std::uint64_t revision{};
141 std::uint64_t focusGeneration{};
142 std::uint64_t inputGeneration{};
143 std::uint64_t scopeGeneration{};
145 std::vector<KeyBindingView> bindings =
146 std::vector<KeyBindingView>(std::initializer_list<KeyBindingView>{});
147};
148
151public:
153 KeyBinding() = default;
154
156 KeyBindingId id() const noexcept {
157 return id_;
158 }
159
161 std::expected<void, ManagedError> setEnabled(bool value) const;
163 std::expected<void, ManagedError> setVisible(bool value) const;
166 std::expected<void, ManagedError> setText(TextValue label, TextValue description,
167 KeyTextResolver resolver = {}) const;
169 std::expected<void, ManagedError> rebind(const KeySelection &selection) const;
171 std::expected<void, ManagedError> unbind() const;
173 std::expected<void, ManagedError> remove() const;
174
175private:
176 friend class ManagedScene;
177 friend class ui::KeyBindingRef;
178 std::expected<ManagedScene *, ManagedError> scene() const;
179 ManagedScene *scene_{};
180 std::weak_ptr<const void> lifetime_;
181 std::thread::id owner_{};
182 KeyBindingId id_;
183};
184
185namespace detail {
187struct KeyBindingSlot {
188 const std::thread::id owner = std::this_thread::get_id();
189 std::optional<KeyBinding> binding;
190};
191
193} // namespace detail
194
195namespace ui {
199public:
201 KeyBindingRef() : slot_(std::make_shared<portals::detail::KeyBindingSlot>()) {
202 }
203
205 std::expected<KeyBindingId, ManagedError> id() const;
207 std::expected<void, ManagedError> setEnabled(bool value,
208 std::source_location origin = std::source_location::current()) const;
210 std::expected<void, ManagedError> setVisible(bool value,
211 std::source_location origin = std::source_location::current()) const;
214 std::expected<void, ManagedError> setText(TextValue label, TextValue description, KeyTextResolver resolver = {},
215 std::source_location origin = std::source_location::current()) const;
217 std::expected<void, ManagedError> rebind(const KeySelection &selection,
218 std::source_location origin = std::source_location::current()) const;
220 std::expected<void, ManagedError> unbind(std::source_location origin = std::source_location::current()) const;
222 std::expected<void, ManagedError> remove(std::source_location origin = std::source_location::current()) const;
223
224private:
225 friend class KeyInfo;
226 std::expected<KeyBinding, ManagedError> binding() const;
227 std::shared_ptr<portals::detail::KeyBindingSlot> slot_;
228};
229} // namespace ui
230} // namespace portals
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Scene-local, never reused registration identity; not a persistence key.
Definition key_binding.hpp:27
KeyBindingId()=default
Construct an invalid identity.
bool operator==(const KeyBindingId &other) const =default
Weak UI-affine command observation; destruction never unregisters the command.
Definition key_binding.hpp:150
std::expected< void, ManagedError > setVisible(bool value) const
std::expected< void, ManagedError > setEnabled(bool value) const
std::expected< void, ManagedError > unbind() const
KeyBinding()=default
Construct an empty, allocation-free observation.
KeyBindingId id() const noexcept
Definition key_binding.hpp:156
std::expected< void, ManagedError > setText(TextValue label, TextValue description, KeyTextResolver resolver={}) const
std::expected< void, ManagedError > remove() const
std::expected< void, ManagedError > rebind(const KeySelection &selection) const
Copyable immutable owning shortcut selection, or a deferred typed construction error....
Definition shortcut.hpp:171
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
Deferred observing slot, published only by a complete successful registration. Construction may alloc...
Definition key_binding.hpp:198
KeyBindingRef()
Create an unpublished slot; may throw std::bad_alloc.
Definition key_binding.hpp:201
std::expected< void, ManagedError > setText(TextValue label, TextValue description, KeyTextResolver resolver={}, std::source_location origin=std::source_location::current()) const
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
std::expected< KeyBindingId, ManagedError > id() const
std::expected< void, ManagedError > unbind(std::source_location origin=std::source_location::current()) const
std::expected< void, ManagedError > setVisible(bool value, std::source_location origin=std::source_location::current()) const
std::expected< void, ManagedError > remove(std::source_location origin=std::source_location::current()) const
Consuming metadata for one canonical annotated registration; refs resolve after complete mount.
Definition ui_key_info.hpp:10
Public Portals API.
Definition action.hpp:8
@ DISABLED
Recording is stopped; retained evidence remains available.
Definition diagnostics.hpp:72
std::function< std::expected< std::string, TextResourceError >(std::string_view)> KeyTextResolver
Same exact resolver type as ui::TextResolver; used only during checked normalization.
Definition key_binding.hpp:57
KeyEligibility
Eligibility observed for one immutable snapshot, not a universal conflict winner.
Definition key_binding.hpp:81
@ HIDDEN
Explicitly hidden from routing and projections.
Definition key_binding.hpp:84
@ UNBOUND
Identity retained without any key alternatives.
Definition key_binding.hpp:85
@ SCOPE_INACTIVE
Original scope is disconnected, ineligible or outside current focus.
Definition key_binding.hpp:88
@ TERMINAL_UNSUPPORTED
Every annotated phase is explicitly unsupported.
Definition key_binding.hpp:89
@ ELIGIBLE
Eligible for matching; conflict winner still depends on the event.
Definition key_binding.hpp:82
@ OWNER_INELIGIBLE
Live owner or ancestor is hidden or disabled.
Definition key_binding.hpp:86
@ OWNER_RETIRED
Original generational owner no longer exists.
Definition key_binding.hpp:87
KeyConflictKind
Conditional alternative overlap.
Definition key_binding.hpp:92
@ TERMINAL_PREFIX
One alternative ends while the other continues.
Definition key_binding.hpp:94
@ SHARED_PREFIX
Alternatives compete initially and diverge later.
Definition key_binding.hpp:95
@ UNKNOWN
EOF proved that the line does not exist.
Definition document_source.hpp:368
KeySupport
Explicit terminal phase knowledge; UNKNOWN does not suppress routing.
Definition key_binding.hpp:41
@ SUPPORTED
The application declares this native phase supported.
Definition key_binding.hpp:43
@ UNSUPPORTED
The application explicitly excludes this annotated phase.
Definition key_binding.hpp:44
@ UNKNOWN
No explicit support claim; annotated routing remains enabled.
Definition key_binding.hpp:42
NodeId owner
Diagnostic identity only.
Definition key_binding.hpp:130
KeyBindingView(GraphemeText caption, GraphemeText detail)
Definition key_binding.hpp:117
GraphemeText description
Independently owned text.
Definition key_binding.hpp:126
~KeyBindingView()=default
Release independently owned text, alternatives and relations.
GraphemeText label
Empty for unannotated legacy bindings.
Definition key_binding.hpp:125
std::optional< Shortcut > shortcut
Owning normalized alternatives, including sequences.
Definition key_binding.hpp:128
std::vector< KeyConflict > conflicts
Conditional alternative relations.
Definition key_binding.hpp:133
std::optional< KeyEvent > legacy
Original exact legacy key, if any.
Definition key_binding.hpp:127
std::vector< KeyAction > actions
Native admitted phases.
Definition key_binding.hpp:129
KeyEligibility eligibility
Observed admission reason.
Definition key_binding.hpp:132
NodeId scope
Diagnostic identity only.
Definition key_binding.hpp:131
std::size_t ordinal
Display-local registration position.
Definition key_binding.hpp:124
Independent historical read snapshot; scope disconnect is observed on the next UI admission/query.
Definition key_binding.hpp:137
std::vector< KeyBindingView > bindings
Owning rows in registration order.
Definition key_binding.hpp:145
std::uint64_t scopeGeneration
Exact cancellation-identity observation revision.
Definition key_binding.hpp:143
~KeyBindingsSnapshot()=default
Release this historical owning projection.
std::uint64_t revision
Canonical metadata revision.
Definition key_binding.hpp:140
KeyCapabilities capabilities
Explicit phase knowledge.
Definition key_binding.hpp:144
std::uint64_t inputGeneration
Tree eligibility observation.
Definition key_binding.hpp:142
std::uint64_t focusGeneration
Recovered focus observation.
Definition key_binding.hpp:141
Application-supplied native phase support, independent of physical-key identity.
Definition key_binding.hpp:48
KeySupport press
Native PRESS support.
Definition key_binding.hpp:49
KeySupport release
Native RELEASE support.
Definition key_binding.hpp:51
bool operator==(const KeyCapabilities &other) const =default
KeySupport repeat
Native REPEAT support.
Definition key_binding.hpp:50
One bounded pair relation; ordinals refer only to its owning snapshot.
Definition key_binding.hpp:99
std::size_t alternative
This alternative's index.
Definition key_binding.hpp:102
std::size_t sharedStrokes
Number of common leading strokes.
Definition key_binding.hpp:107
std::vector< KeyAction > overlapPhases
Actual overlapping native phases.
Definition key_binding.hpp:111
std::optional< KeyBindingId > otherId
Absent for legacy registrations.
Definition key_binding.hpp:104
std::size_t otherOrdinal
Other row's snapshot ordinal.
Definition key_binding.hpp:103
bool thisIsNewer
Registration priority, independent of scope depth.
Definition key_binding.hpp:108
~KeyConflict()=default
Release this independent owning relation.
std::size_t otherAlternative
Other alternative's index.
Definition key_binding.hpp:105
bool thisTerminalAtPrefix
This alternative ends at the common prefix.
Definition key_binding.hpp:109
bool otherTerminalAtPrefix
Other alternative ends at the common prefix.
Definition key_binding.hpp:110
KeyConflictKind kind
Exact, terminal prefix or shared-prefix competition.
Definition key_binding.hpp:106
TextValue label
Required resolved single-line label.
Definition key_binding.hpp:65
TextValue description
Optional owning explanation.
Definition key_binding.hpp:66
KeyTextResolver resolver
Not retained after successful normalization.
Definition key_binding.hpp:67
KeyInfo(TextValue value)
Definition key_binding.hpp:62
bool visible
Hidden commands neither route nor appear in projections.
Definition key_binding.hpp:71
Node< Widget > owner
Empty selects the scene root.
Definition key_binding.hpp:68
Node< Widget > scope
Empty is global; otherwise an exact declared focus scope.
Definition key_binding.hpp:69
bool enabled
Disabled commands remain visible in full help.
Definition key_binding.hpp:70
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15