Get notified when mouse is over a particular NSMenuItem - cocoa

Is there a way to be notified when mouse enters and exit from a particular NSMenuItem without using a custom NSView?
I tried subclassing NSMenuItem and overriding -(BOOL)isHighlighted but it doesn't seem to work.
Have you got any hint?

Why don't you try setting a menu delegate and receiving the menu:willHighlightItem: delegate call?

Related

How can I make a Mouse Event persist through closing an NSPopover?

I have a NSPopover that opens, and if the user clicks somewhere else in the app, the popover closes.
But the problem is that currently that mouseDown event is consumed during the popover-closing process.
Is it possible to still have that mouseDown event go through to the application, but also close the popover?
I had this same problem, so we changed to using NSPopoverBehaviorSemitransient for the behavior type. It no longer steals the mouseDown: and we just added some extra cases for closing the popover manually.
You can subclass the windows contentViewControllers view object.
I did this in the Storyboard file.
In there, implement the mouseDown() method. In there, you can create a notification which can be received at a point in your project where you need to know about the mouse event.
As the 'root view' captures almost all mouseDown() events, you have to filter them in order to only respond to the notification when the popover is displayed.
Don't forget to call super.mouseDown() at the end of your implementation.

How can I dismiss NSPopover when not active?

I have a menu bar app that displays an NSPopover when the status icon is pressed. The problem is that it will only close if you click again on the status icon.
I want the view to close when I click anywhere outside the popover itself.
I tried changing the popovers attributes Behaviour to Transient but the issue persists.
What is the best way I can achieve this?
Use UIPopoverController instead of NSPopover and Present it in the required ViewController using the below method
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
So if any other view is clicked apart from the popover, it will dismiss automatically.

Having context menu for NSButton created dynamically

I am creating NSButton dynamically in the cocoa application which is fine. However once the application launches, I want to perform right click on the NSButton created or would like to have context menu for the NSBUtton. How to achieve this?
Subclass NSButton and override mousedown or menu for event

Determine when a NSOpenPanel will close

I'm trying to determine when an NSOpenPanel is closing before it actually closes. I need to do this so I can overlay another window with a screenshot of the open panel on top of it to be animated. Unfortunately, all the notifications that you seem to be able to access seem to fire AFTER the window's already been closed. This leads to a jarring stutter before you start your transition.
I've tried:
- using NSWindow delegate methods on the open panel (apparently, none of the NSWindow delegate methods work)
- monitoring panel:userEnteredFilename:confirmed: (not called)
- showing the dialog with a callback (callback happens AFTER the panel disappears)
You should register your controller as the open panel's delegate and then implement the -panel:isValidFilename: delegate method. This method will be called just before the open dialog closes.
You should return YES from the method if you just want the notification. Returning NO allows you to prevent the open dialog from being closed.
Another way to handle this was to look through NSOpenPanel's subviews for the Cancel button and swap yourself in as the target/action. This is what i ended up doing.

Programmatically Removing a Button from a Window in Cocoa OSX

I need to programmatically remove a button from a window in Cocoa but the only method close to this was dealloc in NSObject, but this is not what I want to do. Is there a way to actually delete the button from the window not just deallocate its memory?
Send the removeFromSuperview message to the button instance.
Though perhaps you just want to hide it instead (using setHidden:)?
An NSButton is a subclass of NSControl, which itself is a subclass of NSView.
You should be able to remove the button from it's superview by calling -removeFromSuperView on the button instance.

Resources