v2 Menu Object length

Propose new features and changes
User avatar
Posts: 1119
Joined: 06 Oct 2015, 21:39

v2 Menu Object length

25 Sep 2019, 09:54

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")
Posts: 3917
Joined: 11 Jan 2017, 17:59

Re: v2 Menu Object length

26 Sep 2019, 17:38

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?
Posts: 6975
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: v2 Menu Object length

28 Sep 2019, 18:22

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.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 2 guests