I noticed an issue in macOS 14 which I didn't have on macOS 13. I used to be able to set a custom mouse cursor when it moves over a certain view area in my app, but now it's regularly reset to the standard arrow cursor.
This is easily reproduced with the following code. When I move the mouse in and out of the red rectangle. When moving in, the cursor should become a hand, and when moving out an arrow again. It seems that particularly when moving the mouse to the right of the red rectangle it quickly gets reset to the arrow cursor, while moving the mouse on the left side it often stays a hand.
Even uncommenting the line with cursor?.set()
makes the mouse cursor flicker between arrow and hand.
Is this a known bug or am I doing something wrong?
class ViewController: NSViewController {
var cursor: NSCursor?
let subframe = CGRect(x: 100, y: 100, width: 300, height: 100)
override func loadView() {
let subview = NSView(frame: subframe)
subview.wantsLayer = true
subview.layer!.backgroundColor = NSColor.red.cgColor
view = NSView(frame: CGRect(x: 0, y: 0, width: 500, height: 300))
view.addSubview(subview)
view.addTrackingArea(NSTrackingArea(rect: .zero, options: [.activeInKeyWindow, .inVisibleRect, .cursorUpdate, .mouseMoved], owner: self))
}
override func mouseMoved(with event: NSEvent) {
if subframe.contains(view.convert(event.locationInWindow, from: nil)) {
if cursor == nil {
cursor = .openHand
cursor!.push()
print("set cursor")
}
} else if let cursor = cursor {
cursor.pop()
self.cursor = nil
print("unset cursor")
}
// cursor?.set()
}
}