From 015c5e61a87088359077c585275ca05153fb2e72 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 15 Feb 2021 16:59:13 +0100 Subject: [PATCH] LibGUI: Fix bogus TextEditor selection when cursor dragged outside view If the cursor Y position is < 0 in content coordinate space, we should always map that to the first line of the file. This fixes unexpected cursor behavior when dragging the selection above the top of the document. --- Userland/Libraries/LibGUI/TextEditor.cpp | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index 8ae442af26..5a745d52c9 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -157,22 +157,23 @@ TextPosition TextEditor::text_position_at_content_position(const Gfx::IntPoint& size_t line_index = 0; - if (is_wrapping_enabled()) { - for (size_t i = 0; i < line_count(); ++i) { - auto& rect = m_line_visual_data[i].visual_rect; - if (position.y() >= rect.top() && position.y() <= rect.bottom()) { - line_index = i; - break; + if (position.y() >= 0) { + if (is_wrapping_enabled()) { + for (size_t i = 0; i < line_count(); ++i) { + auto& rect = m_line_visual_data[i].visual_rect; + if (position.y() >= rect.top() && position.y() <= rect.bottom()) { + line_index = i; + break; + } + if (position.y() > rect.bottom()) + line_index = line_count() - 1; } - if (position.y() > rect.bottom()) - line_index = line_count() - 1; + } else { + line_index = (size_t)(position.y() / line_height()); } - } else { - line_index = (size_t)(position.y() / line_height()); + line_index = max((size_t)0, min(line_index, line_count() - 1)); } - line_index = max((size_t)0, min(line_index, line_count() - 1)); - size_t column_index = 0; switch (m_text_alignment) { case Gfx::TextAlignment::CenterLeft: