aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/SingleChildControl.h
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-02-09 23:53:18 +0800
committercrupest <crupest@outlook.com>2022-02-09 23:53:18 +0800
commitb8863c403a44c1c7ac35f1a1da92bbf3c8858552 (patch)
tree7e38f029f0657e6c1210a53f1cba331cdb8feab6 /include/cru/ui/controls/SingleChildControl.h
parentd18b5453d7ffd19667ee8ac125b34ab5328f0dc3 (diff)
downloadcru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.gz
cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.tar.bz2
cru-b8863c403a44c1c7ac35f1a1da92bbf3c8858552.zip
...
Diffstat (limited to 'include/cru/ui/controls/SingleChildControl.h')
-rw-r--r--include/cru/ui/controls/SingleChildControl.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/include/cru/ui/controls/SingleChildControl.h b/include/cru/ui/controls/SingleChildControl.h
new file mode 100644
index 00000000..037d6dd3
--- /dev/null
+++ b/include/cru/ui/controls/SingleChildControl.h
@@ -0,0 +1,55 @@
+#pragma once
+#include "Control.h"
+
+namespace cru::ui::controls {
+template <typename TRenderObject>
+class CRU_UI_API SingleChildControl : public Control {
+ protected:
+ SingleChildControl() : container_render_object_(new TRenderObject()) {
+ container_render_object_->SetAttachedControl(this);
+ }
+
+ public:
+ CRU_DELETE_COPY(SingleChildControl)
+ CRU_DELETE_MOVE(SingleChildControl)
+
+ ~SingleChildControl() override {}
+
+ Control* GetChild() const { return child_; }
+ void SetChild(Control* child) {
+ if (child == child_) return;
+
+ assert(child == nullptr || child->GetParent() == nullptr);
+
+ if (child_) {
+ child_->SetParent(nullptr);
+ }
+
+ child_ = child;
+
+ if (child) {
+ child->SetParent(this);
+ }
+
+ container_render_object_->SetChild(child->GetRenderObject());
+ }
+
+ render::RenderObject* GetRenderObject() const override {
+ return container_render_object_.get();
+ }
+
+ TRenderObject* GetContainerRenderObject() const {
+ return container_render_object_.get();
+ }
+
+ void ForEachChild(const std::function<void(Control*)>& predicate) override {
+ if (child_) {
+ predicate(child_);
+ }
+ }
+
+ private:
+ Control* child_;
+ std::unique_ptr<TRenderObject> container_render_object_;
+};
+} // namespace cru::ui::controls