Best practice for menus...

Richard Gaskin ambassador at
Tue Jan 17 15:27:02 EST 2017

Paul Dupuis wrote:

 > For desktop applications on Windows and OSX, I am lookingf or what
 > people consider the best practice to be for enabling, disabling, or
 > updating menu items based on context.
 > You have so many ways in the LiveCode language to do this:
 > enable menuItem 2 of menu "Edit"
 > disable menuItem 3 or menu "File"
 > get the text of btn "Edit"
 > if first char of line 3 of it is "(" then delete first char of line 3
 > of it -- enables 3 menuItem in Edit menu
 > set the text of btn "Edit" to it
 > enable menuitem 2 of btn "File" of grp "Menubar" -- Enable Open
 > and probably a number of other syntax variations I have forgotten. So
 > is there one of the many syntax variations for menus that is "Best"
 > (i.e most reliable, best performance, best balance between
 > reliability and performance)

IIRC the "menu" keyword will favor whatever's in the default menu bar, 
which may or may not be ideal depending on your circumstance.

For example, if you script:

   disable menuitem 2 of menu "File"

...if the topmost stack has a File menu, or you have a custom menu stack 
being used as a menu bar and the defaultMenubar points to it, you may 
get what you expect.

But if you're not quite so careful you may wind up disabling something 
in the LC IDE menubar while it's set to the defaultMenubar.

Also, IIRC the "menuItem" keyword is useful only for enabling and 
disabling menu items, but I don't believe it can be used for setting 
checkmarks, mnemonics, or other options.

Given that the "menu" reference is finicky and "menuItem" limited, I 
rarely use either.

Working with the menuitems as text give me complete control and 
unambiguous object references, for the small price of a little extra typing.

I was curious about performance, so I ran the test copied below, with 
these results:

   MenuItem: 219 ms
   As text:  10 ms

The difference may not be surprising given that the menuItems are a 
single property (rather than separately stored elements for each item as 
with SuperCard), so it seems the engine needs to get all the items for a 
menu, manipulate the ones in question, and restore them to the menu 
object each time a change is made to any one of them.

Those results were in v9.

Interestingly, v6 seems much slower (thanks team for the speed bump with 
menu updating):

  MenuItem: 3473 ms
  As text:  131 ms

-- Test script
-- Compare speed of disabling all even-numbered items in a menu
-- containing 100 items.
-- Requires 1 pulldown menu named "m1"

on mouseUp
    -- Setup menu:
    repeat with i = 1 to 100
       put "item "& i &cr after s
    end repeat
    delete last char of s -- trailing CR
    put s into btn "m1"
    -- Params for test:
    put 10 into n
    put the text of btn "m1" into s
    -- Test 1: menuItem
    put the millisecs into t
    repeat n
       put s into btn "m1"
    end repeat
    put the millisecs - t into t1
    put the text of btn 1 into r1
    -- Test 2: text
    put the millisecs into t
    repeat n
       put s into btn "m1"
    end repeat
    put the millisecs - t into t2
    put the text of btn 1 into r2
    -- Display results:
    put "MenuItem: "& t1 &" ms" &cr \
          & "As text: "& t2 &" ms" &cr \
          & "Match?: "& (r1 is r2)
end mouseUp

on SetMenu1
    repeat with i = 2 to the number of lines of the text of btn "m1" step 2
       disable menuitem i of btn "m1"
    end repeat
end SetMenu1

on SetMenu2
    put the text of btn 1 into s
    put 0 into i
    put empty into tNuS
    repeat for each line tLine in s
       add 1 to i
       if i mod 2 = 0 then
          put "("& tLine &cr after tNuS
          put tLine &cr after tNuS
       end if
    end repeat
    delete last char of tNuS
    set the text of btn "m1" to tNuS
end SetMenu2

  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  Ambassador at      

More information about the use-livecode mailing list