From aba18ef836d5423fd075653144909607e4ec55e7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 15 Nov 2025 14:28:20 +0300 Subject: [PATCH] add 'keep-line-selection' textbox attribute --- src/graphics/ui/elements/TextBox.cpp | 84 ++++++++++++++++------------ src/graphics/ui/elements/TextBox.hpp | 4 ++ src/graphics/ui/gui_xml.cpp | 5 ++ 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 52c33bf1..10127f10 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -231,7 +231,7 @@ TextBox::~TextBox() = default; void TextBox::draw(const DrawContext& pctx, const Assets& assets) { Container::draw(pctx, assets); - if (!isFocused()) { + if (!isFocused() && !keepLineSelection) { return; } const auto& labelText = getText(); @@ -252,7 +252,7 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) { float time = gui.getWindow().time(); - if (editable && static_cast((time - caretLastMove) * 2) % 2 == 0) { + if (isFocused() && editable && static_cast((time - caretLastMove) * 2) % 2 == 0) { uint line = label->getLineByTextIndex(caret); uint lcaret = caret - label->getTextLineOffset(line); int width = rawTextCache.metrics.calcWidth(input, 0, lcaret); @@ -308,42 +308,44 @@ void TextBox::draw(const DrawContext& pctx, const Assets& assets) { } } - if (isFocused() && multiline) { - auto selectionCtx = subctx.sub(batch); - selectionCtx.setBlendMode(BlendMode::addition); - - batch->setColor(glm::vec4(1, 1, 1, 0.1f)); - - uint line = label->getLineByTextIndex(caret); - while (label->isFakeLine(line)) { - line--; - } - do { - int lineY = label->getLineYOffset(line); - - batch->setColor(glm::vec4(1, 1, 1, 0.05f)); - if (showLineNumbers) { - batch->rect( - lcoord.x - 8, - lcoord.y + lineY, - label->getSize().x, - lineHeight - ); - batch->setColor(glm::vec4(1, 1, 1, 0.10f)); - batch->rect( - lcoord.x - LINE_NUMBERS_PANE_WIDTH, - lcoord.y + lineY, - LINE_NUMBERS_PANE_WIDTH - 8, - lineHeight - ); - } else { - batch->rect( - lcoord.x, lcoord.y + lineY, label->getSize().x, lineHeight - ); - } - line++; - } while (line < label->getLinesNumber() && label->isFakeLine(line)); + if (!multiline) { + return; } + + auto selectionCtx = subctx.sub(batch); + selectionCtx.setBlendMode(BlendMode::addition); + + batch->setColor(glm::vec4(1, 1, 1, 0.1f)); + + uint line = label->getLineByTextIndex(caret); + while (label->isFakeLine(line)) { + line--; + } + do { + int lineY = label->getLineYOffset(line); + + batch->setColor(glm::vec4(1, 1, 1, 0.05f)); + if (showLineNumbers) { + batch->rect( + lcoord.x - 8, + lcoord.y + lineY, + label->getSize().x, + lineHeight + ); + batch->setColor(glm::vec4(1, 1, 1, 0.10f)); + batch->rect( + lcoord.x - LINE_NUMBERS_PANE_WIDTH, + lcoord.y + lineY, + LINE_NUMBERS_PANE_WIDTH - 8, + lineHeight + ); + } else { + batch->rect( + lcoord.x, lcoord.y + lineY, label->getSize().x, lineHeight + ); + } + line++; + } while (line < label->getLinesNumber() && label->isFakeLine(line)); } void TextBox::drawBackground(const DrawContext& pctx, const Assets& assets) { @@ -605,6 +607,14 @@ size_t TextBox::getSelectionEnd() const { return selectionEnd; } +void TextBox::setKeepLineSelection(bool flag) { + keepLineSelection = flag; +} + +bool TextBox::isKeepLineSelection() const { + return keepLineSelection; +} + void TextBox::setOnEditStart(runnable oneditstart) { onEditStart = oneditstart; } diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index 682e300c..2f9080a5 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -62,6 +62,7 @@ namespace gui { bool editable = true; bool autoresize = false; bool showLineNumbers = false; + bool keepLineSelection = false; std::string markup; std::string syntax; @@ -222,6 +223,9 @@ namespace gui { size_t getSelectionStart() const; size_t getSelectionEnd() const; + void setKeepLineSelection(bool flag); + bool isKeepLineSelection() const; + /// @brief Set runnable called on textbox focus virtual void setOnEditStart(runnable oneditstart); diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 6dee06eb..3e8bcf5d 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -573,6 +573,11 @@ static std::shared_ptr read_text_box( if (element.has("line-numbers")) { textbox->setShowLineNumbers(element.attr("line-numbers").asBool()); } + if (element.has("keep-line-selection")) { + textbox->setKeepLineSelection( + element.attr("keep-line-selection").asBool() + ); + } if (element.has("markup")) { textbox->setMarkup(element.attr("markup").getText()); }