menupharomorphic

How can I edit the WorldMenu in Pharo


How can I remove or add an entry to the WorldMenu in Pharo at run time?

For example I have a menu option that loads extra tools for working with web tools. Running the code setting up these tools would include adding items to the menu to stop and start the web service. I don't want these stop and start items in normal use but the code setting up the items would be in the image.

I have seen and used the method in this question However this adds the item when the code is loaded.


Solution

  • Let me clarify what @Peter and I mean

    Choose any class, and in the class side add your #menuCommandOn: method on the lines of

    menuCommandOn: aBuilder
      <worldMenu>
      self showsItem ifTrue: [
        (aBuilder item: self itemToken)
          order: 0.1;
          action: [self performItemAction]]
    

    This way, even though the method would be invoked every time the world menu is about to pop up, it will add the menu item only if the logic behind #showsItem enables it. Notice that the dynamic nature of the menu doesn't require you to remove menu items, instead you simply do not add them. In your case such a logic should reflect the availability of the web service.

    The #itemToken message send is a placeholder for the Symbol you want to use to identify the item. In other words, you would probably want to inline it as a literal rather than sending the #itemToken message. This Symbol will be used as the item label.

    For further optional configuration features take a look at other implementors of #menuCommandOn:.