aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/LayoutControl.h
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-11-18 00:46:27 +0800
committerYuqian Yang <crupest@crupest.life>2025-11-18 00:46:27 +0800
commit6b4edc9be8ec556147c195cf2047d92b9439efd7 (patch)
treea1d7b7d1e821b4e1911fd00761f77a24ee483f4a /include/cru/ui/controls/LayoutControl.h
parentf7c4d19df66c602d74795e98ce2ee4390d06fbb4 (diff)
downloadcru-6b4edc9be8ec556147c195cf2047d92b9439efd7.tar.gz
cru-6b4edc9be8ec556147c195cf2047d92b9439efd7.tar.bz2
cru-6b4edc9be8ec556147c195cf2047d92b9439efd7.zip
Bring back ControlHost and refactor tree management of control.
Diffstat (limited to 'include/cru/ui/controls/LayoutControl.h')
-rw-r--r--include/cru/ui/controls/LayoutControl.h67
1 files changed, 15 insertions, 52 deletions
diff --git a/include/cru/ui/controls/LayoutControl.h b/include/cru/ui/controls/LayoutControl.h
index 54407a3f..fad86530 100644
--- a/include/cru/ui/controls/LayoutControl.h
+++ b/include/cru/ui/controls/LayoutControl.h
@@ -10,17 +10,14 @@ class LayoutControl : public Control {
}
public:
- LayoutControl(const LayoutControl& other) = delete;
- LayoutControl(LayoutControl&& other) = delete;
- LayoutControl& operator=(const LayoutControl& other) = delete;
- LayoutControl& operator=(LayoutControl&& other) = delete;
- ~LayoutControl() override { ClearChildren(); }
+ using Control::AddChild;
+ using Control::InsertChildAt;
+ using Control::RemoveChildAt;
- public:
- const std::vector<Control*>& GetChildren() const { return children_; }
Index GetChildCount() const { return children_.size(); }
- Control* GetChild(Index index) const { return children_[index]; }
- Index IndexOf(Control* control) const {
+ Control* GetChildAt(Index index) const { return children_[index]; }
+
+ Index IndexOfChild(Control* control) const {
auto it = std::find(children_.begin(), children_.end(), control);
if (it == children_.end()) {
return -1;
@@ -28,19 +25,6 @@ class LayoutControl : public Control {
return it - children_.begin();
}
- void ForEachChild(const std::function<void(Control*)>& callback) override {
- for (auto child : children_) {
- callback(child);
- }
- }
-
- void RemoveChild(Control* child) override {
- auto index = IndexOf(child);
- if (index != -1) {
- RemoveChildAt(index);
- }
- }
-
render::RenderObject* GetRenderObject() const override {
return container_render_object_.get();
}
@@ -49,36 +33,6 @@ class LayoutControl : public Control {
return container_render_object_.get();
}
- void AddChildAt(Control* child, Index position) {
- Expects(child);
- Expects(child->GetParent() == nullptr);
- if (position < 0) position = 0;
- if (position > children_.size()) position = children_.size();
- children_.insert(children_.begin() + position, child);
- child->SetParent(this);
-
- assert(child->GetRenderObject());
- container_render_object_->AddChild(child->GetRenderObject(), position);
- }
-
- void AddChild(Control* child) { AddChildAt(child, GetChildCount()); }
-
- void RemoveChildAt(Index position) {
- if (position < 0 || position >= children_.size()) return;
- auto child = children_[position];
- children_.erase(children_.begin() + position);
- container_render_object_->RemoveChild(position);
- child->SetParent(nullptr);
- }
-
- void ClearChildren() {
- container_render_object_->ClearChildren();
- for (auto child : children_) {
- child->SetParent(nullptr);
- }
- children_.clear();
- }
-
const typename TRenderObject::ChildLayoutData& GetChildLayoutData(
Index position) {
return container_render_object_->GetChildLayoutDataAt(position);
@@ -89,6 +43,15 @@ class LayoutControl : public Control {
container_render_object_->SetChildLayoutDataAt(position, data);
}
+ protected:
+ void OnChildInserted(Control* control, Index index) override {
+ container_render_object_->AddChild(control->GetRenderObject(), index);
+ }
+
+ void OnChildRemoved([[maybe_unused]] Control* control, Index index) override {
+ container_render_object_->RemoveChild(index);
+ }
+
private:
std::unique_ptr<TRenderObject> container_render_object_;