diff --git a/crates/egui/src/menu.rs b/crates/egui/src/menu.rs index 29441d8a74c..8b374ce7ad9 100644 --- a/crates/egui/src/menu.rs +++ b/crates/egui/src/menu.rs @@ -476,6 +476,7 @@ impl SubMenuButton { let text_style = TextStyle::Button; let sense = Sense::click(); + let text_icon_gap = ui.spacing().item_spacing.x; let button_padding = ui.spacing().button_padding; let total_extra = button_padding + button_padding; let text_available_width = ui.available_width() - total_extra.x; @@ -494,7 +495,7 @@ impl SubMenuButton { text_style, ); let text_and_icon_size = Vec2::new( - text_galley.size().x + icon_galley.size().x, + text_galley.size().x + text_icon_gap + icon_galley.size().x, text_galley.size().y.max(icon_galley.size().y), ); let mut desired_size = text_and_icon_size + 2.0 * button_padding; diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index 4b5aa6b6bf5..a2b29327b04 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -215,16 +215,14 @@ impl Widget for Button<'_> { Vec2::ZERO }; + let gap_before_shortcut_text = ui.spacing().item_spacing.x; + let mut text_wrap_width = ui.available_width() - 2.0 * button_padding.x; if image.is_some() { text_wrap_width -= image_size.x + ui.spacing().icon_spacing; } - if !shortcut_text.is_empty() { - text_wrap_width -= 60.0; // Some space for the shortcut text (which we never wrap). - } - let galley = - text.map(|text| text.into_galley(ui, wrap_mode, text_wrap_width, TextStyle::Button)); + // Note: we don't wrap the shortcut text let shortcut_galley = (!shortcut_text.is_empty()).then(|| { shortcut_text.into_galley( ui, @@ -234,6 +232,14 @@ impl Widget for Button<'_> { ) }); + if let Some(shortcut_galley) = &shortcut_galley { + // Leave space for the shortcut text: + text_wrap_width -= gap_before_shortcut_text + shortcut_galley.size().x; + } + + let galley = + text.map(|text| text.into_galley(ui, wrap_mode, text_wrap_width, TextStyle::Button)); + let mut desired_size = Vec2::ZERO; if image.is_some() { desired_size.x += image_size.x; @@ -246,9 +252,9 @@ impl Widget for Button<'_> { desired_size.x += text.size().x; desired_size.y = desired_size.y.max(text.size().y); } - if let Some(shortcut_text) = &shortcut_galley { - desired_size.x += ui.spacing().item_spacing.x + shortcut_text.size().x; - desired_size.y = desired_size.y.max(shortcut_text.size().y); + if let Some(shortcut_galley) = &shortcut_galley { + desired_size.x += gap_before_shortcut_text + shortcut_galley.size().x; + desired_size.y = desired_size.y.max(shortcut_galley.size().y); } desired_size += 2.0 * button_padding; if !small {