I have builded a custom window; in this window I am showing nsbuttons.
I am showing a contextual menu if button clicked.
The problem is I don't want to close my window but somehow mouse exited event is getting triggered as soon as I choose option from nsmenu.
I want to prevent this effect.
I am not able to figure it out.
Any help would be appreciated.
Thanks in Advance
-(void)rightMouseDown:(NSEvent *)theEvent
{
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:#"Contextual Menu"];
[[theMenu addItemWithTitle:#"Remove From List" action:#selector(removeWithIdentifier) keyEquivalent:#""] setTarget:self];
[[theMenu addItemWithTitle:#"Open" action:#selector(openAppWithIdentifier) keyEquivalent:#""] setTarget:self];
[theMenu popUpMenuPositioningItem:nil atLocation:NSMakePoint(self.bounds.size.width-20, self.bounds.size.height-10) inView:self];
}
-(void)removeWithIdentifier
{
//My custom view is getting mouse exited event from here
//I want prevent it.
}
Hey Friends I have got the answer.
I have found that If your window level is "NSPopUpMenuWindowLevel" and its
view has implemented any context menu over it then this view will always gets mouse exited event.
posting it for future reference.
thanks.
Related
I have a NSTableView and each row contains a button. I also have a menu associated with the table.
The issue is : I want to show the menu on click of button. If possible do not show on right click.
The action method is :
- (IBAction)showMenu:(NSButton *)button {
NSLog(#"show menu");
NSMenu *menu = [self.tableView menu];
NSEvent *event = [[NSEvent alloc] init];
[NSMenu popUpContextMenu:menu
withEvent:event
forView:button];
}
Here what to do with event? If I use nil then the menu is showed at bottom left corner, not next to the button.
Any guidance would be appreciated.
You could try using -[NSMenu popUpMenuPositioningItem:atLocation:inView:]. This method doesn't take an NSEvent argument. Instead, you give it a view and a location in the view's coordinate system, and the menu positions itself (or one of its items) over that location.
But I would suggest you don't use an NSButton at all. If you use an NSPopUpButton instead, it will take care of showing the menu at the correct location on a left-click.
I've got a custom NSButton that I'm actually sticking in an NSStatusItem. When the NSButton is clicked, it launches my window. When the NSButton is clicked again, the window should close.
If the window is open, it appears as if the NSButton stops responding (or doesn't receive) click events! Here's the relevant code:
[statusItem setView:myCustomButton];
[myCustomButton setAction:#selector(showWindow:)];
- (void)showWindow:(id)sender {
if(!myWindowController) {
myWindowController = [[MyWindowController alloc] initWithWindowNibName:#"MyWindow"];
}
[myWindowController showWindow:statusItem];
[myWindowController.window orderFront:nil];
}
Am I doing something crazy? If I set a breakpoint in the above, it is hit when the button is clicked the first time but is not hit when the button is clicked again.
I'm guessing that you're not setting a target on the button. If a button has an action but no target, it gets sent up the responder chain. When the new window is shown, the responder chain is probably being changed, which means your action is being sent to a different place.
tl;dr: try setting a target on the button.
I'm using this method to show the NSStatusItem menu only if application is Active.
-(void)menuWillOpen:(NSMenu*)menu{
if(![NSApp isActive]){
[menu cancelTracking];
}
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:self];
}
It perfectly works, but cancelTracking seems to block the blue highlight of the NSStatusItem. So when i click on status menu item it doesn't show the submenu and it presents the main window, but the icon is not highlighted.
Is there a way to make it happen ?
I suggest creating a custom view if you need more precise control over when the status item highlights itself. Then you can use mouseDown: etc. This is a good example of putting your custom view in a status item.
Im pretty sure the answer must be quite easy, but for some reason I can't get it to work!
I have a window, and this code:
[someWindow setHidesOnDeactivate:YES];
I have a status item, and the following code
- (void)openWindow{
if ([someWindow isVisible]) {
NSLog(#"CLOSING");
[lyricWindow close];
}else {
[someWindow makeKeyAndOrderFront:nil];
NSLog(#"SHOWING");
}
}
This worked when I closed the window and wanted to open it again. Now that I've implemented the hides of deactivate I am lost as to what I need to do! I've tried all sorts of things... I want the window to show again and the window to become active when I click the status item! I think thats my problem.
How can I make the window active when I click on a status item?
I get the following in the log:
CLOSING SHOWING CLOSING SHOWING
Closing is first regardless if the window is hidden or not, which is rather intriguing, because id guess isVisible would return false if the window has been hidden. Anyhow, I get no window. How can I unhide the window?
Thanks!
I had this same problem where I couldn't get a window to re-show after it was hidden from deactivation. In my case, I was showing a NSPanel (subclass of NSWindow) when a NSStatusItem was pressed.
The following code finally got my NSPanel to re-show up:
[NSApp arrangeInFront:sender];
[myWindow makeKeyAndOrderFront:sender];
[NSApp activateIgnoringOtherApps:YES];
I'm pretty sure 'activateIgnoringOtherApps' is the key here. For me, it's not ideal because it takes focus away from the user's current application.
I'm pretty new to cocoa, so please excuse me for any stupid mistakes I make.
I have a NSStatusItem, which I want to use to open up a menu. However as far as I know and have heard across different forms, without a custom view you are restricted to just a pop down menu. Is this true? And if so how do you make a custom view to do something (e.g. open a window in my case)? Thanks for any help.
No, it is not true. You need to set up the target and action for the status item to call a method which does what you want (opens the window).
// This goes where you set up the status item
NSStatusItem *statusItem; // You need to get this from the status bar
[statusItem setTarget:self];
[statusItem setAction:#selector(openWindow:)];
// This method is called when the status item is clicked
- (void)openWindow:(id)sender {
NSWindow *window = [self window]; // Get the window to open
[window makeKeyAndOrderFront:nil];
}
You may also want to call [NSApp activateIgnoringOtherApps:nil]; to your openWindow: method to ensure that the window you open is not behind some other application's window.