2

How do you disable the focus ring around an NSTableView row when the user right-clicks on it? I can't get it to disappear. Setting focus ring of an individual NSTableViewCell in the table to None has no effect.

enter image description here

BadmintonCat
  • 9,416
  • 14
  • 78
  • 129

3 Answers3

3

Subclass the table view and implement the following method:

- (void)drawContextMenuHighlightForRow:(NSInteger)row {
    // do nothing
}

Note:

  1. The blue outline is not the focus ring.
  2. This is an undocumented private method Apple uses to draw the outline. Providing an empty implementation will prevent anything from being drawn, but I am not 100% sure that whether it can pass the review.
Renfei Song
  • 2,941
  • 2
  • 25
  • 30
2

New:

Here is how I did it. You can handle the menu manually. Subclass NSTableRowView or NSTableCellView, then use rightMouseDown: and mouseDown: (check for control key) and then notify your tableViewController (notification or delegate) of the click. Don't forget to pass the event as well, then you can display the menu with the event on the table view without the focus ring. The above answer is easier, but it may not pass the review, as the author mentioned. Plus you can show individual menu items for each row (if you have different sorts of views)

Old:

I think the focus ring is defined by NSTableRowView, not NSTableCellView, because it is responsible for the complete row. Try to change the focus ring there. You can subclass NSTableRowView and add it to the tableView via IB or NSTableViewDelegate's method:

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
mangerlahn
  • 4,746
  • 2
  • 26
  • 50
  • Ok but the question is how do I change the focus ring in the NSTableRowView subclass? There seems to be no property for that. – BadmintonCat Jun 02 '15 at 11:05
  • I tried to replicate what you are doing. How do you implement the secondary click? Because I did not get any focus ring at all. – mangerlahn Jun 02 '15 at 11:20
  • What do you mean with "secondary click"? You mean right-click? – BadmintonCat Jun 02 '15 at 11:46
  • I've added a context menu to my table's view controller in IB and wired it up there (menu, delegate, actions) and that's how the right click context menu works. – BadmintonCat Jun 02 '15 at 12:13
  • Ok, sorry. I did not know there was a menu property on `NSTableView`. I did handle the right click completely different. – mangerlahn Jun 02 '15 at 12:48
0

If your goal is just displaying a contextual menu, you also can try this.

  • Wrap the NSOutlineView within another NSView.
  • Override menuForEvent method on the wrapper view.
  • Do not set menu on outline-view.

Now the wrapper view shows the menu instead of your outline-view, so it won't show the focus ring. See How do you add context senstive menu to NSOutlineView (ie right click menu) for how to find a node at the menu event.

Community
  • 1
  • 1
eonil
  • 83,476
  • 81
  • 317
  • 516