Skip to content

Commit

Permalink
Problem: components can't distinguish custom events
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
yrashk committed Jun 15, 2023
1 parent d9712cf commit 3d84e96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 9 additions & 1 deletion include/ftxui/component/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Event {
static Event Special(std::string);
static Event Mouse(std::string, Mouse mouse);
static Event CursorReporting(std::string, int x, int y);
static Event CustomTagged(int tag);

// --- Arrow ---
static const Event ArrowLeft;
Expand Down Expand Up @@ -74,9 +75,14 @@ struct Event {

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); }

int is_custom(int tag) const { return type_ == Type::Custom && data_.custom_tag == tag; }
int is_custom() const { return type_ == Type::Custom; }

//--- State section ----------------------------------------------------------
ScreenInteractive* screen_ = nullptr;

Expand All @@ -88,6 +94,7 @@ struct Event {
Character,
Mouse,
CursorReporting,
Custom,
};
Type type_ = Type::Unknown;

Expand All @@ -99,6 +106,7 @@ struct Event {
union {
struct Mouse mouse;
struct Cursor cursor;
int custom_tag;
} data_ = {};

std::string input_;
Expand Down
12 changes: 11 additions & 1 deletion src/ftxui/component/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ Event Event::CursorReporting(std::string input, int x, int y) {
return event;
}

// static
Event Event::CustomTagged(int tag) {
Event event;
event.type_ = Type::Custom;
event.data_.custom_tag = tag;
return event;
}


// --- Arrow ---
const Event Event::ArrowLeft = Event::Special("\x1B[D"); // NOLINT
const Event Event::ArrowRight = Event::Special("\x1B[C"); // NOLINT
Expand Down Expand Up @@ -84,7 +93,8 @@ const Event Event::Home = Event::Special({27, 91, 72}); // NOLINT
const Event Event::End = Event::Special({27, 91, 70}); // NOLINT
const Event Event::PageUp = Event::Special({27, 91, 53, 126}); // NOLINT
const Event Event::PageDown = Event::Special({27, 91, 54, 126}); // NOLINT
const Event Event::Custom = Event::Special({0}); // NOLINT

const Event Event::Custom = Event::CustomTagged(-1); // NOLINT

} // namespace ftxui

Expand Down

0 comments on commit 3d84e96

Please sign in to comment.