How can I show NSSearchField menu programmatically? - cocoa

I want to implement an NSSearchField showing search results similar to Safari's, but I cannot figure out how to show the menu programmatically. Any pointers would be greatly appreciated.
Update: I tried doing this programmatically by calling #-performClick:# on the #NSButtonCell# object that represents the search button inside the search field's search field cell, but while I have confirmed that performClick is indeed called, it does not trigger the menu.

Safari seems to use some kind of custom or private API to show search results. It's probably a window that looks like a menu. The problem I found is that a normal pop-up menu will take the keyboard focus away from the search field, which is not what you want, which is to be able to keep typing in the search field while the menu updates. I couldn't find a simple way of doing that, but I suspect it needs to be a child window that looks like a menu.
Here's a blog which documented adding something similar to Camino: http://summerofcamino.com/

Related

macOS: Selecting items from a list by typing on the keyboard

The NSTableView has a feature, called Type Selection, by which the user can type the first letters of a listed item and the view automatically selects the first hit and scrolls to it.
I like to have a similar functionality in a NSCollectionView, where I list images by name.
Before I start writing such code by myself, I wonder if there is an API that can help me with this.
I am especially worred about getting the timing right, as I want to have it use the same timing as the NSTableView does. I imagine that it even changes depending on the user's System Preferences for typing. Also, the NSTableView will select other items with the same typed prefix if waiting long enough. All this can get quite complicated if I want to get it right. I don't want to miss anything.

Can/Should NSSheets be used for text field input?

I have a Cocoa app underway and I'm thinking about how to get people to enter and/or change text fields.
The main window is an NSTableView, and there are subordinate tables, where people will add/choose selections.
I was thinking that for both adding a new record, or selecting from a set, an NSSheet could work quite nicely. But I don't know if this is appropriate.
Finally, are there better ways to do this? Examples?
Ok, so as you wanted to add new record on the basis of user selection in the sheet. If really its requirement then it is fine, there is no any issue on using sheet.
But only problem is performance issue will be there. Because user has to first click on button and then sheet will appear and then enter into textfield and then again click on the ok button or cancel button so it seems like delaying in your application.

Make NSSearchField use NSTokenFieldCell?

I'm trying to emulate the tokenized search feature of Mail.app and Finder.app on 10.7+ where there is what looks to be an NSSearchField with a search magnifying glass icon on the left and cancel (x) button on the right, but also includes tokens inside the search field.
Is there any way to make NSSearchField use an NSTokenFieldCell instead of an NSSearchFieldCell? I've tried calling -setCell: on the NSSearchField instance, which works and appears to swap out the cell as evidenced by calling po [searchField cell] in the debugger, but the drawing of the search field breaks and no text field is drawn.
Am I approaching this in the right way or is there a better way to do it? Thanks!
Update 10/25/2016: Using the Accessibility Inspector I found that Finder and Mail use a mix private subclasses:
SGTSearchField : NSSearchField
SGTSearchFieldCell : NSSearchFieldCell
SGTTokenFieldCell : NSTokenFieldCell
These are part of a private framework called Suggestions.framework. Hopefully Apple will make these classes public in the future. Make sure to file a radar if you'd like to see this functionality public too.
Best answer I have for you two is to simply go with an NSTokenField. You might be able to modify it to display the little search icon. But if that's not make or break, you can get a lot of tokenized functionality from NSTokenField.

How to maintain focus on menu after clicking a menu command

Simple version
Is there any way to maintain focus on menu after clicking a menu command?
Detailed version
Specifically, I've made a menu with some menu item with checked property. The problem is that each time I click checked menu item the menu lose its focus. It can be pretty annoying when there is a number of menu item with checked property and I want to manipulate them at once.
The most elegant solution for the problem would be maintaining focus on menu, but I can't find a way to apply it. Is it possible? And if so, what's the way to do that?
Even if there would a solution for it, sooner or later you will enter a situation in which a numerical or string property is changed via the menu, and then it becomes even impossible to keep the focus on the menu (while the dialog requesting the number or string is on the screen).
The first, simple alternative would be to put the checkable menu items on a toolbar or ribbon (just like Word does with Bold, Italic, Underline, ...). Numerical/string properties can then also be added on the toolbar or ribbon.
A second alternative could be to have a more complete configuration dialog in which the user can change all the configuration items. The configuration dialog can co-exist with the current checkable items, so users simply changing one check and users changing many properties all get a quick way of doing what they want.
You might also pose this question on https://ux.stackexchange.com/ (this sibling site is more oriented towards good user interface practices).

How force NSMenuItem to redraw?

I'm implementing a custom status bar menu, which has a custom view with NSSearchField. I'm updating number of menu items according to search results. The number of menu items is changed as user types in the NSSearchField. I've noticed, that if number of results stays the same, items titles are not updated (redrawn). How do I force them to redraw?
In the function, that rebuilds the menu I remove first all items and then create new items according to the search results.
Thanks,
Nava
I could achieve it by following approach: When the number of search results is the same, i don't recreate them, just change the title and call itemChanged:. When the count is different I recreate menu items. This works anyway. But I was advised anyway to get back from using menu for this purposes.
on the menu set menuChangedMessagesEnabled to YES when you want to update the menu:
[menu setMenuChangedMessagesEnabled:NO];
// change the menu
[menu setMenuChangedMessagesEnabled:YES];
The second invocation causes the menu to apply changes. The first one is so that you can batch a group of changes together.
That said, Apple guidelines discourages changing menus while they're open as users are not used to this and can be confusing. If it's feasible try to redesign your app so you can use something else, say a table or matrix, instead of a menu.

Resources