2

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.

Community
  • 1
  • 1
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • Actually the [answer](http://stackoverflow.com/a/31011296/4081336) to that question should work dynamically because implementors of `#menuCommandOn:` with the `` pragma are collected and invoked every time the world menu is about to pop up. – Leandro Caniglia Mar 25 '17 at 23:06
  • The issue is that I do not want my items to shopw until I run the code - not just have the code in the image – mmmmmm Mar 26 '17 at 10:34
  • Well the `` pragma determines that the method is called. You can add the item conditionally there. – Peter Uhnak Mar 26 '17 at 13:33

1 Answers1

2

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:.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51