2019-01-07 00:10:35 +08:00
|
|
|
#ifndef FTXUI_COMPONENT_EVENT_HPP
|
|
|
|
#define FTXUI_COMPONENT_EVENT_HPP
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <ftxui/component/mouse.hpp> // for Mouse
|
2022-02-13 18:11:34 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <string> // for string, operator==
|
2019-06-30 16:11:37 +08:00
|
|
|
#include <vector>
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
class ScreenInteractive;
|
2021-05-10 02:32:27 +08:00
|
|
|
class ComponentBase;
|
2021-05-02 00:13:56 +08:00
|
|
|
|
2020-08-16 08:24:50 +08:00
|
|
|
/// @brief Represent an event. It can be key press event, a terminal resize, or
|
|
|
|
/// more ...
|
2021-02-14 03:00:00 +08: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
|
2019-06-23 23:47:33 +08:00
|
|
|
struct Event {
|
|
|
|
// --- Constructor section ---------------------------------------------------
|
2021-08-09 05:25:20 +08:00
|
|
|
static Event Character(std::string);
|
2019-06-23 23:47:33 +08:00
|
|
|
static Event Character(char);
|
|
|
|
static Event Character(wchar_t);
|
2021-04-19 00:32:38 +08:00
|
|
|
static Event Special(std::string);
|
2021-04-25 21:22:38 +08:00
|
|
|
static Event Mouse(std::string, Mouse mouse);
|
2021-04-25 00:16:13 +08:00
|
|
|
static Event CursorReporting(std::string, int x, int y);
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
// --- Arrow ---
|
2020-10-24 22:47:03 +08:00
|
|
|
static const Event ArrowLeft;
|
|
|
|
static const Event ArrowRight;
|
|
|
|
static const Event ArrowUp;
|
|
|
|
static const Event ArrowDown;
|
2019-06-23 23:47:33 +08:00
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
// --- Other ---
|
2020-10-24 22:47:03 +08: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;
|
|
|
|
static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-03-28 08:01:04 +08:00
|
|
|
static const Event Home;
|
|
|
|
static const Event End;
|
|
|
|
|
|
|
|
static const Event PageUp;
|
|
|
|
static const Event PageDown;
|
|
|
|
|
2019-01-27 09:33:06 +08:00
|
|
|
// --- Custom ---
|
|
|
|
static Event Custom;
|
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
//--- Method section ---------------------------------------------------------
|
2021-05-02 02:40:35 +08:00
|
|
|
bool is_character() const { return type_ == Type::Character; }
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string character() const { return input_; }
|
2021-04-19 00:32:38 +08:00
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
bool is_mouse() const { return type_ == Type::Mouse; }
|
|
|
|
struct Mouse& mouse() {
|
|
|
|
return mouse_;
|
|
|
|
}
|
|
|
|
|
2021-04-25 00:16:13 +08:00
|
|
|
bool is_cursor_reporting() const { return type_ == Type::CursorReporting; }
|
2021-04-25 21:22:38 +08:00
|
|
|
int cursor_x() const { return cursor_.x; }
|
|
|
|
int cursor_y() const { return cursor_.y; }
|
2021-04-19 00:32:38 +08:00
|
|
|
|
2020-10-24 22:47:03 +08:00
|
|
|
const std::string& input() const { return input_; }
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2020-10-24 22:47:03 +08:00
|
|
|
bool operator==(const Event& other) const { return input_ == other.input_; }
|
2021-03-28 08:01:04 +08:00
|
|
|
bool operator!=(const Event& other) const { return !operator==(other); }
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
//--- State section ----------------------------------------------------------
|
2021-09-26 21:19:17 +08:00
|
|
|
ScreenInteractive* screen_ = nullptr;
|
2021-10-16 05:04:11 +08:00
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
private:
|
2021-05-10 02:32:27 +08:00
|
|
|
friend ComponentBase;
|
2021-05-02 02:40:35 +08:00
|
|
|
friend ScreenInteractive;
|
2021-04-19 00:32:38 +08:00
|
|
|
enum class Type {
|
|
|
|
Unknown,
|
|
|
|
Character,
|
2021-04-25 21:22:38 +08:00
|
|
|
Mouse,
|
2021-04-25 00:16:13 +08:00
|
|
|
CursorReporting,
|
2021-04-19 00:32:38 +08:00
|
|
|
};
|
2021-04-25 21:22:38 +08:00
|
|
|
Type type_ = Type::Unknown;
|
2021-04-19 00:32:38 +08:00
|
|
|
|
2021-04-25 21:22:38 +08:00
|
|
|
struct Cursor {
|
2021-04-19 00:32:38 +08:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
union {
|
2021-04-25 21:22:38 +08:00
|
|
|
struct Mouse mouse_;
|
|
|
|
struct Cursor cursor_;
|
2021-04-19 00:32:38 +08:00
|
|
|
};
|
2019-06-23 23:47:33 +08:00
|
|
|
std::string input_;
|
|
|
|
};
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-06-23 23:47:33 +08:00
|
|
|
} // namespace ftxui
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2019-01-07 00:10:35 +08:00
|
|
|
#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
|
2020-08-16 06:24:18 +08: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.
|