diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-10-02 20:24:29 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-10-02 20:24:29 +0200 |
commit | 183f7c9830c8d2d6e0f1654e4394a9d613ecb4ad (patch) | |
tree | c4ad040f84e984bd8ec3cc72550c4cf3e4b5919b /Libraries | |
parent | 7e2b9c3c40fe20e9044eefe6ff3e380152674eac (diff) | |
download | serenity-183f7c9830c8d2d6e0f1654e4394a9d613ecb4ad.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/GLazyWidget.cpp | 20 | ||||
-rw-r--r-- | Libraries/LibGUI/GLazyWidget.h | 19 | ||||
-rw-r--r-- | Libraries/LibGUI/Makefile | 1 |
3 files changed, 40 insertions, 0 deletions
diff --git a/Libraries/LibGUI/GLazyWidget.cpp b/Libraries/LibGUI/GLazyWidget.cpp new file mode 100644 index 0000000000..e8eed96d0e --- /dev/null +++ b/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); +} diff --git a/Libraries/LibGUI/GLazyWidget.h b/Libraries/LibGUI/GLazyWidget.h new file mode 100644 index 0000000000..69bc235508 --- /dev/null +++ b/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 }; +}; diff --git a/Libraries/LibGUI/Makefile b/Libraries/LibGUI/Makefile index ad806e4108..24d3d70ad7 100644 --- a/Libraries/LibGUI/Makefile +++ b/Libraries/LibGUI/Makefile @@ -55,6 +55,7 @@ OBJS = \ GJsonArrayModel.o \ GAboutDialog.o \ GModelSelection.o \ + GLazyWidget.o \ GWindow.o LIBRARY = libgui.a |