Custom HiDPI cursor on MacOS
Description
I’ve noticed that winit doesn’t have an option to set a custom HiDPI mouse cursor on MacOS. Most MacBook and iMac screens are scaled x2, even the mouse cursor is doubled. I think it’s worth considering because a custom cursor is currently blurred on retina displays.
I took a look at the implementation and managed to achieve a sharp cursor by dividing the size of the NSImage by 2. By keeping the physical representation, I only changed the logical representation. It’s worth mentioning that the hotspot also needs to be changed. For testing, I set it to 0. This is just a test, not a final solution.
File src/platform_impl/apple/appkit/cursor.rs
let image = unsafe {
NSImage::initWithSize(NSImage::alloc(), NSSize::new((width / 2).into(), (height / 2).into()))
};
unsafe { image.addRepresentation(&bitmap) };
let hotspot = NSPoint::new(0 as f64, 0 as f64);
What do you think about it?
Relevant platforms
macOS
Hmm, how does this work if you use it on non-HiDPI screens?
Most MacBook and iMac screens are scaled x2, even the mouse cursor is doubled. I think it’s worth considering because a custom cursor is currently blurred on retina displays.
This causes additional issues on top of being blurry.
NSBitmapImageRep premultiplies by alpha on construction (https://developer.apple.com/documentation/appkit/nsbitmapimagerep#Alpha-Premultiplication-and-Bitmap-Formats). When the original non-hidpi cursor is rendered, it is upscaled (and presumably smoothed?), and produces a dark halo around the cursor, caused by sampling pixels with alpha=0, which it itself set to 0 by the premultiplication.
Not sure what the correct steps forward are, but at least on macOS it looks like we'd like to disconnect the size of the cursor image from the size of the cursor, and potentially having the ability to specify additional options like whether it should smooth/interpolate, premultiply by alpha, etc.
Currently, any cursor with nonblack borders looks incorrect because of the premultiplication.