Search Suggestion Menus in AppKit
I recently implemented a search suggestions menu on macOS for Unread.
Even though Safari, Mail, and Finder implement search suggestion menus, there is no standard AppKit mechanism for providing them in AppKit. Popping up an NSMenu
does not work because the search field cannot receive keystrokes while the menu is displayed.
Interestingly there is a good mechanism for providing search suggestion menus in SwiftUI, but incorporating that mechanism into an AppKit-based application looks much more difficult than just using an NSHostingView
.
I ended up starting with a CustomMenus sample project. This 2018 project by Doug Stein is a Swift port of Apple Objective-C sample code from 2012.
With that as a base, I updated it to support things like dark mode. I also made it a little more focused on search suggestion menus, as opposed to custom menus in general.
The result is a sample project on GitHub. I hope you find it useful.
Thank you to:
- Doug Stein for his work to modernize this code in 2018.
- Lucas Derraugh, Daniel Jalkut, Gui Rambo, Christian Tietze, and Nate Weaver for helping me with this in the AppKit Abusers Slack.
- Apple for the original CustomMenus sample code sample code.