Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
shortcut.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/input.hpp>
6
7#include <initializer_list>
8#include <memory>
9#include <span>
10#include <utility>
11#include <vector>
12
13namespace portals {
32
36 std::size_t offset{};
40 bool operator==(const ShortcutError &other) const = default;
41};
42
45 std::size_t maxSourceBytes{4096};
46 std::size_t maxAlternatives{32};
47 std::size_t maxStrokes{8};
48};
49
52 bool repeat{};
53};
54
60
62 explicit ShortcutSequence(std::initializer_list<key::Chord> chords) : strokes(chords) {
63 }
64
66 ShortcutSequence(const ShortcutSequence &other) = default;
67
70 strokes.swap(other.strokes);
71 }
72
77
82 strokes.swap(other.strokes);
83
84 return *this;
85 }
86
87 std::vector<key::Chord> strokes;
88};
89
91struct Shortcut {
94 }
95
97 explicit Shortcut(std::initializer_list<key::Chord> chords) : alternatives(chords), sequences(0) {
98 }
99
101 Shortcut(const Shortcut &other) = default;
102
108 alternatives.swap(other.alternatives);
109 sequences.swap(other.sequences);
110 }
111
115 Shortcut &operator=(const Shortcut &other) = default;
116
120 Shortcut &operator=(Shortcut &&other) noexcept {
121 alternatives.swap(other.alternatives);
122 sequences.swap(other.sequences);
123
124 return *this;
125 }
126
127 std::vector<key::Chord> alternatives;
128 std::vector<ShortcutSequence> sequences;
129};
130
138std::expected<Shortcut, ShortcutError> parseShortcut(std::string_view source, ShortcutOptions options = {});
139
144std::expected<void, ShortcutError> validateShortcut(const Shortcut &shortcut, ShortcutOptions options = {}) noexcept;
145
153std::optional<KeyEvent> matchShortcut(const Shortcut &shortcut, const InputEvent &event,
154 ShortcutBehavior behavior = {}) noexcept;
155
156namespace detail {
158enum class ShortcutActions : unsigned char {
159 PRESS = 1,
160 REPEAT = 2,
161 RELEASE = 4,
162 PRESS_REPEAT = 3
163};
164struct KeySelectionAccess;
165} // namespace detail
166
171class KeySelection {
172private:
173 friend struct detail::KeySelectionAccess;
174
175 KeySelection(std::expected<std::shared_ptr<const Shortcut>, ShortcutError> shortcut, detail::ShortcutActions actions,
176 ShortcutOptions options) noexcept
177 : shortcut_(std::move(shortcut)), actions_(actions), options_(options) {
178 }
179
180 std::expected<std::shared_ptr<const Shortcut>, ShortcutError> shortcut_;
181 detail::ShortcutActions actions_;
182 ShortcutOptions options_;
183};
184
185namespace detail {
190std::optional<KeyEvent> shortcutEvent(const InputEvent &event, ShortcutActions actions) noexcept;
191
196bool shortcutChordMatches(key::Chord chord, const KeyEvent &event) noexcept;
197
202std::span<const key::Chord> shortcutAlternative(const Shortcut &shortcut, std::size_t index) noexcept;
203
209std::optional<KeyEvent> matchShortcutActions(const Shortcut &shortcut, const InputEvent &event,
210 ShortcutActions actions) noexcept;
211
219 static KeySelection make(std::expected<Shortcut, ShortcutError> &&shortcut, std::initializer_list<KeyAction> actions,
221
225 static const auto &shortcut(const KeySelection &selection) noexcept {
226 return selection.shortcut_;
227 }
228
232 static ShortcutActions actions(const KeySelection &selection) noexcept {
233 return selection.actions_;
234 }
235
239 static ShortcutOptions options(const KeySelection &selection) noexcept {
240 return selection.options_;
241 }
242};
243
248std::expected<Shortcut, ShortcutError> prepareShortcut(key::Chord chord, ShortcutOptions options = {});
253std::expected<Shortcut, ShortcutError> prepareShortcut(std::initializer_list<key::Chord> chords,
254 ShortcutOptions options = {});
255
260inline std::expected<Shortcut, ShortcutError> prepareShortcut(std::string_view source, ShortcutOptions options = {}) {
261 return parseShortcut(source, options);
262}
263
268std::expected<Shortcut, ShortcutError> prepareShortcut(const Shortcut &shortcut, ShortcutOptions options = {});
273std::expected<Shortcut, ShortcutError> prepareShortcut(Shortcut &&shortcut, ShortcutOptions options = {});
274
278std::expected<std::shared_ptr<Shortcut>, ShortcutError>
279retainShortcut(std::expected<Shortcut, ShortcutError> &&shortcut);
280
284template <class Source>
285concept ShortcutSource = requires(Source source) { prepareShortcut(std::move(source)); };
286} // namespace detail
287} // namespace portals
Copyable immutable owning shortcut selection, or a deferred typed construction error....
Definition shortcut.hpp:171
A shortcut value or string accepted by the checked owning normalizer.
Definition shortcut.hpp:285
Public Portals API.
Definition action.hpp:8
std::optional< KeyEvent > matchShortcut(const Shortcut &shortcut, const InputEvent &event, ShortcutBehavior behavior={}) noexcept
std::variant< KeyEvent, TextInputEvent, MouseInputEvent, PasteInputEvent, InputProbeCandidate, InputIssue > InputEvent
Independent owning result, usable beyond decoder reset/destruction.
Definition input.hpp:378
@ INVALID_OPTIONS
Runner options are invalid, or a native run was requested with an injected clock.
Definition application.hpp:31
@ NO_MEMORY
Allocation failed at a runner exception boundary.
Definition application.hpp:33
std::expected< void, ShortcutError > validateShortcut(const Shortcut &shortcut, ShortcutOptions options={}) noexcept
std::expected< Shortcut, ShortcutError > parseShortcut(std::string_view source, ShortcutOptions options={})
ShortcutErrorKind
Bounded shortcut syntax and value-validation failures, never borrowing source text.
Definition shortcut.hpp:15
@ INVALID_CHORD
Invalid semantic key/scalar combination.
Definition shortcut.hpp:26
@ STROKE_LIMIT
Too many key strokes in one alternative.
Definition shortcut.hpp:19
@ AMBIGUOUS_PREFIX
An alternative is a proper prefix of another in this registration.
Definition shortcut.hpp:29
@ ALTERNATIVE_LIMIT
Too many alternatives.
Definition shortcut.hpp:18
@ EMPTY_TOKEN
Missing token around a plus or comma separator.
Definition shortcut.hpp:21
@ DUPLICATE_MODIFIER
One semantic modifier occurs twice in a stroke.
Definition shortcut.hpp:23
@ EXPECTED_KEY
Modifiers were provided without a key.
Definition shortcut.hpp:24
@ UNKNOWN_KEY
Unsupported ASCII key/modifier name, including physical qualifiers.
Definition shortcut.hpp:22
@ INVALID_ACTION_SET
Empty action set or invalid action; offset identifies the action index.
Definition shortcut.hpp:28
@ RESERVED_CANCEL
Unmodified Escape is reserved for cancelling pending input.
Definition shortcut.hpp:30
@ EMPTY_ALTERNATIVE
Missing alternative before or after a separator.
Definition shortcut.hpp:20
@ UNSUPPORTED_SEQUENCE
Reserved legacy diagnostic; valid sequences are now supported.
Definition shortcut.hpp:25
@ SOURCE_LIMIT
Source bytes exceed the configured cap.
Definition scene_resource_error.hpp:16
Owning key value without an inferred physical keyboard scan code.
Definition input.hpp:213
Command admission independent of low-level KeyEvent binding behavior.
Definition shortcut.hpp:51
bool repeat
Explicitly admit native REPEAT events; PRESS is always admitted, RELEASE never is.
Definition shortcut.hpp:52
Allocation-free diagnostic independent of source and scene lifetime.
Definition shortcut.hpp:34
bool operator==(const ShortcutError &other) const =default
std::size_t offset
Zero-based byte offset; typed chord errors use their alternative index.
Definition shortcut.hpp:36
ShortcutErrorKind cause
Exact parser/value failure.
Definition shortcut.hpp:35
Configurable finite parser limits; every value must be positive.
Definition shortcut.hpp:44
std::size_t maxAlternatives
Maximum alternatives retained by one registration.
Definition shortcut.hpp:46
std::size_t maxSourceBytes
Maximum runtime source length in bytes.
Definition shortcut.hpp:45
std::size_t maxStrokes
Maximum strokes in one alternative.
Definition shortcut.hpp:47
ShortcutSequence & operator=(ShortcutSequence &&other) noexcept
Definition shortcut.hpp:81
ShortcutSequence(const ShortcutSequence &other)=default
ShortcutSequence(ShortcutSequence &&other)
Definition shortcut.hpp:69
std::vector< key::Chord > strokes
Owned ordered strokes with exact semantic modifiers.
Definition shortcut.hpp:87
ShortcutSequence()
Construct empty storage; debug iterator proxy allocation may throw.
Definition shortcut.hpp:58
ShortcutSequence & operator=(const ShortcutSequence &other)=default
ShortcutSequence(std::initializer_list< key::Chord > chords)
Definition shortcut.hpp:62
Owning shortcut alternatives; one registration invokes at most once per completed event.
Definition shortcut.hpp:91
std::vector< ShortcutSequence > sequences
Owned sequences sharing the alternative budget with single chords.
Definition shortcut.hpp:128
Shortcut()
Construct empty owning storage; debug-container allocation is allowed to throw.
Definition shortcut.hpp:93
std::vector< key::Chord > alternatives
Owned values, never views into a caller string or initializer list.
Definition shortcut.hpp:127
Shortcut(Shortcut &&other)
Definition shortcut.hpp:107
Shortcut(std::initializer_list< key::Chord > chords)
Definition shortcut.hpp:97
Shortcut(const Shortcut &other)=default
Shortcut & operator=(const Shortcut &other)=default
Shortcut & operator=(Shortcut &&other) noexcept
Definition shortcut.hpp:120
Internal bridge keeping selection payload immutable and checked.
Definition shortcut.hpp:213
static ShortcutActions actions(const KeySelection &selection) noexcept
Definition shortcut.hpp:232
static ShortcutOptions options(const KeySelection &selection) noexcept
Definition shortcut.hpp:239
static const auto & shortcut(const KeySelection &selection) noexcept
Definition shortcut.hpp:225
static KeySelection make(std::expected< Shortcut, ShortcutError > &&shortcut, std::initializer_list< KeyAction > actions, ShortcutOptions options)
Compile-time semantic shortcut; letters match ASCII case with exact modifiers.
Definition input.hpp:96