Search code examples
macoscocoaappkitnscollectionview

Select Item in NSCollectionView on mouseDown, not mouseUp?


I'd like to make it so my NSCollectionView's selection behavior matches the icon-view in Finder. Finder will select and highlight elements when the mouse button is clicked down, but NSCollectionView's built in behavior appears to use mouse up to trigger a selection.

Is there a way to make NSCollectionView act like Finder in this regard?


Solution

  • Per pfandtrade's comment above, it looks like the highlight state of an NSCollectionViewItem will be changed before that item is selected.

    mouseDown = NSCollectionViewItem's highlightState is set to forSelection

    mouseUp = NSCOllectionViewItem's highlightState is set to none, but isSelected property is then set to true.

    I added the following to my NSCollectionViewItem subclass:

    override var highlightState: NSCollectionViewItemHighlightState{
        didSet{
            if self.highlightState == .forSelection{
                self.showSelectedHighlight() //my stylization function
            }
        }
    }