Reputation: 131
I'm trying to detect when a user double click on a image in my NSTextView
to open it in preview for him to see it in a bigger from.
I currently didn't find any information on how to get the event of the double click in the NSTextView
, NSLayoutManager
or NSImage
.
Upvotes: 0
Views: 123
Reputation: 123
In order to detect a double-click event on an image in an NSTextView, you can use the NSTextViewDelegate protocol to receive notifications when the user interacts with the text view.
You can set the text view's delegate to an object that conforms to the NSTextViewDelegate protocol, and then implement the textView(_:clickedOn:at:) method to detect when the user clicks on an image.
class MyTextViewDelegate: NSObject, NSTextViewDelegate {
func textView(_ textView: NSTextView, clickedOn cell: NSTextAttachmentCellProtocol, at charIndex: Int) {
if let attachment = cell.attachment, attachment is NSImage {
// Handle double click on image
}
}
}
Then in your view controller you can set the delegate
textView.delegate = MyTextViewDelegate()
You can track the number of clicks by using a variable to track the last time the function was called, and comparing the current time with the previous time. If the time difference is less than a certain threshold, you can consider it a double-click.
Upvotes: -1