# ui/uicontextmenu (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uicontextmenu/

Context menu. (UIContextMenu of the original ui/uicontextmenu.js, a right click / long press menu)
- show(node_tree, position, item_list) pops up the item list at that position. Pressing outside closes it, and pressing an item closes it and then calls the handler.
- The menu is pinned to the right / bottom edge so it does not go outside the parent area. (960 x 640 if the parent size is unknown)
- Place it at the top of the scene root (the last child).
- The original is a node that inherits WorldNode. The Rust version is a component attached to a node (host), and on attach, like the original constructor, it
  sets up the host (top-left pivot, inactive) and creates an outside-press blocker (transparent UIButton) and a menu panel (rounded Paint) as children.
  UIContextMenu::create(node_tree, parent_id, options) takes the place of the original new UIContextMenu(options) + add_child.
- Usage:
  ```text
    let menu_id = UIContextMenu::create(node_tree, root_id, UIContextMenuOptions::default());
    node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
        context_menu.show(tree, view_position, vec![UIContextMenuItem::create("Copy", Box::new(|context| { ... })), UIContextMenuItem::create_disabled("Delete")]);
    });
  ```
- Differences from the original:
  - show / close change nodes, so they take the tree. Scene code calls them with node_tree.with_component_mut.
  - Items are a UIContextMenuItem struct ({ text, handler?, is_disabled }), and a handler is a one-shot function (FnOnce) that takes the context of the pressed row node.
  - is_showing returns the value changed by show / close instead of the node's active state.
  - Previous rows are deleted instead of detached (removeChild). Row node names are "ContextMenuItem{index}" as in the original.

## UIContextMenuOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Context menu settings. (original options = { menu_width = 168, item_height = 28, font_size = 12, panel_color?, text_color? })

Example: Wide menu, tall rows

```rust
use vanilla::{NodeTree, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let options = UIContextMenuOptions {
	menu_width: 200.0,
	item_height: 32.0,
	..UIContextMenuOptions::default()
};
let menu_id = UIContextMenu::create(&mut node_tree, root_id, options);
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
	context_menu.show(tree, Vector2::create(100.0, 100.0), vec![UIContextMenuItem::create_disabled("Paste")]);
});
let panel_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_panel_node().expect("panel");
assert_eq!(node_tree[panel_id].get_content_size(), Vector2::create(200.0, 40.0));
```

- `pub menu_width: f64` (field, since 0.3.2): Menu panel width. (default 168)
  - returns f64
- `pub item_height: f64` (field, since 0.3.2): Item row height. (default 28, the panel height is item count x row height + 8)
  - returns f64
- `pub font_size: f64` (field, since 0.3.2): Item text size. (default 12)
  - returns f64
- `pub panel_color: Color` (field, since 0.3.2): Menu panel color.
  - returns Color
- `pub text_color: Color` (field, since 0.3.2): Text color of pressable items. (items that cannot be pressed are gray)
  - returns Color

## UIContextMenuHandler (type)

Item handler. (takes the context of the pressed row node. called only once)

```rust
pub type UIContextMenuHandler = Box<dyn FnOnce(&mut ComponentContext)>
```

## UIContextMenuItem (struct)

Menu item. (original { text, handler?, is_disabled? })

Example: A pressable item and an item that cannot be pressed

```rust
use vanilla::{ComponentContext, UIContextMenuItem};

let copy_item = UIContextMenuItem::create("Copy", Box::new(|_context: &mut ComponentContext| {}));
let delete_item = UIContextMenuItem::create_disabled("Delete");
assert!(copy_item.handler.is_some());
assert!(!copy_item.is_disabled);
assert!(delete_item.is_disabled);
assert_eq!(delete_item.text, "Delete");
```

- `pub text: String` (field, since 0.3.2): Item text.
  - returns String
- `pub handler: Option<UIContextMenuHandler>` (field, since 0.3.2): Handler called after closing the menu when pressed. (None only closes)
  - returns Option<UIContextMenuHandler>
- `pub is_disabled: bool` (field, since 0.3.2): If true, it cannot be pressed and is shown in gray text.
  - returns bool
- `pub fn create(text: &str, handler: UIContextMenuHandler) -> UIContextMenuItem` (static, since 0.3.2): Creates an item with a handler.
  - `text` &str: Item text.
  - `handler` UIContextMenuHandler: Handler called after closing the menu when pressed. (takes the context of the pressed row node)
  - returns UIContextMenuItem: A pressable item.
- `pub fn create_disabled(text: &str) -> UIContextMenuItem` (static, since 0.3.2): Creates an item that cannot be pressed. (gray text)
  - `text` &str: Item text.
  - returns UIContextMenuItem: An item with no handler that cannot be pressed.

## UIContextMenu (struct) (Default, Component, UIDocumentNode)

Context menu.

Example: Pressing an item calls the handler and closes the menu

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
let menu_id = UIContextMenu::create(&mut node_tree, root_id, UIContextMenuOptions::default());
let copy_count = Rc::new(Cell::new(0));
let copy_counter = copy_count.clone();
let item_list = vec![
	UIContextMenuItem::create("Copy", Box::new(move |_context: &mut ComponentContext| {
		copy_counter.set(copy_counter.get() + 1);
	})),
	UIContextMenuItem::create_disabled("Delete"),
];
node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
	context_menu.show(tree, Vector2::create(100.0, 100.0), item_list);
});
let copy_item_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_item_nodes()[0];
let item_center = node_tree.convert_node_position_to_view_position(copy_item_id, Vector2::create(80.0, 14.0));
node_tree.dispatch_touch(copy_item_id, &engine, TouchKind::Press, item_center);
node_tree.dispatch_touch(copy_item_id, &engine, TouchKind::Release, item_center);
assert_eq!(copy_count.get(), 1);
assert!(!node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").is_showing());
```

- `pub fn new() -> UIContextMenu` (constructor, since 0.3.2): Constructor. (default settings)
  - returns UIContextMenu: Menu before being attached. (UIContextMenuOptions::default)
- `pub fn create_with_options(options: UIContextMenuOptions) -> UIContextMenu` (static, since 0.3.2): Creates with settings. (attach to a node with node_tree.add_component)
  - `options` UIContextMenuOptions: Menu settings.
  - returns UIContextMenu: Menu before being attached.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UIContextMenuOptions) -> NodeId` (static, since 0.3.2): Creates the node. (creates and attaches a "UIContextMenu" node under parent_id. original new UIContextMenu(options) + add_child)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Parent node. (usually the scene root, its content_size is the area that confines the menu)
  - `options` UIContextMenuOptions: Menu settings.
  - returns NodeId: The created host node. (turned off because it has not been opened yet)
- `pub fn show(&mut self, node_tree: &mut NodeTree, position: Vector2, item_list: Vec<UIContextMenuItem>)` (method, since 0.3.2): Opens the menu. (position is the top-left of the menu in local coordinates under the menu node (an area the size of the parent). Rebuilds the rows and shows it kept inside the area)
  - `node_tree` &mut NodeTree: Node tree that holds the menu.
  - `position` Vector2: Top-left position of the menu panel. (used as the panel's local position, clamped so it does not go past 4 inside the right and bottom edges of the area)
  - `item_list` Vec<UIContextMenuItem>: Item list. (placed in order from top to bottom)

  Example: A menu opened at the bottom-right edge is pinned inside the area

  ```rust
  use vanilla::{NodeTree, UIContextMenu, UIContextMenuItem, UIContextMenuOptions, Vector2};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  node_tree[root_id].set_content_size(Vector2::create(800.0, 600.0));
  let menu_id = UIContextMenu::create(&mut node_tree, root_id, UIContextMenuOptions::default());
  let item_list = vec![UIContextMenuItem::create_disabled("Undo"), UIContextMenuItem::create_disabled("Redo")];
  node_tree.with_component_mut::<UIContextMenu, _>(menu_id, |context_menu, tree| {
  	context_menu.show(tree, Vector2::create(780.0, 590.0), item_list);
  });
  let panel_id = node_tree[menu_id].get_component::<UIContextMenu>().expect("menu").get_panel_node().expect("panel");
  assert_eq!(node_tree[panel_id].get_local_position(), Vector2::create(628.0, 532.0));
  ```

- `pub fn close(&mut self, node_tree: &mut NodeTree)` (method, since 0.3.2): Close.
  - `node_tree` &mut NodeTree: Node tree that holds the menu. (turns off the host node, row nodes remain until the next show)
- `pub fn is_showing(&self) -> bool` (method, since 0.3.2): Whether it is showing.
  - returns bool: true if after show and before close.
- `pub fn get_overlay_node(&self) -> Option<NodeId>` (method, since 0.3.2): Outside-press blocker node handle.
  - returns Option<NodeId>: Transparent blocker node. (None before being attached to a tree)
- `pub fn get_panel_node(&self) -> Option<NodeId>` (method, since 0.3.2): Menu panel node handle.
  - returns Option<NodeId>: Menu panel node. (None before being attached to a tree)
- `pub fn get_item_nodes(&self) -> &[NodeId]` (method, since 0.3.2): List of item row node handles.
  - returns &[NodeId]: Row nodes created by the last show. (in item order, rebuilt on every show)
