diff options
author | 2021-08-21 10:31:26 -0700 | |
---|---|---|
committer | 2021-08-21 10:31:26 -0700 | |
commit | aa63841e2c80ca8130adf41d25e5d731409b92f4 (patch) | |
tree | 4dab3f0405dc002a06f0e36ec40f2f74ff07212b /core | |
parent | 8333b8f88ceaa53c361eb6726b2b7dac6cd2c402 (diff) | |
download | iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.gz iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.tar.bz2 iced-aa63841e2c80ca8130adf41d25e5d731409b92f4.zip |
Implement textual hit testing
Diffstat (limited to 'core')
-rw-r--r-- | core/src/hit_test.rs | 28 | ||||
-rw-r--r-- | core/src/lib.rs | 2 |
2 files changed, 30 insertions, 0 deletions
diff --git a/core/src/hit_test.rs b/core/src/hit_test.rs new file mode 100644 index 00000000..a4a7b610 --- /dev/null +++ b/core/src/hit_test.rs @@ -0,0 +1,28 @@ +use crate::Vector; + +/// The result of hit testing on text. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum HitTestResult { + /// The point was within the bounds of the returned character index. + CharOffset(usize), + /// The provided point was not within the bounds of a glyph. The index + /// of the character with the closest centeroid position is returned, + /// as well as its delta. + NearestCharOffset(usize, Vector), +} + +impl HitTestResult { + /// Computes the cursor position corresponding to this [`HitTestResult`] . + pub fn cursor(&self) -> usize { + match self { + HitTestResult::CharOffset(i) => *i, + HitTestResult::NearestCharOffset(i, delta) => { + if delta.x > f32::EPSILON { + i + 1 + } else { + *i + } + } + } + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs index c4288158..7c4c3c0c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -22,6 +22,7 @@ mod align; mod background; mod color; mod font; +mod hit_test; mod length; mod padding; mod point; @@ -33,6 +34,7 @@ pub use align::{Align, HorizontalAlignment, VerticalAlignment}; pub use background::Background; pub use color::Color; pub use font::Font; +pub use hit_test::HitTestResult; pub use length::Length; pub use menu::Menu; pub use padding::Padding; |