NSSplitView: custom double click handler

394 views Asked by At

I'm trying to find out how can I detect a double-click on the NSSplitView divider. It seems to me that divider is exposed neither to the NSSplitViewDelegate, nor to the NSSplitViewController.

What I have found so far is that the divider is an instance of NSSplitDividerView which is a private class, thus cannot extend or subclass it.

Could you put me back on the correct track of investigation?

Thanks.

2

There are 2 answers

0
Willeke On BEST ANSWER

A possible solution is to subclass NSSplitView and override mouseDown(with:). Check if the location of the event is between the subviews.

override func mouseDown(with event: NSEvent) {
    if event.clickCount == 2 {
        let location = self.convert(event.locationInWindow, from: nil)
        if location.x > NSMaxX(self.arrangedSubviews[0].frame) && location.x < NSMinX(self.arrangedSubviews[1].frame) {
            Swift.print("doubleclick")
            return
        }
    }
    super.mouseDown(with: event)
}
2
James Bucanek On

I'm pretty sure you can implement the optional func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool method in the split view's delegate object, return false, and then implement whatever custom action you want instead.