Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MenuItem

Layer: cell · Path: src/cells/menu_item.rs · Exports: menu_item::MenuItem

A single menu row: optional leading icon, a label, and an optional right-aligned keyboard shortcut rendered as a Kbd. Modelled on the shadcn DropdownMenu item (.checked(..) mirrors the shadcn CheckboxItem). Clicking yields a [Response]; a disabled item drops its interactive sense.

Design

  • Purpose / when to use — Rows inside dropdowns, context menus, and command lists where each entry pairs an action label with an optional accelerator.

  • Anatomy — A Surface (transparent, padded) wrapping a horizontal layout: optional muted Icon + a Text label + (right-to-left) an optional Kbd shortcut.

  • Variants / states

    StateEffect
    default (enabled(true))Surface::interactive() — hover feedback + click sense
    disabled (enabled(false))Surface is non-interactive (no hover/sense)
    with shortcuttrailing Kbd pinned right via Layout::right_to_left(Align::Center)
    checkable (checked(true))leading check-mark Icon before icon/label (shadcn CheckboxItem)
    checkable (checked(false))the mark’s slot (core::ICON_MD + core::SPACE_2) is reserved so checked/unchecked siblings stay aligned
  • Tokens / layout consumedcore::SPACE_1 (4px outer pad), core::SPACE_2 (icon→label gap), core::RADIUS_SM (4px), core::ICON_MD (reserved check slot). See tokens.

  • Accessibility — Disabled rows simply lose interactivity; they are not greyed by the cell itself.

API

MethodSignatureEffect
newnew(label: impl Into<String>) -> SelfConstruct with a label; enabled defaults true.
iconicon(self, glyph: &'static str) -> SelfLeading muted icon (phosphor glyph).
shortcutshortcut(self, shortcut: impl Into<String>) -> SelfRight-aligned Kbd shortcut text.
enabledenabled(self, enabled: bool) -> SelfWhen false, the surface is not interactive.
checkedchecked(self, checked: bool) -> SelfCheckable item (a View-menu toggle): true shows a check mark, false reserves the mark’s width so siblings line up. Unset (default) = plain action row, no slot.
id_sourceid_source(self, id: impl std::hash::Hash) -> SelfStable id for the underlying Surface.
showshow(self, ui: &mut Ui) -> ResponseRender; returns the Surface response (.clicked()).

Usage

#![allow(unused)]
fn main() {
use ouroboros_ui::cells::MenuItem;
use ouroboros_ui::egui_phosphor::light;

MenuItem::new("Copy").icon(light::COPY).shortcut("Ctrl C").id_source("mi_c").show(ui);
}
#![allow(unused)]
fn main() {
// realistic — a small context menu column
MenuItem::new("Copy").icon(light::COPY).shortcut("Ctrl C").id_source("mi_c").show(ui);
MenuItem::new("Paste").icon(light::CLIPBOARD).shortcut("Ctrl V").id_source("mi_v").show(ui);
MenuItem::new("Delete").icon(light::TRASH).id_source("mi_d").show(ui);
MenuItem::new("Disabled").enabled(false).id_source("mi_x").show(ui);
}
#![allow(unused)]
fn main() {
// checkable — a View-menu toggle section; unchecked rows keep the labels aligned
MenuItem::new("Show Grid").checked(show_grid).id_source("mi_g").show(ui);
MenuItem::new("Show Gizmos").checked(show_gizmos).id_source("mi_z").show(ui);
MenuItem::new("Snap to Grid").checked(snap).shortcut("Ctrl G").id_source("mi_s").show(ui);
}

Composition

Composes the Surface, Icon, Text, and Kbd atoms only. No painting — visuals come entirely from Surface + atoms. Enforced by tests/no_painter_in_molecules.rs.

Notes

  • The shortcut is plain text (e.g. "Ctrl C"), rendered by Kbd; it does not bind a real accelerator — wire the key handling separately.
  • Give each row a distinct id_source to avoid surface id collisions.
  • checked only displays state — flip your own bool on .clicked(). Mixing checkable and plain rows in one menu is fine, but keep all rows of a toggle section checkable so the reserved slot keeps their labels aligned.