diff --git a/src/lib.rs b/src/lib.rs index 8af5d8cb..ba115cad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -301,7 +301,7 @@ pub mod application; pub mod changelog; #[macro_use] pub mod error; -mod cursor; +use winit_core::cursor; pub mod event; pub mod event_loop; pub use winit_core::{icon, keyboard, monitor}; diff --git a/src/platform_impl/apple/appkit/cursor.rs b/src/platform_impl/apple/appkit/cursor.rs index 0a965a41..45036a26 100644 --- a/src/platform_impl/apple/appkit/cursor.rs +++ b/src/platform_impl/apple/appkit/cursor.rs @@ -42,8 +42,8 @@ impl CustomCursor { } pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result, RequestError> { - let width = cursor.width; - let height = cursor.height; + let width = cursor.width(); + let height = cursor.height(); let bitmap = unsafe { NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel( @@ -60,15 +60,16 @@ pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result { + CustomCursorSource::Animation(animation) => { + let (duration, cursors) = animation.into_raw(); Self::build_spawn( event_loop, from_animation(event_loop.runner.main_thread(), duration, cursors.into_iter()), @@ -512,17 +511,17 @@ fn from_rgba( fn new(array: Uint8ClampedArray, sw: u32) -> Result; } - let array = Uint8Array::new_with_length(image.rgba.len() as u32); - array.copy_from(&image.rgba); + let array = Uint8Array::new_with_length(image.buffer().len() as u32); + array.copy_from(image.buffer()); let array = Uint8ClampedArray::new(&array); - ImageDataExt::new(array, image.width as u32) + ImageDataExt::new(array, image.width() as u32) .map(JsValue::from) .map(ImageData::unchecked_from_js) }; #[cfg(not(target_feature = "atomics"))] let result = ImageData::new_with_u8_clamped_array( - wasm_bindgen::Clamped(&image.rgba), - image.width as u32, + wasm_bindgen::Clamped(image.buffer()), + image.width() as u32, ); let image_data = result.expect("found wrong image size"); @@ -538,7 +537,10 @@ fn from_rgba( .expect("unexpected exception in `createImageBitmap()`"), ); - let CursorImage { width, height, hotspot_x, hotspot_y, .. } = *image; + let width = image.width(); + let height = image.height(); + let hotspot_x = image.hotspot_x(); + let hotspot_y = image.hotspot_y(); async move { let bitmap: ImageBitmap = bitmap.await.expect("found invalid state in `ImageData`").unchecked_into(); diff --git a/src/platform_impl/windows/icon.rs b/src/platform_impl/windows/icon.rs index e751782a..15b9173d 100644 --- a/src/platform_impl/windows/icon.rs +++ b/src/platform_impl/windows/icon.rs @@ -184,11 +184,11 @@ impl CustomCursorProvider for WinCursor { impl WinCursor { pub(crate) fn new(image: &CursorImage) -> Result { - let mut bgra = image.rgba.clone(); + let mut bgra = Vec::from(image.buffer()); bgra.chunks_exact_mut(4).for_each(|chunk| chunk.swap(0, 2)); - let w = image.width as i32; - let h = image.height as i32; + let w = image.width() as i32; + let h = image.height() as i32; unsafe { let hdc_screen = GetDC(ptr::null_mut()); @@ -215,8 +215,8 @@ impl WinCursor { let icon_info = ICONINFO { fIcon: 0, - xHotspot: image.hotspot_x as u32, - yHotspot: image.hotspot_y as u32, + xHotspot: image.hotspot_x() as u32, + yHotspot: image.hotspot_y() as u32, hbmMask: hbm_mask, hbmColor: hbm_color, }; diff --git a/src/cursor.rs b/winit-core/src/cursor.rs similarity index 92% rename from src/cursor.rs rename to winit-core/src/cursor.rs index 69dba15f..54728d95 100644 --- a/src/cursor.rs +++ b/winit-core/src/cursor.rs @@ -7,7 +7,7 @@ use std::time::Duration; use cursor_icon::CursorIcon; -use crate::utils::{impl_dyn_casting, AsAny}; +use crate::as_any::{impl_dyn_casting, AsAny}; /// The maximum width and height for a cursor when using [`CustomCursorSource::from_rgba`]. pub const MAX_CURSOR_SIZE: u16 = 2048; @@ -75,7 +75,7 @@ impl From for Cursor { /// # } /// ``` #[derive(Clone, Debug)] -pub struct CustomCursor(pub(crate) Arc); +pub struct CustomCursor(pub Arc); pub trait CustomCursorProvider: AsAny + fmt::Debug + Send + Sync { /// Whether a cursor was backed by animation. @@ -235,7 +235,6 @@ impl fmt::Display for BadAnimation { impl Error for BadAnimation {} #[derive(Debug, Clone, Eq, Hash, PartialEq)] -#[allow(dead_code)] pub struct CursorImage { pub(crate) rgba: Vec, pub(crate) width: u16, @@ -277,6 +276,30 @@ impl CursorImage { Ok(CursorImage { rgba, width, height, hotspot_x, hotspot_y }) } + + pub fn buffer(&self) -> &[u8] { + self.rgba.as_slice() + } + + pub fn buffer_mut(&mut self) -> &mut [u8] { + self.rgba.as_mut_slice() + } + + pub fn width(&self) -> u16 { + self.width + } + + pub fn height(&self) -> u16 { + self.height + } + + pub fn hotspot_x(&self) -> u16 { + self.hotspot_x + } + + pub fn hotspot_y(&self) -> u16 { + self.hotspot_y + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -297,4 +320,16 @@ impl CursorAnimation { Ok(Self { duration, cursors }) } + + pub fn duration(&self) -> Duration { + self.duration + } + + pub fn cursors(&self) -> &[CustomCursor] { + self.cursors.as_slice() + } + + pub fn into_raw(self) -> (Duration, Vec) { + (self.duration, self.cursors) + } } diff --git a/winit-core/src/lib.rs b/winit-core/src/lib.rs index 3fa05dc1..659e1a02 100644 --- a/winit-core/src/lib.rs +++ b/winit-core/src/lib.rs @@ -1,5 +1,6 @@ #[macro_use] pub mod as_any; +pub mod cursor; pub mod icon; pub mod keyboard; pub mod monitor;