How to use “Look Up in Dictionary” in NSPopover Swift

131 Views Asked by At

I'm building my macOS app that contains NSPopover with NSTextView inside. If "Look up & data detectors" is active at system preferences, NSTextView will support this functionality without any additional settings. But in case, when you place NSTextView inside of NSPopover, this function won't work anymore. Is there any way to fix this problem without NSTextView.showDefenition(for: at:)?

How it usually works

enter image description here

Same thing, but won't work with NSTextView that placed inside of NSPopover

enter image description here

I've started new test project just to check behaviour, so code is simple as it is.

Main Controller:

import Cocoa

class ViewController: NSViewController {
    
    @IBOutlet weak var popoverButton: NSButton!
    @IBAction func pressButton(_ sender: NSButton) {
        let popover = NSPopover()
        popover.contentViewController = PopoverController()
        popover.contentSize = CGSize(width: 470, height: 300)
        popover.show(relativeTo: popoverButton.bounds, of: popoverButton, preferredEdge: NSRectEdge.minY)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

Popover Controller:

import Cocoa

class PopoverController: NSViewController {
    
    var textView = NSTextView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        view.addSubview(textView)
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.frame = view.bounds
    }
}
0

There are 0 best solutions below