From 9c31c6c8b704831ded7f485d3b756c09ca828342 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 22 Feb 2024 23:53:41 +0300 Subject: [PATCH] minor refactor --- src/frontend/gui/controls.cpp | 18 ++++++++++++++---- src/frontend/gui/controls.h | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 9b29c6b1..a68830be 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -514,10 +514,7 @@ void TextBox::keyPressed(int key) { // Select/deselect all if (key == keycode::A) { if (selectionStart == selectionEnd) { - selectionStart = 0; - selectionEnd = input.length(); - selectionOrigin = 0; - setCaret(selectionEnd); + select(0, input.length()); } else { resetSelection(); } @@ -525,6 +522,19 @@ void TextBox::keyPressed(int key) { } } +void TextBox::select(int start, int end) { + if (end < start) { + std::swap(start, end); + } + start = normalizeIndex(start); + end = normalizeIndex(end); + + selectionStart = start; + selectionEnd = end; + selectionOrigin = start; + setCaret(selectionEnd); +} + std::shared_ptr TextBox::getAt(glm::vec2 pos, std::shared_ptr self) { return UINode::getAt(pos, self); } diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index 2452992b..9e2219b9 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -157,6 +157,7 @@ namespace gui { virtual std::wstring getSelection() const; virtual uint getCaret() const; virtual void setCaret(uint position); + virtual void select(int start, int end); virtual bool validate(); virtual void setValid(bool valid); virtual bool isValid() const;