1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:38:11 +00:00

GLayout: Add a simple spacer concept; dummy item that expands-to-fit.

A spacer can be inserted anywhere in a layout and will simply expand to fill
the available space. This is useful for pushing widgets into place. :^)
This commit is contained in:
Andreas Kling 2019-05-09 03:06:20 +02:00
parent bd5c79aff2
commit bffaa5ece6
4 changed files with 39 additions and 7 deletions

View file

@ -34,6 +34,9 @@ void GBoxLayout::run(GWidget& widget)
dbgprintf("GBoxLayout: Starting with available size: %s\n", available_size.to_string().characters());
for (auto& entry : m_entries) {
if (entry.type == Entry::Type::Spacer) {
++number_of_visible_entries;
}
if (!entry.widget)
continue;
@ -84,6 +87,11 @@ void GBoxLayout::run(GWidget& widget)
int current_y = margins().top();
for (auto& entry : m_entries) {
if (entry.type == Entry::Type::Spacer) {
current_x += automatic_size.width();
current_y += automatic_size.height();
}
if (!entry.widget)
continue;
if (!entry.widget->is_visible())

View file

@ -13,7 +13,7 @@ GFilePicker::GFilePicker(const String& path, CObject* parent)
set_rect(200, 200, 400, 300);
set_main_widget(new GWidget);
main_widget()->set_layout(make<GBoxLayout>(Orientation::Vertical));
main_widget()->layout()->set_margins({ 4, 0, 4, 0 });
main_widget()->layout()->set_margins({ 4, 4, 4, 4 });
main_widget()->layout()->set_spacing(4);
main_widget()->set_fill_with_background_color(true);
main_widget()->set_background_color(Color::LightGray);
@ -42,6 +42,7 @@ GFilePicker::GFilePicker(const String& path, CObject* parent)
button_container->set_preferred_size({ 0, 20 });
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
button_container->layout()->set_spacing(4);
button_container->layout()->add_spacer();
auto* cancel_button = new GButton(button_container);
cancel_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);

View file

@ -22,22 +22,34 @@ void GLayout::notify_disowned(Badge<GWidget>, GWidget& widget)
m_owner.clear();
}
void GLayout::add_layout(OwnPtr<GLayout>&& layout)
void GLayout::add_entry(Entry&& entry)
{
Entry entry;
entry.layout = move(layout);
m_entries.append(move(entry));
if (m_owner)
m_owner->notify_layout_changed(Badge<GLayout>());
}
void GLayout::add_spacer()
{
Entry entry;
entry.type = Entry::Type::Spacer;
add_entry(move(entry));
}
void GLayout::add_layout(OwnPtr<GLayout>&& layout)
{
Entry entry;
entry.type = Entry::Type::Layout;
entry.layout = move(layout);
add_entry(move(entry));
}
void GLayout::add_widget(GWidget& widget)
{
Entry entry;
entry.type = Entry::Type::Widget;
entry.widget = widget.make_weak_ptr();
m_entries.append(move(entry));
if (m_owner)
m_owner->notify_layout_changed(Badge<GLayout>());
add_entry(move(entry));
}
void GLayout::remove_widget(GWidget& widget)

View file

@ -15,6 +15,7 @@ public:
void add_widget(GWidget&);
void add_layout(OwnPtr<GLayout>&&);
void add_spacer();
void remove_widget(GWidget&);
@ -31,9 +32,19 @@ public:
protected:
struct Entry {
enum class Type {
Invalid = 0,
Widget,
Layout,
Spacer,
};
Type type { Type::Invalid };
WeakPtr<GWidget> widget;
OwnPtr<GLayout> layout;
};
void add_entry(Entry&&);
WeakPtr<GWidget> m_owner;
Vector<Entry> m_entries;