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

@@ -1,4 +1,5 @@
#include "ftxui/component/button.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <functional>
@@ -11,6 +12,9 @@ Element Button::Render() {
bool Button::OnEvent(Event event) {
if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
if (!event.screen()->CaptureMouse())
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&

View File

@@ -1,4 +1,5 @@
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <functional>
@@ -26,6 +27,8 @@ bool CheckBox::OnEvent(Event event) {
}
bool CheckBox::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;

View File

@@ -1,4 +1,5 @@
#include "ftxui/component/input.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <algorithm>
@@ -102,6 +103,8 @@ bool Input::OnEvent(Event event) {
}
bool Input::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
if (!input_box_.Contain(event.mouse().x, event.mouse().y))
return false;

View File

@@ -1,4 +1,5 @@
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <algorithm>
#include <iostream>
@@ -27,6 +28,8 @@ Element Menu::Render() {
}
bool Menu::OnEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
if (event.is_mouse())
return OnMouseEvent(event);
@@ -60,6 +63,8 @@ bool Menu::OnEvent(Event event) {
}
bool Menu::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;

View File

@@ -1,4 +1,5 @@
#include "ftxui/component/radiobox.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <algorithm>
#include <functional>
@@ -23,6 +24,8 @@ Element RadioBox::Render() {
}
bool RadioBox::OnEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
if (event.is_mouse())
return OnMouseEvent(event);
@@ -55,6 +58,8 @@ bool RadioBox::OnEvent(Event event) {
}
bool RadioBox::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;

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

View File

@@ -1,9 +1,13 @@
#include "ftxui/component/slider.hpp"
#include "ftxui/component/captured_mouse.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui {
class SliderInt : public Component {
template<class T>
class SliderImpl : public Component {
public:
SliderInt(std::wstring label, int* value, int min, int max, int increment)
SliderImpl(std::wstring label, T* value, T min, T max, T increment)
: label_(label),
value_(value),
min_(min),
@@ -45,36 +49,59 @@ class SliderInt : public Component {
}
bool OnMouseEvent(Event event) {
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;
TakeFocus();
if (!gauge_box_.Contain(event.mouse().x, event.mouse().y))
return false;
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
*value_ = min_ + (event.mouse().x - gauge_box_.x_min) * (max_ - min_) /
(gauge_box_.x_max - gauge_box_.x_min);
if (captured_mouse_ && event.mouse().motion == Mouse::Released) {
captured_mouse_ = nullptr;
return true;
}
return true;
if (box_.Contain(event.mouse().x, event.mouse().y) &&
event.screen()->CaptureMouse()) {
TakeFocus();
}
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed &&
gauge_box_.Contain(event.mouse().x, event.mouse().y) &&
!captured_mouse_) {
captured_mouse_ = event.screen()->CaptureMouse();
}
if (captured_mouse_) {
*value_ = min_ + (event.mouse().x - gauge_box_.x_min) * (max_ - min_) /
(gauge_box_.x_max - gauge_box_.x_min);
*value_ = std::max(min_, std::min(max_, *value_));
return true;
}
return false;
}
private:
std::wstring label_;
int* value_;
int min_;
int max_;
int increment_ = 1;
T* value_;
T min_;
T max_;
T increment_ = 1;
Box box_;
Box gauge_box_;
CapturedMouse captured_mouse_;
};
ComponentPtr Slider(std::wstring label,
int* value,
int min,
int max,
int increment) {
return std::make_unique<SliderInt>(std::move(label), value, min, max,
increment);
template <class T>
ComponentPtr Slider(std::wstring label, T* value, T min, T max, T increment) {
return std::make_unique<SliderImpl<T>>(std::move(label), value, min, max,
increment);
}
template ComponentPtr Slider(std::wstring label,
int* value,
int min,
int max,
int increment);
template ComponentPtr Slider(std::wstring label,
float* value,
float min,
float max,
float increment);
} // namespace ftxui

View File

@@ -1,4 +1,5 @@
#include "ftxui/component/toggle.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include <algorithm>
@@ -59,6 +60,8 @@ bool Toggle::OnEvent(Event event) {
}
bool Toggle::OnMouseEvent(Event event) {
if (!event.screen()->CaptureMouse())
return false;
for (int i = 0; i < boxes_.size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;