How to grey a command and deactivate it in the popup menu from add_context_menu_handler?

Hi there,
In the popup menu, I’m looking to “deactivate” the command if the a test of a valid selection is not valid

	UI.add_context_menu_handler do |popup|

		if validSelection
			
			popup.add_separator
            popup.add_item("Command1") { Command1 }
            popup.add_item("Command2") { Command2 }
		
		end

	end

If validSelection is False, I wanted to “grey” the Command1 and Command2 in the popup like it was deactivated.

Thank you for your help !

See the example in #set_validation_proc method.

Seems to be the clue, but what is this cmd in my particular code up there?
I can’t get how to apply this on my code

It isn’t clear from your snippet what Command1 and Command2 are or where they came from. If they are UI:Comand objects, you can set their validation proc.

Well Command1 and Command2 are Method objects that i defined in the script
Like

def Command1
 puts 'this is Command1'
end

I get it
I have to create an UI Command class…
Command was probably not the best example if my first post …

 	cmd1 = UI::Command.new("Command1") { puts 'this is command1' }
	cmd1 = UI::Command.new("Command1") { puts 'this is command2' }

	cmd1.set_validation_proc { MF_GRAYED }
	cmd2.set_validation_proc { MF_ENABLED }
	
	UI.add_context_menu_handler do |popup|
		
            menu3.add_separator
            menu3.add_item cmd1 
            menu3.add_item cmd2 
		
	end

Please do NOT do this. It is not a popup menu. It is the Context Menu.
This means that ONLY commands that apply to the current context should appear on the context menu.

So your original snippet which wraps the addition of the commands to the menu in a contextual (valid selection) test is correct.

Please refer to …

https://sketchup.github.io/sketchup-extension-ux-guidelines/#dont-abuse-the-context-menu

For sure. It’s good to know. I’ll not do that for shared extensions

1 Like