diff --git a/examples/component/input.cpp b/examples/component/input.cpp index feafbba..aaa49cc 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -68,7 +68,7 @@ int main() { text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)), text("textToCopy " + textToCopy) }) | - border | selected(selection); + border | selected(selection, textToCopy); }); @@ -76,17 +76,26 @@ int main() { if (event.is_mouse()) { auto& mouse = event.mouse(); if (mouse.button == Mouse::Left) { - if (mouse.motion == Mouse::Pressed) { + + if (mouse.motion == Mouse::Pressed) + { selection.startx = mouse.x; selection.starty = mouse.y; - selection.endx = mouse.x; + selection.endx = mouse.x-1; selection.endy = mouse.y; - } else if (mouse.motion == Mouse::Released) { - selection.endx = mouse.x; + + // screen.PixelAt(mouse.x, mouse.y).blink = true; + // screen.PixelAt(mouse.x, mouse.y).character = "K"; + textToCopy += screen.PixelAt(mouse.x, mouse.y).character; + } + else if (mouse.motion == Mouse::Released) + { + selection.endx = mouse.x-1; selection.endy = mouse.y; } - else if (mouse.motion == Mouse::Moved) { - selection.endx = mouse.x; + else if (mouse.motion == Mouse::Moved) + { + selection.endx = mouse.x-1; selection.endy = mouse.y; } diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index 71eb376..517c21a 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, Element); -Decorator selected(Region &selection); +Element selected(Region &selection, std::string &destination, Element); +Decorator selected(Region &selection, std::string &destination); Element underlined(Element); Element underlinedDouble(Element); Element blink(Element); diff --git a/src/ftxui/dom/selected.cpp b/src/ftxui/dom/selected.cpp index ebb9552..dec2253 100644 --- a/src/ftxui/dom/selected.cpp +++ b/src/ftxui/dom/selected.cpp @@ -16,20 +16,23 @@ namespace { class Selected : public NodeDecorator { public: using NodeDecorator::NodeDecorator; - Selected(Element child, Region &selection) - : NodeDecorator(std::move(child)), selection_(selection) {} + Selected(Element child, Region &selection, std::string &destination) + : NodeDecorator(std::move(child)), selection_(selection), destination_(destination) {} void Render(Screen& screen) override { Node::Render(screen); + destination_ = ""; for (int y = selection_.starty; y <= selection_.endy; ++y) { for (int x = selection_.startx; x <= selection_.endx; ++x) { screen.PixelAt(x, y).inverted ^= true; + destination_ += screen.PixelAt(x, y).character; } } } private: Region &selection_; + std::string &destination_; }; } // namespace @@ -37,12 +40,12 @@ private: /// colors. /// @ingroup dom -Element selected(Region &selection, Element child) { - return std::make_shared(std::move(child), selection); +Element selected(Region &selection, std::string &destination, Element child) { + return std::make_shared(std::move(child), selection, destination); } -Decorator selected(Region &selection) { - return [&selection](Element child) { return selected(selection, std::move(child)); }; +Decorator selected(Region &selection, std::string &destination) { + return [&selection, &destination](Element child) { return selected(selection, destination, std::move(child)); }; } } // namespace ftxui