Capture mouse for the slider component.

This commit is contained in:
ArthurSonzogni
2021-05-01 18:13:56 +02:00
parent 0af8201023
commit eb399d20c5
15 changed files with 157 additions and 35 deletions

View File

@@ -9,6 +9,7 @@
#include <stack>
#include <thread>
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/component.hpp"
#include "ftxui/component/terminal_input_parser.hpp"
#include "ftxui/screen/string.hpp"
@@ -216,6 +217,16 @@ void OnResize(int /* signal */) {
on_resize();
}
class CapturedMouseImpl : public CapturedMouseInterface {
public:
CapturedMouseImpl(std::function<void(void)> callback)
: callback_(callback) {}
~CapturedMouseImpl() override { callback_(); }
private:
std::function<void(void)> callback_;
};
} // namespace
ScreenInteractive::ScreenInteractive(int dimx,
@@ -256,6 +267,14 @@ void ScreenInteractive::PostEvent(Event event) {
event_sender_->Send(event);
}
CapturedMouse ScreenInteractive::CaptureMouse() {
if (mouse_captured)
return nullptr;
mouse_captured = true;
return std::make_unique<CapturedMouseImpl>(
[this] { mouse_captured = false; });
}
void ScreenInteractive::Loop(Component* component) {
// Install a SIGINT handler and restore the old handler on exit.
auto old_sigint_handler = std::signal(SIGINT, OnExit);
@@ -387,6 +406,7 @@ void ScreenInteractive::Loop(Component* component) {
event.mouse().y -= cursor_y_;
}
event.SetScreen(this);
component->OnEvent(event);
}