From 183f7c9830c8d2d6e0f1654e4394a9d613ecb4ad Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 2 Oct 2019 20:24:29 +0200 Subject: [PATCH] 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. --- Libraries/LibGUI/GLazyWidget.cpp | 20 ++++++++++++++++++++ Libraries/LibGUI/GLazyWidget.h | 19 +++++++++++++++++++ Libraries/LibGUI/Makefile | 1 + 3 files changed, 40 insertions(+) create mode 100644 Libraries/LibGUI/GLazyWidget.cpp create mode 100644 Libraries/LibGUI/GLazyWidget.h 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 + +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 + +class GLazyWidget : public GWidget { + C_OBJECT(GLazyWidget) +public: + virtual ~GLazyWidget() override; + + Function 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