From 53f179c776ab942ca6373b5c58b3c29e222705d6 Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Fri, 2 Aug 2024 23:08:24 +0700 Subject: [PATCH] Prevent spamming --- examples/component/input.cpp | 1 - include/ftxui/dom/elements.hpp | 1 + src/ftxui/dom/selectable.cpp | 30 ++++++++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/component/input.cpp b/examples/component/input.cpp index 23962ce5..a4c31ece 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -74,7 +74,6 @@ int main() { // TODO: Implement the double click on word to select the word // TODO: Implement the double click and drag to select word by word (optional) // TODO: Implement the tripple click to select an entire line - // TODO: Call onSelectionChange_ only when the selection indeed did change, not at every render? // TODO: Add a "selectable" flag in the pixel class and take it into account when selecting things renderer |= CatchEvent([&](Event event) { diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index 4579c8a8..f2a65b47 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -40,6 +40,7 @@ typedef struct { uint16_t endx = 0; uint16_t starty = 0; uint16_t endy = 0; + bool changed = false; } Region; // Pipe elements into decorator togethers. diff --git a/src/ftxui/dom/selectable.cpp b/src/ftxui/dom/selectable.cpp index f88a0274..d1482ff0 100644 --- a/src/ftxui/dom/selectable.cpp +++ b/src/ftxui/dom/selectable.cpp @@ -12,7 +12,7 @@ namespace ftxui { -Region newSelection; +Region selectedRegion; namespace { class Selectable : public NodeDecorator { @@ -24,14 +24,17 @@ class Selectable : public NodeDecorator { void Render(Screen& screen) override { Node::Render(screen); std::string selectedText = ""; - for (int y = std::min(newSelection.starty, newSelection.endy); y <= std::max(newSelection.starty, newSelection.endy); ++y) { - for (int x = std::min(newSelection.startx, newSelection.endx); x <= std::max(newSelection.startx, newSelection.endx)-1; ++x) { + for (int y = std::min(selectedRegion.starty, selectedRegion.endy); y <= std::max(selectedRegion.starty, selectedRegion.endy); ++y) { + for (int x = std::min(selectedRegion.startx, selectedRegion.endx); x <= std::max(selectedRegion.startx, selectedRegion.endx)-1; ++x) { screen.PixelAt(x, y).inverted ^= true; selectedText += screen.PixelAt(x, y).character; } } - onSelectionChange_(selectedText); + if(selectedRegion.changed == true) { + selectedRegion.changed = false;; + onSelectionChange_(selectedText); + } } private: @@ -58,16 +61,19 @@ bool selectableCatchEvent(Event event) { if (mouse.button == Mouse::Left) { if (mouse.motion == Mouse::Pressed) { - newSelection.startx = mouse.x; - newSelection.starty = mouse.y; - newSelection.endx = mouse.x; - newSelection.endy = mouse.y; + 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) { - newSelection.endx = mouse.x; - newSelection.endy = mouse.y; + selectedRegion.endx = mouse.x; + selectedRegion.endy = mouse.y; + selectedRegion.changed = true; } else if (mouse.motion == Mouse::Moved) { - newSelection.endx = mouse.x; - newSelection.endy = mouse.y; + selectedRegion.endx = mouse.x; + selectedRegion.endy = mouse.y; + selectedRegion.changed = true; } return true;