LibGUI: Rewrite layout system in terms of min and max sizes

This patch removes size policies and preferred sizes, and replaces them
with min-size and max-size for each widget.

Box layout now works in 3 passes:

    1) Set all items (widgets/spacers) to their min-size
    2) Distribute remaining space evenly, respecting max-size
    3) Place widgets one after the other, adding spacing in between

I've also added convenience helpers for setting a fixed size (which is
the same as setting min-size and max-size to the same value.)

This significantly reduces the verbosity of widget layout and makes GML
a bit more pleasant to write, too. :^)
This commit is contained in:
Andreas Kling 2020-12-30 01:23:32 +01:00
parent b2bba5ce5c
commit 7dc5a3ead8
Notes: sideshowbarker 2024-07-19 00:25:30 +09:00
83 changed files with 444 additions and 957 deletions

View file

@ -40,11 +40,9 @@ ToolBar::ToolBar(Orientation orientation, int button_size)
: m_button_size(button_size)
{
if (orientation == Orientation::Horizontal) {
set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
set_preferred_size(0, button_size + 8);
set_fixed_height(button_size + 8);
} else {
set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
set_preferred_size(button_size + 8, 0);
set_fixed_width(button_size + 8);
}
set_layout<BoxLayout>(orientation);
layout()->set_spacing(0);
@ -74,7 +72,6 @@ private:
else
set_text(action.text());
set_button_style(Gfx::ButtonStyle::CoolBar);
set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
}
String tooltip(const Action& action) const
{
@ -96,7 +93,7 @@ void ToolBar::add_action(Action& action)
item->action = action;
auto& button = add<ToolBarButton>(action);
button.set_preferred_size(m_button_size + 8, m_button_size + 8);
button.set_fixed_size(m_button_size + 8, m_button_size + 8);
m_items.append(move(item));
}
@ -106,8 +103,7 @@ class SeparatorWidget final : public Widget {
public:
SeparatorWidget()
{
set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
set_preferred_size(8, 18);
set_fixed_size(8, 18);
}
virtual ~SeparatorWidget() override { }