Skip to content

Commit 3d84e96

Browse files
committed
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`).
1 parent d9712cf commit 3d84e96

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

include/ftxui/component/event.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct Event {
3131
static Event Special(std::string);
3232
static Event Mouse(std::string, Mouse mouse);
3333
static Event CursorReporting(std::string, int x, int y);
34+
static Event CustomTagged(int tag);
3435

3536
// --- Arrow ---
3637
static const Event ArrowLeft;
@@ -74,9 +75,14 @@ struct Event {
7475

7576
const std::string& input() const { return input_; }
7677

77-
bool operator==(const Event& other) const { return input_ == other.input_; }
78+
bool operator==(const Event& other) const {
79+
return type_ == Type::Custom && other.type_ == Type::Custom ? data_.custom_tag == other.data_.custom_tag : input_ == other.input_;
80+
}
7881
bool operator!=(const Event& other) const { return !operator==(other); }
7982

83+
int is_custom(int tag) const { return type_ == Type::Custom && data_.custom_tag == tag; }
84+
int is_custom() const { return type_ == Type::Custom; }
85+
8086
//--- State section ----------------------------------------------------------
8187
ScreenInteractive* screen_ = nullptr;
8288

@@ -88,6 +94,7 @@ struct Event {
8894
Character,
8995
Mouse,
9096
CursorReporting,
97+
Custom,
9198
};
9299
Type type_ = Type::Unknown;
93100

@@ -99,6 +106,7 @@ struct Event {
99106
union {
100107
struct Mouse mouse;
101108
struct Cursor cursor;
109+
int custom_tag;
102110
} data_ = {};
103111

104112
std::string input_;

src/ftxui/component/event.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ Event Event::CursorReporting(std::string input, int x, int y) {
4949
return event;
5050
}
5151

52+
// static
53+
Event Event::CustomTagged(int tag) {
54+
Event event;
55+
event.type_ = Type::Custom;
56+
event.data_.custom_tag = tag;
57+
return event;
58+
}
59+
60+
5261
// --- Arrow ---
5362
const Event Event::ArrowLeft = Event::Special("\x1B[D"); // NOLINT
5463
const Event Event::ArrowRight = Event::Special("\x1B[C"); // NOLINT
@@ -84,7 +93,8 @@ const Event Event::Home = Event::Special({27, 91, 72}); // NOLINT
8493
const Event Event::End = Event::Special({27, 91, 70}); // NOLINT
8594
const Event Event::PageUp = Event::Special({27, 91, 53, 126}); // NOLINT
8695
const Event Event::PageDown = Event::Special({27, 91, 54, 126}); // NOLINT
87-
const Event Event::Custom = Event::Special({0}); // NOLINT
96+
97+
const Event Event::Custom = Event::CustomTagged(-1); // NOLINT
8898

8999
} // namespace ftxui
90100

0 commit comments

Comments
 (0)