From 6b4edc9be8ec556147c195cf2047d92b9439efd7 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 18 Nov 2025 00:46:27 +0800 Subject: Bring back ControlHost and refactor tree management of control. --- include/cru/ui/controls/SingleChildControl.h | 35 ++++++---------------------- 1 file changed, 7 insertions(+), 28 deletions(-) (limited to 'include/cru/ui/controls/SingleChildControl.h') diff --git a/include/cru/ui/controls/SingleChildControl.h b/include/cru/ui/controls/SingleChildControl.h index b2e58c5f..f748a38d 100644 --- a/include/cru/ui/controls/SingleChildControl.h +++ b/include/cru/ui/controls/SingleChildControl.h @@ -10,25 +10,17 @@ class SingleChildControl : public Control { } public: - CRU_DELETE_COPY(SingleChildControl) - CRU_DELETE_MOVE(SingleChildControl) - - ~SingleChildControl() override { SetChild(nullptr); } + Control* GetChild() { + return GetChildren().empty() ? nullptr : GetChildren().front(); + } - Control* GetChild() const { return child_; } void SetChild(Control* child) { - if (child == child_) return; - - assert(child == nullptr || child->GetParent() == nullptr); - - if (child_) { - child_->SetParent(nullptr); + if (GetChild() == child) return; + if (!GetChildren().empty()) { + RemoveChildAt(0); } - - child_ = child; - if (child) { - child->SetParent(this); + InsertChildAt(child, 0); } container_render_object_->SetChild( @@ -43,20 +35,7 @@ class SingleChildControl : public Control { return container_render_object_.get(); } - void ForEachChild(const std::function& predicate) override { - if (child_) { - predicate(child_); - } - } - - void RemoveChild(Control* child) override { - if (child_ == child) { - SetChild(nullptr); - } - } - private: - Control* child_ = nullptr; std::unique_ptr container_render_object_; }; } // namespace cru::ui::controls -- cgit v1.2.3