aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/RenderObject.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/ui/render/RenderObject.hpp')
-rw-r--r--include/cru/ui/render/RenderObject.hpp48
1 files changed, 36 insertions, 12 deletions
diff --git a/include/cru/ui/render/RenderObject.hpp b/include/cru/ui/render/RenderObject.hpp
index 7cfa3883..9de2cc27 100644
--- a/include/cru/ui/render/RenderObject.hpp
+++ b/include/cru/ui/render/RenderObject.hpp
@@ -1,12 +1,22 @@
#pragma once
#include "Base.hpp"
+#include "MeasureRequirement.hpp"
#include "cru/common/Event.hpp"
namespace cru::ui::render {
+
// Render object will not destroy its children when destroyed. Control must
// manage lifecycle of its render objects. Since control will destroy its
// children when destroyed, render objects will be destroyed along with it.
+//
+// To write a custom RenderObject, override following methods:
+// public:
+// void Draw(platform::graph::IPainter* painter) override;
+// RenderObject* HitTest(const Point& point) override;
+// protected:
+// Size OnMeasureContent(const MeasureRequirement& requirement) override;
+// void OnLayoutContent(const Rect& content_rect) override;
class RenderObject : public Object {
friend WindowRenderObject;
@@ -41,12 +51,12 @@ class RenderObject : public Object {
void AddChild(RenderObject* render_object, Index position);
void RemoveChild(Index position);
+ // Offset from parent's lefttop to lefttop of this render object. Margin is
+ // accounted for.
Point GetOffset() const { return offset_; }
- void SetOffset(const Point& offset) { offset_ = offset; }
+ Size GetSize() const { return size_; }
Point GetTotalOffset() const;
Point FromRootToContent(const Point& point) const;
- Size GetSize() const { return size_; }
- void SetSize(const Size& size) { size_ = size; }
Thickness GetMargin() const { return margin_; }
void SetMargin(const Thickness& margin) { margin_ = margin; }
@@ -54,16 +64,16 @@ class RenderObject : public Object {
Thickness GetPadding() const { return padding_; }
void SetPadding(const Thickness& padding) { padding_ = padding; }
- Size GetPreferredSize() const { return preferred_size_; }
- void SetPreferredSize(const Size& preferred_size) {
- preferred_size_ = preferred_size;
- }
+ Size GetMeasuredSize() const { return measured_size_; }
- void Measure(const Size& available_size);
+ void Measure(const MeasureRequirement& requirement);
+ // Size of rect must not be negative.
void Layout(const Rect& rect);
virtual void Draw(platform::graph::IPainter* painter) = 0;
+ // Param point must be relative the lefttop of render object including margin.
+ // Add offset before pass point to children.
virtual RenderObject* HitTest(const Point& point) = 0;
public:
@@ -82,9 +92,23 @@ class RenderObject : public Object {
// default is to invalidate both layout and paint
virtual void OnRemoveChild(RenderObject* removed_child, Index position);
- virtual void OnMeasureCore(const Size& available_size);
- virtual void OnLayoutCore(const Rect& rect);
- virtual Size OnMeasureContent(const Size& available_size) = 0;
+ // Size measure including margin and padding. Please reduce margin and padding
+ // or other custom things and pass the result content measure requirement to
+ // OnMeasureContent.
+ // Return value must not be negative and not bigger than requirement.
+ virtual Size OnMeasureCore(const MeasureRequirement& requirement);
+
+ // Size including margin and padding. Please reduce margin and padding or
+ // other custom things and pass the result content rect to OnLayoutContent.
+ // Parameter size are never negative.
+ virtual void OnLayoutCore(const Size& size);
+
+ // Do not consider margin or padding in this method because they are already
+ // considered in OnMeasureCore. Returned size should never be bigger than
+ // requirement.
+ virtual Size OnMeasureContent(const MeasureRequirement& requirement) = 0;
+
+ // Lefttop of content_rect should be added when calculated children's offset.
virtual void OnLayoutContent(const Rect& content_rect) = 0;
virtual void OnAfterLayout();
@@ -113,6 +137,6 @@ class RenderObject : public Object {
Thickness margin_{};
Thickness padding_{};
- Size preferred_size_{};
+ Size measured_size_{};
};
} // namespace cru::ui::render