Disable NSTextAttachment Action and Sharing Services Menu Drop-Down
By default, NSTextView
will show the NSSharingServicePicker
button when you hover over an image inside the text view. That’s true even for custom image-based NSTextAttachment
s in the attributed string.
The default menu item is limited to “Markup” and a “Services” submenu, I believe. Apps can register to be shown in this menu, and users can customize the menu in System Preferences.
But what if you want to hide this dropdown button thingie completely?
NSTextView.orderFrontSharingServicePicker(_:)
is supposed to be used, but in my tests, I couldn’t get this to be called at all in subclasses ofNSTextView
.NSTextViewDelegate.textView(_:willShow:forItems:)
is supposed to be called by the text view to customize the sharing service menu, but again, this method isn’t called during my tests at all.- In my tests, I found that
NSTextView
registers itself as theNSSharingServicePickerDelegate
, so there’s that.
I don’t know why the official means to react to the service picker don’t work, but here’s something that does the trick: override the default behavior in a NSTextView
subclass.
class MyTextView: NSTextView {}
extension MyTextView: NSSharingServicePickerDelegate {
public func sharingServicePicker(
_ sharingServicePicker: NSSharingServicePicker,
sharingServicesForItems items: [Any],
proposedSharingServices proposedServices: [NSSharingService]
) -> [NSSharingService] {
// Deactivate sharing services completely.
return []
}
}
In my mind, this is only a temporary quick fix until I figure out why the default pathways don’t trigger as expected. Things to look into include the setup of the NSTextAttachments
– do the default pathways work when you use a NSTextAttachment
with a FileWrapper
instead of a custom cell? Do they work if you declare rich text support on the text view?
If you know anything, I’d love to hear how you tackled this.