Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Jun 9, 2012, 05:41 PM   #1
macrumors 601
ArtOfWarfare's Avatar
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Change image when NSMenuItem highlights?

Does anyone have any suggestions for how to set up an NSMenuItem to have two images that it alternates between based on whether it's currently highlighted or not?

The best I have so far is I change the item's image whenever the delegate receives either menuWillOpen or menuDidClose.

This has two issues, unfortunately:
1 - There's a slight, but noticeable, delay when the item is highlighted.
2 - It's possible to have a menu open but not actually highlight the item. (IE, if the item is the first one in the menu, you can move the cursor above the menu. The submenu will remain open, but the highlight will go away. Leaving me with a highlighted icon, but not a highlighted item.)

I've attempted to subclass NSMenuItem and see if various method names get called ever... (things like setHighlighted:, setIsHighlighted:, isHighlighted) but none work...

I can't believe how difficult this seems to be. Apple's own built in menus perform this. (Just click on the wi-fi menu and highlight an item. Notice that the various icons change from black to white when you highlight them.)

2X Edit:
I've discovered a delegate method, menu:willHighlightItem:, which has proven to be very useful for properly changing an item's image right when it gets highlighted.

3X Edit:
The delegate occasionally isn't called when it should be. (The documentation says it gets called with item being nil when all the items are being unhighlighted... but it doesn't if the cursor simply moves back to the supermenu.)

So... my code looks like this, and since the menuItems with the images that should change back and forth each have submenus, I can just look for when the menu closes.
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.

Last edited by ArtOfWarfare; Jun 10, 2012 at 02:16 PM.
ArtOfWarfare is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 07:16 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC