From 6ba00ae5b934c33a4ff2de7ed41619a3acf6ca04 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 2 Apr 2021 22:53:58 +0200 Subject: 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. --- Userland/Libraries/LibGUI/BoxLayout.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibGUI/BoxLayout.cpp b/Userland/Libraries/LibGUI/BoxLayout.cpp index b9abc69309..74e0f3c2fd 100644 --- a/Userland/Libraries/LibGUI/BoxLayout.cpp +++ b/Userland/Libraries/LibGUI/BoxLayout.cpp @@ -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); } -- cgit v1.2.3