Better spacing and sizes for (menu) buttons (#4558)

This commit is contained in:
Emil Ernerfeldt 2024-05-28 14:19:25 +02:00 committed by GitHub
parent 26206526d6
commit 1888d19b4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 9 deletions

View File

@ -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;

View File

@ -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 {