Skip to content

Commit

Permalink
Introduce Levitating variant for mouse::Cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 3, 2025
1 parent 3029481 commit 0c0651d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
39 changes: 32 additions & 7 deletions core/src/mouse/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{Point, Rectangle, Transformation, Vector};

use std::ops::Mul;

/// The mouse cursor state.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Cursor {
/// The cursor has a defined position.
Available(Point),

/// The cursor has a defined position, but it's levitating over a layer above.
Levitating(Point),

/// The cursor is currently unavailable (i.e. out of bounds or busy).
#[default]
Unavailable,
Expand All @@ -18,7 +19,7 @@ impl Cursor {
pub fn position(self) -> Option<Point> {
match self {
Cursor::Available(position) => Some(position),
Cursor::Unavailable => None,
Cursor::Levitating(_) | Cursor::Unavailable => None,
}
}

Expand Down Expand Up @@ -51,17 +52,41 @@ impl Cursor {
pub fn is_over(self, bounds: Rectangle) -> bool {
self.position_over(bounds).is_some()
}

/// Returns true if the [`Cursor`] is levitating over a layer above.
pub fn is_levitating(self) -> bool {
matches!(self, Self::Levitating(_))
}

/// Makes the [`Cursor`] levitate over a layer above.
pub fn levitate(self) -> Self {
match self {
Self::Available(position) => Self::Levitating(position),
_ => self,
}
}

/// Brings the [`Cursor`] back to the current layer.
pub fn land(self) -> Self {
match self {
Cursor::Levitating(position) => Cursor::Available(position),
_ => self,
}
}
}

impl Mul<Transformation> for Cursor {
impl std::ops::Mul<Transformation> for Cursor {
type Output = Self;

fn mul(self, transformation: Transformation) -> Self {
match self {
Cursor::Unavailable => Cursor::Unavailable,
Cursor::Available(point) => {
Cursor::Available(point * transformation)
Self::Available(position) => {
Self::Available(position * transformation)
}
Self::Levitating(position) => {
Self::Levitating(position * transformation)
}
Self::Unavailable => Self::Unavailable,
}
}
}
6 changes: 4 additions & 2 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ where
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
if let Some(scrollbar) = scrollbars.y {
let Some(cursor_position) = cursor.position()
let Some(cursor_position) =
cursor.land().position()
else {
return;
};
Expand Down Expand Up @@ -635,7 +636,8 @@ where
match event {
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
let Some(cursor_position) = cursor.position() else {
let Some(cursor_position) = cursor.land().position()
else {
return;
};

Expand Down
21 changes: 9 additions & 12 deletions widget/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ where
viewport: &Rectangle,
) {
let is_over = cursor.is_over(layout.bounds());
let is_mouse_movement =
matches!(event, Event::Mouse(mouse::Event::CursorMoved { .. }));
let end = self.children.len() - 1;

for ((child, state), layout) in self
for (i, ((child, state), layout)) in self
.children
.iter_mut()
.rev()
.zip(tree.children.iter_mut().rev())
.zip(layout.children().rev())
.enumerate()
{
child.as_widget_mut().update(
state,
Expand All @@ -237,22 +237,19 @@ where
viewport,
);

if is_over
&& !is_mouse_movement
&& cursor != mouse::Cursor::Unavailable
{
if shell.is_event_captured() {
return;
}

if i < end && is_over && !cursor.is_levitating() {
let interaction = child.as_widget().mouse_interaction(
state, layout, cursor, viewport, renderer,
);

if interaction != mouse::Interaction::None {
cursor = mouse::Cursor::Unavailable;
cursor = cursor.levitate();
}
}

if shell.is_event_captured() {
return;
}
}
}

Expand Down

0 comments on commit 0c0651d

Please sign in to comment.