Skip to content
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
89 changes: 58 additions & 31 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,12 @@ const PIXEL_SIZE: usize = 4;
/// let window = Window::new(&event_loop).unwrap();
/// window.set_custom_cursor(&custom_cursor);
/// ```
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct CustomCursor {
pub(crate) inner: Arc<PlatformCustomCursor>,
/// Platforms should make sure this is cheap to clone.
pub(crate) inner: PlatformCustomCursor,
}

impl Hash for CustomCursor {
fn hash<H: Hasher>(&self, state: &mut H) {
Arc::as_ptr(&self.inner).hash(state);
}
}

impl PartialEq for CustomCursor {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}

impl Eq for CustomCursor {}

impl CustomCursor {
/// Creates a new cursor from an rgba buffer.
///
Expand All @@ -80,7 +67,7 @@ impl CustomCursor {
hotspot_y: u16,
) -> Result<CustomCursorBuilder, BadImage> {
Ok(CustomCursorBuilder {
inner: PlatformCustomCursor::from_rgba(
inner: PlatformCustomCursorBuilder::from_rgba(
rgba.into(),
width,
height,
Expand All @@ -102,7 +89,7 @@ pub struct CustomCursorBuilder {
impl CustomCursorBuilder {
pub fn build<T>(self, window_target: &EventLoopWindowTarget<T>) -> CustomCursor {
CustomCursor {
inner: self.inner.build(&window_target.p),
inner: PlatformCustomCursor::build(self.inner, &window_target.p),
}
}
}
Expand Down Expand Up @@ -165,19 +152,63 @@ impl fmt::Display for BadImage {

impl Error for BadImage {}

/// Platforms export this directly as `PlatformCustomCursor` if they need to only work with images.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CursorImage {
/// Platforms export this directly as `PlatformCustomCursorBuilder` if they need to only work with images.
#[derive(Debug)]
pub(crate) struct OnlyCursorImageBuilder(pub(crate) CursorImage);

#[allow(dead_code)]
impl OnlyCursorImageBuilder {
pub(crate) fn from_rgba(
rgba: Vec<u8>,
width: u16,
height: u16,
hotspot_x: u16,
hotspot_y: u16,
) -> Result<Self, BadImage> {
CursorImage::from_rgba(rgba, width, height, hotspot_x, hotspot_y).map(Self)
}
}

/// Platforms export this directly as `PlatformCustomCursor` if they don't implement caching.
#[derive(Debug, Clone)]
pub(crate) struct OnlyCursorImage(pub(crate) Arc<CursorImage>);

impl Hash for OnlyCursorImage {
fn hash<H: Hasher>(&self, state: &mut H) {
Arc::as_ptr(&self.0).hash(state);
}
}

impl PartialEq for OnlyCursorImage {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}

impl Eq for OnlyCursorImage {}

#[allow(dead_code)]
impl OnlyCursorImage {
fn build<T>(
builder: OnlyCursorImageBuilder,
_: &platform_impl::EventLoopWindowTarget<T>,
) -> Self {
Self(Arc::new(builder.0))
}
}

#[derive(Debug)]
#[allow(dead_code)]
pub(crate) struct CursorImage {
pub(crate) rgba: Vec<u8>,
pub(crate) width: u16,
pub(crate) height: u16,
pub(crate) hotspot_x: u16,
pub(crate) hotspot_y: u16,
}

#[allow(dead_code)]
impl CursorImage {
pub fn from_rgba(
pub(crate) fn from_rgba(
rgba: Vec<u8>,
width: u16,
height: u16,
Expand Down Expand Up @@ -222,19 +253,15 @@ impl CursorImage {
hotspot_y,
})
}

fn build<T>(self, _: &platform_impl::EventLoopWindowTarget<T>) -> Arc<CursorImage> {
Arc::new(self)
}
}

// Platforms that don't support cursors will export this as `PlatformCustomCursor`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub(crate) struct NoCustomCursor;

#[allow(dead_code)]
impl NoCustomCursor {
pub fn from_rgba(
pub(crate) fn from_rgba(
rgba: Vec<u8>,
width: u16,
height: u16,
Expand All @@ -245,7 +272,7 @@ impl NoCustomCursor {
Ok(Self)
}

fn build<T>(self, _: &platform_impl::EventLoopWindowTarget<T>) -> Arc<NoCustomCursor> {
Arc::new(self)
fn build<T>(self, _: &platform_impl::EventLoopWindowTarget<T>) -> NoCustomCursor {
self
}
}
2 changes: 1 addition & 1 deletion src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ impl Window {

pub fn set_cursor_icon(&self, _: window::CursorIcon) {}

pub(crate) fn set_custom_cursor(&self, _: Arc<PlatformCustomCursor>) {}
pub(crate) fn set_custom_cursor(&self, _: PlatformCustomCursor) {}

pub fn set_cursor_position(&self, _: Position) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(clippy::unnecessary_cast)]

use std::collections::VecDeque;
use std::sync::Arc;

use icrate::Foundation::{CGFloat, CGPoint, CGRect, CGSize, MainThreadBound, MainThreadMarker};
use objc2::rc::Id;
Expand Down Expand Up @@ -178,7 +177,7 @@ impl Inner {
debug!("`Window::set_cursor_icon` ignored on iOS")
}

pub(crate) fn set_custom_cursor(&self, _: Arc<PlatformCustomCursor>) {
pub(crate) fn set_custom_cursor(&self, _: PlatformCustomCursor) {
debug!("`Window::set_custom_cursor` ignored on iOS")
}

Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub use x11::XNotSupported;
#[cfg(x11_platform)]
use x11::{util::WindowType as XWindowType, X11Error, XConnection, XError};

pub(crate) use crate::cursor::CursorImage as PlatformCustomCursorBuilder;
pub(crate) use crate::cursor::CursorImage as PlatformCustomCursor;
pub(crate) use crate::cursor::OnlyCursorImage as PlatformCustomCursor;
pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuilder;
pub(crate) use crate::icon::RgbaIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

Expand Down Expand Up @@ -427,7 +427,7 @@ impl Window {
}

#[inline]
pub(crate) fn set_custom_cursor(&self, cursor: Arc<PlatformCustomCursor>) {
pub(crate) fn set_custom_cursor(&self, cursor: PlatformCustomCursor) {
x11_or_wayland!(match self; Window(w) => w.set_custom_cursor(cursor))
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/types/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct CustomCursor {
}

impl CustomCursor {
pub fn new(pool: &mut SlotPool, image: &CursorImage) -> Self {
pub(crate) fn new(pool: &mut SlotPool, image: &CursorImage) -> Self {
let (buffer, canvas) = pool
.create_buffer(
image.width as i32,
Expand Down
7 changes: 5 additions & 2 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,11 @@ impl Window {
}

#[inline]
pub(crate) fn set_custom_cursor(&self, cursor: Arc<PlatformCustomCursor>) {
self.window_state.lock().unwrap().set_custom_cursor(&cursor);
pub(crate) fn set_custom_cursor(&self, cursor: PlatformCustomCursor) {
self.window_state
.lock()
.unwrap()
.set_custom_cursor(&cursor.0);
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/wayland/window/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ impl WindowState {
}

/// Set the custom cursor icon.
pub fn set_custom_cursor(&mut self, cursor: &CursorImage) {
pub(crate) fn set_custom_cursor(&mut self, cursor: &CursorImage) {
let cursor = {
let mut pool = self.custom_cursor_pool.lock().unwrap();
CustomCursor::new(&mut pool, cursor)
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/util/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl XConnection {
.expect("Failed to set cursor");
}

pub fn set_custom_cursor(&self, window: xproto::Window, cursor: &CustomCursor) {
pub(crate) fn set_custom_cursor(&self, window: xproto::Window, cursor: &CustomCursor) {
self.update_cursor(window, cursor.inner.cursor)
.expect("Failed to set cursor");
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,8 +1550,8 @@ impl UnownedWindow {
}

#[inline]
pub(crate) fn set_custom_cursor(&self, cursor: Arc<PlatformCustomCursor>) {
let new_cursor = unsafe { CustomCursor::new(&self.xconn, &cursor) };
pub(crate) fn set_custom_cursor(&self, cursor: PlatformCustomCursor) {
let new_cursor = unsafe { CustomCursor::new(&self.xconn, &cursor.0) };

#[allow(clippy::mutex_atomic)]
if *self.cursor_visible.lock().unwrap() {
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub(crate) use self::{
use crate::event::DeviceId as RootDeviceId;

pub(crate) use self::window::Window;
pub(crate) use crate::cursor::CursorImage as PlatformCustomCursor;
pub(crate) use crate::cursor::CursorImage as PlatformCustomCursorBuilder;
pub(crate) use crate::cursor::OnlyCursorImage as PlatformCustomCursor;
pub(crate) use crate::cursor::OnlyCursorImageBuilder as PlatformCustomCursorBuilder;
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

Expand Down
5 changes: 2 additions & 3 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::f64;
use std::ops;
use std::os::raw::c_void;
use std::ptr::NonNull;
use std::sync::Arc;
use std::sync::{Mutex, MutexGuard};

use crate::{
Expand Down Expand Up @@ -848,9 +847,9 @@ impl WinitWindow {
}

#[inline]
pub(crate) fn set_custom_cursor(&self, cursor: Arc<PlatformCustomCursor>) {
pub(crate) fn set_custom_cursor(&self, cursor: PlatformCustomCursor) {
let view = self.view();
view.set_cursor_icon(NSCursor::from_image(&cursor));
view.set_cursor_icon(NSCursor::from_image(&cursor.0));
self.invalidateCursorRectsForView(&view);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/orbital/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl Window {
#[inline]
pub fn set_cursor_icon(&self, _: window::CursorIcon) {}

pub(crate) fn set_custom_cursor(&self, _: Arc<PlatformCustomCursor>) {}
pub(crate) fn set_custom_cursor(&self, _: PlatformCustomCursor) {}

#[inline]
pub fn set_cursor_position(&self, _: Position) -> Result<(), error::ExternalError> {
Expand Down
Loading