2023-08-19 13:56:36 +02:00
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
2019-01-06 17:10:35 +01:00
|
|
|
#ifndef FTXUI_COMPONENT_EVENT_HPP
|
|
|
|
#define FTXUI_COMPONENT_EVENT_HPP
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2021-05-01 20:40:35 +02:00
|
|
|
#include <ftxui/component/mouse.hpp> // for Mouse
|
2024-05-01 11:40:49 +02:00
|
|
|
#include <string> // for string, operator==
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2019-01-12 15:00:08 +01:00
|
|
|
namespace ftxui {
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2021-05-01 18:13:56 +02:00
|
|
|
class ScreenInteractive;
|
2021-05-09 20:32:27 +02:00
|
|
|
class ComponentBase;
|
2021-05-01 18:13:56 +02:00
|
|
|
|
2020-08-16 02:24:50 +02:00
|
|
|
/// @brief Represent an event. It can be key press event, a terminal resize, or
|
|
|
|
/// more ...
|
2021-02-13 20:00:00 +01:00
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
/// - Printable character can be created using Event::Character('a').
|
|
|
|
/// - Some special are predefined, like Event::ArrowLeft.
|
|
|
|
/// - One can find arbitrary code for special Events using:
|
|
|
|
/// ./example/util/print_key_press
|
|
|
|
/// For instance, CTLR+A maps to Event::Special({1});
|
|
|
|
///
|
|
|
|
/// Useful documentation about xterm specification:
|
|
|
|
/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
2025-06-05 11:35:14 +02:00
|
|
|
///
|
|
|
|
/// @ingroup component
|
2019-06-23 17:47:33 +02:00
|
|
|
struct Event {
|
|
|
|
// --- Constructor section ---------------------------------------------------
|
2021-08-08 23:25:20 +02:00
|
|
|
static Event Character(std::string);
|
2019-06-23 17:47:33 +02:00
|
|
|
static Event Character(char);
|
|
|
|
static Event Character(wchar_t);
|
2021-04-18 18:32:38 +02:00
|
|
|
static Event Special(std::string);
|
2021-04-25 15:22:38 +02:00
|
|
|
static Event Mouse(std::string, Mouse mouse);
|
2023-12-17 10:24:33 +01:00
|
|
|
static Event CursorPosition(std::string, int x, int y); // Internal
|
|
|
|
static Event CursorShape(std::string, int shape); // Internal
|
2019-06-23 17:47:33 +02:00
|
|
|
|
2018-10-18 22:58:38 +02:00
|
|
|
// --- Arrow ---
|
2020-10-24 10:47:03 -04:00
|
|
|
static const Event ArrowLeft;
|
|
|
|
static const Event ArrowRight;
|
|
|
|
static const Event ArrowUp;
|
|
|
|
static const Event ArrowDown;
|
2019-06-23 17:47:33 +02:00
|
|
|
|
2022-10-06 21:16:55 +02:00
|
|
|
static const Event ArrowLeftCtrl;
|
|
|
|
static const Event ArrowRightCtrl;
|
|
|
|
static const Event ArrowUpCtrl;
|
|
|
|
static const Event ArrowDownCtrl;
|
|
|
|
|
2018-10-18 22:58:38 +02:00
|
|
|
// --- Other ---
|
2020-10-24 10:47:03 -04:00
|
|
|
static const Event Backspace;
|
|
|
|
static const Event Delete;
|
|
|
|
static const Event Return;
|
|
|
|
static const Event Escape;
|
|
|
|
static const Event Tab;
|
|
|
|
static const Event TabReverse;
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2024-04-28 15:17:54 +02:00
|
|
|
// --- Navigation keys ---
|
2023-08-28 20:07:26 +01:00
|
|
|
static const Event Insert;
|
2021-03-28 02:01:04 +02:00
|
|
|
static const Event Home;
|
|
|
|
static const Event End;
|
|
|
|
static const Event PageUp;
|
|
|
|
static const Event PageDown;
|
|
|
|
|
2024-04-28 15:17:54 +02:00
|
|
|
// --- Function keys ---
|
|
|
|
static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
|
|
|
|
|
|
|
|
// --- Control keys ---
|
|
|
|
static const Event a, A, CtrlA, AltA, CtrlAltA;
|
|
|
|
static const Event b, B, CtrlB, AltB, CtrlAltB;
|
|
|
|
static const Event c, C, CtrlC, AltC, CtrlAltC;
|
|
|
|
static const Event d, D, CtrlD, AltD, CtrlAltD;
|
|
|
|
static const Event e, E, CtrlE, AltE, CtrlAltE;
|
|
|
|
static const Event f, F, CtrlF, AltF, CtrlAltF;
|
|
|
|
static const Event g, G, CtrlG, AltG, CtrlAltG;
|
|
|
|
static const Event h, H, CtrlH, AltH, CtrlAltH;
|
|
|
|
static const Event i, I, CtrlI, AltI, CtrlAltI;
|
|
|
|
static const Event j, J, CtrlJ, AltJ, CtrlAltJ;
|
|
|
|
static const Event k, K, CtrlK, AltK, CtrlAltK;
|
|
|
|
static const Event l, L, CtrlL, AltL, CtrlAltL;
|
|
|
|
static const Event m, M, CtrlM, AltM, CtrlAltM;
|
|
|
|
static const Event n, N, CtrlN, AltN, CtrlAltN;
|
|
|
|
static const Event o, O, CtrlO, AltO, CtrlAltO;
|
|
|
|
static const Event p, P, CtrlP, AltP, CtrlAltP;
|
|
|
|
static const Event q, Q, CtrlQ, AltQ, CtrlAltQ;
|
|
|
|
static const Event r, R, CtrlR, AltR, CtrlAltR;
|
|
|
|
static const Event s, S, CtrlS, AltS, CtrlAltS;
|
|
|
|
static const Event t, T, CtrlT, AltT, CtrlAltT;
|
|
|
|
static const Event u, U, CtrlU, AltU, CtrlAltU;
|
|
|
|
static const Event v, V, CtrlV, AltV, CtrlAltV;
|
|
|
|
static const Event w, W, CtrlW, AltW, CtrlAltW;
|
|
|
|
static const Event x, X, CtrlX, AltX, CtrlAltX;
|
|
|
|
static const Event y, Y, CtrlY, AltY, CtrlAltY;
|
|
|
|
static const Event z, Z, CtrlZ, AltZ, CtrlAltZ;
|
|
|
|
|
2019-01-27 02:33:06 +01:00
|
|
|
// --- Custom ---
|
2022-03-31 02:17:43 +02:00
|
|
|
static const Event Custom;
|
2019-01-27 02:33:06 +01:00
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
//--- Method section ---------------------------------------------------------
|
2023-12-17 10:24:33 +01:00
|
|
|
bool operator==(const Event& other) const { return input_ == other.input_; }
|
|
|
|
bool operator!=(const Event& other) const { return !operator==(other); }
|
2024-04-28 15:17:54 +02:00
|
|
|
bool operator<(const Event& other) const { return input_ < other.input_; }
|
2023-12-17 10:24:33 +01:00
|
|
|
|
|
|
|
const std::string& input() const { return input_; }
|
|
|
|
|
2021-05-01 20:40:35 +02:00
|
|
|
bool is_character() const { return type_ == Type::Character; }
|
2021-08-08 23:25:20 +02:00
|
|
|
std::string character() const { return input_; }
|
2021-04-18 18:32:38 +02:00
|
|
|
|
2021-04-25 15:22:38 +02:00
|
|
|
bool is_mouse() const { return type_ == Type::Mouse; }
|
2023-03-26 20:20:02 +02:00
|
|
|
struct Mouse& mouse() { return data_.mouse; }
|
2021-04-25 15:22:38 +02:00
|
|
|
|
2023-12-17 10:24:33 +01:00
|
|
|
// --- Internal Method section -----------------------------------------------
|
|
|
|
bool is_cursor_position() const { return type_ == Type::CursorPosition; }
|
2023-03-26 20:20:02 +02:00
|
|
|
int cursor_x() const { return data_.cursor.x; }
|
|
|
|
int cursor_y() const { return data_.cursor.y; }
|
2021-04-18 18:32:38 +02:00
|
|
|
|
2023-12-17 10:24:33 +01:00
|
|
|
bool is_cursor_shape() const { return type_ == Type::CursorShape; }
|
|
|
|
int cursor_shape() const { return data_.cursor_shape; }
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2024-04-28 15:17:54 +02:00
|
|
|
// Debug
|
|
|
|
std::string DebugString() const;
|
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
//--- State section ----------------------------------------------------------
|
2021-09-26 15:19:17 +02:00
|
|
|
ScreenInteractive* screen_ = nullptr;
|
2021-10-15 23:04:11 +02:00
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
private:
|
2021-05-09 20:32:27 +02:00
|
|
|
friend ComponentBase;
|
2021-05-01 20:40:35 +02:00
|
|
|
friend ScreenInteractive;
|
2021-04-18 18:32:38 +02:00
|
|
|
enum class Type {
|
|
|
|
Unknown,
|
|
|
|
Character,
|
2021-04-25 15:22:38 +02:00
|
|
|
Mouse,
|
2023-12-17 10:24:33 +01:00
|
|
|
CursorPosition,
|
|
|
|
CursorShape,
|
2021-04-18 18:32:38 +02:00
|
|
|
};
|
2021-04-25 15:22:38 +02:00
|
|
|
Type type_ = Type::Unknown;
|
2021-04-18 18:32:38 +02:00
|
|
|
|
2021-04-25 15:22:38 +02:00
|
|
|
struct Cursor {
|
2023-03-26 20:20:02 +02:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2021-04-18 18:32:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
union {
|
2023-03-26 20:20:02 +02:00
|
|
|
struct Mouse mouse;
|
|
|
|
struct Cursor cursor;
|
2023-12-17 10:24:33 +01:00
|
|
|
int cursor_shape;
|
2023-03-26 20:20:02 +02:00
|
|
|
} data_ = {};
|
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
std::string input_;
|
|
|
|
};
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2019-06-23 17:47:33 +02:00
|
|
|
} // namespace ftxui
|
2018-10-18 22:58:38 +02:00
|
|
|
|
2019-01-06 17:10:35 +01:00
|
|
|
#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
|