FTXUI 7.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_COMPONENT_EVENT_HPP
5#define FTXUI_COMPONENT_EVENT_HPP
6
7#include <cstdint>
8#include <ftxui/component/mouse.hpp> // for Mouse
9#include <memory>
10#include <string> // for string, operator==
11#include <string_view>
12#include <vector>
13
14#include "ftxui/util/export.hpp"
15
16namespace ftxui {
17
18class App;
19class ComponentBase;
20
21/// @brief Represent an event. It can be key press event, a terminal resize, or
22/// more ...
23///
24/// For example:
25/// - Printable character can be created using Event::Character('a').
26/// - Some special are predefined, like Event::ArrowLeft.
27/// - One can find arbitrary code for special Events using:
28/// ./example/util/print_key_press
29/// For instance, CTLR+A maps to Event::Special({1});
30///
31/// Useful documentation about xterm specification:
32/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
33///
34/// @ingroup component
35struct FTXUI_EXPORT(COMPONENT) Event {
36 // --- Constructor section ---------------------------------------------------
37 static Event Character(std::string_view);
38 static Event Character(char);
39 static Event Character(wchar_t);
40 static Event Special(std::string_view);
41 static Event Special(std::initializer_list<char>);
42 static Event Mouse(std::string_view, Mouse mouse);
43 static Event CursorPosition(std::string_view, int x, int y); // Internal
44 static Event CursorShape(std::string_view, int shape); // Internal
45 static Event TerminalNameVersion(std::string_view,
46 std::string name,
47 int version);
48 static Event TerminalEmulator(std::string_view,
49 std::string name,
50 std::string version);
51 static Event TerminalCapabilities(std::string_view,
52 std::vector<int> capabilities);
53
54 // --- Arrow ---
55 static const Event ArrowLeft;
56 static const Event ArrowRight;
57 static const Event ArrowUp;
58 static const Event ArrowDown;
59
60 static const Event ArrowLeftCtrl;
61 static const Event ArrowRightCtrl;
62 static const Event ArrowUpCtrl;
63 static const Event ArrowDownCtrl;
64
65 // --- Other ---
66 static const Event Backspace;
67 static const Event Delete;
68 static const Event Return;
69 static const Event Escape;
70 static const Event Tab;
71 static const Event TabReverse;
72
73 // --- Navigation keys ---
74 static const Event Insert;
75 static const Event Home;
76 static const Event End;
77 static const Event PageUp;
78 static const Event PageDown;
79
80 // --- Function keys ---
81 static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
82
83 // --- Control keys ---
84 static const Event a, A, CtrlA, AltA, CtrlAltA;
85 static const Event b, B, CtrlB, AltB, CtrlAltB;
86 static const Event c, C, CtrlC, AltC, CtrlAltC;
87 static const Event d, D, CtrlD, AltD, CtrlAltD;
88 static const Event e, E, CtrlE, AltE, CtrlAltE;
89 static const Event f, F, CtrlF, AltF, CtrlAltF;
90 static const Event g, G, CtrlG, AltG, CtrlAltG;
91 static const Event h, H, CtrlH, AltH, CtrlAltH;
92 static const Event i, I, CtrlI, AltI, CtrlAltI;
93 static const Event j, J, CtrlJ, AltJ, CtrlAltJ;
94 static const Event k, K, CtrlK, AltK, CtrlAltK;
95 static const Event l, L, CtrlL, AltL, CtrlAltL;
96 static const Event m, M, CtrlM, AltM, CtrlAltM;
97 static const Event n, N, CtrlN, AltN, CtrlAltN;
98 static const Event o, O, CtrlO, AltO, CtrlAltO;
99 static const Event p, P, CtrlP, AltP, CtrlAltP;
100 static const Event q, Q, CtrlQ, AltQ, CtrlAltQ;
101 static const Event r, R, CtrlR, AltR, CtrlAltR;
102 static const Event s, S, CtrlS, AltS, CtrlAltS;
103 static const Event t, T, CtrlT, AltT, CtrlAltT;
104 static const Event u, U, CtrlU, AltU, CtrlAltU;
105 static const Event v, V, CtrlV, AltV, CtrlAltV;
106 static const Event w, W, CtrlW, AltW, CtrlAltW;
107 static const Event x, X, CtrlX, AltX, CtrlAltX;
108 static const Event y, Y, CtrlY, AltY, CtrlAltY;
109 static const Event z, Z, CtrlZ, AltZ, CtrlAltZ;
110
111 // --- Custom ---
112 static const Event Custom;
113
114 //--- Method section ---------------------------------------------------------
115 bool operator==(const Event& other) const { return input_ == other.input_; }
116 bool operator!=(const Event& other) const { return !operator==(other); }
117 bool operator<(const Event& other) const { return input_ < other.input_; }
118
119 const std::string& input() const { return input_; }
120
121 bool is_character() const { return type_ == Type::Character; }
122 std::string character() const { return input_; }
123
124 bool is_mouse() const { return type_ == Type::Mouse; }
125 struct Mouse& mouse() { return data_.mouse; }
126
127 // --- Internal Method section -----------------------------------------------
128 bool is_cursor_position() const { return type_ == Type::CursorPosition; }
129 int cursor_x() const { return data_.cursor.x; }
130 int cursor_y() const { return data_.cursor.y; }
131
132 bool is_cursor_shape() const { return type_ == Type::CursorShape; }
133 int cursor_shape() const { return data_.cursor_shape; }
134
135 bool IsTerminalNameVersion() const;
136 const std::string& TerminalName() const;
137 int TerminalVersion() const;
138
139 bool IsTerminalEmulator() const;
140 const std::string& TerminalEmulatorName() const;
141 const std::string& TerminalEmulatorVersion() const;
142
143 bool IsTerminalCapabilities() const;
144 const std::vector<int>& TerminalCapabilities() const;
145 std::vector<std::string> TerminalCapabilityNames() const;
146
147 // Debug
148 std::string DebugString() const;
149
150 //--- State section ----------------------------------------------------------
151 App* screen_ = nullptr;
152
153 private:
154 friend ComponentBase;
155 friend App;
156 enum class Type : uint8_t {
157 Unknown,
158 Character,
159 Mouse,
160 CursorPosition,
161 CursorShape,
162 TerminalNameVersion,
163 TerminalEmulator,
164 TerminalCapabilities,
165 };
166 Type type_ = Type::Unknown;
167 struct Cursor {
168 int x = 0;
169 int y = 0;
170 };
171
172 union {
173 struct Mouse mouse;
174 struct Cursor cursor;
175 int cursor_shape;
176 int terminal_version;
177 } data_ = {};
178
179 std::string input_;
180 std::shared_ptr<std::string> terminal_name_;
181 std::shared_ptr<std::string> terminal_emulator_version_;
182 std::shared_ptr<std::vector<int>> terminal_capabilities_;
183};
184
185} // namespace ftxui
186
187#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
int y
Definition elements.hpp:127