Przeglądaj źródła

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. :^)
Andreas Kling 6 lat temu
rodzic
commit
bffaa5ece6
4 zmienionych plików z 39 dodań i 7 usunięć
  1. 8 0
      LibGUI/GBoxLayout.cpp
  2. 2 1
      LibGUI/GFilePicker.cpp
  3. 18 6
      LibGUI/GLayout.cpp
  4. 11 0
      LibGUI/GLayout.h

+ 8 - 0
LibGUI/GBoxLayout.cpp

@@ -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())

+ 2 - 1
LibGUI/GFilePicker.cpp

@@ -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);

+ 18 - 6
LibGUI/GLayout.cpp

@@ -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)

+ 11 - 0
LibGUI/GLayout.h

@@ -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;