Menu
Dropdown and context menus with icons, shortcuts, checks, labels and nested submenus, driven by the pointer or the keyboard.
Popup menu
let trigger = ui.add(Button::new("Edit"));
let mut menu = Menu::new().min_width(250.0).default_open(true);
if state.check_right {
menu = menu.checks_on_right();
}
menu.show(&trigger, |menu| {
menu.header("notes.md", Some("edited"));
menu.separator();
menu.link("About", "https://github.com/crmne/fastframe");
menu.separator();
if menu.item("Handle click").clicked() {
state.message = "You clicked Handle click".to_owned();
}
menu.separator();
for (label, key) in [("Copy", Key::C), ("Cut", Key::X), ("Paste", Key::V)] {
let shortcut = KeyboardShortcut::new(Modifiers::COMMAND, key);
if menu.add(MenuItem::new(label).shortcut(shortcut)).clicked() {
state.message = format!("You clicked {label}");
}
}
menu.separator();
menu.checkbox("Wrap lines", &mut state.wrap);
let minimap = MenuItem::new("Show minimap").checked(state.minimap);
if menu.add(minimap.keep_open()).clicked() {
state.minimap = !state.minimap;
}
menu.checkbox("Checks on the right", &mut state.check_right);
menu.separator();
let search = MenuItem::new("Search")
.icon(glyph(menu.ui().ctx(), "search"))
.shortcut(KeyboardShortcut::new(
Modifiers::COMMAND | Modifiers::SHIFT,
Key::F,
))
// On a keycap the look's `shape.keycap` styles.
.keycap();
if menu.add(search).clicked() {
state.message = "You clicked Search".to_owned();
}
menu.custom(|ui| {
ui.vertical(|ui| {
ui.label("Custom element");
ui.label(
egui::RichText::new("This is a subtitle")
.size(12.0)
.color(tokens(ui.ctx()).muted_foreground),
);
});
});
menu.separator();
menu.add(
MenuItem::new("Disabled item")
.disabled(true)
.accessibility_label("Disabled item, unavailable in this demo"),
);
menu.add(MenuItem::new("Delete file").destructive());
menu.separator();
menu.submenu(Submenu::new("Links").width(220.0), |menu| {
menu.link("fastframe", "https://github.com/crmne/fastframe");
menu.separator();
menu.link("Zed", "https://zed.dev");
});
let colours = [
("Orange", egui::Color32::from_rgb(0xf5, 0x9e, 0x0b)),
("Green", egui::Color32::from_rgb(0x22, 0xc5, 0x5e)),
("Blue", egui::Color32::from_rgb(0x3b, 0x82, 0xf6)),
];
let colour = MenuItem::new("Colour").swatch(colours[0].1);
menu.submenu(colour, |menu| {
for (name, colour) in colours {
menu.add(MenuItem::new(name).swatch(colour));
}
});
let folder = glyph(menu.ui().ctx(), "folder");
menu.submenu(MenuItem::new("Other links").icon(folder), |menu| {
menu.link("Crates", "https://crates.io");
menu.link("Rust docs", "https://docs.rs");
menu.separator();
menu.submenu("Nested", |menu| {
menu.link("Docs.rs", "https://docs.rs");
menu.submenu("Deeper", |menu| {
menu.link("egui", "https://www.egui.rs");
});
});
});
});
Context menu
let theme = tokens(ui.ctx());
for (text, rename) in [
("Right-click here, or focus it and press Shift+F10", true),
("Another area with its own context menu", false),
] {
let area = egui::Frame::new()
.stroke(egui::Stroke::new(1.0, theme.border))
.corner_radius(theme.shape.radius_lg)
.inner_margin(24.0)
.show(ui, |ui| {
ui.set_width(ui.available_width());
ui.label(text);
})
.response
.interact(egui::Sense::click());
Menu::new().show_context(&area, |menu| {
if rename {
menu.item("Cut");
menu.item("Copy");
menu.item("Paste");
menu.separator();
menu.label("This is a label");
menu.checkbox("Wrap lines", &mut state.wrap);
menu.separator();
menu.submenu("Settings", |menu| {
menu.item("Info 0");
menu.separator();
menu.submenu("More", |menu| {
menu.item("More item 1");
menu.item("More item 2");
});
});
menu.separator();
menu.item("Search all");
} else {
menu.link("About", "https://github.com/crmne/fastframe");
menu.separator();
menu.item("Item 1");
}
});
}
Scrollable
let long = ui.add(Button::new("Scrollable menu (100 items)"));
Menu::new()
.max_height(300.0)
.min_width(100.0)
.show(&long, |menu| {
menu.label("Total 100 items");
for index in 0..100 {
if index % 5 == 0 {
menu.separator();
}
if index % 10 == 9 {
menu.submenu(format!("More {index}"), |menu| {
menu.item(format!("Item {index} copy"));
menu.item(format!("Item {index} duplicate"));
});
} else {
menu.item(format!("Item {index}"));
}
}
});
let section = ui.add(Button::new("Scrolling section (40 presets)"));
Menu::new().width(240.0).show(§ion, |menu| {
menu.header("HX Stomp", Some("connected"));
menu.separator();
menu.scroll_area(200.0, |menu| {
for index in 0..40 {
menu.item(format!("Preset {index}"));
}
});
menu.separator();
menu.item("Save preset");
});
let short = ui.add(Button::new("Scrollable menu (5 items)"));
Menu::new().max_height(300.0).small().show(&short, |menu| {
for index in 0..5 {
menu.item(format!("Item {index}"));
}
});
A style for a whole area
// Every menu opened inside takes the style and size, unless it
// picks its own.
ui.control_size(Size::Small, |ui| {
ui.menu_style(Square, |ui| {
let trigger = ui.add(Button::new("File"));
Menu::new().show(&trigger, |menu| {
menu.item("New");
menu.item("Open...");
menu.separator();
menu.add(MenuItem::new("Close all").destructive());
});
});
});
Your own style
let trigger = ui.add(Button::new("Arrange"));
Menu::new().style(&Square).show(&trigger, |menu| {
let front = KeyboardShortcut::new(Modifiers::COMMAND | Modifiers::SHIFT, Key::F);
menu.add(MenuItem::new("Bring to front").shortcut(front));
menu.item("Send to back");
menu.submenu("Align", |menu| {
menu.item("Left");
menu.item("Centre");
menu.item("Right");
});
menu.add(MenuItem::new("Group").disabled(true));
});
/// A square menu with a hairline and no shadow, its rows lit in the
/// primary colour.
struct Square;
impl MenuStyle for Square {
fn paint(&self, ui: &egui::Ui, item: &MenuItemConfiguration<'_>) {
let theme = tokens(ui.ctx());
let lit = item.highlighted && item.state.enabled;
if lit {
item.paint_behind(egui::Shape::rect_filled(
item.rect,
0.0,
theme.primary.background,
));
}
let color = self.foreground(&theme, item);
item.paint_content(ui, MenuItemColors::new(color, color.gamma_multiply(0.7)));
}
fn foreground(&self, theme: &Tokens, item: &MenuItemConfiguration<'_>) -> egui::Color32 {
if !item.state.enabled {
theme.muted_foreground
} else if item.highlighted {
theme.primary.foreground
} else {
theme.foreground
}
}
fn frame(&self, theme: &Tokens) -> egui::Frame {
egui::Frame::new()
.fill(theme.popover)
.stroke(egui::Stroke::new(1.0, theme.border))
}
}
The story above is gallery/src/stories/menu.rs, running in your browser. Every snippet on this page is cut from it.