diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-18 17:31:23 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-18 17:31:23 +0800 |
| commit | 04d243699cbde40fe69472f4c4df38c36f7942ef (patch) | |
| tree | af2a2031ab7f2d9ce03cf677eba066e11f32a0c9 /include | |
| parent | 358dc1498623d53f1d8bedf5172743deb6c03811 (diff) | |
| download | cru-04d243699cbde40fe69472f4c4df38c36f7942ef.tar.gz cru-04d243699cbde40fe69472f4c4df38c36f7942ef.tar.bz2 cru-04d243699cbde40fe69472f4c4df38c36f7942ef.zip | |
Move delete later to platform::gui.
Diffstat (limited to 'include')
| -rw-r--r-- | include/cru/platform/gui/DeleteLater.h (renamed from include/cru/ui/DeleteLater.h) | 21 | ||||
| -rw-r--r-- | include/cru/platform/gui/UiApplication.h | 14 | ||||
| -rw-r--r-- | include/cru/platform/gui/win/UiApplication.h | 3 | ||||
| -rw-r--r-- | include/cru/ui/components/Component.h | 7 | ||||
| -rw-r--r-- | include/cru/ui/controls/Control.h | 4 |
5 files changed, 35 insertions, 14 deletions
diff --git a/include/cru/ui/DeleteLater.h b/include/cru/platform/gui/DeleteLater.h index 95301bc0..c0578974 100644 --- a/include/cru/ui/DeleteLater.h +++ b/include/cru/platform/gui/DeleteLater.h @@ -1,20 +1,21 @@ #pragma once -#include "Base.h" +#include "UiApplication.h" #include <cru/base/Guard.h> #include <memory> #include <utility> -namespace cru::ui { -class CRU_UI_API DeleteLaterImpl { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::DeleteLaterImpl") +namespace cru::platform::gui { +template <typename TSelf> +class DeleteLaterImpl { + CRU_DEFINE_CLASS_LOG_TAG("cru::platform::gui::DeleteLaterImpl") + public: - DeleteLaterImpl(); - virtual ~DeleteLaterImpl(); - void DeleteLater(); + virtual ~DeleteLaterImpl() {} - private: - bool delete_later_scheduled_; + void DeleteLater() { + IUiApplication::GetInstance()->DeleteLater(static_cast<TSelf*>(this)); + } }; namespace details { @@ -37,4 +38,4 @@ DeleteLaterPtr<T> MakeDeleteLater(Args&&... args) { return DeleteLaterPtr<T>(new T(std::forward<Args>(args)...)); } -} // namespace cru::ui +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/UiApplication.h b/include/cru/platform/gui/UiApplication.h index 84011275..c4454b2e 100644 --- a/include/cru/platform/gui/UiApplication.h +++ b/include/cru/platform/gui/UiApplication.h @@ -16,6 +16,18 @@ struct INativeWindow; struct IInputMethodContext; struct IClipboard; +class CRU_PLATFORM_GUI_API DeleteLaterPool : public Object { + public: + void Add(Object* object); + + void Clean(); + + private: + // May contain duplicate object pointer. When performing deleting, use a set + // to record deleted objects to avoid double delete. + std::vector<Object*> pool_; +}; + // The entry point of a ui application. struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { public: @@ -51,6 +63,8 @@ struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { // result in no-op. virtual void CancelTimer(long long id) = 0; + virtual void DeleteLater(Object* object) = 0; + virtual std::vector<INativeWindow*> GetAllWindow() = 0; virtual INativeWindow* CreateWindow() = 0; diff --git a/include/cru/platform/gui/win/UiApplication.h b/include/cru/platform/gui/win/UiApplication.h index b2d1f39b..c5057c4b 100644 --- a/include/cru/platform/gui/win/UiApplication.h +++ b/include/cru/platform/gui/win/UiApplication.h @@ -48,6 +48,8 @@ class CRU_WIN_GUI_API WinUiApplication : public WinNativeResource, std::function<void()> action) override; void CancelTimer(long long id) override; + void DeleteLater(Object* object) override; + std::vector<INativeWindow*> GetAllWindow() override; INativeWindow* CreateWindow() override; @@ -79,6 +81,7 @@ class CRU_WIN_GUI_API WinUiApplication : public WinNativeResource, graph_factory_; TimerRegistry<std::function<void()>> timers_; + DeleteLaterPool delete_later_pool_; std::unique_ptr<WindowClass> general_window_class_; std::vector<WinNativeWindow*> windows_; diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index d8966a89..1e002e5f 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -1,6 +1,7 @@ #pragma once #include "../Base.h" -#include "../DeleteLater.h" + +#include <cru/platform/gui/DeleteLater.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 DeleteLaterImpl { +class CRU_UI_API Component + : public Object, + public cru::platform::gui::DeleteLaterImpl<Component> { public: virtual controls::Control* GetRootControl() = 0; diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index 9e5e86b8..6a85d25a 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -1,6 +1,5 @@ #pragma once #include "../Base.h" -#include "../DeleteLater.h" #include "../events/KeyEventArgs.h" #include "../events/MouseWheelEventArgs.h" #include "../events/UiEvents.h" @@ -9,6 +8,7 @@ #include "../style/StyleRuleSet.h" #include <cru/base/SelfResolvable.h> +#include <cru/platform/gui/DeleteLater.h> namespace cru::ui::controls { @@ -22,7 +22,7 @@ namespace cru::ui::controls { * The last two methods are totally for convenient control tree management. */ class CRU_UI_API Control : public Object, - public DeleteLaterImpl, + public cru::platform::gui::DeleteLaterImpl<Control>, public SelfResolvable<Control> { friend class ControlHost; |
