From fe6e1686ce484cb0dd9a69f130e82f647c30016f Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 10 Sep 2025 01:30:59 +0800 Subject: xcb window reparent. --- include/cru/platform/Check.h | 28 ++++++++++++++++++++++++++++ include/cru/platform/Exception.h | 2 +- include/cru/platform/Resource.h | 2 ++ include/cru/platform/gui/xcb/Window.h | 10 ++++++---- 4 files changed, 37 insertions(+), 5 deletions(-) (limited to 'include/cru') diff --git a/include/cru/platform/Check.h b/include/cru/platform/Check.h index 270150e8..202ee86e 100644 --- a/include/cru/platform/Check.h +++ b/include/cru/platform/Check.h @@ -35,4 +35,32 @@ std::shared_ptr CheckPlatform(const std::shared_ptr& resource, } return result; } + +template +TTarget* CheckPlatform(IPlatformResource* resource, + std::string target_platform) { + if (resource == nullptr) return nullptr; + const auto result = dynamic_cast(resource); + if (result == nullptr) { + throw PlatformNotMatchException( + resource->GetPlatformIdUtf8(), target_platform, + "Try to convert resource to target platform failed."); + } + return result; +} + +template +std::shared_ptr CheckPlatform(const std::shared_ptr& resource, + std::string target_platform) { + if (resource == nullptr) return nullptr; + static_assert(std::is_base_of_v, + "TSource must be a subclass of IPlatformResource."); + const auto result = std::dynamic_pointer_cast(resource); + if (result == nullptr) { + throw PlatformNotMatchException( + resource->GetPlatformIdUtf8(), target_platform, + "Try to convert resource to target platform failed."); + } + return result; +} } // namespace cru::platform diff --git a/include/cru/platform/Exception.h b/include/cru/platform/Exception.h index 25017869..f8ed5b0c 100644 --- a/include/cru/platform/Exception.h +++ b/include/cru/platform/Exception.h @@ -14,7 +14,7 @@ class CRU_PLATFORM_API PlatformNotMatchException : public PlatformException { public: PlatformNotMatchException( std::string resource_platform, std::string target_platform, - std::optional additional_message = std::nullopt); + std::optional additional_message = std::nullopt); PlatformNotMatchException( StringView resource_platform, StringView target_platform, diff --git a/include/cru/platform/Resource.h b/include/cru/platform/Resource.h index 69210348..a16a9dbd 100644 --- a/include/cru/platform/Resource.h +++ b/include/cru/platform/Resource.h @@ -11,5 +11,7 @@ struct CRU_PLATFORM_API IPlatformResource : virtual Interface { virtual String GetPlatformId() const = 0; virtual String GetDebugString() { return String(); } + + virtual std::string GetPlatformIdUtf8() const; }; } // namespace cru::platform diff --git a/include/cru/platform/gui/xcb/Window.h b/include/cru/platform/gui/xcb/Window.h index e09fdbfb..f9fbf735 100644 --- a/include/cru/platform/gui/xcb/Window.h +++ b/include/cru/platform/gui/xcb/Window.h @@ -18,10 +18,10 @@ class XcbWindow : public XcbResource, public virtual INativeWindow { explicit XcbWindow(XcbUiApplication* application); ~XcbWindow() override; - virtual void Close() = 0; + void Close() override; - virtual INativeWindow* GetParent() = 0; - virtual void SetParent(INativeWindow* parent) = 0; + INativeWindow* GetParent() override; + void SetParent(INativeWindow* parent) override; virtual WindowStyleFlag GetStyleFlag() = 0; virtual void SetStyleFlag(WindowStyleFlag flag) = 0; @@ -91,9 +91,11 @@ class XcbWindow : public XcbResource, public virtual INativeWindow { private: XcbUiApplication* application_; std::optional xcb_window_; - cairo_surface_t *cairo_surface_; + cairo_surface_t* cairo_surface_; Size current_size_; + XcbWindow* parent_; + Event create_event_; Event destroy_event_; Event paint_event_; -- cgit v1.2.3