Menus

Nib has two kinds of menus:

  • The status bar menu, app.menu: a list of MenuItem and MenuDivider shown when the user right-clicks the menu bar icon.
  • A view context menu, context_menu=: a list of views shown when the user right-clicks a view in the popover.

menu

import nib


def main(app: nib.App):
    app.title = "Menu"
    app.icon = nib.SFSymbol("contextualmenu.and.cursorarrow")
    app.width = 300
    app.height = 150

    app.menu = [
        nib.MenuItem("Preferences", icon="gear", shortcut="cmd+,"),
        nib.MenuItem(
            "More Options",
            menu=[
                nib.MenuItem("Option A", badge="3"),
                nib.MenuItem("Option B", shortcut="cmd+R"),
            ],
        ),
        nib.MenuDivider(),
        nib.MenuItem(
            "Quit",
            icon="power",
            shortcut="cmd+Q",
            on_click=app.quit,
        ),
    ]

    app.build(
        nib.VStack(
            controls=[
                nib.Text("Right-click the status bar icon", font=nib.Font.HEADLINE),
                nib.Text(
                    "to see the context menu.",
                    font=nib.Font.CAPTION,
                    foreground_color=nib.Color.SECONDARY,
                ),
            ],
            spacing=4,
            padding=20,
        )
    )


nib.run(main)

Status bar menu

import nib

def main(app: nib.App):
    app.title = "My App"
    app.icon = nib.SFSymbol("star.fill")

    def open_settings():
        print("Opening settings")

    app.menu = [
        nib.MenuItem("Settings", on_click=open_settings, icon="gear", shortcut="cmd+,"),
        nib.MenuDivider(),
        nib.MenuItem("Quit", on_click=app.quit, shortcut="cmd+q"),
    ]

    app.build(nib.Text("Right-click the menu bar icon", padding=20))

nib.run(main)

Context menu with custom items and keyboard shortcuts

nib.MenuItem(
    title=None,
    on_click=None,
    icon=None,
    content=None,
    menu=None,
    shortcut=None,
    state=None,
    badge=None,
    enabled=True,
    height=None,
    id=None,
)
Parameter Type Default Description
title str | None None The menu item text. Optional if content is provided
on_click Callable[[], None] | None None Callback invoked when the item is clicked. Not called on an item that has a menu
icon str | SFSymbol | None None SF Symbol name as a string (e.g. "gear") or an SFSymbol instance with full configuration
content View | None None Custom view for the item. Replaces title, icon, state and badge
menu list[MenuItem] | None None Child items to create a submenu
shortcut str | None None Key equivalent shown next to the item, e.g. "cmd+q", "cmd+shift+n", "opt+x"
state MenuItemState | str | None None MenuItemState.ON (or "on") shows a checkmark, OFF nothing, MIXED a dash
badge str | None None Badge text shown on the right side of the item
enabled bool True Whether the item is clickable. Disabled items appear grayed out
id str | None None The item's id, echoed in its events; a UUID by default
height float | None None Height in points for a content item

Every parameter is stored as a public attribute of the same name; menu is an empty list when there is no submenu.

MenuDivider() takes no parameters and draws a separator line.

Icons

Pass a string for an SF Symbol, or an SFSymbol for a styled one:

nib.MenuItem("Settings", on_click=handler, icon="gear")
nib.MenuItem(
    "Favorites",
    on_click=handler,
    icon=nib.SFSymbol("star.fill", foreground_color=nib.Color.YELLOW),
)

Shortcuts

Join modifiers and one key with +. Modifiers are cmd, shift, opt (or alt) and ctrl.

nib.MenuItem("Refresh", on_click=refresh, shortcut="cmd+shift+r")

A menu item shortcut is a key equivalent: it works only while the menu is open. For a shortcut that works at any time, use app.on_hotkey().

Badges, state and disabled items

nib.MenuItem("Updates", on_click=check_updates, badge="3")
nib.MenuItem("Enabled", on_click=toggle_feature, state="on")      # checkmark
nib.MenuItem("Partial", on_click=toggle_feature, state="mixed")   # dash
nib.MenuItem("Premium Only", on_click=upgrade, enabled=is_premium)

Pass a menu list to nest items, to any depth:

nib.MenuItem(
    "Export",
    icon="square.and.arrow.up",
    menu=[
        nib.MenuItem("Export as PDF", on_click=export_pdf, icon="doc"),
        nib.MenuItem("Export as PNG", on_click=export_png, icon="photo"),
    ],
)

Changing the menu

Every constructor argument of MenuItem is a property. Assign one and the menu updates:

import nib

def main(app: nib.App):
    app.title = "Editor"
    app.icon = nib.SFSymbol("doc.text")

    def set_theme(name):
        for theme, item in themes.items():
            item.state = "on" if theme == name else "off"

    themes = {
        name: nib.MenuItem(name.title(), on_click=lambda n=name: set_theme(n))
        for name in ("light", "dark")
    }
    set_theme("light")

    app.menu = [
        nib.MenuItem("Theme", icon="paintbrush", menu=list(themes.values())),
        nib.MenuDivider(),
        nib.MenuItem("Quit", on_click=app.quit),
    ]
    app.build(nib.Text("Right-click for menu", padding=20))

nib.run(main)

Views inside content are reactive on their own, so row_title.content = "..." updates the row. To add or remove items, assign a new list to app.menu, or change the list in place and call app.update():

app.menu.insert(0, nib.MenuItem("New note", on_click=new_note))
app.update()

Custom view content

content takes any nib view. Set height so the view fits. The view starts at the leading edge and spans the menu width; a horizontal padding of 14 lines it up with title items. The item highlights on hover and keyboard navigation like a title item. A click on the row runs the item's on_click and closes the menu. A click on a control inside the content, such as a Button or a view with on_click, runs that handler instead and closes the menu. A context_menu inside the content never opens: macOS cannot show a second menu while one is open. Put the secondary action in the row as a control:

nib.MenuItem(
    content=nib.HStack(
        controls=[
            nib.SFSymbol("star.fill", foreground_color=nib.Color.YELLOW),
            nib.VStack(
                controls=[
                    nib.Text("Premium", font=nib.Font.HEADLINE),
                    nib.Text("Upgrade now", font=nib.Font.CAPTION,
                             foreground_color=nib.Color.SECONDARY),
                ],
                alignment=nib.HorizontalAlignment.LEADING,
                spacing=2,
            ),
        ],
        spacing=8,
        padding={"horizontal": 14},
    ),
    on_click=upgrade,
    height=50,
)

View context menu

View context menu

Every view takes context_menu=, a list of views. Right-clicking the view shows them as a native macOS context menu (SwiftUI's .contextMenu):

nib.Text(
    "Right-click me",
    context_menu=[
        nib.Button("Copy", on_click=copy_fn),
        nib.Button("Paste", on_click=paste_fn),
        nib.Divider(),
        nib.Button("Delete", on_click=delete_fn, role="destructive"),
    ],
    padding=20,
)

A context menu renders only menu-compatible views:

View In the menu
Button An item. role="destructive" shows it in red. Its content= can be an HStack of an icon and a text, used as the item's label
Divider A separator
Toggle An item with a checkmark
Picker An inline list of options
Text A label that cannot be clicked
ShareLink An item that opens the share sheet

Other views placed directly in the list, such as VStack, HStack, Image or List, are not rendered.

context_menu=[
    nib.Button("Copy", on_click=copy_fn),
    nib.Button(
        content=nib.HStack(controls=[nib.SFSymbol("doc.on.doc"), nib.Text("Duplicate")]),
        on_click=duplicate_fn,
    ),
    nib.Toggle("Dark Mode", is_on=dark_mode, on_change=toggle_dark),
    nib.Picker("Size", selection="Medium", options=["Small", "Medium", "Large"],
               on_change=on_pick),
    nib.ShareLink(items=["photo.jpg"], label="Share"),
]
  • App: app.menu, app.on_hotkey()
  • SFSymbol: menu item icons
  • SettingsWindow: a menu item that opens the settings window
  • Modifiers: context_menu among the other view parameters