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