diff options
author | 2021-09-15 15:50:10 +0700 | |
---|---|---|
committer | 2021-09-15 15:50:10 +0700 | |
commit | 5870cbb312ec5de1219b6983e09e460419d95912 (patch) | |
tree | add25c986ba0582628d921e45c2eae4006e19ee4 /wgpu/src | |
parent | 93fec8d273ef8305e1c2456abe0c8ecd7a9d9407 (diff) | |
parent | c914b2a05be2c33ec936f63efd25daaf2b4c4739 (diff) | |
download | iced-5870cbb312ec5de1219b6983e09e460419d95912.tar.gz iced-5870cbb312ec5de1219b6983e09e460419d95912.tar.bz2 iced-5870cbb312ec5de1219b6983e09e460419d95912.zip |
Merge pull request #1046 from iced-rs/fix/empty-text-hit-test
Use `Option` to encode empty text case in hit test methods
Diffstat (limited to '')
-rw-r--r-- | wgpu/src/backend.rs | 2 | ||||
-rw-r--r-- | wgpu/src/text.rs | 26 |
2 files changed, 14 insertions, 14 deletions
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index b31bf92c..51429e84 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -284,7 +284,7 @@ impl backend::Text for Backend { bounds: Size, point: iced_native::Point, nearest_only: bool, - ) -> text::Hit { + ) -> Option<text::Hit> { self.text_pipeline.hit_test( contents, size, diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index ee49ee4b..336696ee 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -129,7 +129,7 @@ impl Pipeline { bounds: iced_native::Size, point: iced_native::Point, nearest_only: bool, - ) -> Hit { + ) -> Option<Hit> { use wgpu_glyph::GlyphCruncher; let wgpu_glyph::FontId(font_id) = self.find_font(font); @@ -190,23 +190,23 @@ 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()) - } else { - acc - } - }, - ); + let nearest = bounds + .map(|(index, bounds)| (index, bounds.center())) + .min_by(|(_, center_a), (_, center_b)| { + center_a + .distance(point) + .partial_cmp(¢er_b.distance(point)) + .unwrap_or(std::cmp::Ordering::Greater) + }); - Hit::NearestCharOffset(char_index(idx), (point - nearest).into()) + nearest.map(|(idx, center)| { + Hit::NearestCharOffset(char_index(idx), point - center) + }) } pub fn trim_measurement_cache(&mut self) { |