diff options
author | 2021-08-26 14:53:15 +0700 | |
---|---|---|
committer | 2021-08-26 14:53:15 +0700 | |
commit | 6821114cae2e41fd2bc69d6fcaee1e8574ac061d (patch) | |
tree | 5741859eba63251190eb0a901a72ef4e185349e7 /graphics | |
parent | 2d65621a3b680457e689b93c800e74f726ffc175 (diff) | |
parent | 7614127d3641cf3224798c2f0ff07b6ae57d9a53 (diff) | |
download | iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.tar.gz iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.tar.bz2 iced-6821114cae2e41fd2bc69d6fcaee1e8574ac061d.zip |
Merge pull request #670 from twitchyliquid64/text_backend
Refactor textual hit testing into a `renderer::Backend` method
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/backend.rs | 20 | ||||
-rw-r--r-- | graphics/src/widget/text.rs | 21 |
2 files changed, 39 insertions, 2 deletions
diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index ed1b9e08..656949c5 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,8 @@ //! Write a graphics backend. use iced_native::image; use iced_native::svg; -use iced_native::{Font, Size}; +use iced_native::text; +use iced_native::{Font, Point, Size}; /// The graphics backend of a [`Renderer`]. /// @@ -43,6 +44,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, + ) -> text::Hit; } /// A graphics backend that supports image rendering. diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index 7e22e680..c235f254 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -4,7 +4,7 @@ use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::text; use iced_native::{ - Color, Font, HorizontalAlignment, Rectangle, Size, VerticalAlignment, + Color, Font, HorizontalAlignment, Point, Rectangle, Size, VerticalAlignment, }; /// A paragraph of text. @@ -35,6 +35,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, + ) -> text::Hit { + self.backend().hit_test( + content, + size, + font, + bounds, + point, + nearest_only, + ) + } + fn draw( &mut self, defaults: &Self::Defaults, |