We can catch mouse events

This commit is contained in:
Clement Roblot
2024-08-27 17:35:21 +07:00
committed by ArthurSonzogni
parent 75abd75b67
commit 1325256aa9
6 changed files with 71 additions and 4 deletions

View File

@@ -35,6 +35,12 @@
#include "ftxui/screen/pixel.hpp" // for Pixel
#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
#include <iostream>
#include <fstream>
#if defined(_WIN32)
#define DEFINE_CONSOLEV2_PROPERTIES
#define WIN32_LEAN_AND_MEAN
@@ -781,7 +787,15 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
arg.screen_ = this;
const bool handled = component->OnEvent(arg);
bool handled = component->OnEvent(arg);
if(handled == false)
{
if(selectableCatchEvent(arg))
{
handled = true;
}
}
if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
RecordSignal(SIGABRT);
@@ -824,6 +838,41 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
// clang-format on
}
bool ScreenInteractive::selectableCatchEvent(Event event) {
// std::ofstream MyFile("debug.log", std::ios_base::app);
// MyFile << "Top dog!" << std::endl;
// MyFile.close();
if (event.is_mouse()) {
auto& mouse = event.mouse();
if (mouse.button == Mouse::Left) {
if (mouse.motion == Mouse::Pressed) {
selectedRegion.startx = mouse.x;
selectedRegion.starty = mouse.y;
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
selectedRegion.changed = true;
} else if (mouse.motion == Mouse::Released) {
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
selectedRegion.changed = true;
} else if (mouse.motion == Mouse::Moved) {
selectedRegion.endx = mouse.x;
selectedRegion.endy = mouse.y;
selectedRegion.changed = true;
}
}
}
return false;
}
std::string ScreenInteractive::getSelection(void) {
return "Selection";
}
// private
// NOLINTNEXTLINE
void ScreenInteractive::Draw(Component component) {

View File

@@ -1,6 +1,6 @@
#include "ftxui/dom/elements.hpp" // for Element, Decorator
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/component/event.hpp" // for Event
namespace ftxui {
@@ -16,7 +16,7 @@ class Selectable : public NodeDecorator {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).inverted = true;
screen.PixelAt(x, y).selectable = true;
}
}