In general, regardless of the type of menu you want to create,
you follow three steps to create a menu with the MenuBuilder framework:
-
Define the menu structure:
Create a file containing
XML or JSON that defines the menu structure. For some menu types,
the top-level menu items are menus (for example in a window or application
menu). For other menu types, the top-level items are individual
menu commands (such as in a context menu). For details on the format
for defining menu structure, see
Defining MenuBuilder menu structure
.
-
Load the menu structure:
Call the appropriate Menu
class method, either
Menu.createFromXML()
or
Menu.createFromJSON()
,
to load the menu structure file and parse it into an actual menu
object. Either method returns a NativeMenu object that can be passed
to one of the framework’s menu-setting methods.
-
Assign the menu:
Call the appropriate Menu class method
according to how the menu is used. The options are:
-
Menu.setAsMenu()
for a window or application
menu
-
Menu.setAsContextMenu()
to display the menu
as a context menu for a DOM element
-
Menu.setAsIconMenu()
to set the menu as
the context menu for a system tray or dock icon
The timing of when the code executes can be important. In particular,
a window menu must be assigned before the actual operating system
window is created. Any
setAsMenu()
call that sets
a menu as a window menu must execute directly in the HTML page rather
than in the
onload
or other event handler. The code
to create the menu must run before the operating system opens the window.
At the same time, any
setAsContextMenu()
call that
refers to a DOM elements must occur after the DOM element is created.
The safest approach is to place the
<script>
block
containing the menu assignment code just inside the closing
</body>
tag
at the end of the HTML page.
|
|
|