Refactored context menu item visibility rules code.