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

Better spacing and sizes for (menu) buttons #4558

Merged
merged 2 commits into from
May 28, 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
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
Loading