diff options
author | 2020-04-29 04:25:49 +0200 | |
---|---|---|
committer | 2020-04-29 04:25:49 +0200 | |
commit | dc51080328caa12d2b1fc02febc72cab70bb9f50 (patch) | |
tree | 0a9781223d1b78a8c404d57b17c61fb0136b196e /wgpu/src/widget/canvas/cursor.rs | |
parent | 5586034d6626e013cdd718aca1c4f19f6a060ff3 (diff) | |
download | iced-dc51080328caa12d2b1fc02febc72cab70bb9f50.tar.gz iced-dc51080328caa12d2b1fc02febc72cab70bb9f50.tar.bz2 iced-dc51080328caa12d2b1fc02febc72cab70bb9f50.zip |
Introduce `Cursor` type in `canvas`
Diffstat (limited to 'wgpu/src/widget/canvas/cursor.rs')
-rw-r--r-- | wgpu/src/widget/canvas/cursor.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/wgpu/src/widget/canvas/cursor.rs b/wgpu/src/widget/canvas/cursor.rs new file mode 100644 index 00000000..a559782a --- /dev/null +++ b/wgpu/src/widget/canvas/cursor.rs @@ -0,0 +1,50 @@ +use iced_native::{Point, Rectangle}; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Cursor { + Available(Point), + Unavailable, +} + +impl Cursor { + // TODO: Remove this once this type is used in `iced_native` to encode + // proper cursor availability + pub(crate) fn from_window_position(position: Point) -> Self { + if position.x < 0.0 || position.y < 0.0 { + Cursor::Unavailable + } else { + Cursor::Available(position) + } + } + + pub fn position(&self) -> Option<Point> { + match self { + Cursor::Available(position) => Some(*position), + Cursor::Unavailable => None, + } + } + + pub fn relative_position(&self, bounds: &Rectangle) -> Option<Point> { + match self { + Cursor::Available(position) => { + Some(Point::new(position.x - bounds.x, position.y - bounds.y)) + } + _ => None, + } + } + + pub fn internal_position(&self, bounds: &Rectangle) -> Option<Point> { + if self.is_over(bounds) { + self.relative_position(bounds) + } else { + None + } + } + + pub fn is_over(&self, bounds: &Rectangle) -> bool { + match self { + Cursor::Available(position) => bounds.contains(*position), + Cursor::Unavailable => false, + } + } +} |