aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/SingleChildControl.h
blob: f748a38df54a2f5390040d2b5c2c670d8905c49c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include "Control.h"

namespace cru::ui::controls {
template <typename TRenderObject>
class SingleChildControl : public Control {
 protected:
  SingleChildControl() : container_render_object_(new TRenderObject()) {
    container_render_object_->SetAttachedControl(this);
  }

 public:
  Control* GetChild() {
    return GetChildren().empty() ? nullptr : GetChildren().front();
  }

  void SetChild(Control* child) {
    if (GetChild() == child) return;
    if (!GetChildren().empty()) {
      RemoveChildAt(0);
    }
    if (child) {
      InsertChildAt(child, 0);
    }

    container_render_object_->SetChild(
        child == nullptr ? nullptr : child->GetRenderObject());
  }

  render::RenderObject* GetRenderObject() const override {
    return container_render_object_.get();
  }

  TRenderObject* GetContainerRenderObject() const {
    return container_render_object_.get();
  }

 private:
  std::unique_ptr<TRenderObject> container_render_object_;
};
}  // namespace cru::ui::controls