Skip to content

Commit

Permalink
feat: Easily change the cursor with use_platform
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Oct 22, 2023
1 parent 19b61aa commit b433db0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 3 additions & 8 deletions crates/components/src/cursor_area.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,27 +46,23 @@ 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);
}
};

let onmouseleave = {
to_owned![platform];
move |_| {
*is_hovering.write_silent() = false;
platform
.send(EventMessage::SetCursorIcon(CursorIcon::default()))
.unwrap();
platform.set_cursor(CursorIcon::default());
}
};

use_on_unmount(cx, {
to_owned![is_hovering];
move || {
if *is_hovering.read() {
platform
.send(EventMessage::SetCursorIcon(CursorIcon::default()))
.unwrap();
platform.set_cursor(CursorIcon::default());
}
}
});
Expand Down
6 changes: 5 additions & 1 deletion crates/hooks/src/use_platform.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit b433db0

Please sign in to comment.