Lore archive · Warcraft Wiki, 2023 dump
Using UIDropDownMenu
Last wiki revision 2021-05-31
UIDropDownMenuTemplate is a FrameXML Frame template that can be used to create contextual menus and dropdown boxes in World of Warcraft. This tutorial explains how to use it in your addon.
Summary: implement a function describing the contents of your drop-down menu, and possibly another function to respond to the user selecting a particular menu item.
The template can be used to create two UI elements: drop-down lists and context menus, both of which can be used to present a multi-level menu to the user. The menu's items may be disabled, checked, show a color picker swatch, or be styled as a title. The template automatically creates a drop-down box, as well as any list buttons as necessary.
The differences between the two display modes are inconsequential from a coding perspective -- if you're creating a context menu, you'll need to provide an appropriate argument to the call slightly and call yourself when you want the context menu to appear.
In order to display a menu, you'll need to create an initialization function which will describe what items your menu contains. The function will be passed these three arguments:
- frame : - A reference to your UIDropDownMenuTemplate-inheriting frame.
- level : - Nesting depth of the dropdown menu your function should describe; 1 corresponds to the outermost level, with 2 and 3 being accessible if the user hovers over a menu item that is described as having a nested menu.
- menuList : any type - A value from the item description of the parent menu item, which can be used to identify which nested menu should be described.
Your function must, rather than returning any values, call with a description of each menu item you wish to create. Menu items are described via a table argument, with a large number of named keys providing customization options. The most important ones to set are:
- info.text : - text to display for this menu item.
- info.checked : - if true, a checkmark/depressed radio button is displayed next to the item.
In practice, your initialization function may be as simple as the one in the following example.
Typically, your addon will want to respond to the user making a dropdown selection in some fashion. The easiest way to accomplish this is to use the func and arg1/arg2 keys of the info table: This means that you'll frequently need to write two functions: one to respond to the user making selections in the menu, and the other to describe the items in your menu.
- info.func : - if set, info.func(self, info.arg1, info.arg2, checked) will be called when this menu item is selected (clicked).
- info.arg1, info.arg2 : any type - these arguments will be passed to func when this menu item is pressed.
The following snippet illustrates the pattern. The info.func value is set to the function handling the clicks, and the menu initializer is modified to set the info.arg1 values in addition to info.text, which allows the _OnClick function to determine which option was selected easily.
Dropdown and context menus may be nested -- menu items may be marked such that they open an additional level of menus when hovered over. The following two info table keys are relevant to this purpose:
- info.hasArrow : - if true, this menu item will open a sub-menu when hovered over.
- info.menuList : any type - will be passed to the initializer function when a sub-menu is opened from this item.
Your initializer function will therefore need to be aware of which level of the menu its being asked to describe. For instance: UIDropDownMenu_AddButton's second argument, level, specifies to which level of the currently open menu the menu item should be appended. If you fail to specify it for sub-menus, your "nested" menu items will be added at the bottom of the outermost menu level.
In order to use your menu initializer function, you'll need to create a frame inheriting from and call to bind your menu function to your frame. Your frame must have a name.
The following code creates, positions, and initializes a dropdown menu widget:
Generally, you'll want to have your dropdown display text indicating the current value of whatever choice the user can make using the dropdown. To adjust the text displayed by the dropdown, you can use the following function:
The text displayed by the menu will be automatically set to the newly selected menu item's info.text value whenever the user makes a selection, so you'll usually only need to explicitly alter the text when your dropdown is first presented to the user, or when its value is changed without user interaction.
The following code creates and initializes a context menu widget.
To show a context menu, you'll need to call function when you wish to show the context menu. Generally, the call to show a menu would look like this:
The following example illustrates how UIDropDownMenuTemplate can be used in a typical addon, allowing the user to customize the value of some variable in the addon. In this example, a multi-level dropdown menu is used to let the user pick a favorite number from 0 to 49.
- The default UIDropDownMenu implementation does not provide scrolling -- if you attempt to display too many menu items in a single level, some of them will be inaccessible.
- UIDropDownMenu provides more customization options than are discussed in this HOWTO. You can, among other things, divide menus into multiple sections with non-selectable titles, control whether selection is reflected by a checkmark or a radio button, display icons alongside text and much more. See the full list of info table keys.
- Avoid using dropdown menus to encompass the entirety of your addon's configuration -- interacting with menu structures several levels deep does not make for a pleasant user experience.
- Avoid using the and families of functions, since some of the former generally only work correctly while your menu is open. You can achieve the same effect using , and , as described in this HOWTO.
- UI Object UIDropDownMenu: template documentation, including additional table keys.
- API EasyMenu: allows you to specify the entirety of your context menu dropdown as a table, removes the need to write initializer functions.
Source
https://warcraft.wiki.gg/wiki/Using_UIDropDownMenu · retrieved 2023-11-13 · CC BY-SA 4.0
Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.
This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.