Implement basic cursor availability

This commit is contained in:
Héctor Ramón Jiménez 2023-06-08 20:11:59 +02:00
parent c15f1b5f65
commit 34451bff18
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
55 changed files with 731 additions and 886 deletions

View file

@ -1,64 +0,0 @@
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,
}
}
}

View file

@ -1,6 +1,5 @@
use crate::canvas::event::{self, Event};
use crate::canvas::mouse;
use crate::canvas::Cursor;
use crate::core::Rectangle;
use crate::graphics::geometry::{self, Geometry};
@ -33,7 +32,7 @@ where
_state: &mut Self::State,
_event: Event,
_bounds: Rectangle,
_cursor: Cursor,
_cursor: mouse::Cursor,
) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None)
}
@ -51,7 +50,7 @@ where
renderer: &Renderer,
theme: &Renderer::Theme,
bounds: Rectangle,
cursor: Cursor,
cursor: mouse::Cursor,
) -> Vec<Geometry>;
/// Returns the current mouse interaction of the [`Program`].
@ -64,7 +63,7 @@ where
&self,
_state: &Self::State,
_bounds: Rectangle,
_cursor: Cursor,
_cursor: mouse::Cursor,
) -> mouse::Interaction {
mouse::Interaction::default()
}
@ -82,7 +81,7 @@ where
state: &mut Self::State,
event: Event,
bounds: Rectangle,
cursor: Cursor,
cursor: mouse::Cursor,
) -> (event::Status, Option<Message>) {
T::update(self, state, event, bounds, cursor)
}
@ -93,7 +92,7 @@ where
renderer: &Renderer,
theme: &Renderer::Theme,
bounds: Rectangle,
cursor: Cursor,
cursor: mouse::Cursor,
) -> Vec<Geometry> {
T::draw(self, state, renderer, theme, bounds, cursor)
}
@ -102,7 +101,7 @@ where
&self,
state: &Self::State,
bounds: Rectangle,
cursor: Cursor,
cursor: mouse::Cursor,
) -> mouse::Interaction {
T::mouse_interaction(self, state, bounds, cursor)
}