Page 1 of 1

v2 Menu Object length

Posted: 25 Sep 2019, 09:54
by kczx3
I know its really easy to get the menu item count via DllCall but it seems like a standard enough property for the Menu object to implement.

MenuObj.length could just indirectly call DllCall("GetMenuItemCount", "Ptr", hwnd, "Int")

Re: v2 Menu Object length

Posted: 26 Sep 2019, 17:38
by swagfag
i was just about to suggest to extend the prototype, but that doesn't appear to be at all possible

is that intended to be like so? or maybe still in the works?

Re: v2 Menu Object length

Posted: 28 Sep 2019, 18:22
by lexikos
It's a bug. Initialization order of object prototypes/classes is something I got wrong on several counts (but most are fixed). Most of the prototypes are constructed via static initializers, and there's no guarantee of order for static initializers in different files. Initially the Tray menu had no base because the prototype hadn't been constructed yet, but I changed it to construct the prototype when the first Menu is created. However, the Tray menu is always first; it is instantiated from the Script constructor, which is called by a static initializer in script.cpp, which (by chance) is executed before the static initializers in script_object.cpp. So in other words, as a result of the change, the Tray menu has a prototype but the prototype no longer inherits from Object.Prototype.

The prototype can be fixed like this:

Code: Select all

if !ObjGetBase(A_TrayMenu.base)
	ObjSetBase(A_TrayMenu.base, Object.Prototype)
Then you can use it like this:

Code: Select all

A_TrayMenu.base.DefineProp("Length", {get: this => DllCall("GetMenuItemCount", "Ptr", this.Handle, "Int")})

M := MenuCreate()
M.Add("Foo", (*) => MsgBox("Bar"))
MsgBox M.Length
I chose not to add Count or Length because it is not possible to retrieve items, and because my priority was on replacing the Menu command, which has no such capabilities.

With Length, there may be an expectation that you can iterate from this[1] to this[this.Length]. Map has Count, but there's no relation between the value of Count and the values you use as keys.