diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp index 7d10216624..a0cafd1a46 100644 --- a/Userland/Libraries/LibGUI/Breadcrumbbar.cpp +++ b/Userland/Libraries/LibGUI/Breadcrumbbar.cpp @@ -99,11 +99,18 @@ void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String auto button_text_width = button.font().width(text); auto icon_width = icon ? icon->width() : 0; auto icon_padding = icon ? 4 : 0; - button.set_fixed_size(button_text_width + icon_width + icon_padding + 16, 16 + 8); - Segment segment { icon, text, data, button.make_weak_ptr() }; + const int 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_fixed_size(button_width, 16 + 8); + + Segment segment { icon, text, data, button_width, shrunken_width, button.make_weak_ptr() }; m_segments.append(move(segment)); + relayout(); } void Breadcrumbbar::remove_end_segments(size_t start_segment_index) @@ -136,6 +143,7 @@ void Breadcrumbbar::set_selected_segment(Optional index) auto& segment = m_segments[index.value()]; VERIFY(segment.button); segment.button->set_checked(true); + relayout(); } void Breadcrumbbar::doubleclick_event(MouseEvent& event) @@ -144,4 +152,26 @@ void Breadcrumbbar::doubleclick_event(MouseEvent& event) on_doubleclick(event); } +void Breadcrumbbar::resize_event(ResizeEvent&) +{ + relayout(); +} + +void Breadcrumbbar::relayout() +{ + auto remaining_width = 0; + + for (auto& segment : m_segments) + remaining_width += segment.width; + + for (auto& segment : m_segments) { + if (remaining_width > width() && !segment.button->is_checked()) { + segment.button->set_fixed_width(segment.shrunken_width); + remaining_width -= (segment.width - segment.shrunken_width); + continue; + } + segment.button->set_fixed_width(segment.width); + } +} + } diff --git a/Userland/Libraries/LibGUI/Breadcrumbbar.h b/Userland/Libraries/LibGUI/Breadcrumbbar.h index 0695fc2f9b..b07dbe3951 100644 --- a/Userland/Libraries/LibGUI/Breadcrumbbar.h +++ b/Userland/Libraries/LibGUI/Breadcrumbbar.h @@ -19,6 +19,7 @@ public: void clear_segments(); void append_segment(String text, Gfx::Bitmap const* icon = nullptr, String data = {}, String tooltip = {}); void remove_end_segments(size_t segment_index); + void relayout(); size_t segment_count() const { return m_segments.size(); } String segment_data(size_t index) const { return m_segments[index].data; } @@ -35,10 +36,14 @@ public: private: Breadcrumbbar(); + virtual void resize_event(ResizeEvent&) override; + struct Segment { RefPtr icon; String text; String data; + int width { 0 }; + int shrunken_width { 0 }; WeakPtr button; };