Bug 36853 - validateMenuItem and validateUserInterfaceItem is not called.
Summary: validateMenuItem and validateUserInterfaceItem is not called.
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 2.4.0 (C6)
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Chris Hamons
Depends on:
Reported: 2015-12-10 21:59 UTC by Yvonne Du
Modified: 2015-12-19 01:52 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

sample project (460.35 KB, application/zip)
2015-12-10 21:59 UTC, Yvonne Du

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Yvonne Du 2015-12-10 21:59:59 UTC
Created attachment 14234 [details]
sample project

validateMenuItem and validateUserInterfaceItem are not called automatically in some projects, in some new created project, validateMenuItem is called (I did not find the cause). Attached the sample project.
Comment 1 Chris Hamons 2015-12-11 20:43:02 UTC
I believe there is some confusion on how NSMenu and it's components works. If you take a look at the documentation from Apple:


You can see that validateMenuItem: and validateUserInterfaceItem: are only enabled when you allow automatic menu handling. However, in your code, you have:

			// Disable automatice item enabling on the Edit menu
			EditMenu.AutoEnablesItems = false;
			EditMenu.Delegate = new EditMenuDelegate ();

If you disable setting EditMenu.AutoEnablesItems, then you'll start getting calls.

Secondarily, you aren't setting a target for those menus, which is why you are getting different behavior in some cases. 

A review of (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html#//apple_ref/doc/uid/10000060i-CH3-SW2) may be useful to understand how things are bubbling up to your App Delegate.

As well, you can often search for OS X specific information with "Cocoa X", such as "cocoa validate menu item not called", which linked to the documentation I referenced above.
Comment 2 Yvonne Du 2015-12-16 01:50:50 UTC
In fact, I have tried that before opening this bug. Please try the attached projects, it does not call the validateItems in a proper responder chain order. 

See apple's document about Responder chain of a document-based application for action messages. The validateItem should be called in ImageDocument.cs

Or maybe I am doing something wrong?
Comment 3 Yvonne Du 2015-12-16 02:00:18 UTC
I created a similar project in Xcode directly with a NSDocument based application, it works.
Comment 4 Yvonne Du 2015-12-16 03:57:45 UTC
Chris, please help us take a look this bug. You can disable the code in AppDelegate, while the paste/testAction/validateMenuItem is not called in ImageDocument.cs.

EditMenu.AutoEnablesItems = false;
EditMenu.Delegate = new EditMenuDelegate ();
Comment 5 Chris Hamons 2015-12-18 03:09:52 UTC
@Yyonne - In general bug reports are not the place for help requests. We have both the forums (https://forums.xamarin.com/categories/mac) for general community discussion and support (http://support.xamarin.com) for Business and Enterprise support.

As I mentioned above, when you set this:

	 		EditMenu.AutoEnablesItems = false;

in Application.cs line 24, in your attached example, you are telling Cocoa to not invoke the validate methods you are trying to get called. Once you disable this setting, then you'll start seeing validate methods be called based button the responder chain (you can see a picture of the general idea here, in the doc I linked:



By default, the NSDocument class is not in this chain, which is why you aren't seeing the methods called. You main window controller is, which is why you'll see ValidateMenuItem be called for paste, since paste is exported.

If you export additional selectors there, like copy, you'll start setting validate be called for them.

You can modify the responder chain, as described in the documentation, to change the way events are routed in your application.

Please free free to post on the forums or contact support if you have additional questions.
Comment 6 Yvonne Du 2015-12-19 00:51:17 UTC
I think i waste a lot time on the MacCopyPaste project. It is not a NSDocumentBased application.
Comment 7 Yvonne Du 2015-12-19 01:52:54 UTC
Thanks Chris.