FTXUI  5.0.0
C++ functional terminal UI.
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 <functional>
9 #include <string> // for string, operator==
10 #include <vector>
11 
12 namespace ftxui {
13 
14 class ScreenInteractive;
15 class ComponentBase;
16 
17 /// @brief Represent an event. It can be key press event, a terminal resize, or
18 /// more ...
19 ///
20 /// For example:
21 /// - Printable character can be created using Event::Character('a').
22 /// - Some special are predefined, like Event::ArrowLeft.
23 /// - One can find arbitrary code for special Events using:
24 /// ./example/util/print_key_press
25 /// For instance, CTLR+A maps to Event::Special({1});
26 ///
27 /// Useful documentation about xterm specification:
28 /// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
29 struct Event {
30  // --- Constructor section ---------------------------------------------------
31  static Event Character(std::string);
32  static Event Character(char);
33  static Event Character(wchar_t);
34  static Event Special(std::string);
35  static Event Mouse(std::string, Mouse mouse);
36  static Event CursorPosition(std::string, int x, int y); // Internal
37  static Event CursorShape(std::string, int shape); // Internal
38 
39  // --- Arrow ---
40  static const Event ArrowLeft;
41  static const Event ArrowRight;
42  static const Event ArrowUp;
43  static const Event ArrowDown;
44 
45  static const Event ArrowLeftCtrl;
46  static const Event ArrowRightCtrl;
47  static const Event ArrowUpCtrl;
48  static const Event ArrowDownCtrl;
49 
50  // --- Other ---
51  static const Event Backspace;
52  static const Event Delete;
53  static const Event Return;
54  static const Event Escape;
55  static const Event Tab;
56  static const Event TabReverse;
57  static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
58 
59  static const Event Insert;
60  static const Event Home;
61  static const Event End;
62 
63  static const Event PageUp;
64  static const Event PageDown;
65 
66  // --- Custom ---
67  static const Event Custom;
68 
69  //--- Method section ---------------------------------------------------------
70  bool operator==(const Event& other) const { return input_ == other.input_; }
71  bool operator!=(const Event& other) const { return !operator==(other); }
72 
73  const std::string& input() const { return input_; }
74 
75  bool is_character() const { return type_ == Type::Character; }
76  std::string character() const { return input_; }
77 
78  bool is_mouse() const { return type_ == Type::Mouse; }
79  struct Mouse& mouse() { return data_.mouse; }
80 
81  // --- Internal Method section -----------------------------------------------
82  bool is_cursor_position() const { return type_ == Type::CursorPosition; }
83  int cursor_x() const { return data_.cursor.x; }
84  int cursor_y() const { return data_.cursor.y; }
85 
86  bool is_cursor_shape() const { return type_ == Type::CursorShape; }
87  int cursor_shape() const { return data_.cursor_shape; }
88 
89  //--- State section ----------------------------------------------------------
91 
92  private:
93  friend ComponentBase;
94  friend ScreenInteractive;
95  enum class Type {
96  Unknown,
97  Character,
98  Mouse,
99  CursorPosition,
100  CursorShape,
101  };
102  Type type_ = Type::Unknown;
103 
104  struct Cursor {
105  int x = 0;
106  int y = 0;
107  };
108 
109  union {
110  struct Mouse mouse;
111  struct Cursor cursor;
112  int cursor_shape;
113  } data_ = {};
114 
115  std::string input_;
116 };
117 
118 } // namespace ftxui
119 
120 #endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:29
static const Event TabReverse
Definition: event.hpp:56
int cursor_shape() const
Definition: event.hpp:87
static const Event ArrowLeftCtrl
Definition: event.hpp:45
std::string character() const
Definition: event.hpp:76
static Event CursorShape(std::string, int shape)
An event corresponding to a terminal DCS (Device Control String).
Definition: event.cpp:54
int cursor_y() const
Definition: event.hpp:84
int cursor_x() const
Definition: event.hpp:83
static const Event PageUp
Definition: event.hpp:63
static const Event Escape
Definition: event.hpp:54
bool is_mouse() const
Definition: event.hpp:78
static const Event F12
Definition: event.hpp:57
static Event Mouse(std::string, Mouse mouse)
An event corresponding to a given typed character.
Definition: event.cpp:44
const std::string & input() const
Definition: event.hpp:73
bool is_cursor_position() const
Definition: event.hpp:82
static const Event F5
Definition: event.hpp:57
static const Event F3
Definition: event.hpp:57
static const Event F9
Definition: event.hpp:57
ScreenInteractive * screen_
Definition: event.hpp:90
static const Event Custom
Definition: event.hpp:67
static Event Character(std::string)
An event corresponding to a given typed character.
Definition: event.cpp:16
static const Event F2
Definition: event.hpp:57
static const Event Backspace
Definition: event.hpp:51
static const Event F7
Definition: event.hpp:57
static const Event ArrowUp
Definition: event.hpp:42
static const Event Tab
Definition: event.hpp:55
static const Event ArrowDown
Definition: event.hpp:43
static const Event End
Definition: event.hpp:61
static const Event F11
Definition: event.hpp:57
static const Event Home
Definition: event.hpp:60
static const Event F8
Definition: event.hpp:57
static const Event F4
Definition: event.hpp:57
static const Event ArrowUpCtrl
Definition: event.hpp:47
struct Mouse & mouse()
Definition: event.hpp:79
static const Event F10
Definition: event.hpp:57
static const Event PageDown
Definition: event.hpp:64
static Event CursorPosition(std::string, int x, int y)
Definition: event.cpp:74
static const Event F6
Definition: event.hpp:57
static const Event F1
Definition: event.hpp:57
static const Event Return
Definition: event.hpp:53
bool operator==(const Event &other) const
Definition: event.hpp:70
static const Event ArrowLeft
Definition: event.hpp:40
bool operator!=(const Event &other) const
Definition: event.hpp:71
bool is_character() const
Definition: event.hpp:75
static const Event Delete
Definition: event.hpp:52
static const Event ArrowDownCtrl
Definition: event.hpp:48
static const Event Insert
Definition: event.hpp:59
static const Event ArrowRightCtrl
Definition: event.hpp:46
bool is_cursor_shape() const
Definition: event.hpp:86
static Event Special(std::string)
An custom event whose meaning is defined by the user of the library.
Definition: event.cpp:66
static const Event ArrowRight
Definition: event.hpp:41
A mouse event. It contains the coordinate of the mouse, the button pressed and the modifier (shift,...
Definition: mouse.hpp:11