Close context menus when a command executes

This is for #5105.