From 5181fafce67dc861e53c2f6412adb63bd37c4c5b Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Fri, 14 Apr 2023 08:53:04 -0400 Subject: [PATCH] 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. --- Userland/Libraries/LibGUI/Button.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp index 88b999cdbb..10874bd0e4 100644 --- a/Userland/Libraries/LibGUI/Button.cpp +++ b/Userland/Libraries/LibGUI/Button.cpp @@ -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 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(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); }