Re: Which menu item called my method? & dock menu issues
Re: Which menu item called my method? & dock menu issues
- Subject: Re: Which menu item called my method? & dock menu issues
- From: "Clark S. Cox III" <email@hidden>
- Date: Mon, 3 Dec 2001 21:40:37 -0500
On Monday, December 3, 2001, at 08:27 , Adam Iser wrote:
Hi all,
I've got a menu that contains a list of pre-defined messages. Since the
list is variable, I need some way to dynamically know which menu item was
selected.
Currently, I'm using the 'sender' argument of my action method. So I have
-(void)menuItemSelected:(id)sender{
NSLog(@"selected = %@",[(NSMenuItem*)sender title]);
}
This works (successfully printing out the selected menu item's title)...
but is it the best way?
Yes, that is what the sender parameter is there for.
Okay - now onto my application's dock menu... I'd like to have the same
setup down there (with a list of pre-defined messages for easy picking).
But when an item is selected from my dock menu, an instance of
NSApplication is passed in the sender argument... whoops! :)
That looks like a bug to me, it says in the NSApplication
documentation that menu items in the dock menu call [ NSApp sendAction:
selector to:target from: nil ]. I would think that it would make more
sense for it to call [NSApp sendAction: selector to:target from:
theMenuItemThatWasSelected ]. Maybe you should file a bug report / feature
request.
The only other option for now (that I can see) is to give each menu
item a unique action, but for a dynamic meny, that could get hairy.
So how should I go about handling a dynamic list in the dock menu? Am I
missing something simple here?
-Adam Iser
BTW I'm still having issues finding a way to determine the system's idle
time (how long it's been since the user typed or moved the mouse). I
tried a class dump of loginWindow - but didn't find much besides a
private variable and a few functions that didn't look right (if I could
figure out how to use a class that isn't in a framework maybe I could
check ;P) - I also got some help in the form of a CGS function -
unfortunately that doesn't work if the system is set to never sleep (it
appears). Really frustrating stuff! I'm debating putting up a dialog
that has to be clicked every 46 minutes or my app will assume you're idle.
.. ;P - hey, it works for AOL! ;)
I can't help with this one, but I would advise against putting up such
a dialog, or if you do, allow the time interval to be user-specifiable. I
can remember back to the days of aol, and that darn dialog annoyed me to
no end. :)
... Thinks ...
On second thought, you could probably set up an NSTimer that checks for
events like so:
{
static const unsigned eventMask = NSLeftMouseDownMask |
NSLeftMouseUpMask | NSRightMouseDownMask | NSRightMouseUpMask |
NSMouseMovedMask | NSLeftMouseDraggedMask | NSRightMouseDraggedMask |
NSMouseEnteredMask | NSMouseExitedMask | NSKeyDownMask | NSKeyUpMask;
if( nil == [ NSApp nextEventMatchingMask: eventMask untilDate:
[ NSDate distantFuture ] inMode:NSEventTrackingRunLoopMode dequeue: NO ] )
{
//If no user events are found in the queue, the user is idle, make
a note of the time
//and if the user has been idle for a given amount of time, quit
}
}
Although this feels kind of hacky to me (What if events are processed
to quickly for the timer to pick up on them?).
--
Clark S. Cox, III
email@hidden
http://www.whereismyhead.com/clark/