From 737315c2280341ba6ba14f730d5cfdf43009bd0f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 25 Apr 2019 02:15:45 +0200 Subject: [PATCH] GTextEditor: Improve cursor positioning accuracy when using a mouse. Put the cursor on the left or right side of a glyph when clicking it based on distance to middle of glyphs. This is such an obvious change that makes a small but noticeable difference in feel. :^) --- LibGUI/GTextEditor.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index a90571914e..ee2683750f 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -107,6 +107,8 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const auto position = a_position; position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value()); position.move_by(-(m_horizontal_content_padding + ruler_width()), 0); + position.move_by(-frame_thickness(), -frame_thickness()); + int line_index = position.y() / line_height(); line_index = max(0, min(line_index, line_count() - 1)); auto& line = *m_lines[line_index]; @@ -114,16 +116,15 @@ GTextPosition GTextEditor::text_position_at(const Point& a_position) const int column_index; switch (m_text_alignment) { case TextAlignment::CenterLeft: - column_index = position.x() / glyph_width(); + column_index = (position.x() + glyph_width() / 2) / glyph_width(); break; case TextAlignment::CenterRight: - column_index = (position.x() - (content_width() - (line.length() * glyph_width()))) / glyph_width(); + column_index = (position.x() - content_x_for_position({ line_index, 0 }) + glyph_width() / 2) / glyph_width(); break; default: ASSERT_NOT_REACHED(); } - column_index = max(0, min(column_index, m_lines[line_index]->length())); return { line_index, column_index }; }