From b433db0d664a624e1ca5d8bec9525a5333f4919a Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sun, 22 Oct 2023 21:51:26 +0200 Subject: [PATCH] feat: Easily change the cursor with use_platform --- crates/components/src/cursor_area.rs | 11 +++-------- crates/hooks/src/use_platform.rs | 6 +++++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/components/src/cursor_area.rs b/crates/components/src/cursor_area.rs index d6cbc0029..33abeb77c 100644 --- a/crates/components/src/cursor_area.rs +++ b/crates/components/src/cursor_area.rs @@ -1,5 +1,4 @@ use dioxus::prelude::*; -use freya_common::EventMessage; use freya_elements::elements as dioxus_elements; use freya_hooks::use_platform; use winit::window::CursorIcon; @@ -47,7 +46,7 @@ pub fn CursorArea<'a>(cx: Scope<'a, CursorAreaProps<'a>>) -> Element<'a> { to_owned![platform]; move |_| { *is_hovering.write_silent() = true; - platform.send(EventMessage::SetCursorIcon(icon)).unwrap(); + platform.set_cursor(icon); } }; @@ -55,9 +54,7 @@ pub fn CursorArea<'a>(cx: Scope<'a, CursorAreaProps<'a>>) -> Element<'a> { to_owned![platform]; move |_| { *is_hovering.write_silent() = false; - platform - .send(EventMessage::SetCursorIcon(CursorIcon::default())) - .unwrap(); + platform.set_cursor(CursorIcon::default()); } }; @@ -65,9 +62,7 @@ pub fn CursorArea<'a>(cx: Scope<'a, CursorAreaProps<'a>>) -> Element<'a> { to_owned![is_hovering]; move || { if *is_hovering.read() { - platform - .send(EventMessage::SetCursorIcon(CursorIcon::default())) - .unwrap(); + platform.set_cursor(CursorIcon::default()); } } }); diff --git a/crates/hooks/src/use_platform.rs b/crates/hooks/src/use_platform.rs index 40f78ec70..47a4e4874 100644 --- a/crates/hooks/src/use_platform.rs +++ b/crates/hooks/src/use_platform.rs @@ -1,7 +1,7 @@ use dioxus_core::ScopeState; use freya_common::EventMessage; use tokio::sync::mpsc::UnboundedSender; -use winit::event_loop::EventLoopProxy; +use winit::{event_loop::EventLoopProxy, window::CursorIcon}; #[derive(Clone)] pub struct UsePlatform { @@ -28,6 +28,10 @@ impl UsePlatform { } Ok(()) } + + pub fn set_cursor(&self, cursor_icon: CursorIcon) { + self.send(EventMessage::SetCursorIcon(cursor_icon)).ok(); + } } pub fn use_platform(cx: &ScopeState) -> UsePlatform {