3

How can I add a menu to a list model with decent actions?

I've seen some code like:

list menu: [ :menu |
  menu
    add: 'Name'
    action: [ "some action" ].

but when I do this I get an error because the block is not evaluated but sent as a message to something else…

Are there any guidelines for menus?

Uko
  • 13,134
  • 6
  • 58
  • 106

1 Answers1

3

Menus have been redone.

Now you should do something like

list menu: [ :menu | aMenu addGroup: [:aGroup |
    aGroup addItem: [ :item |
        item
            name: 'Inspect' translated;
            action: [ self inspectSelectedObjectInNewWindow ];
            shortcut: $i command mac | $i alt win | $i alt unix ].
    aGroup addItem: [ :item |
        item
            name: 'Explore' translated;
            action: [ self exploreSelectedObject ];
            shortcut: $i shift command mac | $i shift alt win | $i shift alt unix ] ].

HTH,

Benjamin Van Ryseghem

#

EDIT: ListModel is still using the old menus (for compatibility reason in Pharo 3.0). A working example is

ListModel new
    menu: [:m | 
        m 
            add: 'test' 
            target: [self halt ] 
            action: #value. 
        m ];
    openWithSpec

Note that the menu block should return the menu (a limitation from PluggableListMorph that should be encapsulated)

  • If it helps, we can write a small tutorial to go the spec website - spec.st – Benjamin Van Ryseghem Aug 11 '14 at 11:59
  • When I do it this way, I get: `MenuMorph(Object)>>doesNotUnderstand: #addGroup:`. Do I have the correct version? – Uko Aug 11 '14 at 14:27
  • That must be an issue in Spec then. Could you report it here https://github.com/spec-framework/spec/issues, I will have a look later today – Benjamin Van Ryseghem Aug 12 '14 at 13:59
  • Pharo 6.1 is still using the old model (last code snippet), but MenuMorph no longer takes `action:`, now it is `m add: 'test' target: self selector: #messageToSend.` . – MKaama Aug 09 '18 at 10:11