beetlebug

Menus on macOS

On macOS each menu of a script is a native menu of macOS. This page shows the menu bar at the top of the screen, the application menu, the keys of the items, and the context menus.

The menu bar

Give a toplevel a menu bar with -menu, as in Tk. macOS shows the bar of the active window at the top of the screen, after the application menu:

menu .bar
. configure -menu .bar
menu .bar.file -tearoff 0
.bar add cascade -label File -menu .bar.file
.bar.file add command -label "New" -accelerator Command-N -command newDoc
.bar.file add command -label "Open..." -accelerator Command-O -command openDoc
.bar.file add separator
.bar.file add command -label "Close" -accelerator Command-W -command {destroy .}
proc newDoc {} {puts new}
proc openDoc {} {puts open}
The menu bar of macOS with the File menu of the gallery open
The File menu of the gallery in the menu bar of macOS.

The window has no row for the bar, so its content keeps its full height. When no window has a bar, the menu bar shows only the application menu.

The application menu

The application menu is the first menu of the bar, with the name of the app. ztk fills it with the standard items, and three of them call a proc of the script:

Item What it does
About NAME Calls the proc tkAboutDialog of the script, if it exists.
Settings... (Command-,) Calls the proc ::tk::mac::ShowPreferences, if it exists.
Services The Services menu of macOS.
Hide NAME, Hide Others, Show All Hide and show the windows of the apps, as in each Mac app.
Quit NAME (Command-Q) Calls the proc ::tk::mac::Quit. With no such proc, it closes the main window, and the app ends.

NAME is the name of the app of ztk bundle, or ztk for a script that the ztk program runs. Define the procs to connect the items:

proc tkAboutDialog {} {
    tk_messageBox -message "Notes 1.2" -detail "Made with ztk."
}
proc ::tk::mac::ShowPreferences {} {
    toplevel .settings
}
proc ::tk::mac::Quit {} {
    if {[tk_messageBox -type yesno -message "Quit Notes?"] eq "yes"} exit
}
The application menu of a ztk app open: About, Settings, Services, Hide, Hide Others, Show All, and Quit
The application menu that ztk fills.

Items of your own

A cascade of the bar with the name apple puts its items at the top of the application menu, in place of About:

menu .bar.apple -tearoff 0
.bar add cascade -menu .bar.apple
.bar.apple add command -label "About Notes" -command tkAboutDialog
.bar.apple add command -label "Check for Updates..." -command checkUpdates
proc checkUpdates {} {puts check}

The Help menu and the Window menu

Two other names of a cascade have a special place, as in Tk on macOS:

Cascade Menu of macOS
.bar.help The Help menu, at the end of the bar.
.bar.window The Window menu.
menu .bar.window -tearoff 0
.bar add cascade -label Window -menu .bar.window
menu .bar.help -tearoff 0
.bar add cascade -label Help -menu .bar.help
.bar.help add command -label "Notes Help" -command {puts help}

Keys of the menu items

An accelerator such as Command-N becomes the key equivalent of its item. The menu shows it with the symbols of macOS.

Accelerator Menu shows
Command-N or Cmd+N ⌘N
Command-Shift-S ⇧⌘S
Option-Command-I ⌥⌘I
Command-, ⌘,

The key of an accelerator goes to the window as a key event, as in Tk, and the item does not run. So bind each key to the command of its item:

bind all <Command-n> newDoc
bind all <Command-o> openDoc

One script for each platform

macOS uses Command, and the other platforms use Control. This script gives each platform its own keys:

if {[ztk::has mac]} {
    set mod Command
    set label Command
} else {
    set mod Control
    set label Ctrl
}
.bar.file entryconfigure "New" -accelerator $label+N
bind all <$mod-n> newDoc

Check and radio items, cascades, and state

Each kind of entry of Tk is a native item:

Entry On macOS
checkbutton A check mark while its variable is on.
radiobutton A check mark on the entry of the current value.
cascade A submenu.
separator The line of macOS.
-state disabled A gray item that runs no command.
menu .bar
. configure -menu .bar
menu .bar.view -tearoff 0
.bar add cascade -label View -menu .bar.view
.bar.view add checkbutton -label "Show Sidebar" -variable sidebar
.bar.view add separator
.bar.view add radiobutton -label "Small" -variable size -value small
.bar.view add radiobutton -label "Large" -variable size -value large
menu .bar.view.sort -tearoff 0
.bar.view add cascade -label "Sort By" -menu .bar.view.sort
.bar.view.sort add command -label Name
.bar.view.sort add command -label Date -state disabled
set sidebar 1
set size small

-postcommand runs before the menu opens, so it can change the entries. A change of the active item sends <<MenuSelect>> to the menu.

A native menu shows the label, the accelerator, the check mark, and the state of each entry. It does not show the -image, the -font, or the colors of an entry, and it has no tear-off entry.

Context menus

tk_popup opens a menu of macOS at the pointer. A right click, or a click with two fingers on the trackpad, is button 3. Bind Control-click too, for a mouse with one button:

menu .ctx -tearoff 0
.ctx add command -label Cut -command {puts cut}
.ctx add command -label Copy -command {puts copy}
.ctx add command -label Paste -command {puts paste}
text .t -width 40 -height 6
pack .t -padx 10 -pady 10
bind .t <Button-3> {tk_popup .ctx %X %Y}
bind .t <Control-Button-1> {tk_popup .ctx %X %Y}

The menu of a menubutton, a ttk::menubutton, and tk_optionMenu is also a menu of macOS.

The gallery on macOS with the menu of the menubutton open, a native menu with Apples, Pears, Plums, and Cherries
The menu of a ttk::menubutton is a menu of macOS: ztk examples/platform-gallery.tcl menu=1.

A menu in the menu bar icon

The menu of a menu bar icon is a native menu too. macOS features shows it, and the menu of the Dock icon.

The next page, macOS features, shows the parts of a Mac that only macOS has.