How to add a contextual menu to a Spec ListModel

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to add a contextual menu to a Spec ListModel

Sven Van Caekenberghe-2
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.


Reply | Threaded
Open this post in threaded view
|

Re: How to add a contextual menu to a Spec ListModel

Nicolai Hess-3-2


2017-05-24 11:25 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
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.

Reply | Threaded
Open this post in threaded view
|

Re: How to add a contextual menu to a Spec ListModel

Sven Van Caekenberghe-2

> On 24 May 2017, at 11:58, Nicolai Hess <[hidden email]> wrote:
>
>
>
> 2017-05-24 11:25 GMT+02:00 Sven Van Caekenberghe <[hidden email]>:
> 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.



Reply | Threaded
Open this post in threaded view
|

Re: How to add a contextual menu to a Spec ListModel

Stephan Eggermont-3
In reply to this post by Nicolai Hess-3-2
On 24/05/17 11:58, Nicolai Hess wrote:
> It is a problem or at least confusing that the menu: method does not
> uses a MenuModel, but a Morphic menu.

Yes. That is a bug.

Stephan