0
votes

I have NSTextView and I want to show autocompletion options using NSTableView (like Xcode autocompletion). The problem is that when textView is the first responder, tableView is shown as unfocused (which is true), but I want to pretend that it's also active. Is there an easy way to achieve this (having firstResponder textView and tableView with active cell selection color)?

1

1 Answers

0
votes

So I managed to resolve the issue with lots of experiments. Here are my solution:

setup NSTableView:

tableView.refusesFirstResponder = true
tableView.selectionHighlightStyle = .none

on NSTableCellView subclass implement following code:

override func awakeFromNib() {
    super.awakeFromNib()

    registerNotifications()
}

deinit {
    unregisterNotifications()
}

private func registerNotifications() {
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(selectionIsChanging),
                                           name: NSNotification.Name.NSTableViewSelectionIsChanging,
                                           object: nil)
}

private func unregisterNotifications() {
    NotificationCenter.default.removeObserver(self)
}

@objc private func selectionIsChanging() {
    if let row = superview as? NSTableRowView, row.isSelected == true {
        self.backgroundColor = NSColor.alternateSelectedControlColor
    } else {
        self.backgroundColor = NSColor.clear
    }
}