Module zng::menu

source ·
Expand description

Menu widgets, properties and other types.

use zng::prelude::*;

fn main_menu() -> impl UiNode {
    Menu!(ui_vec![
        SubMenu!(
            "File",
            ui_vec![
                Button!(zng::app::NEW_CMD.scoped(WINDOW.id())),
                Button!(zng::app::OPEN_CMD.scoped(WINDOW.id())),
                Toggle! {
                    child = Text!("Auto Save");
                    checked = var(true);
                },
                Hr!(),
                SubMenu!(
                    "Recent",
                    (0..10)
                        .map(|i| Button! { child = Text!(formatx!("recent file {i}")) })
                        .collect::<UiVec>()
                ),
                Hr!(),
                Button!(zng::app::EXIT_CMD),
            ]
        ),
        SubMenu!(
            "Help",
            ui_vec![Button! {
                child = Text!("About");
                on_click = hn!(|_| { });
            }]
        ),
    ])
}

Window! {
    child_top = main_menu(), 0;
    zng::app::on_new = hn!(|_| { });
    zng::app::on_open = hn!(|_| { });
    // ..
}

The example above declares a Menu! for a window, it demonstrates nested SubMenu!, and menu items, Button!, Toggle! and Hr!. There is no menu item widget, the SubMenu! widget re-styles button and toggle.

§Context Menu

This module also provides a context menu. The example below declares a context menu for the window, it will show on context click, that is, by right-clicking the window, long pressing it or pressing the context menu key.

use zng::prelude::*;

Window! {
    context_menu = ContextMenu!(ui_vec![
        Button!(zng::app::NEW_CMD.scoped(WINDOW.id())),
        Button!(zng::app::OPEN_CMD.scoped(WINDOW.id())),
        Toggle! {
            child = Text!("Auto Save");
            checked = var(true);
        },
        Hr!(),
        SubMenu!("Help", ui_vec![Button! {
            child = Text!("About");
            on_click = hn!(|_| { });
        }]),
        Hr!(),
        Button!(zng::app::EXIT_CMD),
    ]);
}

§Full API

See zng_wgt_menu for the full widget API.

Modules§

  • Context menu widget and properties.
  • Sub-menu popup widget and properties.
  • Submenu widget and properties.

Structs§

Functions§