Skip to main content

Module menu_item

Module menu_item 

Source
Expand description

MenuItem — a single command row in a menu or context menu.

Each item consists of an optional leading icon, a label, an optional trailing shortcut label, and an activation closure. MenuItem is non-generic: actions are type-erased closures identical to Button’s on_activate_fn model. Submenus are declared with MenuItem::submenu — the factory builds the nested MenuList lazily at hover time.

Every item operates in one of three modes selected by builder methods:

BuilderAT RoleLeading glyph
(default)Role::MenuItemicon or blank
.checked(signal)Role::MenuItemCheckBoxcheckmark / blank
.check_state(signal)Role::MenuItemCheckBoxcheck / dash / blank
.reflect_checked(signal)Role::MenuItemCheckBoxcheckmark (read-only)
.radio(value, selected)Role::MenuItemRadiofilled dot / blank

Check and radio modes are mutually exclusive with .icon(...) — the Windows convention reserves the leading slot for state glyphs on checkable items; a debug_assert! fires when both are set.

§An icon that keeps its own colour

.icon(...) recolours whatever it is handed with the row’s text role, so the glyph follows hover, press and disabled alongside the label. That is right for an icon that says the same thing as the label, and wrong for one whose colour is the content — a tag’s swatch, a status light, a colour a person chose.

.icon_keeps_color() leaves it alone. Two costs come with it: the icon no longer follows the highlight (on a style whose highlighted row is a solid accent fill, it has to carry its own contrast against that fill), and a literal colour does not dim in a disabled row — ColorProp::Static and Bound ignore the enabled state, while every role variant substitutes its disabled counterpart. An icon that should dim wants a role, and then it does not want this at all.

let swatch = IconWidget::from_path(Path::circle(Point::new(5.0, 5.0), 4.5), 10.0)
    .color(Color::from_hex("#e91e63"));
let _w = MenuItem::new(lit!("Characters"))
    .icon(swatch)
    .icon_keeps_color();

Mnemonic markers use the in-string & convention (&Save → underline ‘S’ when Alt is held; && → literal &). The enclosing MenuList wires bare-letter in-menu activation automatically.

let _w = MenuItem::new(lit!("&Save"))
    .on_activate_fn(|ctx| ctx.send_intent(Intent::new("app.save")));

Structs§

MenuItem
A single command row in a MenuList or context menu.