From e4a63318adaf6c793270132b45d214fbd75691bd Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Tue, 27 Aug 2024 17:40:48 +0700 Subject: [PATCH] We can act on the screen --- examples/component/selectable_input.cpp | 3 +++ include/ftxui/component/screen_interactive.hpp | 5 +++-- include/ftxui/dom/elements.hpp | 3 --- src/ftxui/component/screen_interactive.cpp | 18 ++++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/component/selectable_input.cpp b/examples/component/selectable_input.cpp index fd16b2d..c256482 100644 --- a/examples/component/selectable_input.cpp +++ b/examples/component/selectable_input.cpp @@ -55,6 +55,9 @@ int main() { // Tweak how the component tree is rendered: auto renderer = Renderer(component, [&] { + + textToCopy = screen.getSelection(); + return vbox({ hbox(text(" First name : "), input_first_name->Render()), hbox(text(" Last name : ") | selectable(), input_last_name->Render()), diff --git a/include/ftxui/component/screen_interactive.hpp b/include/ftxui/component/screen_interactive.hpp index a01fa95..e6d1560 100644 --- a/include/ftxui/component/screen_interactive.hpp +++ b/include/ftxui/component/screen_interactive.hpp @@ -32,7 +32,6 @@ typedef struct { uint16_t endx = 0; uint16_t starty = 0; uint16_t endy = 0; - bool changed = false; } Region; class ScreenInteractive : public Screen { @@ -77,6 +76,8 @@ class ScreenInteractive : public Screen { void ForceHandleCtrlC(bool force); void ForceHandleCtrlZ(bool force); + std::string getSelection(void); + private: void ExitNow(); @@ -92,7 +93,7 @@ class ScreenInteractive : public Screen { void HandleTask(Component component, Task& task); bool selectableCatchEvent(Event event); - std::string getSelection(void); + void refreshSelection(void); void Draw(Component component); void ResetCursorPosition(); diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index cf79899..325caa9 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -116,9 +116,6 @@ Element hyperlink(std::string link, Element child); Element selectable(Element child); Decorator selectable(void); -// -- Selection -- -std::string getSelection(void); - // --- Layout is // Horizontal, Vertical or stacked set of elements. Element hbox(Elements); diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 7b71887..75acc0a 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -853,15 +853,15 @@ bool ScreenInteractive::selectableCatchEvent(Event event) { selectedRegion.starty = mouse.y; selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - selectedRegion.changed = true; + refreshSelection(); } else if (mouse.motion == Mouse::Released) { selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - selectedRegion.changed = true; + refreshSelection(); } else if (mouse.motion == Mouse::Moved) { selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - selectedRegion.changed = true; + refreshSelection(); } } } @@ -869,8 +869,18 @@ bool ScreenInteractive::selectableCatchEvent(Event event) { return false; } +void ScreenInteractive::refreshSelection(void) { + + 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) { + PixelAt(x, y).inverted ^= true; + //selectedText += PixelAt(x, y).character; + } + } +} + std::string ScreenInteractive::getSelection(void) { - return "Selection"; + return "Kikoo"; } // private