A macOS menu that supports items filtration via keyboard input
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
DemoObjC.xcodeproj
DemoObjC
DemoObjC_Old.xcodeproj
DemoObjC_Old
DemoSwift.xcodeproj
DemoSwift
LICENSE
MGKMenuWithFilter.h
MGKMenuWithFilter.mm
README.md

README.md

MGKMenuWithFilter

A macOS menu that supports items filtration via keyboard input

About

MGKMenuWithFilter is a class that lets the user show only menu items which have a title with a query string inside. The query string can be changed via keyboard typing while the menu instance is shown. Here's the example:

How to use it

Add the MGKMenuWithFilter source code to the project and programmatically populate a menu instance as usually, but allocate a MGKMenuWithFilter object instead of NSMenu. Here's the code snippet for the example above:

@IBAction func showPopUpMenu(_ sender: Any) {
    let folder = NSURL.fileURL(withPath: "/Library/Desktop Pictures")
    let all_files = try! FileManager.default.contentsOfDirectory(at:folder,
                                                                 includingPropertiesForKeys:nil,
                                                                 options:.skipsSubdirectoryDescendants)
    let jpg_files = all_files.filter() { $0.absoluteString.hasSuffix(".jpg") }
    let menu = MGKMenuWithFilter.init(title: "Pictures")!
    for picture in jpg_files {
        let item = NSMenuItem()
        item.title = picture.lastPathComponent
        item.action = #selector(showPicture(_:))
        item.target = self
        let size = NSMakeSize(32, 32)
        let preview = QLThumbnailImageCreate(nil, picture as CFURL!, size, nil as CFDictionary!)
        if preview != nil  {
            item.image = NSImage.init(cgImage:preview!.takeUnretainedValue(), size: size)
            preview!.release()
        }
        item.representedObject = picture
        menu.addItem(item)
    }
    menu.popUp(positioning: nil,
               at: NSMakePoint(0, pushMeButton.bounds.size.height),
               in: pushMeButton)
}

More

MacOS versions supported: [10.8-10.12] are fully supported, 10.7 is partially supported.

Rationale and design notes: https://kazakov.life/2017/05/18/hacking-nsmenu-keyboard-navigation/