Skip to content

Commit

Permalink
Better spacing and sizes for (menu) buttons (emilk#4558)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored and hacknus committed Oct 30, 2024
1 parent e79e1d7 commit 0a46b92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
22 changes: 14 additions & 8 deletions crates/egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 0a46b92

Please sign in to comment.