Make NSSearchField use NSTokenFieldCell? - cocoa

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.

Related

(Mac OS X, Cocoa) How to make drag-and-droppable placeholder elements in an input text field?

I'm writing a Cocoa app where I would like the user to be able to put together a template string using placeholders. (For example, an (artist) placeholder would be filled in by the artist of the song currently playing in iTunes, etc.) I've seen apps that do something like this where each possible placeholder term is displayed in a blue "lozenge," and the user can drag and drop these "lozenges" into an input text field to construct a string, optionally entering some custom text of their own (e.g. separating (artist) and (title) "lozenges" with a hyphen).
Does anyone know if there is any sample code anywhere that will help me implement something like this?
I'm talking about something like this: (this comes from the "Hazel" app where, in a Hazel rule, you can rename a file based on a template pattern you specify)
NSTokenField is focused in the above pic (has the blue ring around it). Each "token" (your lozenges) is an auto-recognized string for the token field. As rdelmar comments above, read up on NSTokenField and you'll be most of the way there.
The "source" of tokens is likely a rounded-edged NSBox containing lined-up borderless, no-background NSTokenFields with one token each. That'll give you easy drag-and-drop as well as easy alignment.
if you need to customize the l&f of the individual tokens, you need to implement your own stuff: NSTextView with NSTextAttachment which have NSTextAttachmentCells... Its painful and a lot of code but actually not that hard
The NSTokenAttachment cell only has lots of private l&f options :(

How do I change the short display form of an NSMenuItem in an NSPopUpButton?

I have an NSPopUpButton with a long menu of items, some of which are quite wide - wider than the button's closed width. Some of the menu item titles are like so:
"Start of a really long name not that interesting (important info)"
When the menu is closed, this displays as:
"Start of a really long name not th..."
I'd like it to display as:
"Start of a really l... (important info)"
I can't figure out how NSPopUpButton is creating that ellipsis. Is there a selector being called on NSMenuItem? Does NSPopUpButton or NSMenu handle that somehow?
Which class do I need to subclass and which selector to I need to override or implement?
Many thanks.
This mechanism is called truncation. There is an option in Interface Builder called Line Breaks which is grouped in the Control group. The setting you're probably looking for is called Truncate Middle which will start picking letters from the middle of the menu item.
Note however that this won't solve your problem entirely as the (important info) part might not be the only one remaining after "…", but rather Name of my Me…tem (important info). Furthermore you might also end up with the important info partially truncated. But I figure you're fine as long as the important info is some number or a few letters.
Maybe adding an image to the menu item might suit your needs more (some sort of badge)? Maybe there is also an option for attributed strings to force letters to not be truncated… not sure however.
Fabian gave me the clue I needed. Thanks!
[[self cell] setLineBreakMode: NSLineBreakByTruncatingMiddle]
However, if there is a way to get finer-grained control, that would be even better. Is there a way to control the truncation more precisely?

slide-in search box on windows phone (like in the bing maps app)

In the bing maps app on windows phone, when I click the search button I get a search box sliding in from the top of the screen, and the keyboard sliding in from the bottom. I want to achieve the same behaviour in my own windows phone app (based around a bing map control).
I will want a few drop-in boxes, such as for setting up a filter (which will need a few check boxes and text entry), and adding an item (which will require a text entry for the name, and ideally still allow the map in the main panel to be panned to fine-tune the location of the item).
I'm pretty sure the keyboard comes up automatically when a textbox gets focus, but I'm not sure what might be the best approach for dropping in the search box. It looks like it would need something with storyboards/animations/projections, but I haven't found a clear standard approach so far, and I want to make sure I do it the right way from the start (as I don't really have time to do it twice).
Is there a standard/best practice way to achieve the effect?
Yes, and you don't need a single line of code. You can express the whole animation using XAML. Get a text on Silverlight and read the chapters on animation with particular reference to storyboards and Easing.

How can I show NSSearchField menu programmatically?

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/

Is there a simple way to combine a text and icon in an NSCell in Cocoa?

I'm trying to create a very simple selection list widget based on NSOutlineView. However, I'm having a hard time figuring out how to display an icon and a label right next to it, which is really the expected behavior in all the mainstream implementations of that kind of widget out there (iTunes, mail, Finder,...).
So far I am just binding two separate cells, but then when I'm expanding the tree, the icon cell grows larger and a gap appears between the icon and its accompanying label. I know I can probably overcome this problem by extending NSCell and provide a custom class, but as what I'm trying to achieve is really the standard thing, I can't be resigned to accept that there isn't a simpler solution.
Candide
Sadly, there isn't a 'text and icon' cell that you can just use, fresh out of the box as you would like. However, when I was working on a project, I found that Apple released some sample code that implements this, since it is such a common idiom.
This can be found here, specifically ImageAndTextCell.h/m
It can help teach you about UI customization by reading through this example, but scratching that, just dropping the ImageAndTextCell straight into your project should do just fine.
You need to create ImageAndTextcell to combine text and icon..
you can create ImageAndTextcell like this Sample Project

Resources