Problem: components can't distinguish custom events

They all look the same, which means they would have to check or poll all
potential sources of change.

Solution: make custom events take an integer tag

I've kept the backwards compatibility by using `-1` for the default custom
event (`Event::Custom`).
This commit is contained in:
Yurii Rashkovskii 2023-06-14 07:45:34 -07:00
parent d9712cf1e8
commit 395459fafe
2 changed files with 18 additions and 1 deletions

View File

@ -31,6 +31,7 @@ struct Event {
static Event Special(std::string); static Event Special(std::string);
static Event Mouse(std::string, Mouse mouse); static Event Mouse(std::string, Mouse mouse);
static Event CursorReporting(std::string, int x, int y); static Event CursorReporting(std::string, int x, int y);
static Event CustomTagged(int tag);
// --- Arrow --- // --- Arrow ---
static const Event ArrowLeft; static const Event ArrowLeft;
@ -74,9 +75,14 @@ struct Event {
const std::string& input() const { return input_; } const std::string& input() const { return input_; }
bool operator==(const Event& other) const { return input_ == other.input_; } bool operator==(const Event& other) const {
return type_ == Type::Custom && other.type_ == Type::Custom ? data_.custom_tag == other.data_.custom_tag : input_ == other.input_;
}
bool operator!=(const Event& other) const { return !operator==(other); } bool operator!=(const Event& other) const { return !operator==(other); }
int is_custom(int tag) const { return type_ == Type::Custom && data_.custom_tag == tag; }
int is_custom() const { return type_ == Type::Custom; }
//--- State section ---------------------------------------------------------- //--- State section ----------------------------------------------------------
ScreenInteractive* screen_ = nullptr; ScreenInteractive* screen_ = nullptr;
@ -88,6 +94,7 @@ struct Event {
Character, Character,
Mouse, Mouse,
CursorReporting, CursorReporting,
Custom,
}; };
Type type_ = Type::Unknown; Type type_ = Type::Unknown;
@ -99,6 +106,7 @@ struct Event {
union { union {
struct Mouse mouse; struct Mouse mouse;
struct Cursor cursor; struct Cursor cursor;
int custom_tag;
} data_ = {}; } data_ = {};
std::string input_; std::string input_;

View File

@ -49,6 +49,15 @@ Event Event::CursorReporting(std::string input, int x, int y) {
return event; return event;
} }
// static
Event Event::CustomTagged(int tag) {
Event event;
event.type_ = Type::Custom;
event.data_.custom_tag = tag;
return event;
}
// --- Arrow --- // --- Arrow ---
const Event Event::ArrowLeft = Event::Special("\x1B[D"); // NOLINT const Event Event::ArrowLeft = Event::Special("\x1B[D"); // NOLINT
const Event Event::ArrowRight = Event::Special("\x1B[C"); // NOLINT const Event Event::ArrowRight = Event::Special("\x1B[C"); // NOLINT