1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:27:45 +00:00

LibGUI: Improve calculated_min_size() for Button

Previously min_width() was fixed, rendering width calculations in
calculated_min_size() useless for layout. Minimum width now shrinks
to 22, the historical minimum for height as well, and takes into
account elided text and icons. Height no longer sums the icon, fixing
a layout regression for icon buttons.

Also removes an unnecessary FIXME: Calculated size should only
need to account for the height of a single line of elided text
plus padding. Font's pixel_size_rounded_up() is the right solution
even though it and the underlying pixel metrics of some ScaledFonts
don't always correspond yet.
This commit is contained in:
thankyouverycool 2023-04-14 08:53:04 -04:00 committed by Andreas Kling
parent 55423b4ed0
commit 5181fafce6

View file

@ -23,7 +23,7 @@ namespace GUI {
Button::Button(String text)
: AbstractButton(move(text))
{
set_min_size({ 40, SpecialDimension::Shrink });
set_min_size({ SpecialDimension::Shrink });
set_preferred_size({ SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink });
set_focus_policy(GUI::FocusPolicy::StrongFocus);
@ -275,25 +275,19 @@ void Button::timer_event(Core::TimerEvent&)
Optional<UISize> Button::calculated_min_size() const
{
int width = 0;
int height = 0;
int width = 22;
int height = 22;
int constexpr padding = 6;
if (!text().is_empty()) {
auto& font = this->font();
width = static_cast<int>(ceilf(font.width(text()))) + 2;
height = font.pixel_size_rounded_up() + 4; // FIXME: Use actual maximum total height
width = max(width, font().width_rounded_up("..."sv) + padding);
height = max(height, font().pixel_size_rounded_up() + padding);
}
if (m_icon) {
height += max(height, m_icon->height());
width += m_icon->width() + icon_spacing();
if (icon()) {
int icon_width = icon()->width() + icon_spacing();
width = text().is_empty() ? max(width, icon_width) : width + icon_width;
height = max(height, icon()->height() + padding);
}
width += 8;
height += 4;
height = max(22, height);
return UISize(width, height);
}