Repository: bevyengine/bevy
File: crates/bevy_feathers/src/cursor.rs (Rust)

Code before:
/// any other entity. This is used to set the default cursor icon for the window.
#[derive(Deref, Resource, Debug, Clone, Default, Reflect)]
#[reflect(Resource, Debug, Default)]
pub struct DefaultCursor(pub EntityCursor);

/// A component that specifies the cursor shape to be used when the pointer hovers over an entity.
/// This is copied to the windows's [`CursorIcon`] component.
///
/// This is effectively the same type as [`CustomCursor`] but with different methods, and used
/// in different places.
#[derive(Component, Debug, Clone, Reflect, PartialEq, Eq)]
#[reflect(Component, Debug, Default, PartialEq, Clone)]
pub enum EntityCursor {
    #[cfg(feature = "custom_cursor")]
    /// Custom cursor image.
    Custom(CustomCursor),
    /// System provided cursor icon.
    System(SystemCursorIcon),
}

/// A component used to override any [`EntityCursor`] cursor changes.
///
/// This is meant for cases like loading where you don't want the cursor to imply you
/// can interact with something.
#[derive(Deref, Resource, Debug, Clone, Default, Reflect)]
pub struct OverrideCursor(pub Option<SystemCursorIcon>);

impl EntityCursor {
    /// Convert the [`EntityCursor`] to a [`CursorIcon`] so that it can be inserted into a
    /// window.
    pub fn to_cursor_icon(&self) -> CursorIcon {
        match self {
            #[cfg(feature = "custom_cursor")]
            EntityCursor::Custom(custom_cursor) => CursorIcon::Custom(custom_cursor.clone()),
            EntityCursor::System(icon) => CursorIcon::from(*icon),
        }
    }

    /// Compare the [`EntityCursor`] to a [`CursorIcon`] so that we can see whether or not
    /// the window cursor needs to be changed.
    pub fn eq_cursor_icon(&self, cursor_icon: &CursorIcon) -> bool {
        // If feature custom_cursor is not enabled in bevy_feathers, we can't know if it is or not
        // in bevy_window. So we use the wrapper function `as_system` to let bevy_window check its own feature.
        // Otherwise it is not possible to have a match that both covers all cases and doesn't have unreachable
        // branches under all feature combinations.
        match (self, cursor_icon, cursor_icon.as_system()) {
            #[cfg(feature = "custom_cursor")]
            (EntityCursor::Custom(custom), CursorIcon::Custom(other), _) => custom == other,
            (EntityCursor::System(system), _, Some(cursor_icon)) => *system == *cursor_icon,
            _ => false,
        }

Review comment (suggestion):
I would suggest to store `Option<EntityCursor>` instead.  Similarly as it is done in `DefaultCursor`.
This would allow to use CustomCursors when overriding current cursor.

This additionally minimizes other changes that are done in this PR. 

Code after:
/// any other entity. This is used to set the default cursor icon for the window.
#[derive(Deref, Resource, Debug, Clone, Default, Reflect)]
#[reflect(Resource, Debug, Default)]
pub struct DefaultCursor(pub EntityCursor);

/// A component that specifies the cursor shape to be used when the pointer hovers over an entity.
/// This is copied to the windows's [`CursorIcon`] component.
///
/// This is effectively the same type as [`CustomCursor`] but with different methods, and used
/// in different places.
#[derive(Component, Debug, Clone, Reflect, PartialEq, Eq)]
#[reflect(Component, Debug, Default, PartialEq, Clone)]
pub enum EntityCursor {
    #[cfg(feature = "custom_cursor")]
    /// Custom cursor image.
    Custom(CustomCursor),
    /// System provided cursor icon.
    System(SystemCursorIcon),
}

/// A component used to override any [`EntityCursor`] cursor changes.
///
/// This is meant for cases like loading where you don't want the cursor to imply you
/// can interact with something.
#[derive(Deref, Resource, Debug, Clone, Default, Reflect)]
pub struct OverrideCursor(pub Option<EntityCursor>);

impl EntityCursor {
    /// Convert the [`EntityCursor`] to a [`CursorIcon`] so that it can be inserted into a
    /// window.
    pub fn to_cursor_icon(&self) -> CursorIcon {
        match self {
            #[cfg(feature = "custom_cursor")]
            EntityCursor::Custom(custom_cursor) => CursorIcon::Custom(custom_cursor.clone()),
            EntityCursor::System(icon) => CursorIcon::from(*icon),
        }
    }

    /// Compare the [`EntityCursor`] to a [`CursorIcon`] so that we can see whether or not
    /// the window cursor needs to be changed.
    pub fn eq_cursor_icon(&self, cursor_icon: &CursorIcon) -> bool {
        // If feature custom_cursor is not enabled in bevy_feathers, we can't know if it is or not
        // in bevy_window. So we use the wrapper function `as_system` to let bevy_window check its own feature.
        // Otherwise it is not possible to have a match that both covers all cases and doesn't have unreachable
        // branches under all feature combinations.
        match (self, cursor_icon, cursor_icon.as_system()) {
            #[cfg(feature = "custom_cursor")]
            (EntityCursor::Custom(custom), CursorIcon::Custom(other), _) => custom == other,
            (EntityCursor::System(system), _, Some(cursor_icon)) => *system == *cursor_icon,
            _ => false,
        }