Xcode Plugin Context Menu - objective-c

I'm trying to add items to the context menu in Xcode with a plugin. To do this, I created a right click mouse event and used it as a parameter for the menuForEvent method of the contentView of the mainWindow. The context menu is never initialized. The mouse click event is initialized, but I'm not sure if it's initialized correctly. My code is below. Am I at least on the right track? Thanks in advance.
NSEvent* mouseClickEvent =[NSEvent mouseEventWithType:NSRightMouseDown location:[NSEvent mouseLocation] modifierFlags:[NSEvent modifierFlags] timestamp:NSTimeIntervalSince1970 windowNumber:[[NSApp mainWindow] windowNumber] context:[[NSApp mainWindow] graphicsContext] eventNumber:0 clickCount:1 pressure:1.0];
NSMenu* rightClickContextMenu = [[[NSApp mainWindow] contentView] menuForEvent: mouseClickEvent];

Related

NSEvent from a NSButton

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.

Custom Created Window Getting Closed Automatically By Selecting Contextual Menu Option Automatically

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.

How I add a mouse right-down menu to the NSCollectionViewItem

I have a question.How I add a mouse right-down menu to the NSCollectionViewItem.
As an attempt I alse use the Apple's demo app IconCollection.I tryed drag a NSMenu to the IconViewPrototype.xib and connect it to the view's menu outlet in IB.but when build and run,click the mouse right click,nothing happened.I think the NSBox also a subclass for NSView,the mouse right-down menu should be support.
I ended up creating an NSView subclass to use as a View for the CollectionViewItem. In there I set a delegate (connected in IB), and used this to catch the right mouse click and open the menu:
-(void)rightMouseDown:(NSEvent *)theEvent {
NSMenu *menu = [self.delegate menuForCollectionItemView:self];
[menu popUpMenuPositioningItem:[[menu itemArray] objectAtIndex:0]
atLocation:NSZeroPoint
inView:self];
}
This still needs some code to position the menu where the user clicked, but it's a start.
If anyone has a cleaner method I'd love to hear it.

Open and close NSWindow with NSButton through NSWindowController?

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.

How to open a window on click on NSStatusItem?

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.