From 643500bbdf1a169db1bcdcb8e971334037274d38 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 15 Sep 2021 14:49:13 +0700 Subject: Use `Option` to encode empty text case in hit test methods --- glow/src/backend.rs | 2 +- glow/src/text.rs | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'glow') diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 37c0ac9d..526965cb 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -221,7 +221,7 @@ impl backend::Text for Backend { bounds: Size, point: iced_native::Point, nearest_only: bool, - ) -> text::Hit { + ) -> Option { self.text_pipeline.hit_test( contents, size, diff --git a/glow/src/text.rs b/glow/src/text.rs index d6915d92..4ebd7087 100644 --- a/glow/src/text.rs +++ b/glow/src/text.rs @@ -121,7 +121,7 @@ impl Pipeline { bounds: iced_native::Size, point: iced_native::Point, nearest_only: bool, - ) -> Hit { + ) -> Option { use glow_glyph::GlyphCruncher; let glow_glyph::FontId(font_id) = self.find_font(font); @@ -182,23 +182,25 @@ impl Pipeline { if !nearest_only { for (idx, bounds) in bounds.clone() { if bounds.contains(point) { - return Hit::CharOffset(char_index(idx)); + return Some(Hit::CharOffset(char_index(idx))); } } } let (idx, nearest) = bounds.fold( - (0usize, iced_native::Point::ORIGIN), - |acc: (usize, iced_native::Point), (idx, bounds)| { - if bounds.center().distance(point) < acc.1.distance(point) { - (idx, bounds.center()) + (None, iced_native::Point::ORIGIN), + |best, (idx, bounds)| { + let center = bounds.center(); + + if center.distance(point) < best.1.distance(point) { + (Some(idx), center) } else { - acc + best } }, ); - Hit::NearestCharOffset(char_index(idx), (point - nearest).into()) + idx.map(|idx| Hit::NearestCharOffset(char_index(idx), point - nearest)) } pub fn trim_measurement_cache(&mut self) { -- cgit