diff options
author | 2021-08-21 10:31:26 -0700 | |
---|---|---|
committer | 2021-08-21 10:31:26 -0700 | |
commit | aa63841e2c80ca8130adf41d25e5d731409b92f4 (patch) | |
tree | 4dab3f0405dc002a06f0e36ec40f2f74ff07212b /graphics | |
parent | 8333b8f88ceaa53c361eb6726b2b7dac6cd2c402 (diff) | |
download | iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.gz iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.bz2 iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.zip |
Implement textual hit testing
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/backend.rs | 19 | ||||
-rw-r--r-- | graphics/src/lib.rs | 4 | ||||
-rw-r--r-- | graphics/src/widget/text.rs | 22 |
3 files changed, 41 insertions, 4 deletions
diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index ed1b9e08..94124509 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,7 @@ //! Write a graphics backend. use iced_native::image; use iced_native::svg; -use iced_native::{Font, Size}; +use iced_native::{Font, HitTestResult, Point, Size}; /// The graphics backend of a [`Renderer`]. /// @@ -43,6 +43,23 @@ pub trait Text { font: Font, bounds: Size, ) -> (f32, f32); + + /// Tests whether the provided point is within the boundaries of [`Text`] + /// laid out with the given parameters, returning information about + /// the nearest character. + /// + /// If nearest_only is true, the hit test does not consider whether the + /// the point is interior to any glyph bounds, returning only the character + /// with the nearest centeroid. + fn hit_test( + &self, + contents: &str, + size: f32, + font: Font, + bounds: Size, + point: Point, + nearest_only: bool, + ) -> HitTestResult; } /// A graphics backend that supports image rendering. diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 14388653..687294fc 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -40,6 +40,6 @@ pub use transformation::Transformation; pub use viewport::Viewport; pub use iced_native::{ - Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size, - Vector, VerticalAlignment, + Background, Color, Font, HitTestResult, HorizontalAlignment, Point, + Rectangle, Size, Vector, VerticalAlignment, }; diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index 7e22e680..e1b4e4e8 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -4,7 +4,8 @@ use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::text; use iced_native::{ - Color, Font, HorizontalAlignment, Rectangle, Size, VerticalAlignment, + Color, Font, HitTestResult, HorizontalAlignment, Point, Rectangle, Size, + VerticalAlignment, }; /// A paragraph of text. @@ -35,6 +36,25 @@ where .measure(content, f32::from(size), font, bounds) } + fn hit_test( + &self, + content: &str, + size: f32, + font: Font, + bounds: Size, + point: Point, + nearest_only: bool, + ) -> HitTestResult { + self.backend().hit_test( + content, + size, + font, + bounds, + point, + nearest_only, + ) + } + fn draw( &mut self, defaults: &Self::Defaults, |