From 4a4195f0274cc0e08ef487121e928d14256d655b Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 7 Oct 2024 13:46:28 -0400 Subject: [PATCH] feat: add Hide variant to mouse Interaction --- core/src/mouse/interaction.rs | 1 + sctk/src/conversion.rs | 7 ++++--- sctk/src/event_loop/mod.rs | 12 +++++++++--- widget/src/container.rs | 1 + winit/src/application.rs | 14 +++++++++++--- winit/src/conversion.rs | 7 ++++--- winit/src/multi_window.rs | 32 ++++++++++++++++++++++++++------ 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/core/src/mouse/interaction.rs b/core/src/mouse/interaction.rs index 072033fdd4..41442ebe2d 100644 --- a/core/src/mouse/interaction.rs +++ b/core/src/mouse/interaction.rs @@ -13,4 +13,5 @@ pub enum Interaction { ResizingHorizontally, ResizingVertically, NotAllowed, + Hide, } diff --git a/sctk/src/conversion.rs b/sctk/src/conversion.rs index f1f46d99f9..ca9b0cbb0e 100644 --- a/sctk/src/conversion.rs +++ b/sctk/src/conversion.rs @@ -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 { + Some(match cursor { Interaction::Idle => CursorIcon::Default, Interaction::Pointer => CursorIcon::Pointer, Interaction::Grab => CursorIcon::Grab, @@ -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, + }) } diff --git a/sctk/src/event_loop/mod.rs b/sctk/src/event_loop/mod.rs index f9994d0656..92e42cb18e 100644 --- a/sctk/src/event_loop/mod.rs +++ b/sctk/src/event_loop/mod.rs @@ -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(); + } + } + } } diff --git a/widget/src/container.rs b/widget/src/container.rs index 0c5d356ffa..e6eb7af49c 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -245,6 +245,7 @@ where viewport: &Rectangle, renderer: &Renderer, ) -> mouse::Interaction { + return mouse::Interaction::Hide; self.content.as_widget().mouse_interaction( tree, layout.children().next().unwrap(), diff --git a/winit/src/application.rs b/winit/src/application.rs index a8a898a655..868bae2170 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -691,9 +691,17 @@ async fn run_instance( 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; } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 7aa7186701..34b509ed06 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -375,10 +375,10 @@ pub fn mode(mode: Option) -> window::Mode { /// [`winit`]: https://github.com/rust-windowing/winit pub fn mouse_interaction( interaction: mouse::Interaction, -) -> winit::window::CursorIcon { +) -> Option { 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, @@ -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. diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 0eaa7daa55..884215d5cc 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -575,11 +575,21 @@ async fn run_instance( 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; } @@ -916,11 +926,21 @@ async fn run_instance( 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;