findComponents not finding Menus and MenuItems?

I want to use addEventListener to add a common handler to all of my Menus but findComponents isn’t finding them. Also doesn’t find MenuItems. Finds other components fine and findComponent finds them.

This github issue mentions a similar, now fixed, problem with findComponent caused by menus being dynamically generated.

I’m not sure if it’s a similar bug or if I’m using the wrong approach.

EDIT: got what I wanted working with MenuBar.registerEvent(MenuEvent.MENU_SELECTED) but still unable to use findComponents.

1 Like

Hi!

That should work - however, generally you use a menu event for menus (rather than a click), that said, either should be fine - ive got a few things i need to take care of then ill knock up a couple of examples and make sure they work.

Cheers,
Ian

How are you creating the menu? I cant seem to reproduce.

XML plus @:build macro.

main.xml:

<?xml version="1.0" encoding="utf-8" ?>
<vbox id="window" width="100%" height="100%">
	<hbox width="100%">
		<menubar id="menuBar" width="100%">
			<menu text="File">
				<menuitem id="newFileMenuItem" text="New..." />
				<menuitem id="openFileMenuItem" text="Open..." />
				<menuitem id="revertFileMenuItem" text="Revert" />
				<menu id="recentFilesMenu" text="Recent">
					<menuitem text="File 1" />
					<menuitem text="File 2" />
				</menu>
				<menuseparator />
				<menuitem id="saveFileMenuItem" text="Save" />
				<menuitem id="saveAsFileMenuItem" text="Save As..." />
				<menuitem id="saveAllFileMenuItem" text="Save All" />
				<menuseparator />
				<menuitem id="closeFileMenuItem" text="Close" />
				<menuitem id="closeAllFileMenuItem" text="Close All" />
				<menuseparator />
				<menuitem id="exitMenuItem" text="Exit" />
			</menu>
			<menu text="Edit">
				<menuitem id="undoEditMenuItem" text="Undo" />
				<menuitem id="redoEditMenuItem" text="Redo" />
				<menuseparator />
				<menuitem id="cutEditMenuItem" text="Cut" />
				<menuitem id="copyEditMenuItem" text="Copy" />
				<menuitem id="pasteEditMenuItem" text="Paste" />
			</menu>
			<menu text="Image">
				<menuitem id="clearMenuItem" text="Clear" />
				<menuitem id="resizeMenuItem" text="Resize..." />
			</menu>
			<menu text="View">
				<menuitem id="clearViewTransformItem" text="Clear View Transform" />
				<menuseparator />
				<menuitem id="fitViewItem" text="Fit View" />
				<menuseparator />
				<menuitem id="horizontalFlipViewItem" text="Horizontal Flip" />
				<menuitem id="verticalFlipViewItem" text="Vertical Flip" />
			</menu>
		</menubar>
	</hbox>
</vbox>

Main.hx:

package ;

import haxe.ui.HaxeUIApp;
import haxe.ui.containers.VBox;
import haxe.ui.containers.menus.MenuBar;
import haxe.ui.containers.menus.Menu;
import haxe.ui.containers.menus.MenuItem;
import haxe.ui.containers.menus.Menu.MenuEvent;

@:build(haxe.ui.macros.ComponentMacros.build("assets/main.xml"))
class Ui extends VBox {
    public function new() {
        super();

        var menuBars = this.findComponents(MenuBar);
        trace("MenuBars:", menuBars.length, menuBars);
        var menus = this.findComponents(Menu);
        trace("Menus:", menus.length, menus);
        var menuItems = this.findComponents(MenuItem);
        trace("MenuItems:", menuItems.length, menuItems);
    }
}

class Main {
    public static function main() {
        var app = new HaxeUIApp();
        app.ready(function() {
            app.addComponent(new Ui());
            app.start();
        });
    }
}
1 Like

Interesting that you used findComponents (plural)… maybe thats the difference here, i was using findComponent(singular)… ill update my test and get back to you…

Thanks for the full example :+1:

Cheers,
Ian

Just an fyi, i can repro this, looking at fix now.

OK, so that should be fixed now in haxeui-core git, with this menu:

<menubar id="mainMenu" width="100%">
    <menu id="menu1" text="Menu 1">
        <menu-item id="menu1a" text="Menu 1A" />
        <menu-item id="menu1b" text="Menu 1B" />
        <menu-item id="menu1c" text="Menu 1C" />
    </menu>
    <menu id="menu2" text="Menu 2">
        <menu-item id="menu2a" text="Menu 2A" />
        <menu id="sub" text="Sub Menu">
            <menu-item id="sub1" text="Sub 1" />
            <menu-item id="sub2" text="Sub 2" />
            <menu-item id="sub3" text="Sub 3" />
        </menu>    
        <menu-item id="menu2c" text="Menu 2C" />
    </menu>
</menubar>

and this test code:

var menuBars = this.findComponents(MenuBar);
trace("MenuBars:", menuBars.length);
for (m in menuBars) {
    trace(" ---> " + m.id);
}
var menus = this.findComponents(Menu);
trace("Menus:", menus.length);
for (m in menus) {
    trace(" ---> " + m.id);
}
var menuItems = this.findComponents(MenuItem);
trace("MenuItems:", menuItems.length);
for (m in menuItems) {
    trace(" ---> " + m.id);
}

i get:

MenuBars:, 1
---> mainMenu
Menus:, 3
---> menu1
---> menu2
---> sub
MenuItems:, 9
---> menu1a
---> menu1b
---> menu1c
---> sub1
---> sub2
---> sub3
---> menu2a
---> subItem
---> menu2c

Lemme know if that works for you.

Cheers,
Ian