From 2339ead35c6286cd21f5b57d0403539afb208650 Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Tue, 27 Aug 2024 21:13:00 +0700 Subject: [PATCH] It somewhat works --- examples/component/selectable_input.cpp | 2 +- .../ftxui/component/screen_interactive.hpp | 1 + src/ftxui/component/screen_interactive.cpp | 27 ++++++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/component/selectable_input.cpp b/examples/component/selectable_input.cpp index c256482f..a3dcb23b 100644 --- a/examples/component/selectable_input.cpp +++ b/examples/component/selectable_input.cpp @@ -69,7 +69,7 @@ int main() { text("Your phone number is " + phoneNumber), // text("select_start " + std::to_string(selection.startx) + ";" + std::to_string(selection.starty)), // text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)), - text("textToCopy " + textToCopy) + text("textToCopy is " + textToCopy) }) | border; // | selectable([&textToCopy](std::string txtSelected){textToCopy = txtSelected;}) }); diff --git a/include/ftxui/component/screen_interactive.hpp b/include/ftxui/component/screen_interactive.hpp index e6d15601..faf1e838 100644 --- a/include/ftxui/component/screen_interactive.hpp +++ b/include/ftxui/component/screen_interactive.hpp @@ -139,6 +139,7 @@ class ScreenInteractive : public Screen { bool force_handle_ctrl_z_ = true; Region selectedRegion; + std::string selectedText; // The style of the cursor to restore on exit. int cursor_reset_shape_ = 1; diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 1c779e16..acc4a7d8 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -357,7 +357,8 @@ ScreenInteractive::ScreenInteractive(int dimx, bool use_alternative_screen) : Screen(dimx, dimy), dimension_(dimension), - use_alternative_screen_(use_alternative_screen) { + use_alternative_screen_(use_alternative_screen), + selectedText("") { task_receiver_ = MakeReceiver(); } @@ -853,15 +854,15 @@ bool ScreenInteractive::selectableCatchEvent(Event event) { selectedRegion.starty = mouse.y; selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - refreshSelection(); + // refreshSelection(); } else if (mouse.motion == Mouse::Released) { selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - refreshSelection(); + // refreshSelection(); } else if (mouse.motion == Mouse::Moved) { selectedRegion.endx = mouse.x; selectedRegion.endy = mouse.y; - refreshSelection(); + // refreshSelection(); } } } @@ -871,16 +872,26 @@ bool ScreenInteractive::selectableCatchEvent(Event event) { void ScreenInteractive::refreshSelection(void) { + selectedText = ""; + 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; + if(PixelAt(x, y).selectable == true) + { + PixelAt(x, y).inverted ^= true; + selectedText += PixelAt(x, y).character; + } } } } std::string ScreenInteractive::getSelection(void) { - return "Kikoo"; + + // std::ofstream MyFile("debug.log", std::ios_base::app); + // MyFile << "Top dog!" << std::endl; + // MyFile.close(); + + return selectedText; } // private @@ -960,6 +971,8 @@ void ScreenInteractive::Draw(Component component) { Render(*this, document); + refreshSelection(); + // Set cursor position for user using tools to insert CJK characters. { const int dx = dimx_ - 1 - cursor_.x + int(dimx_ != terminal.dimx);