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:
parent
7f6b16a6af
commit
af93167237
42 changed files with 1243 additions and 57 deletions
|
|
@ -1,18 +1,23 @@
|
|||
use std::{fmt, io, mem, path::Path, sync::Arc};
|
||||
use std::{ffi::c_void, fmt, io, mem, path::Path, sync::Arc};
|
||||
|
||||
use cursor_icon::CursorIcon;
|
||||
use windows_sys::{
|
||||
core::PCWSTR,
|
||||
Win32::{
|
||||
Foundation::HWND,
|
||||
Graphics::Gdi::{
|
||||
CreateBitmap, CreateCompatibleBitmap, DeleteObject, GetDC, ReleaseDC, SetBitmapBits,
|
||||
},
|
||||
UI::WindowsAndMessaging::{
|
||||
CreateIcon, DestroyIcon, LoadImageW, SendMessageW, HICON, ICON_BIG, ICON_SMALL,
|
||||
IMAGE_ICON, LR_DEFAULTSIZE, LR_LOADFROMFILE, WM_SETICON,
|
||||
CreateIcon, CreateIconIndirect, DestroyCursor, DestroyIcon, LoadImageW, SendMessageW,
|
||||
HCURSOR, HICON, ICONINFO, ICON_BIG, ICON_SMALL, IMAGE_ICON, LR_DEFAULTSIZE,
|
||||
LR_LOADFROMFILE, WM_SETICON,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
use crate::dpi::PhysicalSize;
|
||||
use crate::icon::*;
|
||||
use crate::{cursor::CursorImage, dpi::PhysicalSize};
|
||||
|
||||
use super::util;
|
||||
|
||||
|
|
@ -160,3 +165,92 @@ pub fn unset_for_window(hwnd: HWND, icon_type: IconType) {
|
|||
SendMessageW(hwnd, WM_SETICON, icon_type as usize, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SelectedCursor {
|
||||
Named(CursorIcon),
|
||||
Custom(WinCursor),
|
||||
}
|
||||
|
||||
impl Default for SelectedCursor {
|
||||
fn default() -> Self {
|
||||
Self::Named(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct WinCursor {
|
||||
inner: Arc<RaiiCursor>,
|
||||
}
|
||||
|
||||
impl WinCursor {
|
||||
pub fn as_raw_handle(&self) -> HICON {
|
||||
self.inner.handle
|
||||
}
|
||||
|
||||
fn from_handle(handle: HCURSOR) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(RaiiCursor { handle }),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(image: &CursorImage) -> Result<Self, io::Error> {
|
||||
let mut bgra = image.rgba.clone();
|
||||
bgra.chunks_exact_mut(4).for_each(|chunk| chunk.swap(0, 2));
|
||||
|
||||
let w = image.width as i32;
|
||||
let h = image.height as i32;
|
||||
|
||||
unsafe {
|
||||
let hdc_screen = GetDC(0);
|
||||
if hdc_screen == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let hbm_color = CreateCompatibleBitmap(hdc_screen, w, h);
|
||||
ReleaseDC(0, hdc_screen);
|
||||
if hbm_color == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
if SetBitmapBits(hbm_color, bgra.len() as u32, bgra.as_ptr() as *const c_void) == 0 {
|
||||
DeleteObject(hbm_color);
|
||||
return Err(io::Error::last_os_error());
|
||||
};
|
||||
|
||||
// Mask created according to https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createbitmap#parameters
|
||||
let mask_bits: Vec<u8> = vec![0xff; ((((w + 15) >> 4) << 1) * h) as usize];
|
||||
let hbm_mask = CreateBitmap(w, h, 1, 1, mask_bits.as_ptr() as *const _);
|
||||
if hbm_mask == 0 {
|
||||
DeleteObject(hbm_color);
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let icon_info = ICONINFO {
|
||||
fIcon: 0,
|
||||
xHotspot: image.hotspot_x as u32,
|
||||
yHotspot: image.hotspot_y as u32,
|
||||
hbmMask: hbm_mask,
|
||||
hbmColor: hbm_color,
|
||||
};
|
||||
|
||||
let handle = CreateIconIndirect(&icon_info as *const _);
|
||||
DeleteObject(hbm_color);
|
||||
DeleteObject(hbm_mask);
|
||||
if handle == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
|
||||
Ok(Self::from_handle(handle))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RaiiCursor {
|
||||
handle: HCURSOR,
|
||||
}
|
||||
|
||||
impl Drop for RaiiCursor {
|
||||
fn drop(&mut self) {
|
||||
unsafe { DestroyCursor(self.handle) };
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue