Reputation: 1792
I am developing a macOS application, and I have a subclassed NSSplitView that I sometime completely disable by reducing its alphaValue, as the following happens in the subclass:
override func hitTest(_ point: NSPoint) -> NSView? {
if self.alphaValue == 1.0 {
return super.hitTest(point)
}
return nil
}
I know this is just a detail, but nevertheless I would like to solve it. The SplitView correctly ignores mouse clicks because of the overridden method, but when the mouse enters the separator view, the cursor duly changes to reflect this, and displays the separator movement cursor. Can I temporarily prevent this from happening ? it is a very minor thing, but, at this point, I am also curious. Thanks for any help.
Upvotes: 0
Views: 286
Reputation: 15589
From the documentation of resetCursorRects:
Overridden by subclasses to define their default cursor rectangles.
A subclass’s implementation must invoke addCursorRect(_:cursor:) for each cursor rectangle it wants to establish. The default implementation does nothing.
Application code should never invoke this method directly; it’s invoked automatically as described in "Responding to User Events and Actions." Use the invalidateCursorRects(for:) method instead to explicitly rebuild cursor rectangles.
Prevent the split view from defining its cursor rectangles if it's disabled:
override func resetCursorRects() {
if self.alphaValue == 1.0 {
super.resetCursorRects()
}
}
Rebuild the cursor rectangles when the split view is enabled and disabled, for example:
func enable() {
self.alphaValue = 1.0
self.window?.invalidateCursorRects(for:self)
}
func disable() {
self.alphaValue = 0.5
self.window?.invalidateCursorRects(for:self)
}
Upvotes: 1
Reputation: 1792
I have found a way that works. I modified the hitTest overriding in this way:
override func hitTest(_ point: NSPoint) -> NSView? {
if self.alphaValue == 1.0 {
self.resetCursorRects()
return super.hitTest(point)
}
self.discardCursorRects()
return nil
}
It does not show the cursor change any more. But the docs recommend against doing this:
- (void)discardCursorRects; You need never invoke this method directly; neither is it typically invoked during the invalidation of cursor rectangles. NSWindow automatically invalidates cursor rectangles in response to invalidateCursorRectsForView: and before the view's cursor rectangles are reestablished using resetCursorRects. This method is invoked just before the view is removed from a window and when the view is deallocated.
I will wait for somebody with more experience that can find a better solution that does not go against the docs. Thanks
Upvotes: 0