Skip to content

Commit

Permalink
feat: add Hide variant to mouse Interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Oct 7, 2024
1 parent 3dc0bbf commit a2be497
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions core/src/mouse/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pub enum Interaction {
ResizingHorizontally,
ResizingVertically,
NotAllowed,
Hide,
}
7 changes: 4 additions & 3 deletions sctk/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pub fn modifiers_to_native(mods: Modifiers) -> keyboard::Modifiers {
// key_conversion.get(&keysym).cloned()
// }

pub(crate) fn cursor_icon(cursor: Interaction) -> CursorIcon {
match cursor {
pub(crate) fn cursor_icon(cursor: Interaction) -> Option<CursorIcon> {
Some(match cursor {
Interaction::Idle => CursorIcon::Default,
Interaction::Pointer => CursorIcon::Pointer,
Interaction::Grab => CursorIcon::Grab,
Expand All @@ -92,5 +92,6 @@ pub(crate) fn cursor_icon(cursor: Interaction) -> CursorIcon {
Interaction::ResizingHorizontally => CursorIcon::EwResize,
Interaction::ResizingVertically => CursorIcon::NsResize,
Interaction::NotAllowed => CursorIcon::NotAllowed,
}
Interaction::Hide => return None,
})
}
12 changes: 9 additions & 3 deletions sctk/src/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,15 @@ where
},
Event::SetCursor(iced_icon) => {
if let Some(seat) = self.state.seats.get_mut(0) {
let icon = conversion::cursor_icon(iced_icon);
seat.icon = Some(icon);
seat.set_cursor(self.wayland_dispatcher.as_source_ref().connection(), icon);
if let Some(icon) = conversion::cursor_icon(iced_icon) {
seat.icon = Some(icon);
seat.set_cursor(self.wayland_dispatcher.as_source_ref().connection(), icon);
} else {
if let Some(ptr) = seat.ptr.as_ref() {
_ = ptr.hide_cursor();
}
}

}

}
Expand Down
14 changes: 11 additions & 3 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,17 @@ async fn run_instance<A, E, C>(
debug.draw_finished();

if new_mouse_interaction != mouse_interaction {
window.set_cursor_icon(conversion::mouse_interaction(
new_mouse_interaction,
));
if let Some(interation) =
conversion::mouse_interaction(new_mouse_interaction)
{
if matches!(mouse_interaction, mouse::Interaction::Hide)
{
window.set_cursor_visible(true);
}
window.set_cursor_icon(interation);
} else {
window.set_cursor_visible(false);
}

mouse_interaction = new_mouse_interaction;
}
Expand Down
7 changes: 4 additions & 3 deletions winit/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ pub fn mode(mode: Option<winit::window::Fullscreen>) -> window::Mode {
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn mouse_interaction(
interaction: mouse::Interaction,
) -> winit::window::CursorIcon {
) -> Option<winit::window::CursorIcon> {
use mouse::Interaction;

match interaction {
Some(match interaction {
Interaction::Idle => winit::window::CursorIcon::Default,
Interaction::Pointer => winit::window::CursorIcon::Pointer,
Interaction::Working => winit::window::CursorIcon::Progress,
Expand All @@ -391,7 +391,8 @@ pub fn mouse_interaction(
}
Interaction::ResizingVertically => winit::window::CursorIcon::NsResize,
Interaction::NotAllowed => winit::window::CursorIcon::NotAllowed,
}
Interaction::Hide => return None,
})
}

/// Converts a `MouseButton` from [`winit`] to an [`iced`] mouse button.
Expand Down
32 changes: 26 additions & 6 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,21 @@ async fn run_instance<A, E, C>(
debug.draw_finished();

if new_mouse_interaction != window.mouse_interaction {
window.raw.set_cursor_icon(
if let Some(interation) =
conversion::mouse_interaction(
new_mouse_interaction,
),
);
)
{
if matches!(
mouse_interaction,
mouse::Interaction::Hide
) {
window.set_cursor_visible(true);
}
window.set_cursor_icon(interation);
} else {
window.set_cursor_visible(false);
}

window.mouse_interaction = new_mouse_interaction;
}
Expand Down Expand Up @@ -916,11 +926,21 @@ async fn run_instance<A, E, C>(

if new_mouse_interaction != window.mouse_interaction
{
window.raw.set_cursor_icon(
if let Some(interation) =
conversion::mouse_interaction(
new_mouse_interaction,
),
);
)
{
if matches!(
mouse_interaction,
mouse::Interaction::Hide
) {
window.set_cursor_visible(true);
}
window.set_cursor_icon(interation);
} else {
window.set_cursor_visible(false);
}

window.mouse_interaction =
new_mouse_interaction;
Expand Down

0 comments on commit a2be497

Please sign in to comment.