From be3a9048be24486e9a58ba669afe6f8e03f7e9ae Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 1 Feb 2023 23:28:18 +0100 Subject: [PATCH] LibGUI: Relayout Breadcrumbbar on font change --- Userland/Libraries/LibGUI/Breadcrumbbar.cpp | 45 ++++++++++++++------- Userland/Libraries/LibGUI/Breadcrumbbar.h | 3 ++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp index e9af6543f2..632daccfcd 100644 --- a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp +++ b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp @@ -100,21 +100,14 @@ void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* ico on_segment_drag_enter(index, event); }; - auto button_text_width = button.font().width(text); - auto icon_width = icon ? icon->width() : 0; - auto icon_padding = icon ? 4 : 0; - - int const max_button_width = 100; - - auto button_width = min(button_text_width + icon_width + icon_padding + 16, max_button_width); - auto shrunken_width = icon_width + icon_padding + (icon ? 4 : 16); - - button.set_max_size(static_cast(ceilf(button_width)), 16 + 8); - button.set_min_size(shrunken_width, 16 + 8); - - Segment segment { icon, text, data, static_cast(ceilf(button_width)), shrunken_width, button.make_weak_ptr() }; - - m_segments.append(move(segment)); + m_segments.append(Segment { + .icon = icon, + .text = move(text), + .data = move(data), + .width = 0, + .shrunken_width = 0, + .button = button.make_weak_ptr(), + }); relayout(); } @@ -170,8 +163,30 @@ void Breadcrumbbar::resize_event(ResizeEvent&) relayout(); } +void Breadcrumbbar::did_change_font() +{ + Widget::did_change_font(); + relayout(); +} + void Breadcrumbbar::relayout() { + for (auto& segment : m_segments) { + VERIFY(segment.button); + auto& button = *segment.button; + auto button_text_width = button.font().width(segment.text); + auto icon_width = button.icon() ? button.icon()->width() : 0; + auto icon_padding = button.icon() ? 4 : 0; + + int const max_button_width = 100; + + segment.width = static_cast(ceilf(min(button_text_width + icon_width + icon_padding + 16, max_button_width))); + segment.shrunken_width = icon_width + icon_padding + (button.icon() ? 4 : 16); + + button.set_max_size(segment.width, 16 + 8); + button.set_min_size(segment.shrunken_width, 16 + 8); + } + auto remaining_width = 0; for (auto& segment : m_segments) diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.h b/Userland/Libraries/LibGUI/Breadcrumbbar.h index bec356cf9e..1d1cefe9df 100644 --- a/Userland/Libraries/LibGUI/Breadcrumbbar.h +++ b/Userland/Libraries/LibGUI/Breadcrumbbar.h @@ -35,6 +35,9 @@ public: Function on_segment_drag_enter; Function on_doubleclick; +protected: + virtual void did_change_font() override; + private: Breadcrumbbar();