summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVille Hakulinen <ville.hakulinen@gmail.com>2020-07-23 12:58:23 +0300
committerVille Hakulinen <ville.hakulinen@gmail.com>2020-07-23 12:58:23 +0300
commite97d36efb4febed8c532497be466c3f1379ab013 (patch)
tree17e343a36d5614dccb58ce89e8c0781aeef38018
parent2adccccd6b96c23a22d19c5bed8d17df681a4d4f (diff)
Correctly get cursor position
-rw-r--r--src/ui/grid/context.rs4
-rw-r--r--src/ui/grid/cursor.rs25
2 files changed, 26 insertions, 3 deletions
diff --git a/src/ui/grid/context.rs b/src/ui/grid/context.rs
index d62ab1e..8f4cab1 100644
--- a/src/ui/grid/context.rs
+++ b/src/ui/grid/context.rs
@@ -288,9 +288,7 @@ impl Context {
}
pub fn cell_at_cursor(&self) -> Option<&Cell> {
- // TODO(ville): In some (all?) cases we want to get animation's target position if it
- // differs from the current position.
- self.cursor.pos.and_then(|pos| {
+ self.cursor.get_position().and_then(|pos| {
self.rows
.get(pos.0.ceil() as usize)
.and_then(|row| row.cell_at(pos.1.ceil() as usize))
diff --git a/src/ui/grid/cursor.rs b/src/ui/grid/cursor.rs
index 3a3a5d4..6b4239c 100644
--- a/src/ui/grid/cursor.rs
+++ b/src/ui/grid/cursor.rs
@@ -91,6 +91,17 @@ impl Cursor {
}
}
}
+
+ /// Gets the position of the cursor.
+ pub fn get_position(&self) -> Option<(f64, f64)> {
+ if let Some(ref a) = self.animation {
+ // The end position of our animation is the "real" position where
+ // the cursor is.
+ Some(a.end)
+ } else {
+ self.pos
+ }
+ }
}
/// From clutter-easing.c, based on Robert Penner's
@@ -167,4 +178,18 @@ mod tests {
cursor.tick(25000);
assert_eq!(cursor.pos, Some((10.0, 10.0)));
}
+
+ #[test]
+ fn test_get_position() {
+ let mut cursor = Cursor::default();
+
+ assert_eq!(cursor.get_position(), None);
+ cursor.pos = Some((10.0, 10.0));
+ assert_eq!(cursor.get_position(), Some((10.0, 10.0)));
+ cursor.animation = Some(Animation {
+ end: (15.0, 15.0),
+ ..Animation::default()
+ });
+ assert_eq!(cursor.get_position(), Some((15.0, 15.0)));
+ }
}