From 8ee2f12ef9da5fe2de48a3d81f0f2c2cfe4f6727 Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Tue, 24 Oct 2023 14:16:48 +0700 Subject: [PATCH] Prevent drag spamming buttons --- src/ftxui/component/button.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ftxui/component/button.cpp b/src/ftxui/component/button.cpp index c983f31f..60f9fbcf 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -50,6 +50,11 @@ class ButtonBase : public ComponentBase, public ButtonOption { } auto focus_management = focused ? focus : active ? select : nothing; + + if (focus_management == nothing) { + isMousePressed = false; + } + const EntryState state = { *label, false, @@ -125,9 +130,16 @@ class ButtonBase : public ComponentBase, public ButtonOption { } if (event.mouse().button == Mouse::Left && - event.mouse().motion == Mouse::Pressed) { + event.mouse().motion == Mouse::Pressed && isMousePressed == false) { TakeFocus(); OnClick(); + isMousePressed = true; + return true; + } + + if(event.mouse().button == Mouse::Left && + event.mouse().motion == Mouse::Released) { + isMousePressed = false; return true; } @@ -137,6 +149,7 @@ class ButtonBase : public ComponentBase, public ButtonOption { bool Focusable() const final { return true; } private: + bool isMousePressed = false; bool mouse_hover_ = false; Box box_; ButtonOption option_;