diff options
author | 2023-09-14 00:47:04 +0200 | |
---|---|---|
committer | 2023-09-14 00:47:04 +0200 | |
commit | e6c2db8a9312e3fe37f30f049d1fa497892f1a86 (patch) | |
tree | 4c169d1091026b60f7b99c0745dfc4296bef2a73 /graphics | |
parent | ab020383b9fd7f2cc15d145dd1a3c0870dc71d8b (diff) | |
download | iced-e6c2db8a9312e3fe37f30f049d1fa497892f1a86.tar.gz iced-e6c2db8a9312e3fe37f30f049d1fa497892f1a86.tar.bz2 iced-e6c2db8a9312e3fe37f30f049d1fa497892f1a86.zip |
Fix `Cursor::Caret` position on lines that wrap on whitespace
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/text/editor.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs index c0f8d9d5..83d41c85 100644 --- a/graphics/src/text/editor.rs +++ b/graphics/src/text/editor.rs @@ -157,7 +157,7 @@ impl editor::Editor for Editor { .map(|glyph| glyph.end) .unwrap_or(0); - let is_cursor_after_start = start <= cursor.index; + let is_cursor_before_start = start > cursor.index; let is_cursor_before_end = match cursor.affinity { cosmic_text::Affinity::Before => { @@ -166,7 +166,17 @@ impl editor::Editor for Editor { cosmic_text::Affinity::After => cursor.index < end, }; - if is_cursor_after_start && is_cursor_before_end { + if is_cursor_before_start { + // Sometimes, the glyph we are looking for is right + // between lines. This can happen when a line wraps + // on a space. + // In that case, we can assume the cursor is at the + // end of the previous line. + // i is guaranteed to be > 0 because `start` is always + // 0 for the first line, so there is no way for the + // cursor to be before it. + Some((i - 1, layout[i - 1].w)) + } else if is_cursor_before_end { let offset = line .glyphs .iter() |