Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set_cursor shortcut #352

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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