diff options
| author | 2023-03-04 05:37:11 +0100 | |
|---|---|---|
| committer | 2023-03-04 05:37:11 +0100 | |
| commit | 3a0d34c0240f4421737a6a08761f99d6f8140d02 (patch) | |
| tree | c9a4a6b8e9c1db1b8fcd05bc98e3f131d5ef4bd5 /widget/src/canvas/cursor.rs | |
| parent | c54409d1711e1f615c7ea4b02c082954e340632a (diff) | |
| download | iced-3a0d34c0240f4421737a6a08761f99d6f8140d02.tar.gz iced-3a0d34c0240f4421737a6a08761f99d6f8140d02.tar.bz2 iced-3a0d34c0240f4421737a6a08761f99d6f8140d02.zip | |
Create `iced_widget` subcrate and re-organize the whole codebase
Diffstat (limited to 'widget/src/canvas/cursor.rs')
| -rw-r--r-- | widget/src/canvas/cursor.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/widget/src/canvas/cursor.rs b/widget/src/canvas/cursor.rs new file mode 100644 index 00000000..5a65e9a7 --- /dev/null +++ b/widget/src/canvas/cursor.rs @@ -0,0 +1,64 @@ +use crate::core::{Point, Rectangle}; + +/// The mouse cursor state. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Cursor { + /// The cursor has a defined position. + Available(Point), + + /// The cursor is currently unavailable (i.e. out of bounds or busy). + 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) + } + } + + /// Returns the absolute position of the [`Cursor`], if available. + pub fn position(&self) -> Option<Point> { + match self { + Cursor::Available(position) => Some(*position), + Cursor::Unavailable => None, + } + } + + /// Returns the relative position of the [`Cursor`] inside the given bounds, + /// if available. + /// + /// If the [`Cursor`] is not over the provided bounds, this method will + /// return `None`. + pub fn position_in(&self, bounds: &Rectangle) -> Option<Point> { + if self.is_over(bounds) { + self.position_from(bounds.position()) + } else { + None + } + } + + /// Returns the relative position of the [`Cursor`] from the given origin, + /// if available. + pub fn position_from(&self, origin: Point) -> Option<Point> { + match self { + Cursor::Available(position) => { + Some(Point::new(position.x - origin.x, position.y - origin.y)) + } + Cursor::Unavailable => None, + } + } + + /// Returns whether the [`Cursor`] is currently over the provided bounds + /// or not. + pub fn is_over(&self, bounds: &Rectangle) -> bool { + match self { + Cursor::Available(position) => bounds.contains(*position), + Cursor::Unavailable => false, + } + } +} |
