From 91695b78f565239223fc6f3a10b0219b8dc1b6f8 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 28 Mar 2022 21:46:40 +0800 Subject: ... --- include/cru/ui/DeleteLater.h | 25 +++++++++++++++++++++++++ include/cru/ui/components/Component.h | 5 ++++- include/cru/ui/controls/Control.h | 5 ++++- src/ui/CMakeLists.txt | 1 + src/ui/DeleteLater.cpp | 12 ++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 include/cru/ui/DeleteLater.h create mode 100644 src/ui/DeleteLater.cpp diff --git a/include/cru/ui/DeleteLater.h b/include/cru/ui/DeleteLater.h new file mode 100644 index 00000000..84c0e1c2 --- /dev/null +++ b/include/cru/ui/DeleteLater.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +namespace cru::ui { +class DeleteLaterImpl { + public: + virtual ~DeleteLaterImpl(); + void DeleteLater(); +}; + +template +struct DeleteLaterDeleter { + void operator()(T* p) const { p->DeleteLater(); } +}; + +template +using DeleteLaterPtr = std::unique_ptr>; + +template +DeleteLaterPtr CreateDeleteLaterPtr(Args&&... args) { + return DeleteLaterPtr(new T(std::forward(args)...)); +} +} // namespace cru::ui diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index 34add594..2625f445 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -1,5 +1,6 @@ #pragma once #include "../Base.h" +#include "../DeleteLater.h" #include "cru/common/SelfResolvable.h" namespace cru::ui::components { @@ -8,7 +9,9 @@ namespace cru::ui::components { * \remarks Component should respect children's Component::IsDeleteByParent * value and decide whether to delete it. */ -class CRU_UI_API Component : public Object, public SelfResolvable { +class CRU_UI_API Component : public Object, + public SelfResolvable, + public DeleteLaterImpl { public: Component() = default; diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index 158a1578..8c9f11d3 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -1,5 +1,6 @@ #pragma once #include "../Base.h" +#include "../DeleteLater.h" #include "../events/UiEvents.h" #include "../render/RenderObject.h" #include "../style/StyleRuleSet.h" @@ -17,7 +18,9 @@ namespace cru::ui::controls { * - RemoveChild(Control* child) * The last two methods are totally for convenient control tree management. */ -class CRU_UI_API Control : public Object, public SelfResolvable { +class CRU_UI_API Control : public Object, + public SelfResolvable, + public DeleteLaterImpl { friend class RootControl; protected: diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 408c93d8..66b473d2 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(cru_ui SHARED + DeleteLater.cpp Helper.cpp ThemeManager.cpp ThemeResourceDictionary.cpp diff --git a/src/ui/DeleteLater.cpp b/src/ui/DeleteLater.cpp new file mode 100644 index 00000000..8a40642f --- /dev/null +++ b/src/ui/DeleteLater.cpp @@ -0,0 +1,12 @@ +#include "cru/ui/DeleteLater.h" + +#include "Helper.h" +#include "cru/platform/gui/UiApplication.h" + +namespace cru::ui { +DeleteLaterImpl::~DeleteLaterImpl() {} + +void DeleteLaterImpl::DeleteLater() { + GetUiApplication()->SetImmediate([this] { delete this; }); +} +} // namespace cru::ui -- cgit v1.2.3