aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/controls/Control.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/Control.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/Control.h')
-rw-r--r--include/cru/ui/controls/Control.h45
1 files changed, 28 insertions, 17 deletions
diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h
index 94de4cdc..9e5e86b8 100644
--- a/include/cru/ui/controls/Control.h
+++ b/include/cru/ui/controls/Control.h
@@ -24,9 +24,9 @@ namespace cru::ui::controls {
class CRU_UI_API Control : public Object,
public DeleteLaterImpl,
public SelfResolvable<Control> {
- friend class RootControl;
+ friend class ControlHost;
- CRU_DEFINE_CLASS_LOG_TAG("Control")
+ CRU_DEFINE_CLASS_LOG_TAG("cru::ui::controls::Control")
protected:
Control();
@@ -41,24 +41,33 @@ class CRU_UI_API Control : public Object,
//*************** region: tree ***************
public:
- Window* GetWindow();
+ ControlHost* GetControlHost();
- Control* GetParent() const { return parent_; }
- void SetParent(Control* parent);
+ Control* GetParent();
bool HasAncestor(Control* control);
+ const std::vector<Control*>& GetChildren();
+ void RemoveChild(Control* child);
+ void RemoveAllChild();
+ void RemoveFromParent();
- virtual void ForEachChild(const std::function<void(Control*)>& predicate) = 0;
-
- /**
- * \remarks This method should be permissive, which means if the specified
- * child control is not a real child of this then nothing will be done.
- */
- virtual void RemoveChild(Control* child) = 0;
+ template <typename F>
+ void TraverseDescendents(F&& f, bool include_this) {
+ if (include_this) {
+ f(this);
+ }
- void RemoveFromParent();
+ for (auto child : GetChildren()) {
+ child->TraverseDescendents(std::forward<F>(f), true);
+ }
+ }
controls::Control* HitTest(const Point& point);
+ protected:
+ void InsertChildAt(Control* control, Index index);
+ void RemoveChildAt(Index index);
+ void AddChild(Control* control);
+
public:
virtual render::RenderObject* GetRenderObject() const = 0;
@@ -89,7 +98,7 @@ class CRU_UI_API Control : public Object,
//*************** region: mouse ***************
public:
- bool IsMouseOver() const { return is_mouse_over_; }
+ bool IsMouseOver();
bool CaptureMouse();
@@ -137,12 +146,14 @@ class CRU_UI_API Control : public Object,
//*************** region: tree ***************
protected:
- virtual void OnParentChanged(Control* old_parent, Control* new_parent) {}
+ virtual void OnParentChanged(Control* old_parent, Control* new_parent);
+ virtual void OnChildInserted(Control* control, Index index);
+ virtual void OnChildRemoved(Control* control, Index index);
private:
+ ControlHost* host_ = nullptr;
Control* parent_ = nullptr;
-
- bool is_mouse_over_ = false;
+ std::vector<Control*> children_;
std::shared_ptr<platform::gui::ICursor> cursor_ = nullptr;