0
votes

I have an NSCollectionView up and running fairly nicely with an NSArrayController. My collection view items are configured to be multi-selectable and I am happy with the it. However, when a mouse drag event occurs within the collection view, a selection rectangle is drawn. How do I disable this selection rectangle drawing and still keep multiple selection in my NSCollectionView? Thanks for any help!

2
I took another shot at figuring this one out. I overrode mouseDragged: in both my NSCollectionView subclass and within my CollectionViewItem subclass. I added breakpoints and discovered that neither are ever called during mouse drags. Will have to see what other views are being used behind the scenes. - ctpenrose
So you just want multi-select to work like it does now but not show any rectangle? - Ken Aspeslagh
I would like to allow point and click, and shift|command click selection to work, without drawing the selection rectangle on drags. Overriding mouseDragged: did not help. This behavior is had in Apple's Mail.app, for example, for its mail message subject collection view. - ctpenrose

2 Answers

2
votes

NSCollectionView is a fairly opaque class, there are not too many ways you can easily modify its fundamental behaviour.

The drag actions appear to be handled by the private method _performDragFromMouseDown: rather than by overriding mouseDragged:. Exactly what the private method does I'm not sure.

The simple answer is that you can't change the selection appearance except by modifying private methods of NSCollectionView.

You may need to use a custom view instead of NSCollectionView if you must modify this behaviour.

0
votes

With the latest NSCollectionView (released 2015 I think?) it calls viewForSupplementaryElementOfKind on the delegate to get the view for drawing the selection rectangle.

You can "catch" this situation by comparing kind.rawValue to "NSCollectionElementKindSelectionRectIndicator" in the delegate, and return NSView() if you don't want it to draw.

If you do want to draw it, return collectionView.makeSupplementaryView(ofKind: using a kind/identifier made manually using the rawValue of "NSCollectionElementKindSelectionRectIndicator". These kinds/identifiers have been missing from the enums since 2015 I believe, and cause me grief everytime I implement NSCollectionView.

Edit: I looked up an old post I made about this on the Apple Developer Forums, and I mentioned that I had to guard against numberOfSections == 0, and return NSView() in that case.