feat(all): Custom cursor images for all desktop platforms

There seems to be many PRs relating to this issue, but they don't include all
platforms and for some reason lost steam. This PR again tries to make this
feature happen, and does it for all desktop platforms (x11, wayland, macos,
windows, web).

I think the best user of this feature and the reason I'm doing this is Bevy and
game engines in general. There non laggy hardware cursors with custom images are
very important. Game devs also like their PNGs so supporting platform native
cursor files is not that important, but I guess could be added too.

Co-authored-by: daxpedda <daxpedda@gmail.com>
Co-authored-by: Mads Marquart <mads@marquart.dk>
Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
Eero Lehtinen 2023-12-16 22:02:17 +02:00 committed by GitHub
parent 7f6b16a6af
commit af93167237
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 1243 additions and 57 deletions

View file

@ -1,3 +1,4 @@
use crate::cursor::CustomCursor;
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{ExternalError, NotSupportedError, OsError as RootOE};
use crate::icon::Icon;
@ -7,10 +8,10 @@ use crate::window::{
};
use crate::SendSyncWrapper;
use web_sys::HtmlCanvasElement;
use super::cursor::SelectedCursor;
use super::r#async::Dispatcher;
use super::{backend, monitor::MonitorHandle, EventLoopWindowTarget, Fullscreen};
use web_sys::HtmlCanvasElement;
use std::cell::RefCell;
use std::collections::VecDeque;
@ -24,7 +25,7 @@ pub struct Inner {
id: WindowId,
pub window: web_sys::Window,
canvas: Rc<RefCell<backend::Canvas>>,
previous_pointer: RefCell<&'static str>,
selected_cursor: RefCell<SelectedCursor>,
destroy_fn: Option<Box<dyn FnOnce()>>,
}
@ -53,7 +54,7 @@ impl Window {
id,
window: window.clone(),
canvas,
previous_pointer: RefCell::new("auto"),
selected_cursor: Default::default(),
destroy_fn: Some(destroy_fn),
};
@ -195,10 +196,22 @@ impl Inner {
#[inline]
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
*self.previous_pointer.borrow_mut() = cursor.name();
*self.selected_cursor.borrow_mut() = SelectedCursor::Named(cursor);
self.canvas.borrow().style().set("cursor", cursor.name());
}
#[inline]
pub fn set_custom_cursor(&self, cursor: CustomCursor) {
let canvas = self.canvas.borrow();
let new_cursor = cursor.inner.build(
canvas.window(),
canvas.document(),
canvas.style(),
self.selected_cursor.take(),
);
*self.selected_cursor.borrow_mut() = new_cursor;
}
#[inline]
pub fn set_cursor_position(&self, _position: Position) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
@ -225,10 +238,9 @@ impl Inner {
if !visible {
self.canvas.borrow().style().set("cursor", "none");
} else {
self.canvas
self.selected_cursor
.borrow()
.style()
.set("cursor", &self.previous_pointer.borrow());
.set_style(self.canvas.borrow().style());
}
}