Skip to content

Commit de34f29

Browse files
hecrjEmmanuelDodoo
andcommitted
Fix Paragraph::hit_span returning false positives at end of line
Co-authored-by: EmmanuelDodoo <[email protected]>
1 parent b9eeb34 commit de34f29

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

graphics/src/text/paragraph.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,28 +271,30 @@ impl core::text::Paragraph for Paragraph {
271271
let cursor = internal.buffer.hit(point.x, point.y)?;
272272
let line = internal.buffer.lines.get(cursor.line)?;
273273

274-
let mut last_glyph = None;
275-
let mut glyphs = line
274+
if cursor.index >= line.text().len() {
275+
return None;
276+
}
277+
278+
let index = match cursor.affinity {
279+
cosmic_text::Affinity::Before => cursor.index.saturating_sub(1),
280+
cosmic_text::Affinity::After => cursor.index,
281+
};
282+
283+
let mut hit = None;
284+
let glyphs = line
276285
.layout_opt()
277286
.as_ref()?
278287
.iter()
279-
.flat_map(|line| line.glyphs.iter())
280-
.peekable();
288+
.flat_map(|line| line.glyphs.iter());
281289

282-
while let Some(glyph) = glyphs.peek() {
283-
if glyph.start <= cursor.index && cursor.index < glyph.end {
290+
for glyph in glyphs {
291+
if glyph.start <= index && index < glyph.end {
292+
hit = Some(glyph);
284293
break;
285294
}
286-
287-
last_glyph = glyphs.next();
288295
}
289296

290-
let glyph = match cursor.affinity {
291-
cosmic_text::Affinity::Before => last_glyph,
292-
cosmic_text::Affinity::After => glyphs.next(),
293-
}?;
294-
295-
Some(glyph.metadata)
297+
Some(hit?.metadata)
296298
}
297299

298300
fn span_bounds(&self, index: usize) -> Vec<Rectangle> {

0 commit comments

Comments
 (0)