summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/src/hit_test.rs28
-rw-r--r--core/src/lib.rs2
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;