Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some distance between parent menu and submenu #4230

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,22 @@ pub(crate) fn menu_ui<'c, R>(
.constrain_to(ctx.screen_rect())
.interactable(true);

area.show(ctx, |ui| {
let area_response = area.show(ctx, |ui| {
set_menu_style(ui.style_mut());

let frame = Frame::menu(ui.style()).show(ui, |ui| {
ui.set_max_width(ui.spacing().menu_width);
ui.set_menu_state(Some(menu_state_arc.clone()));
ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
.inner
});
Frame::menu(ui.style())
.show(ui, |ui| {
ui.set_max_width(ui.spacing().menu_width);
ui.set_menu_state(Some(menu_state_arc.clone()));
ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents)
.inner
})
.inner
});

menu_state_arc.write().rect = frame.response.rect;
menu_state_arc.write().rect = area_response.response.rect;

frame.inner
})
area_response
}

/// Build a top level menu with a button.
Expand Down Expand Up @@ -549,7 +551,8 @@ pub(crate) struct MenuState {
/// The opened sub-menu and its [`Id`]
sub_menu: Option<(Id, Arc<RwLock<MenuState>>)>,

/// Bounding box of this menu (without the sub-menu)
/// Bounding box of this menu (without the sub-menu),
/// including the frame and everything.
pub rect: Rect,

/// Used to check if any menu in the tree wants to close
Expand Down Expand Up @@ -620,7 +623,8 @@ impl MenuState {
// ensure to repaint once even when pointer is not moving
ui.ctx().request_repaint();
} else if !open && button.hovered() {
let pos = button.rect.right_top();
let mut pos = button.rect.right_top();
pos.x = self.rect.right() + ui.spacing().menu_spacing;
self.open_submenu(sub_id, pos);
} else if open
&& ui.interact_bg(Sense::hover()).contains_pointer()
Expand Down
10 changes: 10 additions & 0 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ pub struct Spacing {
/// The default width of a menu.
pub menu_width: f32,

/// Horizontal distance between a menu and a submenu.
pub menu_spacing: f32,

/// End indented regions with a horizontal line
pub indent_ends_with_horizontal_line: bool,

Expand Down Expand Up @@ -1239,6 +1242,7 @@ impl Default for Spacing {
icon_spacing: 4.0,
tooltip_width: 600.0,
menu_width: 150.0,
menu_spacing: 3.0,
combo_height: 200.0,
scroll: Default::default(),
indent_ends_with_horizontal_line: false,
Expand Down Expand Up @@ -1592,6 +1596,7 @@ impl Spacing {
icon_spacing,
tooltip_width,
menu_width,
menu_spacing,
indent_ends_with_horizontal_line,
combo_height,
scroll,
Expand Down Expand Up @@ -1659,6 +1664,11 @@ impl Spacing {
ui.label("Default width of a menu");
});

ui.horizontal(|ui| {
ui.add(DragValue::new(menu_spacing).clamp_range(0.0..=10.0));
ui.label("Horizontal spacing between menus");
});

ui.checkbox(
indent_ends_with_horizontal_line,
"End indented regions with a horizontal separator",
Expand Down
Loading