From 024dc7b135a978abf83c90d260679e3aaa22b2a5 Mon Sep 17 00:00:00 2001 From: Sven Niederberger <73159570+s-nie@users.noreply.github.com> Date: Tue, 18 Mar 2025 11:13:59 +0100 Subject: [PATCH] Add a toggle for the compact menu style (#5777) Menus currently have their own style that removes outlines and backgrounds. This is nice if the menu only contains buttons. However if the menu contains other widgets, e.g. a drag value, the style change makes it quite difficult to identify such widgets. This is a simple way to make this configurable. * [x] I have followed the instructions in the PR template --- crates/egui/src/menu.rs | 12 +++++++----- crates/egui/src/style.rs | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/egui/src/menu.rs b/crates/egui/src/menu.rs index fa99cebb..ef84451d 100644 --- a/crates/egui/src/menu.rs +++ b/crates/egui/src/menu.rs @@ -76,11 +76,13 @@ impl std::ops::DerefMut for BarState { } fn set_menu_style(style: &mut Style) { - style.spacing.button_padding = vec2(2.0, 0.0); - style.visuals.widgets.active.bg_stroke = Stroke::NONE; - style.visuals.widgets.hovered.bg_stroke = Stroke::NONE; - style.visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT; - style.visuals.widgets.inactive.bg_stroke = Stroke::NONE; + if style.compact_menu_style { + style.spacing.button_padding = vec2(2.0, 0.0); + style.visuals.widgets.active.bg_stroke = Stroke::NONE; + style.visuals.widgets.hovered.bg_stroke = Stroke::NONE; + style.visuals.widgets.inactive.weak_bg_fill = Color32::TRANSPARENT; + style.visuals.widgets.inactive.bg_stroke = Stroke::NONE; + } } /// The menu bar goes well in a [`crate::TopBottomPanel::top`], diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 4d227f63..d0b368f0 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -332,6 +332,9 @@ pub struct Style { /// The animation that should be used when scrolling a [`crate::ScrollArea`] using e.g. [`Ui::scroll_to_rect`]. pub scroll_animation: ScrollAnimation, + + /// Use a more compact style for menus. + pub compact_menu_style: bool, } #[test] @@ -1277,6 +1280,7 @@ impl Default for Style { url_in_tooltip: false, always_scroll_the_only_direction: false, scroll_animation: ScrollAnimation::default(), + compact_menu_style: true, } } } @@ -1578,6 +1582,7 @@ impl Style { url_in_tooltip, always_scroll_the_only_direction, scroll_animation, + compact_menu_style, } = self; crate::Grid::new("_options").show(ui, |ui| { @@ -1683,6 +1688,8 @@ impl Style { #[cfg(debug_assertions)] ui.collapsing("🐛 Debug", |ui| debug.ui(ui)); + ui.checkbox(compact_menu_style, "Compact menu style"); + ui.checkbox(explanation_tooltips, "Explanation tooltips") .on_hover_text( "Show explanatory text when hovering DragValue:s and other egui widgets",