Kaynağa Gözat

LibGUI: Add GLazyWidget, a convenience widget for lazily-built UI's

Here's how you can use this to speed up startup time:

    auto widget = GLazyWidget::construct();
    widget->on_first_show = [](auto& self) {
        self.set_layout(...);
        ...
    };

Basically, it allows you to delay building the widget subtree until
it's shown for the first time.
Andreas Kling 5 yıl önce
ebeveyn
işleme
183f7c9830

+ 20 - 0
Libraries/LibGUI/GLazyWidget.cpp

@@ -0,0 +1,20 @@
+#include <LibGUI/GLazyWidget.h>
+
+GLazyWidget::GLazyWidget(GWidget* parent)
+    : GWidget(parent)
+{
+}
+
+GLazyWidget::~GLazyWidget()
+{
+}
+
+void GLazyWidget::show_event(GShowEvent&)
+{
+    if (m_has_been_shown)
+        return;
+    m_has_been_shown = true;
+
+    ASSERT(on_first_show);
+    on_first_show(*this);
+}

+ 19 - 0
Libraries/LibGUI/GLazyWidget.h

@@ -0,0 +1,19 @@
+#pragma once
+
+#include <LibGUI/GWidget.h>
+
+class GLazyWidget : public GWidget {
+    C_OBJECT(GLazyWidget)
+public:
+    virtual ~GLazyWidget() override;
+
+    Function<void(GLazyWidget&)> on_first_show;
+
+protected:
+    explicit GLazyWidget(GWidget* parent = nullptr);
+
+private:
+    virtual void show_event(GShowEvent&) override;
+
+    bool m_has_been_shown { false };
+};

+ 1 - 0
Libraries/LibGUI/Makefile

@@ -55,6 +55,7 @@ OBJS = \
     GJsonArrayModel.o \
     GAboutDialog.o \
     GModelSelection.o \
+    GLazyWidget.o \
     GWindow.o
 
 LIBRARY = libgui.a