2023-12-16 22:02:17 +02:00
|
|
|
use std::ffi::c_void;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::sync::Arc;
|
2025-02-23 22:40:11 -05:00
|
|
|
use std::{fmt, io, mem, ptr};
|
2022-03-07 22:58:12 +01:00
|
|
|
|
2023-12-16 22:02:17 +02:00
|
|
|
use cursor_icon::CursorIcon;
|
2022-03-07 22:58:12 +01:00
|
|
|
use windows_sys::core::PCWSTR;
|
|
|
|
|
use windows_sys::Win32::Foundation::HWND;
|
2023-12-16 22:02:17 +02:00
|
|
|
use windows_sys::Win32::Graphics::Gdi::{
|
|
|
|
|
CreateBitmap, CreateCompatibleBitmap, DeleteObject, GetDC, ReleaseDC, SetBitmapBits,
|
|
|
|
|
};
|
2022-03-07 22:58:12 +01:00
|
|
|
use windows_sys::Win32::UI::WindowsAndMessaging::{
|
2023-12-16 22:02:17 +02:00
|
|
|
CreateIcon, CreateIconIndirect, DestroyCursor, DestroyIcon, LoadImageW, SendMessageW, HCURSOR,
|
|
|
|
|
HICON, ICONINFO, ICON_BIG, ICON_SMALL, IMAGE_ICON, LR_DEFAULTSIZE, LR_LOADFROMFILE, WM_SETICON,
|
2019-06-21 11:33:15 -04:00
|
|
|
};
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2024-02-03 07:27:17 +04:00
|
|
|
use super::util;
|
2025-03-13 17:18:37 +03:00
|
|
|
use crate::cursor::{CursorImage, CustomCursorProvider};
|
2024-02-03 07:27:17 +04:00
|
|
|
use crate::dpi::PhysicalSize;
|
2024-09-06 17:20:11 +03:00
|
|
|
use crate::error::RequestError;
|
2020-03-07 19:42:21 +00:00
|
|
|
use crate::icon::*;
|
2018-05-07 17:36:21 -04:00
|
|
|
|
|
|
|
|
impl Pixel {
|
2022-03-24 05:08:04 +11:00
|
|
|
fn convert_to_bgra(&mut self) {
|
2018-05-07 17:36:21 -04:00
|
|
|
mem::swap(&mut self.r, &mut self.b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:21 +00:00
|
|
|
impl RgbaIcon {
|
|
|
|
|
fn into_windows_icon(self) -> Result<WinIcon, BadIcon> {
|
2022-03-07 22:58:12 +01:00
|
|
|
let rgba = self.rgba;
|
2020-03-07 19:42:21 +00:00
|
|
|
let pixel_count = rgba.len() / PIXEL_SIZE;
|
|
|
|
|
let mut and_mask = Vec::with_capacity(pixel_count);
|
|
|
|
|
let pixels =
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe { std::slice::from_raw_parts_mut(rgba.as_ptr() as *mut Pixel, pixel_count) };
|
2020-03-07 19:42:21 +00:00
|
|
|
for pixel in pixels {
|
2024-04-22 17:21:53 +04:00
|
|
|
and_mask.push(pixel.a.wrapping_sub(u8::MAX)); // invert alpha channel
|
2022-03-24 05:08:04 +11:00
|
|
|
pixel.convert_to_bgra();
|
2020-03-07 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
assert_eq!(and_mask.len(), pixel_count);
|
|
|
|
|
let handle = unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
CreateIcon(
|
2025-02-23 22:40:11 -05:00
|
|
|
ptr::null_mut(),
|
2022-03-07 22:58:12 +01:00
|
|
|
self.width as i32,
|
|
|
|
|
self.height as i32,
|
2020-03-07 19:42:21 +00:00
|
|
|
1,
|
2022-03-07 22:58:12 +01:00
|
|
|
(PIXEL_SIZE * 8) as u8,
|
|
|
|
|
and_mask.as_ptr(),
|
|
|
|
|
rgba.as_ptr(),
|
|
|
|
|
)
|
2020-03-07 19:42:21 +00:00
|
|
|
};
|
2025-02-23 22:40:11 -05:00
|
|
|
if !handle.is_null() {
|
2020-03-07 19:42:21 +00:00
|
|
|
Ok(WinIcon::from_handle(handle))
|
|
|
|
|
} else {
|
|
|
|
|
Err(BadIcon::OsError(io::Error::last_os_error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum IconType {
|
2022-03-07 22:58:12 +01:00
|
|
|
Small = ICON_SMALL as isize,
|
|
|
|
|
Big = ICON_BIG as isize,
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 00:46:28 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
2020-03-07 19:42:21 +00:00
|
|
|
struct RaiiIcon {
|
|
|
|
|
handle: HICON,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-23 22:40:11 -05:00
|
|
|
unsafe impl Send for RaiiIcon {}
|
|
|
|
|
unsafe impl Sync for RaiiIcon {}
|
|
|
|
|
|
2024-08-08 00:46:28 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2018-05-07 17:36:21 -04:00
|
|
|
pub struct WinIcon {
|
2020-03-07 19:42:21 +00:00
|
|
|
inner: Arc<RaiiIcon>,
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 23:34:08 +01:00
|
|
|
unsafe impl Send for WinIcon {}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
impl WinIcon {
|
2020-03-07 19:42:21 +00:00
|
|
|
pub fn as_raw_handle(&self) -> HICON {
|
|
|
|
|
self.inner.handle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_path<P: AsRef<Path>>(
|
|
|
|
|
path: P,
|
|
|
|
|
size: Option<PhysicalSize<u32>>,
|
|
|
|
|
) -> Result<Self, BadIcon> {
|
|
|
|
|
// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size
|
|
|
|
|
let (width, height) = size.map(Into::into).unwrap_or((0, 0));
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
let wide_path = util::encode_wide(path.as_ref());
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
let handle = unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
LoadImageW(
|
2025-02-23 22:40:11 -05:00
|
|
|
ptr::null_mut(),
|
2022-03-07 22:58:12 +01:00
|
|
|
wide_path.as_ptr(),
|
|
|
|
|
IMAGE_ICON,
|
2022-12-22 21:35:33 +02:00
|
|
|
width,
|
|
|
|
|
height,
|
2022-03-07 22:58:12 +01:00
|
|
|
LR_DEFAULTSIZE | LR_LOADFROMFILE,
|
|
|
|
|
)
|
2018-05-07 17:36:21 -04:00
|
|
|
};
|
2025-02-23 22:40:11 -05:00
|
|
|
if !handle.is_null() {
|
2022-03-07 22:58:12 +01:00
|
|
|
Ok(WinIcon::from_handle(handle as HICON))
|
2018-05-07 17:36:21 -04:00
|
|
|
} else {
|
2020-03-07 19:42:21 +00:00
|
|
|
Err(BadIcon::OsError(io::Error::last_os_error()))
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:21 +00:00
|
|
|
pub fn from_resource(
|
2022-03-07 22:58:12 +01:00
|
|
|
resource_id: u16,
|
2020-03-07 19:42:21 +00:00
|
|
|
size: Option<PhysicalSize<u32>>,
|
2025-02-27 00:02:32 +03:00
|
|
|
) -> Result<Self, BadIcon> {
|
|
|
|
|
Self::from_resource_ptr(resource_id as PCWSTR, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn from_resource_name(
|
|
|
|
|
resource_name: &str,
|
|
|
|
|
size: Option<PhysicalSize<u32>>,
|
|
|
|
|
) -> Result<Self, BadIcon> {
|
|
|
|
|
let wide_name = util::encode_wide(resource_name);
|
|
|
|
|
Self::from_resource_ptr(wide_name.as_ptr(), size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_resource_ptr(
|
|
|
|
|
resource: PCWSTR,
|
|
|
|
|
size: Option<PhysicalSize<u32>>,
|
2020-03-07 19:42:21 +00:00
|
|
|
) -> Result<Self, BadIcon> {
|
|
|
|
|
// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size
|
|
|
|
|
let (width, height) = size.map(Into::into).unwrap_or((0, 0));
|
2018-05-07 17:36:21 -04:00
|
|
|
let handle = unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
LoadImageW(
|
2022-05-29 17:12:46 +02:00
|
|
|
util::get_instance_handle(),
|
2025-02-27 00:02:32 +03:00
|
|
|
resource,
|
2022-03-07 22:58:12 +01:00
|
|
|
IMAGE_ICON,
|
2022-12-22 21:35:33 +02:00
|
|
|
width,
|
|
|
|
|
height,
|
2022-03-07 22:58:12 +01:00
|
|
|
LR_DEFAULTSIZE,
|
|
|
|
|
)
|
2018-05-07 17:36:21 -04:00
|
|
|
};
|
2025-02-23 22:40:11 -05:00
|
|
|
if !handle.is_null() {
|
2022-03-07 22:58:12 +01:00
|
|
|
Ok(WinIcon::from_handle(handle as HICON))
|
2018-05-07 17:36:21 -04:00
|
|
|
} else {
|
2020-03-07 19:42:21 +00:00
|
|
|
Err(BadIcon::OsError(io::Error::last_os_error()))
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:21 +00:00
|
|
|
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
|
|
|
|
|
let rgba_icon = RgbaIcon::from_rgba(rgba, width, height)?;
|
|
|
|
|
rgba_icon.into_windows_icon()
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
pub fn set_for_window(&self, hwnd: HWND, icon_type: IconType) {
|
|
|
|
|
unsafe {
|
2025-02-23 22:40:11 -05:00
|
|
|
SendMessageW(hwnd, WM_SETICON, icon_type as usize, self.as_raw_handle() as isize);
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-07 19:42:21 +00:00
|
|
|
|
|
|
|
|
fn from_handle(handle: HICON) -> Self {
|
|
|
|
|
Self { inner: Arc::new(RaiiIcon { handle }) }
|
|
|
|
|
}
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:21 +00:00
|
|
|
impl Drop for RaiiIcon {
|
2018-05-07 17:36:21 -04:00
|
|
|
fn drop(&mut self) {
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe { DestroyIcon(self.handle) };
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:42:21 +00:00
|
|
|
impl fmt::Debug for WinIcon {
|
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
|
(*self.inner).fmt(formatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
pub fn unset_for_window(hwnd: HWND, icon_type: IconType) {
|
|
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
SendMessageW(hwnd, WM_SETICON, icon_type as usize, 0);
|
2018-05-07 17:36:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-16 22:02:17 +02:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum SelectedCursor {
|
|
|
|
|
Named(CursorIcon),
|
2024-01-05 17:02:08 +01:00
|
|
|
Custom(Arc<RaiiCursor>),
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for SelectedCursor {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::Named(Default::default())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 17:02:08 +01:00
|
|
|
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
|
2024-08-06 18:57:03 +02:00
|
|
|
pub struct WinCursor(pub(super) Arc<RaiiCursor>);
|
2023-12-16 22:02:17 +02:00
|
|
|
|
2025-03-13 17:18:37 +03:00
|
|
|
impl CustomCursorProvider for WinCursor {
|
|
|
|
|
fn is_animated(&self) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 22:02:17 +02:00
|
|
|
impl WinCursor {
|
2024-09-06 17:20:11 +03:00
|
|
|
pub(crate) fn new(image: &CursorImage) -> Result<Self, RequestError> {
|
2023-12-16 22:02:17 +02:00
|
|
|
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 {
|
2025-02-23 22:40:11 -05:00
|
|
|
let hdc_screen = GetDC(ptr::null_mut());
|
|
|
|
|
if hdc_screen.is_null() {
|
2024-09-06 17:20:11 +03:00
|
|
|
return Err(os_error!(io::Error::last_os_error()).into());
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
let hbm_color = CreateCompatibleBitmap(hdc_screen, w, h);
|
2025-02-23 22:40:11 -05:00
|
|
|
ReleaseDC(ptr::null_mut(), hdc_screen);
|
|
|
|
|
if hbm_color.is_null() {
|
2024-09-06 17:20:11 +03:00
|
|
|
return Err(os_error!(io::Error::last_os_error()).into());
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
if SetBitmapBits(hbm_color, bgra.len() as u32, bgra.as_ptr() as *const c_void) == 0 {
|
|
|
|
|
DeleteObject(hbm_color);
|
2024-09-06 17:20:11 +03:00
|
|
|
return Err(os_error!(io::Error::last_os_error()).into());
|
2023-12-16 22:02:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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 _);
|
2025-02-23 22:40:11 -05:00
|
|
|
if hbm_mask.is_null() {
|
2023-12-16 22:02:17 +02:00
|
|
|
DeleteObject(hbm_color);
|
2024-09-06 17:20:11 +03:00
|
|
|
return Err(os_error!(io::Error::last_os_error()).into());
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2025-02-23 22:40:11 -05:00
|
|
|
if handle.is_null() {
|
2024-09-06 17:20:11 +03:00
|
|
|
return Err(os_error!(io::Error::last_os_error()).into());
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 18:57:03 +02:00
|
|
|
Ok(Self(Arc::new(RaiiCursor { handle })))
|
2024-01-05 17:02:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-12-16 22:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 17:02:08 +01:00
|
|
|
#[derive(Debug, Hash, Eq, PartialEq)]
|
|
|
|
|
pub struct RaiiCursor {
|
2023-12-16 22:02:17 +02:00
|
|
|
handle: HCURSOR,
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-23 22:40:11 -05:00
|
|
|
unsafe impl Send for RaiiCursor {}
|
|
|
|
|
unsafe impl Sync for RaiiCursor {}
|
|
|
|
|
|
2023-12-16 22:02:17 +02:00
|
|
|
impl Drop for RaiiCursor {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe { DestroyCursor(self.handle) };
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-05 17:02:08 +01:00
|
|
|
|
|
|
|
|
impl RaiiCursor {
|
|
|
|
|
pub fn as_raw_handle(&self) -> HICON {
|
|
|
|
|
self.handle
|
|
|
|
|
}
|
|
|
|
|
}
|