> On 24 May 2017, at 11:58, Nicolai Hess <[email protected]> wrote:
>
>
>
> 2017-05-24 11:25 GMT+02:00 Sven Van Caekenberghe <[email protected]>:
> Hi,
>
> I am struggle a bit in find the right way to add a contextual menu to a Spec
> ListModel.
>
> Consider:
>
> ListModel new
> title: 'List Example';
> items: Smalltalk allClasses;
> openWithSpec.
>
> I would like to add a right-click contextual menu to the list, with for
> example a 'Browse' action. I can't find such a simple example. I know there
> is a #menu: option, I can see some usages in my image, but they seem to do
> different things. What would be the recommended approach ?
>
> ListModel new
> title: 'List Example';
> items: Smalltalk allClasses;
> "menu: [ ];"
> openWithSpec.
>
> Thanks,
>
> Sven
>
> PS: Probably the reference to the #selectedItem of the ListModel for the
> action is hard to write in a one-liner, that is not a requirement.
>
>
> It is a problem or at least confusing that the menu: method does not uses a
> MenuModel, but a Morphic menu.
>
> If you don't want to touch a Morphic class from within the ListModel,
> you can do it like it is done in some tools, just ignore the MenuMorph and
> instantiate a new MenuModel and call
> buildWithSpecAsPopup.
>
> Or add your menu items to the menu argument:
>
> l:=ListModel new.
> l
> title: 'List Example';
> items: Smalltalk allClasses;
> menu: [:menu :shifted |
> (menu
> add: 'Browse'
> target: l selectedItem
> selector: #browse)
> isEnabled: l selectedItem notNil.
> "return the menu object"
> menu
> ];
> openWithSpec.
Thanks, Nicolai, would this then be the 'official recommended way' if I want to
stay within Spec as much as possible ?
| list |
list := ListModel new.
list
title: 'List Example';
items: Smalltalk allClasses;
menu: [
MenuModel new
addGroup: [ :group |
group addItem: [ :item |
item
name: 'Browse';
enabled: [ list selectedItem notNil ];
action: [ list selectedItem browse ] ] ];
buildWithSpecAsPopup ];
openWithSpec.