简体   繁体   English

将鼠标悬停在分隔符上时,是否可以在 NSSplitView 中暂时禁用鼠标光标更改?

[英]Is it possible in a NSSplitView to temporarily disable the mouse cursor change when hovering on the separator?

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:我正在开发一个 macOS 应用程序,我有一个子类 NSSplitView,有时我会通过减少它的 alphaValue 来完全禁用它,因为子类中发生以下情况:

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.由于重写的方法,SplitView 正确地忽略了鼠标点击,但是当鼠标进入分隔符视图时,光标会适当地改变以反映这一点,并显示分隔符移动光标。 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.谢谢你的帮助。

From the documentation of resetCursorRects :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.子类的实现必须为它想要建立的每个光标矩形调用 addCursorRect(_:cursor:) 。 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.使用 invalidateCursorRects(for:) 方法来显式重建光标矩形。

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)
}

I have found a way that works.我找到了一种有效的方法。 I modified the hitTest overriding in this way:我以这种方式修改了 hitTest 覆盖:

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; (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. NSWindow 自动使光标矩形无效以响应 invalidateCursorRectsForView: 并且在使用 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谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM