2
votes

I'm building a MenuBar Swift Cocoa application that displays a list of user's github repositories. After clicking on one, it opens a new browser window with the repository on github.com.

I am successfully loading the list of repos and rendering them to NSMenuItem objects. Now, I'm trying to add a search bar on the top of the list and re-render the items according to the search phrase.

My questions are:

  1. What is the best approach to display a "search bar" in the NSMenu?
  2. Is there a standard way to "search/filter" NSMenuItems?
  3. Should I re-render the whole menu (calling .removeAllItems()) and add new items dynamically? Or should I have two static menus? I'm asking this because the layout (eg. NSMenuItems) is going to be difference when user is searching and when they're not.

I'm using Swift 3 and Xcode 8.3. Thanks!

1
There is nothing special build in for filtering a menu. So I would just try removeAllItems and add them as needed.catlan
thanks @catlan I'll do that!Jerguš Lejko

1 Answers

4
votes

Well, after few months of trying, I figured it out.

  1. What is the best approach to display a "search bar" in the NSMenu?

    You can add a NSMenuItem to the NSMenu and set it's view to a CustomView (searchItem.view = searchView, connect searchItem and searchView by dragging it from the storyboard into the controller).

  2. Is there a standard way to "search/filter" NSMenuItems?

    No, there is not. Clear out the items and re-render.

  3. Should I re-render the whole menu (calling .removeAllItems()) and add new items dynamically? Or should I have two static menus? I'm asking this because the layout (eg. NSMenuItems) is going to be difference when user is searching and when they're not.

    Having two submenus turned out to be a good solution.