NAME

Chandra::Menu - A native menu bar for a Chandra app

SYNOPSIS

use utf8;
use Chandra::App;

my $app  = Chandra::App->new(title => 'Editor');
my $menu = $app->menu;                 # shown when the app runs

$menu->add_standard('app');            # macOS only, ignored elsewhere
$menu->add_menu(File => [
    { label => 'New',     accel => 'mod+n', handler => \&new_file, name => 'new' },
    { label => 'Open…',   accel => 'mod+o', handler => \&open_file },
    { separator => 1 },
    { role => 'quit' },                # Cmd+Q / Ctrl+Q, ends run() like the close button
]);
$menu->add_standard('edit');           # undo, redo, cut, copy, paste, select all
$menu->add_standard('view');           # reload, zoom, full screen (and dev tools when debugging)
$menu->add_menu(Tools => [
    { label => 'Word wrap', type => 'checkbox', checked => 1,
      handler => sub { my ($item, $menu) = @_; wrap($item->{checked}) } },
    { label => 'Theme', submenu => [
        { label => 'Light', type => 'radio', group => 'theme', checked => 1, handler => \&light },
        { label => 'Dark',  type => 'radio', group => 'theme', handler => \&dark },
    ] },
]);
$menu->add_standard('window');

$menu->update_item('new', disabled => 1);    # by name, id, label or 'File/New'
$menu->on_activate(sub { my ($item) = @_ }); # items without a handler

$app->run;

# or all at once
$app->menu([
    { role => 'appMenu' },
    { label => 'File', submenu => [ { label => 'Save', accel => 'mod+s', handler => \&save } ] },
    { role => 'editMenu' },
]);

DESCRIPTION

A Chandra::Menu is the menu bar of an app's main window: an NSMenu that is the application's main menu on macOS, a GtkMenuBar above the page on Linux, and the window's menu on Windows. Its items are the items Chandra::Tray and Chandra::ContextMenu take (see "ITEMS"): labels, accelerators, separators, checkboxes, radio groups, submenus, disabled items and standard roles. Every item runs a Perl handler, or the menu's on_activate, or, for a role, does what the platform does.

The menu can be built before the app runs and changed at any time, including from inside a handler: the native bar follows. The bar is rebuilt when the event loop next turns, not under the item that was chosen.

CONSTRUCTOR

new(app => $app, items => \@items, on_activate => \&code)

new(window => $window, items => \@items, on_activate => \&code)

my $menu = Chandra::Menu->new(app => $app, items => [ ... ]);
$menu->install;

items are the menus of the bar (items with a submenu). A menu is for an app (its main window) or for a Chandra::Window, not both. Most code does not call new: $app->menu and $window->menu make the menu and install it. The menu holds its app or window weakly; the app or window holds the menu it shows.

METHODS

Every method but find, items and is_installed returns the menu, so calls can be chained.

add_menu($label, \@items, %item)

A menu at the end of the bar. %item adds keys to the menu's own item, a name for instance.

add_standard($which, %opts)

A standard menu at the end of the bar:

app

About, Hide, Hide Others, Show All, Quit. macOS only: elsewhere it adds nothing.

file

Close Window on macOS, Quit elsewhere.

edit

Undo, Redo, Cut, Copy, Paste, Paste and Match Style (macOS), Delete, Select All.

view

Reload, Force Reload, Toggle Developer Tools (when the app debugs or has its developer tools), Actual Size, Zoom In, Zoom Out, Toggle Full Screen.

window

Minimize, Zoom, and Bring All to Front on macOS or Close Window elsewhere.

help

Empty but for what items adds; About when asked for outside macOS.

Options: label (the menu's title), name (the menu's name, $which by default), items (items added at the end), and any role by name: a code reference is the item's handler (it runs instead of what the platform does), a false value leaves the item out, a true one keeps it (and adds toggledevtools, or about to Help outside macOS).

$menu->add_standard('edit', delete => 0, paste => sub { paste_special() });
$menu->add_standard('help', items => [ { label => 'Manual', handler => \&manual } ]);

Each item of a standard menu is named after its role, so $menu->update_item('copy', disabled => 1) finds it.

In a list of items, { role => 'editMenu' } (also appMenu, fileMenu, viewMenu, windowMenu, helpMenu, in any case) is the standard menu, with the hash's other keys as its options.

add_item($menu, \%item)

add_item($menu, $label, \&handler)

An item at the end of a menu, found by id, name, label or a path of labels ('File/Recent'). With $menu undef or empty the item goes on the bar.

add_separator($menu)

update_item($key, key => value, ...)

Changes an item: its label, disabled/enabled, checked, accel, handler, submenu and so on. $key is an id, a name, a label or a path.

remove_item($key)

remove_menu($key)

Removes an item, with its submenu.

find($key)

The item, in its canonical form (see "ITEMS"), or undef. Read it; change it with update_item.

items

The menus of the bar, as a new list.

on_activate(\&code)

Runs, with ($item, $menu), for a chosen item without a handler of its own.

install

Makes this the menu of its app or window: an app's shows while the app runs (from run, or init for an app driven by hand), a window's at once, and a menu the app or window showed before goes. A menu without items shows no bar.

remove

Takes the bar away. install shows it again.

is_installed

Whether the window's bar shows this menu.

ITEMS

An item is a hash:

label     the text
handler   a code reference, called with ($item, $menu)   (alias: action)
accel     an accelerator, "mod+s", "ctrl+shift+z", "f5"  (alias: accelerator)
type      normal, checkbox, radio, separator or submenu  (usually implied)
checked   for a checkbox or radio item
disabled  1 for an item that cannot be chosen            (or enabled => 0)
group     the radio group; without one, a submenu's radio items are one group
submenu   a list of items                                (alias: items)
role      a standard item (below)
name      a name to find the item by
separator 1 for a separator

mod is Command on macOS and Control elsewhere. A checkbox changes state before its handler runs, and a radio item unchecks the others of its group.

Labels are Perl character strings: write non-ASCII labels in a file with use utf8, or they show as Latin-1.

Roles

Roles have a label and an accelerator by default, and do what the platform does unless the item has a handler:

about hide hideothers unhide quit                         (app)
undo redo cut copy paste pasteandmatchstyle delete selectall   (edit)
reload forcereload toggledevtools resetzoom zoomin zoomout togglefullscreen   (view)
minimize zoom close front                                 (window)

quit ends the event loop as the close button does, so run returns and on_close runs. toggledevtools without a handler toggles the app's Chandra::DevTools. About, Hide, Hide Others and Show All only do something on macOS; elsewhere they run their handler or on_activate.

On Linux and Windows the editing roles (undo, redo, cut, copy, paste, delete, select all) show their accelerator but leave the key to the page, so text fields keep it. An item without a role whose accelerator is one of those keys takes it from the page; Chandra::Menu warns about that when the menu is installed, and about an accelerator that Chandra::Shortcut binds too (the menu takes the key first, so the shortcut never runs).

PLATFORMS

On macOS the bar is the application's: the first menu is the application menu when it has one of its roles (about, hide, hideothers, unhide, quit); otherwise the default one comes first. A bar without editing roles gets the default Edit menu, less any key equivalent the bar uses itself, so Cmd+C and Cmd+V keep working. Menus titled Window and Help become the Window and Help menus.

On Linux the window grows by the bar's height, so the page keeps its size. On Windows the window keeps its client size the same way.

A child window (Chandra::Window) has a bar of its own through $window->menu. On macOS NSApp's main menu is the key window's bar: a child window's while it is key, else the main window's, else the default menu. Roles act on the window they are chosen in; Quit ends the app's event loop. Toggle Developer Tools without a handler runs on_activate in a window's menu (the developer tools are the main window's). Chandra::Shortcut clashes are checked for an app's menu only, since shortcuts are the main page's.

SEE ALSO

Chandra::App, Chandra::Tray, Chandra::ContextMenu, Chandra::Shortcut