1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:37:34 +00:00

LibGUI: Adjust BreadcrumbButtons width on resize

This commit relayouts the BreadcrumbButtons on resize to a shrunken
state if they don't fit. It also caps the button width to 100px to
avoid overflowing the widget.
This commit is contained in:
Marcus Nilsson 2021-08-29 23:16:21 +02:00 committed by Andreas Kling
parent 8649996201
commit 71f345bbb8
2 changed files with 37 additions and 2 deletions

View file

@ -99,11 +99,18 @@ void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String
auto button_text_width = button.font().width(text); auto button_text_width = button.font().width(text);
auto icon_width = icon ? icon->width() : 0; auto icon_width = icon ? icon->width() : 0;
auto icon_padding = icon ? 4 : 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<GUI::Button>() }; 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<GUI::Button>() };
m_segments.append(move(segment)); m_segments.append(move(segment));
relayout();
} }
void Breadcrumbbar::remove_end_segments(size_t start_segment_index) void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
@ -136,6 +143,7 @@ void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
auto& segment = m_segments[index.value()]; auto& segment = m_segments[index.value()];
VERIFY(segment.button); VERIFY(segment.button);
segment.button->set_checked(true); segment.button->set_checked(true);
relayout();
} }
void Breadcrumbbar::doubleclick_event(MouseEvent& event) void Breadcrumbbar::doubleclick_event(MouseEvent& event)
@ -144,4 +152,26 @@ void Breadcrumbbar::doubleclick_event(MouseEvent& event)
on_doubleclick(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);
}
}
} }

View file

@ -19,6 +19,7 @@ public:
void clear_segments(); void clear_segments();
void append_segment(String text, Gfx::Bitmap const* icon = nullptr, String data = {}, String tooltip = {}); void append_segment(String text, Gfx::Bitmap const* icon = nullptr, String data = {}, String tooltip = {});
void remove_end_segments(size_t segment_index); void remove_end_segments(size_t segment_index);
void relayout();
size_t segment_count() const { return m_segments.size(); } size_t segment_count() const { return m_segments.size(); }
String segment_data(size_t index) const { return m_segments[index].data; } String segment_data(size_t index) const { return m_segments[index].data; }
@ -35,10 +36,14 @@ public:
private: private:
Breadcrumbbar(); Breadcrumbbar();
virtual void resize_event(ResizeEvent&) override;
struct Segment { struct Segment {
RefPtr<const Gfx::Bitmap> icon; RefPtr<const Gfx::Bitmap> icon;
String text; String text;
String data; String data;
int width { 0 };
int shrunken_width { 0 };
WeakPtr<GUI::Button> button; WeakPtr<GUI::Button> button;
}; };