diff --git a/examples/component/input.cpp b/examples/component/input.cpp index cae98263..b83a73a8 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -68,10 +68,9 @@ int main() { text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)), text("textToCopy " + textToCopy) }) | - border | selected(selection, textToCopy, [&textToCopy](std::string selected){textToCopy = selected;}); + border | selected(selection, [&textToCopy](std::string selected){textToCopy = selected;}); }); - // TODO: Make the textToCopy a callback called every times the selected text change // TODO: Is there a way for me to embedd the catchEvent in the selected decorator? At a minimum move the function in the selected.cpp file and add doc to call it // TODO: Implement the double click on word to select the word // TODO: Implement the double click and drag to select word by word (optional) diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index 458c839a..3f4db35f 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -104,8 +104,8 @@ Element canvas(std::function); Element bold(Element); Element dim(Element); Element inverted(Element); -Element selected(Region &selection, std::string &destination, std::function onSelectionChange, Element); -Decorator selected(Region &selection, std::string &destination, std::function onSelectionChange); +Element selected(Region &selection, std::function onSelectionChange, Element); +Decorator selected(Region &selection, std::function onSelectionChange); Element underlined(Element); Element underlinedDouble(Element); Element blink(Element); diff --git a/src/ftxui/dom/selected.cpp b/src/ftxui/dom/selected.cpp index d1fc5a69..701b6885 100644 --- a/src/ftxui/dom/selected.cpp +++ b/src/ftxui/dom/selected.cpp @@ -16,26 +16,24 @@ namespace { class Selected : public NodeDecorator { public: using NodeDecorator::NodeDecorator; - Selected(Element child, Region &selection, std::string &destination, std::function onSelectionChange) - : NodeDecorator(std::move(child)), selection_(selection), destination_(destination), onSelectionChange_(onSelectionChange) {} + Selected(Element child, Region &selection, std::function onSelectionChange) + : NodeDecorator(std::move(child)), selection_(selection), onSelectionChange_(onSelectionChange) {} void Render(Screen& screen) override { Node::Render(screen); - destination_ = ""; - std::string textToCopy = ""; + std::string selectedText = ""; for (int y = std::min(selection_.starty, selection_.endy); y <= std::max(selection_.starty, selection_.endy); ++y) { for (int x = std::min(selection_.startx, selection_.endx); x <= std::max(selection_.startx, selection_.endx)-1; ++x) { screen.PixelAt(x, y).inverted ^= true; - textToCopy += screen.PixelAt(x, y).character; + selectedText += screen.PixelAt(x, y).character; } } - onSelectionChange_(textToCopy); + onSelectionChange_(selectedText); } private: Region &selection_; - std::string &destination_; std::function onSelectionChange_; }; } // namespace @@ -44,12 +42,12 @@ private: /// colors. /// @ingroup dom -Element selected(Region &selection, std::string &destination, std::function onSelectionChange, Element child) { - return std::make_shared(std::move(child), selection, destination, onSelectionChange); +Element selected(Region &selection, std::function onSelectionChange, Element child) { + return std::make_shared(std::move(child), selection, onSelectionChange); } -Decorator selected(Region &selection, std::string &destination, std::function onSelectionChange) { - return [&selection, &destination, onSelectionChange](Element child) { return selected(selection, destination, onSelectionChange, std::move(child)); }; +Decorator selected(Region &selection, std::function onSelectionChange) { + return [&selection, onSelectionChange](Element child) { return selected(selection, onSelectionChange, std::move(child)); }; } } // namespace ftxui