1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:08:11 +00:00

LibGUI: Make text selection feel better in single-line editors

We should always stay on the only line when selecting in a single-line
editor, instead of requiring the user to keep the cursor inside the
text when selecting.

This broke with the variable-width font changes.
This commit is contained in:
Andreas Kling 2020-05-18 17:55:21 +02:00
parent 3d5233ae40
commit bdfd1f1545

View file

@ -164,24 +164,23 @@ TextPosition TextEditor::text_position_at(const Gfx::Point& a_position) const
switch (m_text_alignment) { switch (m_text_alignment) {
case Gfx::TextAlignment::CenterLeft: case Gfx::TextAlignment::CenterLeft:
for_each_visual_line(line_index, [&](const Gfx::Rect& rect, auto& view, size_t start_of_line) { for_each_visual_line(line_index, [&](const Gfx::Rect& rect, auto& view, size_t start_of_line) {
if (rect.contains_vertically(position.y())) { if (is_multi_line() && !rect.contains_vertically(position.y()))
column_index = start_of_line; return IterationDecision::Continue;
if (position.x() <= 0) { column_index = start_of_line;
// We're outside the text on the left side, put cursor at column 0 on this visual line. if (position.x() <= 0) {
} else { // We're outside the text on the left side, put cursor at column 0 on this visual line.
int glyph_x = 0; } else {
size_t i = 0; int glyph_x = 0;
for (; i < view.length(); ++i) { size_t i = 0;
int advance = font().glyph_width(view.codepoints()[i]) + font().glyph_spacing(); for (; i < view.length(); ++i) {
if ((glyph_x + (advance / 2)) >= position.x()) int advance = font().glyph_width(view.codepoints()[i]) + font().glyph_spacing();
break; if ((glyph_x + (advance / 2)) >= position.x())
glyph_x += advance; break;
} glyph_x += advance;
column_index += i;
} }
return IterationDecision::Break; column_index += i;
} }
return IterationDecision::Continue; return IterationDecision::Break;
}); });
break; break;
case Gfx::TextAlignment::CenterRight: case Gfx::TextAlignment::CenterRight: