[go: up one dir, main page]

[][src]Module druid::menu

Menus.

How menus work

The types here are a generalized 'menu description'; concrete menus are part of druid-shell.

We deal principally with the MenuDesc type. When you create a window, you can give it a MenuDesc, which will be turned into a concrete menu object on the current platform when the window is built.

Commands

To handle an event from a menu, you assign that menu a Command, and handle the Command event somewhere in your widget tree. Certain special events are handled by the system; these special commands are available as consts in Selector.

Changing the menu

To change the menu for a window, you issue a SET_MENU command, the payload of which should be a new MenuDesc. The new menu will replace the old menu.

The macOS app menu

On macOS, the main menu belongs to the application, not to the window.

In druid, whichever window is frontmost will have its menu displayed as the application menu.

Examples

Creating the default Application menu for macOS:

use druid::{Data, LocalizedString, RawMods};
use druid::command;
use druid::menu::{MenuDesc, MenuItem};

fn macos_application_menu<T: Data>() -> MenuDesc<T> {
    MenuDesc::new(LocalizedString::new("macos-menu-application-menu"))
        .append(MenuItem::new(
            LocalizedString::new("macos-menu-about-app"),
            command::sys::SHOW_ABOUT,
        ))
        .append_separator()
        .append(
            MenuItem::new(
                LocalizedString::new("macos-menu-preferences"),
                command::sys::SHOW_PREFERENCES,
            )
            .hotkey(RawMods::Meta, ",")
            .disabled(),
        )
        .append_separator()
        .append(MenuDesc::new(LocalizedString::new("macos-menu-services")))
        .append(
            MenuItem::new(
                LocalizedString::new("macos-menu-hide-app"),
                command::sys::HIDE_APPLICATION,
            )
            .hotkey(RawMods::Meta, "h"),
        )
        .append(
            MenuItem::new(
                LocalizedString::new("macos-menu-hide-others"),
                command::sys::HIDE_OTHERS,
            )
            .hotkey(RawMods::AltMeta, "h"),
        )
        .append(
            MenuItem::new(
                LocalizedString::new("macos-menu-show-all"),
                command::sys::SHOW_ALL,
            )
            .disabled(),
        )
        .append_separator()
        .append(
            MenuItem::new(
                LocalizedString::new("macos-menu-quit-app"),
                command::sys::QUIT_APP,
            )
            .hotkey(RawMods::Meta, "q"),
        )
}

Modules

sys

Standard system menu items.

Structs

ContextMenu
MenuDesc

A platform-agnostic description of an application, window, or context menu.

MenuItem

A normal menu item.

Enums

MenuEntry

An item in a menu, which may be a normal item, a submenu, or a separator.