On Jul 8, 2009, at 6:46 AM, Graham Cox wrote:


On 08/07/2009, at 11:33 PM, I. Savant wrote:

[NSMenuItem setAttributedTitle:];

... and if you want to get really fancy (ie "modern"), you could build the list with regular strings (just "setTitle:") relatively, but use NSOperation/Queue to create / set the attributed titles with the actual fonts in the background, replacing the plain-text ones.

I suggest this because grabbing a list of font name strings is quicker than building an attributed string for each font. Do the quick thing first so it's available, but do the longer fancy thing in the background, updating while the UI is idling. Remember (99.9% of) all AppKit-related stuff should be done on the main thread.


A good idea, though I think the menu delegate can save you even this much effort. The docs are not explicit on this point, but a careful reading suggests that:

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex: (NSInteger)index shouldCancel:(BOOL)shouldCancel

is actually invoked on some sort of background thread/queue, so implementing these methods (the above + numberOfItemsInMenu:) in your menu delegate would allow you to update each item with the attributed title fairly simply and it wouldn't drag your main thread's performance down.

--Graham

What this method does is check at regular intervals for events that would cancel tracking. If one is detected, it passes YES for shouldCancel. So if your design is "if the user clicks on the menu, sit and spin until it's built," then you could use this API to detect cancellation, to stop spinning.

However, "sit and spin" isn't a very good user experience, and Leopard allows you to append items to a menu in the menu bar while it is open. So for menus that may take a while to build, consider appending the items as they come in (like the Airport status item), or showing a "Building..." item until you're ready to add the complete set of items.

-Peter

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to