1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +00:00

LibGUI: Subtract layout margin when placing items along secondary axis

While space distribution along the primary axis of a BoxLayout is
pretty sophisticated, the secondary axis is very simple: we simply
center the widget.

However, this doesn't always look very nice if we don't take margins
into account, so make sure we subtract them from the rect we do all
the centering within.
This commit is contained in:
Andreas Kling 2021-04-02 22:53:58 +02:00
parent 05d7869dc0
commit 6ba00ae5b9

View file

@ -198,6 +198,12 @@ void BoxLayout::run(Widget& widget)
int current_x = margins().left();
int current_y = margins().top();
auto widget_rect_with_margins_subtracted = widget.rect();
widget_rect_with_margins_subtracted.take_from_left(margins().left());
widget_rect_with_margins_subtracted.take_from_top(margins().top());
widget_rect_with_margins_subtracted.take_from_right(margins().right());
widget_rect_with_margins_subtracted.take_from_bottom(margins().bottom());
for (auto& item : items) {
Gfx::IntRect rect { current_x, current_y, 0, 0 };
@ -220,9 +226,9 @@ void BoxLayout::run(Widget& widget)
rect.set_secondary_size_for_orientation(orientation(), secondary);
if (orientation() == Gfx::Orientation::Horizontal)
rect.center_vertically_within(widget.rect());
rect.center_vertically_within(widget_rect_with_margins_subtracted);
else
rect.center_horizontally_within(widget.rect());
rect.center_horizontally_within(widget_rect_with_margins_subtracted);
item.widget->set_relative_rect(rect);
}