From 18b92145751b8b69abf1a84a99d747f700a95aa2 Mon Sep 17 00:00:00 2001 From: Paul Shen Date: Sun, 24 Jan 2021 06:05:59 -0800 Subject: [PATCH] Prefer overlapping row in galley cursor_from_pos (#127) --- epaint/src/text/galley.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/epaint/src/text/galley.rs b/epaint/src/text/galley.rs index 0eb729db..4e28fdd9 100644 --- a/epaint/src/text/galley.rs +++ b/epaint/src/text/galley.rs @@ -197,8 +197,9 @@ impl Galley { let mut pcursor_it = PCursor::default(); for (row_nr, row) in self.rows.iter().enumerate() { + let is_pos_within_row = pos.y >= row.y_min && pos.y <= row.y_max; let y_dist = (row.y_min - pos.y).abs().min((row.y_max - pos.y).abs()); - if y_dist < best_y_dist { + if is_pos_within_row || y_dist < best_y_dist { best_y_dist = y_dist; let column = row.char_at(pos.x); let prefer_next_row = column < row.char_count_excluding_newline(); @@ -216,6 +217,10 @@ impl Galley { offset: pcursor_it.offset + column, prefer_next_row, }, + }; + + if is_pos_within_row { + return cursor; } } ccursor_index += row.char_count_including_newline();