Improve mouse support for menu and toggle.

This commit is contained in:
ArthurSonzogni
2021-04-24 18:16:13 +02:00
parent 890a41a64c
commit 8037a5fa5f
14 changed files with 124 additions and 36 deletions

View File

@@ -36,6 +36,7 @@ struct Event {
static Event MouseMiddleDown(std::string, int x, int y);
static Event MouseRightMove(std::string, int x, int y);
static Event MouseRightDown(std::string, int x, int y);
static Event CursorReporting(std::string, int x, int y);
// --- Arrow ---
static const Event ArrowLeft;
@@ -68,6 +69,7 @@ struct Event {
bool is_mouse_right_move() const { return type_ == Type::MouseRightMove; }
bool is_mouse_up() const { return type_ == Type::MouseUp; }
bool is_mouse_move() const { return type_ == Type::MouseMove; }
bool is_cursor_reporting() const { return type_ == Type::CursorReporting; }
int mouse_x() const { return mouse_.x; }
int mouse_y() const { return mouse_.y; }
@@ -90,6 +92,7 @@ struct Event {
MouseMiddleMove,
MouseRightDown,
MouseRightMove,
CursorReporting,
};
struct Mouse {

View File

@@ -19,10 +19,12 @@ class Menu : public Component {
// State.
std::vector<std::wstring> entries = {};
int selected = 0;
int focused = 0;
Decorator normal_style = nothing;
Decorator focused_style = inverted;
Decorator selected_style = bold;
Decorator normal_style = nothing;
Decorator selected_focused_style = focused_style | selected_style;
// State update callback.
std::function<void()> on_change = []() {};

View File

@@ -52,6 +52,9 @@ class ScreenInteractive : public Screen {
std::string reset_cursor_position;
std::atomic<bool> quit_ = false;
int cursor_x_ = 0;
int cursor_y_ = 0;
};
} // namespace ftxui

View File

@@ -16,12 +16,14 @@ class Toggle : public Component {
~Toggle() override = default;
// State.
int selected = 0;
std::vector<std::wstring> entries = {L"On", L"Off"};
int selected = 0;
int focused = 0;
Decorator normal_style = dim;
Decorator focused_style = inverted;
Decorator selected_style = bold;
Decorator normal_style = dim;
Decorator selected_focused_style = focused_style | selected_style;
// Callback.
std::function<void()> on_change = []() {};