5#include <portals/input.hpp>
6#include <portals/widget.hpp>
47using RoutedEvent = std::variant<ActionEvent, KeyEvent, TextInputEvent, PasteInputEvent, MouseInputEvent>;
81struct ActionRegistration;
82struct ActionCancellation;
83struct FocusScopeRegistration;
84struct PointerCaptureCancellation;
85struct PointerCaptureAccess;
86struct EventRouterAccess;
117 friend class EventRouter;
118 explicit
ActionBinding(std::weak_ptr<detail::ActionCancellation> registration);
119 friend struct detail::EventRouterAccess;
120 std::weak_ptr<detail::ActionCancellation> registration_;
155 friend class EventRouter;
156 friend struct detail::PointerCaptureAccess;
157 explicit
PointerCapture(std::weak_ptr<detail::PointerCaptureCancellation> cancellation) noexcept;
158 std::weak_ptr<detail::PointerCaptureCancellation> cancellation_;
180 EventRouter(
const EventRouter &) =
delete;
181 EventRouter &operator=(
const EventRouter &) =
delete;
233 template <
class T,
class Callback>
234 std::expected<ActionBinding, TreeError>
bind(
Node<T> node, Callback &&callback,
bool focusable =
true) {
235 return bind(node, std::forward<Callback>(callback), std::function<std::expected<void, TreeError>(T &,
bool)>{},
253 template <
class T,
class Callback,
class FocusCallback>
254 requires std::invocable<FocusCallback &, T &, bool>
255 std::expected<ActionBinding, TreeError>
bind(
Node<T> node, Callback &&callback, FocusCallback &&focusCallback,
256 bool focusable =
true) {
257 static_assert(std::same_as<std::invoke_result_t<Callback &, T &, const RoutedEvent &>,
EventReply>);
258 static_assert(std::same_as<std::invoke_result_t<FocusCallback &, T &, bool>, std::expected<void, TreeError>>);
260 auto allowed = canBind(node);
263 return std::unexpected(allowed.error());
267 std::function<std::expected<void, TreeError>(T &,
bool)> focus(std::forward<FocusCallback>(focusCallback));
268 std::function<std::expected<void, TreeError>(
bool)> focusDelivery;
271 focusDelivery = [
this, node, function = std::move(focus)](
bool focused) {
272 std::expected<void, TreeError> outcome;
273 auto status = tree().edit(node, [&](T &widget) {
274 outcome = function(widget,
focused);
277 return status ? outcome : status;
283 [
this, node, function = std::forward<Callback>(callback)](
284 const RoutedEvent &event)
mutable -> std::expected<EventReply, TreeError> {
286 auto status = tree().edit(node, [&](T &widget) {
287 reply = std::invoke(function, widget, event);
291 return std::unexpected(status.error());
296 focusable, std::move(focusDelivery));
297 }
catch (
const std::bad_alloc &) {
393 friend class ManagedScene;
394 friend struct detail::RowTreeAccess;
395 friend struct detail::EventRouterAccess;
396 std::uint64_t focusGeneration() const noexcept;
398 explicit EventRouter(std::unique_ptr<State> state);
400 std::expected<
void,
TreeError> check() const;
404 bool focusable, std::function<std::expected<
void,
TreeError>(
bool)> focusCallback);
405 std::expected<
void,
TreeError> collect();
406 std::shared_ptr<detail::ActionRegistration> registration(
Node<
Widget> node) const;
408 std::shared_ptr<detail::DialogRecord> dialogLayer(Node<Widget> node)
const;
409 Node<Widget> modalRoot(Node<Widget> exclude = {})
const;
410 bool dialogEligible(Node<Widget> node, Node<Widget> reveal = {}, Node<Widget> exclude = {})
const;
411 bool notificationHit(Node<Widget> node)
const;
412 std::expected<void, TreeError> stageFocus(Node<Widget> node, detail::FocusChange &change);
413 void publishFocus(detail::FocusChange &change)
noexcept;
414 std::expected<void, TreeError> notifyFocus(
const detail::FocusChange &change);
415 detail::FocusScopeRegistration *scope(Node<Widget> root)
const;
416 bool within(Node<Widget> root, Node<Widget> node)
const;
417 Node<Widget> scopeEntry(Node<Widget> root,
const std::vector<Node<Widget>> &order,
bool reverse)
const;
418 Node<Widget> entering(Node<Widget> node, Node<Widget> previous,
const std::vector<Node<Widget>> &order,
420 bool endedFocusScope(std::size_t index)
const;
421 std::expected<Node<Widget>,
TreeError> returnFocusScopes(std::size_t first);
422 std::expected<bool, TreeError> recoverFocusScopes();
423 Node<Widget> fallbackFocus(
const std::vector<Node<Widget>> &scopes,
const std::vector<Node<Widget>> &order)
const;
424 std::expected<void, TreeError> changeFocus(Node<Widget> node);
425 std::expected<void, TreeError> deliverFocus(Node<Widget> node,
bool focused);
426 std::expected<Node<Widget>,
TreeError> checkedFocus();
427 std::expected<DispatchResult, TreeError> route(Node<Widget> target,
const RoutedEvent &event,
bool physical =
false);
428 std::expected<EventReply, TreeError> deliver(std::shared_ptr<detail::ActionRegistration> entry,
430 std::expected<Node<Widget>,
TreeError> checkedPointerCapture();
431 std::expected<void, TreeError> retirePointerCapture(
bool requestPaint);
432 std::expected<DispatchResult, TreeError> routeCaptured(
const MouseInputEvent &event);
433 std::unique_ptr<State> state_;
Move-only scoped disconnect token; never owns a tree or widget. Disconnect/destruction may run on any...
Definition action.hpp:96
void disconnect() noexcept
Idempotent; active calls retain their captures until return.
ActionBinding()=default
Empty disconnected token.
~ActionBinding()
Disconnect without invoking user code.
ActionBinding & operator=(ActionBinding &&other) noexcept
Disconnect old registration, then transfer.
ActionBinding(ActionBinding &&other) noexcept
Transfer disconnect responsibility.
std::expected< ActionBinding, TreeError > bind(Node< T > node, Callback &&callback, bool focusable=true)
Register one typed handler; the scoped token owns disconnect responsibility. Callback must be copy-co...
Definition action.hpp:234
std::expected< Node< Widget >, TreeError > leaveFocusScope(Node< Widget > root)
Leave the top scope and restore eligible saved focus, scope fallback or global fallback....
std::expected< void, TreeError > setPointerCaptureCapabilities(PointerCaptureCapabilities capabilities)
Declare host motion/release guarantees, without terminal I/O.
std::expected< DispatchResult, TreeError > dispatch(const InputEvent &event)
Deliver decoded input synchronously; replies/issues are ignored. Unmodified Tab/Shift+Tab traverse....
~EventRouter()
Disconnect and reclaim all captures on UI; tree must still exist.
std::expected< Node< Widget >, TreeError > enterScope(Node< Widget > root)
Enter a declared scope at its eligible last/default/preorder target.
std::expected< Node< Widget >, TreeError > pointerCapture()
Revalidate action identity and visible/enabled ancestry without consulting geometry.
std::expected< ActionBinding, TreeError > bind(Node< T > node, Callback &&callback, FocusCallback &&focusCallback, bool focusable=true)
Register input and optional synchronous focus delivery in one scoped registration....
Definition action.hpp:255
std::expected< void, TreeError > cancelPointerCapture(bool requestPaint=true)
Explicitly end capture for host resize, stop, capability loss or application policy.
std::expected< void, TreeError > focusDefault(Node< Widget > node)
Set the default of the nearest live enclosing scope without changing current focus.
std::expected< Node< Widget >, TreeError > traverse(bool reverse=false)
Traverse eligible registrations in tree preorder with wrap; reverse implements Shift+Tab....
std::expected< DispatchResult, TreeError > invoke(Node< Widget > target, ActionEvent action)
Invoke the same handler path with a semantic command; target need not be focusable....
std::expected< ActionBinding, TreeError > focusScope(Node< Widget > root, Node< Widget > defaultTarget={})
Declare a nested focus scope using an existing owning disconnect token. Scope and action records shar...
std::expected< Node< Widget >, TreeError > enterFocusScope(Node< Widget > root)
Enter a scope while saving exact focus and ancestry for a later return. This focus-only stack does no...
std::expected< PointerCapture, TreeError > capturePointer(Node< Widget > owner, Node< Widget > focusRoot={})
Capture LEFT continuation from this owner's currently admitted LEFT PRESS handler.
std::expected< void, TreeError > setFocus(Node< Widget > node)
Focus a live eligible registered node, or return INVALID_ARGUMENT; requests paint.
std::expected< Node< Widget >, TreeError > hitTest(CellPoint point)
Hit target in paint order; descendants and later sibling subtrees take precedence....
static std::expected< std::unique_ptr< EventRouter >, TreeError > create(WidgetTree &tree, EventRouterOptions options={})
Validate the tree's owner thread and create a bounded router.
std::expected< Node< Widget >, TreeError > focused()
Recover focus if needed; empty Node means no eligible focusable registration. Recovery tries containi...
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
PointerCapture(PointerCapture &&other) noexcept
Transfer cancellation responsibility and empty the source.
bool connected() const noexcept
Observe cancellation flags without consulting live UI/tree eligibility.
PointerCapture()=default
Construct an empty disconnected token.
void disconnect() noexcept
Idempotently deny future admission, without cancelling an already admitted call.
PointerCapture & operator=(PointerCapture &&other) noexcept
Disconnect the old token, then transfer; self-assignment is a no-op.
~PointerCapture()
Disconnect without user callbacks or tree access, on any thread.
Public Portals API.
Definition action.hpp:8
DispatchStatus
Distinguish a miss/ineligible target from an eligible unhandled event.
Definition action.hpp:55
@ NO_TARGET
No eligible destination was selected.
Definition action.hpp:56
@ UNHANDLED
An eligible destination existed but no handler consumed the event.
Definition action.hpp:57
Action
Frontend-independent commands; text and paste remain distinct data events.
Definition action.hpp:10
@ CANCEL
Cancel the current interaction.
Definition action.hpp:12
@ ERASE_BACKWARD
Erase before the current position.
Definition action.hpp:19
@ MOVE_RIGHT
Move right according to the receiving widget.
Definition action.hpp:14
@ MOVE_DOWN
Move down according to the receiving widget.
Definition action.hpp:16
@ ACTIVATE
Activate the target's primary command.
Definition action.hpp:11
@ MOVE_HOME
Move to the beginning according to the receiving widget.
Definition action.hpp:17
@ MOVE_END
Move to the end according to the receiving widget.
Definition action.hpp:18
@ ERASE_FORWARD
Erase after the current position.
Definition action.hpp:20
@ MOVE_LEFT
Move left according to the receiving widget.
Definition action.hpp:13
@ MOVE_UP
Move up according to the receiving widget.
Definition action.hpp:15
KeyAction
Native adapters may report repeat/release; VT emits press only.
Definition input.hpp:206
@ PRESS
Key press; the only action emitted by the VT decoder.
Definition input.hpp:207
std::variant< KeyEvent, TextInputEvent, MouseInputEvent, PasteInputEvent, InputProbeCandidate, InputIssue > InputEvent
Independent owning result, usable beyond decoder reset/destruction.
Definition input.hpp:378
EventReply
Explicit bubbling decision; ignored continues toward the original root.
Definition action.hpp:49
@ IGNORED
Continue bubbling along the original ancestry.
Definition action.hpp:50
@ HANDLED
Stop bubbling; this registration consumed the event.
Definition action.hpp:51
std::variant< ActionEvent, KeyEvent, TextInputEvent, PasteInputEvent, MouseInputEvent > RoutedEvent
Synchronous input value; callback borrows it only until return.
Definition action.hpp:47
ClickSource
Origin of an admitted activation, without inferred physical input.
Definition action.hpp:24
@ MOUSE
Mouse input supplied by the existing router.
Definition action.hpp:27
@ PROGRAMMATIC
Explicit semantic action, the default for caller-created ActionEvent.
Definition action.hpp:25
@ KEYBOARD
Keyboard input, including a key translated into a semantic action.
Definition action.hpp:26
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
@ CALLBACK_FAILED
A user extension or callback threw.
Definition widget_types.hpp:37
@ NO_MEMORY
Allocation failed inside the operation boundary.
Definition widget_types.hpp:33
Owning semantic command, usable without terminal input.
Definition action.hpp:31
KeyAction keyAction
Observed keyboard phase; independent of semantic action equality.
Definition action.hpp:35
constexpr bool operator==(const ActionEvent &other) const noexcept
Compare semantic command intent, preserving the original equality contract.
Definition action.hpp:41
Action action
Command identity.
Definition action.hpp:32
ClickSource source
Preserved origin when translating keyboard input.
Definition action.hpp:34
InputModifiers modifiers
Selection and other modifier intent.
Definition action.hpp:33
An owning value in terminal columns/rows, x right and y down.
Definition geometry.hpp:24
Owning routing outcome; empty identities mean no eligible target/handler.
Definition action.hpp:62
DispatchStatus status
Delivery outcome; traversal may handle without a handler.
Definition action.hpp:65
NodeId handler
Node that consumed the event, possibly removed during delivery.
Definition action.hpp:64
NodeId target
Initial target, possibly removed during delivery.
Definition action.hpp:63
Bounded router storage; no event queue is created.
Definition action.hpp:69
std::size_t max_registrations
Positive shared maximum of action and scope records; one of each per node.
Definition action.hpp:70
Owning identity, independent of addresses; zero is never a live identity.
Definition widget_types.hpp:15
Host-declared input guarantees; this value does not enable terminal reporting modes.
Definition action.hpp:74
bool motion
Host supplies pointer motion while dragging.
Definition action.hpp:75
bool motionButtonIdentity
MOVE button reliably reports held LEFT versus no/different button.
Definition action.hpp:77
bool release
Host supplies matching LEFT release or explicitly cancels a lost sequence.
Definition action.hpp:76