Friday, 26 September 2008

Menu in Shoes

Shoes is a lightweight GUI binding for Ruby programming language. It has many useful widgets description of which can be found in the docs. It however lacks menus, which are found in most modern applications. I spent few days trying to emmulate menu behaviour in Shoes and this is what I made:
Here we have two independent menus "Menu1" and "Menu2". Method
make_menu1(title, left, top, width, items, orientation, clickk, leavee)
takes 8 arguments:
  • title - title of the menu (string)
  • top, left - coordinates of the left upper corner (in pixels) of the menu title in the current slot
  • width - width of the menu (in pixels)
  • items - array of items (strings) to be displayed when menu pop-ups
  • orientation - should menu open horizontally (:hor) or vertically (:ver)
  • clickk - sholud menu open upon clicking the menu title (true) or by hoovering over it (false)
  • leavee - should menu close after mouse leaves the menu area (:leave_of) or should it stays on (:leave_on)
After clicking on Menu2 it opens as shown on the figure to the left:
Now if we click any item the method returns the item clicked and we can use it for any furhter action, e.g. like here:

make_menu("Menu2",80, 130, 70, items1, :ver, true, :leave_on){|x| alert "You clicked: #{x}"}

where the alert will be shown with the information on the item clicked :


It is very simple and easy to modify so that one could set up colors as well etc.
The code can be found here.

Hope you will have fun with it.

2 comments:

ozwaldtwistle said...

Hi and thank you for the menu code. With Shoes r0.970 under XP Pro and rapid mouse movement the internal state of the menu gets confused. I have attempted to put a monitor around the make_menu body to see if it is a reentrancy problem but to no avail.

XY$/kwicher said...

Thanks I know but it general problem with "hover" on Shoes. You cannot be too quick :)