From 23a8c94beffb9dd2bb286b936af8fd78a1114def Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Thu, 31 Oct 2024 21:05:39 +0700 Subject: [PATCH] Implementation of the selection in the text node --- src/ftxui/component/screen_interactive.cpp | 24 +++------------------- src/ftxui/dom/text.cpp | 9 ++++++++ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index 93bc56d2..6489eec8 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -877,25 +877,6 @@ bool ScreenInteractive::HandleSelection(Event event) { return false; } -void ScreenInteractive::RefreshSelection() { - if (!selection_enabled) { - return; - } - selection_text = ""; - - for (int y = std::min(selection_region.y_min, selection_region.y_max); - y <= std::max(selection_region.y_min, selection_region.y_max); ++y) { - for (int x = std::min(selection_region.x_min, selection_region.x_max); - x <= std::max(selection_region.x_min, selection_region.x_max) - 1; - ++x) { - if (PixelAt(x, y).selectable == true) { - PixelAt(x, y).inverted ^= true; - selection_text += PixelAt(x, y).character; - } - } - } -} - std::string ScreenInteractive::GetSelection() { return selection_text; } @@ -975,9 +956,10 @@ void ScreenInteractive::Draw(Component component) { #endif previous_frame_resized_ = resized; - Render(*this, document); + // Clear selection text. + selection_text = ""; - RefreshSelection(); + Render(*this, document); // Set cursor position for user using tools to insert CJK characters. { diff --git a/src/ftxui/dom/text.cpp b/src/ftxui/dom/text.cpp index 228e7142..82b29a67 100644 --- a/src/ftxui/dom/text.cpp +++ b/src/ftxui/dom/text.cpp @@ -42,6 +42,15 @@ class Text : public Node { continue; } screen.PixelAt(x, y).character = cell; + + if(screen.PixelAt(x, y).selectable == true) + { + if(screen.selection_region.Contain(x, y)) { + screen.PixelAt(x, y).inverted ^= true; + screen.selection_text += screen.PixelAt(x, y).character; + } + } + ++x; } }