Skip to content

Add MenuConfig::submenu_gap to override the hardcoded gap between a menu and its submenus - #8338

Open
kiwirm wants to merge 1 commit into
emilk:mainfrom
kiwirm:submenu-gap
Open

Add MenuConfig::submenu_gap to override the hardcoded gap between a menu and its submenus#8338
kiwirm wants to merge 1 commit into
emilk:mainfrom
kiwirm:submenu-gap

Conversation

@kiwirm

@kiwirm kiwirm commented Jul 24, 2026

Copy link
Copy Markdown

The gap between a menu and the submenu it opens is hardcoded in SubMenu::show
(frame.total_margin().sum().x / 2.0 + 2.0), so a submenu can't be placed flush
against its parent — even with a zero menu margin a ~2px gap remains.

This adds an optional submenu_gap: Option<f32> to MenuConfig (plus a
MenuConfig::submenu_gap(f32) builder). When set it overrides the offset; when
None it falls back to the existing computed value, so existing menus are
unaffected. Because a menu's MenuConfig propagates to its submenus, setting it
once on the root menu applies to the whole tree.

egui::MenuButton::new("Add")
    .config(egui::MenuConfig::new().submenu_gap(0.0)) // flush submenus
    .ui(ui, |ui| { /* … */ });
  • I have followed the instructions in the PR template

@github-actions

Copy link
Copy Markdown

Preview is being built...

Preview will be available at https://egui-pr-preview.github.io/pr/8338-submenu-gap

View snapshot changes at kitdiff

Comment on lines +512 to 515
let gap = menu_config
.submenu_gap
.unwrap_or_else(|| frame.total_margin().sum().x / 2.0 + 2.0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

Suggested change
let gap = menu_config
.submenu_gap
.unwrap_or_else(|| frame.total_margin().sum().x / 2.0 + 2.0);
let gap = frame.total_margin().sum().x / 2.0 + menu_config
.submenu_gap
.unwrap_or(2.0);

instead?
frame.total_margin().sum().x / 2.0 should basically mean "0 visual gap between the menus" which I think is what you'd expect when setting the gap to 0? Otherwise at 0 there would be some overlap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants