diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-22 16:55:53 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-22 16:55:53 +0800 |
| commit | 7ce185d5a3fcfad8c8f746f95f3d50a8829faac4 (patch) | |
| tree | c1f00529a1286748836332b62ae79404b3a83f36 | |
| parent | d97c14a922b60e89a9892e519b95b7e793aff543 (diff) | |
| download | cru-7ce185d5a3fcfad8c8f746f95f3d50a8829faac4.tar.gz cru-7ce185d5a3fcfad8c8f746f95f3d50a8829faac4.tar.bz2 cru-7ce185d5a3fcfad8c8f746f95f3d50a8829faac4.zip | |
Clean codes. Remove Measure1.
| -rw-r--r-- | include/cru/base/Range.h | 2 | ||||
| -rw-r--r-- | include/cru/platform/GraphicsBase.h | 40 | ||||
| -rw-r--r-- | include/cru/ui/render/BorderRenderObject.h | 2 | ||||
| -rw-r--r-- | include/cru/ui/render/CanvasRenderObject.h | 2 | ||||
| -rw-r--r-- | include/cru/ui/render/GeometryRenderObject.h | 2 | ||||
| -rw-r--r-- | include/cru/ui/render/RenderObject.h | 34 | ||||
| -rw-r--r-- | include/cru/ui/render/ScrollRenderObject.h | 2 | ||||
| -rw-r--r-- | include/cru/ui/render/TextRenderObject.h | 2 | ||||
| -rw-r--r-- | src/ui/render/BorderRenderObject.cpp | 7 | ||||
| -rw-r--r-- | src/ui/render/CanvasRenderObject.cpp | 4 | ||||
| -rw-r--r-- | src/ui/render/GeometryRenderObject.cpp | 4 | ||||
| -rw-r--r-- | src/ui/render/RenderObject.cpp | 77 | ||||
| -rw-r--r-- | src/ui/render/ScrollRenderObject.cpp | 7 | ||||
| -rw-r--r-- | src/ui/render/TextRenderObject.cpp | 11 |
14 files changed, 25 insertions, 171 deletions
diff --git a/include/cru/base/Range.h b/include/cru/base/Range.h index 44c5aca0..a2e15cd3 100644 --- a/include/cru/base/Range.h +++ b/include/cru/base/Range.h @@ -32,6 +32,8 @@ struct Range final { return Range::FromTwoSides(coerce(GetStart()), coerce(GetEnd())); } + constexpr bool operator==(const Range& other) const = default; + Index position = 0; Index count = 0; }; diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h index 23a95865..43c36494 100644 --- a/include/cru/platform/GraphicsBase.h +++ b/include/cru/platform/GraphicsBase.h @@ -27,6 +27,8 @@ struct Point final { return std::format("Point(x: {}, y: {})", x, y); } + constexpr bool operator==(const Point& other) const = default; + float x = 0; float y = 0; }; @@ -39,10 +41,6 @@ constexpr Point operator-(const Point& left, const Point& right) { return Point(left.x - right.x, left.y - right.y); } -constexpr bool operator==(const Point& left, const Point& right) { - return left.x == right.x && left.y == right.y; -} - struct Size final { static CRU_PLATFORM_API const Size kMax; static CRU_PLATFORM_API const Size kZero; @@ -62,6 +60,8 @@ struct Size final { return std::format("Size(width: {}, height: {})", width, height); } + constexpr bool operator==(const Size& other) const = default; + float width = 0; float height = 0; }; @@ -76,10 +76,6 @@ constexpr Size operator-(const Size& left, const Size& right) { return Size(left.width - right.width, left.height - right.height); } -constexpr bool operator==(const Size& left, const Size& right) { - return left.width == right.width && left.height == right.height; -} - struct Thickness final { constexpr Thickness() : Thickness(0) {} @@ -107,6 +103,8 @@ struct Thickness final { return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; } + constexpr bool operator==(const Thickness& other) const = default; + float left; float top; float right; @@ -127,11 +125,6 @@ constexpr Thickness operator+(const Thickness& left, const Thickness& right) { left.right + right.right, left.bottom + right.bottom}; } -constexpr bool operator==(const Thickness& left, const Thickness& right) { - return left.left == right.left && left.top == right.top && - left.right == right.right && left.bottom == right.bottom; -} - struct Rect final { constexpr Rect() = default; constexpr Rect(const float left, const float top, const float width, @@ -226,33 +219,27 @@ struct Rect final { top, width, height); } + constexpr bool operator==(const Rect& other) const = default; + float left = 0.0f; float top = 0.0f; float width = 0.0f; float height = 0.0f; }; -constexpr bool operator==(const Rect& left, const Rect& right) { - return left.left == right.left && left.top == right.top && - left.width == right.width && left.height == right.height; -} - struct RoundedRect final { constexpr RoundedRect() = default; constexpr RoundedRect(const Rect& rect, const float radius_x, const float radius_y) : rect(rect), radius_x(radius_x), radius_y(radius_y) {} + constexpr bool operator==(const RoundedRect& other) const = default; + Rect rect{}; float radius_x = 0.0f; float radius_y = 0.0f; }; -constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) { - return left.rect == right.rect && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - struct Ellipse final { constexpr Ellipse() = default; constexpr Ellipse(const Point& center, const float radius_x, @@ -267,16 +254,13 @@ struct Ellipse final { return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); } + constexpr bool operator==(const Ellipse& other) const = default; + Point center{}; float radius_x = 0.0f; float radius_y = 0.0f; }; -constexpr bool operator==(const Ellipse& left, const Ellipse& right) { - return left.center == right.center && left.radius_x == right.radius_x && - left.radius_y == right.radius_y; -} - using TextRange = Range; } // namespace cru::platform diff --git a/include/cru/ui/render/BorderRenderObject.h b/include/cru/ui/render/BorderRenderObject.h index 16f04962..6060dc4c 100644 --- a/include/cru/ui/render/BorderRenderObject.h +++ b/include/cru/ui/render/BorderRenderObject.h @@ -54,8 +54,6 @@ class CRU_UI_API BorderRenderObject : public SingleChildRenderObject { const MeasureSize& preferred_size) override; void OnLayoutContent(const Rect& content_rect) override; - Size OnMeasureContent1(const BoxConstraint& constraint) override; - void OnResize(const Size& new_size) override; private: diff --git a/include/cru/ui/render/CanvasRenderObject.h b/include/cru/ui/render/CanvasRenderObject.h index 3471d75f..63da9cd9 100644 --- a/include/cru/ui/render/CanvasRenderObject.h +++ b/include/cru/ui/render/CanvasRenderObject.h @@ -44,8 +44,6 @@ class CRU_UI_API CanvasRenderObject : public RenderObject { const MeasureSize& preferred_size) override; void OnLayoutContent(const Rect& content_rect) override; - Size OnMeasureContent1(const BoxConstraint& constraint) override; - private: Size desired_size_{}; diff --git a/include/cru/ui/render/GeometryRenderObject.h b/include/cru/ui/render/GeometryRenderObject.h index 1c998ef0..2320b71b 100644 --- a/include/cru/ui/render/GeometryRenderObject.h +++ b/include/cru/ui/render/GeometryRenderObject.h @@ -39,8 +39,6 @@ class GeometryRenderObject : public RenderObject { const MeasureSize& preferred_size) override; void OnLayoutContent(const Rect& content_rect) override; - Size OnMeasureContent1(const BoxConstraint& constraint) override; - private: std::shared_ptr<platform::graphics::IGeometry> geometry_ = nullptr; Rect view_port_{}; diff --git a/include/cru/ui/render/RenderObject.h b/include/cru/ui/render/RenderObject.h index 5569c788..ce788ca6 100644 --- a/include/cru/ui/render/RenderObject.h +++ b/include/cru/ui/render/RenderObject.h @@ -103,12 +103,6 @@ class CRU_UI_API RenderObject : public Object { return custom_measure_requirement_; } - Size GetMinSize1() { return min_size_; } - void SetMinSize1(const Size& min_size); - Size GetMaxSize1() { return max_size_; } - void SetMaxSize1(const Size& max_size); - BoxConstraint CalculateMergedConstraint(const BoxConstraint& constraint); - // This method will merge requirement passed by argument and requirement of // the render object using MeasureRequirement::Merge and then call // MeasureRequirement::Normalize on it. And it will use preferred size of the @@ -121,8 +115,6 @@ class CRU_UI_API RenderObject : public Object { // This will set offset of this render object and call OnLayoutCore. void Layout(const Point& offset); - Size Measure1(const BoxConstraint& constraint); - virtual Thickness GetTotalSpaceThickness(); virtual Thickness GetInnerSpaceThickness(); @@ -156,8 +148,6 @@ class CRU_UI_API RenderObject : public Object { virtual Size OnMeasureCore(const MeasureRequirement& requirement, const MeasureSize& preferred_size); - virtual Size OnMeasureCore1(const BoxConstraint& constraint); - // Please reduce margin and padding or other custom things and pass the result // content rect to OnLayoutContent. virtual void OnLayoutCore(); @@ -169,8 +159,6 @@ class CRU_UI_API RenderObject : public Object { virtual Size OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) = 0; - virtual Size OnMeasureContent1(const BoxConstraint& constraint); - // Layout all content and children(Call Layout on them). // Lefttop of content_rect should be added when calculated children's offset. virtual void OnLayoutContent(const Rect& content_rect) = 0; @@ -182,22 +170,18 @@ class CRU_UI_API RenderObject : public Object { private: std::string name_; - controls::Control* control_ = nullptr; - - RenderObject* parent_ = nullptr; - - Point offset_{}; - Size size_{}; + controls::Control* control_; + RenderObject* parent_; - Size desired_size_{}; + Point offset_; + Size size_; - Thickness margin_{}; - Thickness padding_{}; + Size desired_size_; - MeasureSize preferred_size_{}; - MeasureRequirement custom_measure_requirement_{}; + Thickness margin_; + Thickness padding_; - Size min_size_ = Size::kZero; - Size max_size_ = Size::kMax; + MeasureSize preferred_size_; + MeasureRequirement custom_measure_requirement_; }; } // namespace cru::ui::render diff --git a/include/cru/ui/render/ScrollRenderObject.h b/include/cru/ui/render/ScrollRenderObject.h index b3d1a09f..417ebf1c 100644 --- a/include/cru/ui/render/ScrollRenderObject.h +++ b/include/cru/ui/render/ScrollRenderObject.h @@ -73,8 +73,6 @@ class CRU_UI_API ScrollRenderObject : public SingleChildRenderObject { const MeasureSize& preferred_size) override; void OnLayoutContent(const Rect& content_rect) override; - Size OnMeasureContent1(const BoxConstraint& constraint) override; - void OnAttachedControlChanged(controls::Control* old_control, controls::Control* new_control) override; diff --git a/include/cru/ui/render/TextRenderObject.h b/include/cru/ui/render/TextRenderObject.h index 19ff800a..28d674aa 100644 --- a/include/cru/ui/render/TextRenderObject.h +++ b/include/cru/ui/render/TextRenderObject.h @@ -94,8 +94,6 @@ class CRU_UI_API TextRenderObject : public RenderObject { Size OnMeasureContent(const MeasureRequirement& requirement, const MeasureSize& preferred_size) override; - Size OnMeasureContent1(const BoxConstraint& constraint) override; - void OnLayoutContent(const Rect& content_rect) override; private: diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index 954bf6c7..23405aac 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -124,13 +124,6 @@ Size BorderRenderObject::OnMeasureContent(const MeasureRequirement& requirement, } } -Size BorderRenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - auto child = GetChild(); - if (child == nullptr) return constraint.min; - auto child_size = child->Measure1(BoxConstraint::kNotLimit); - return constraint.Coerce(child_size); -} - void BorderRenderObject::OnLayoutContent(const Rect& content_rect) { if (auto child = GetChild()) { child->Layout(content_rect.GetLeftTop()); diff --git a/src/ui/render/CanvasRenderObject.cpp b/src/ui/render/CanvasRenderObject.cpp index 318f1ffb..b68f9afd 100644 --- a/src/ui/render/CanvasRenderObject.cpp +++ b/src/ui/render/CanvasRenderObject.cpp @@ -21,10 +21,6 @@ Size CanvasRenderObject::OnMeasureContent(const MeasureRequirement& requirement, preferred_size.height.GetLengthOr(100)}); } -Size CanvasRenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - return constraint.Coerce(Size{100, 100}); -} - void CanvasRenderObject::OnLayoutContent(const Rect& content_rect) { CRU_UNUSED(content_rect) } diff --git a/src/ui/render/GeometryRenderObject.cpp b/src/ui/render/GeometryRenderObject.cpp index b311665f..9b4eb572 100644 --- a/src/ui/render/GeometryRenderObject.cpp +++ b/src/ui/render/GeometryRenderObject.cpp @@ -105,9 +105,5 @@ Size GeometryRenderObject::OnMeasureContent( return requirement.Coerce(result); } -Size GeometryRenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - return constraint.Coerce(GetViewPort().GetSize()); -} - void GeometryRenderObject::OnLayoutContent(const Rect& content_rect) {} } // namespace cru::ui::render diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index 8bd85784..9081d553 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -9,7 +9,8 @@ namespace cru::ui::render { const BoxConstraint BoxConstraint::kNotLimit{Size::kMax, Size::kZero}; -RenderObject::RenderObject(std::string name) : name_(std::move(name)) {} +RenderObject::RenderObject(std::string name) + : name_(std::move(name)), control_(nullptr), parent_(nullptr) {} RenderObject::~RenderObject() { DestroyEvent_.Raise(this); } @@ -77,42 +78,6 @@ void RenderObject::SetMaxSize(const MeasureSize& max_size) { InvalidateLayout(); } -void RenderObject::SetMinSize1(const Size& min_size) { - min_size_ = min_size; - InvalidateLayout(); -} - -void RenderObject::SetMaxSize1(const Size& max_size) { - max_size_ = max_size; - InvalidateLayout(); -} - -BoxConstraint RenderObject::CalculateMergedConstraint( - const BoxConstraint& constraint) { - auto result = constraint; - if (max_size_.width >= constraint.min.width && - max_size_.width < constraint.max.width) { - result.max.width = max_size_.width; - } - - if (max_size_.height >= constraint.min.height && - max_size_.height < constraint.max.height) { - result.max.height = max_size_.height; - } - - if (min_size_.width <= constraint.max.width && - min_size_.width > constraint.min.width) { - result.min.width = min_size_.width; - } - - if (min_size_.height <= constraint.max.height && - min_size_.height > constraint.min.height) { - result.min.height = min_size_.height; - } - - return result; -} - void RenderObject::Measure(const MeasureRequirement& requirement, const MeasureSize& preferred_size) { MeasureRequirement merged_requirement = @@ -139,13 +104,6 @@ void RenderObject::Measure(const MeasureRequirement& requirement, Ensures(desired_size_.height >= 0); } -Size RenderObject::Measure1(const BoxConstraint& constraint) { - Expects(constraint.Validate()); - desired_size_ = OnMeasureCore1(constraint); - Ensures(constraint.Satisfy(desired_size_)); - return desired_size_; -} - void RenderObject::Layout(const Point& offset) { if constexpr (cru::ui::debug_flags::layout) { CRU_LOG_TAG_DEBUG("{} Layout :\noffset: {} size: {}", @@ -185,33 +143,6 @@ Size RenderObject::OnMeasureCore(const MeasureRequirement& requirement, return space_size + content_size; } -Size RenderObject::OnMeasureCore1(const BoxConstraint& constraint) { - auto merged_constraint = CalculateMergedConstraint(constraint); - - const Thickness outer_space = GetTotalSpaceThickness(); - Size space_size{outer_space.GetHorizontalTotal(), - outer_space.GetVerticalTotal()}; - - if (space_size.width > merged_constraint.max.width) { - space_size.width = merged_constraint.max.width; - CRU_LOG_TAG_WARN("{} space width is over constraint.max.width", - this->GetDebugPathInTree()); - } - - if (space_size.height > merged_constraint.max.height) { - space_size.height = merged_constraint.max.height; - CRU_LOG_TAG_WARN("{} space height is over constraint.max.height", - this->GetDebugPathInTree()); - } - - BoxConstraint content_constraint{merged_constraint.max - space_size, - merged_constraint.min - space_size}; - - const auto content_size = OnMeasureContent1(content_constraint); - - return space_size + content_size; -} - void RenderObject::OnLayoutCore() { Size total_size = GetDesiredSize(); const Thickness outer_space = GetTotalSpaceThickness(); @@ -240,10 +171,6 @@ void RenderObject::OnLayoutCore() { OnLayoutContent(content_rect); } -Size RenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - throw Exception("Not implemented."); -} - Rect RenderObject::GetPaddingRect() { const auto size = GetDesiredSize(); Rect rect{Point{}, size}; diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp index 49cb6bf3..d3e00db1 100644 --- a/src/ui/render/ScrollRenderObject.cpp +++ b/src/ui/render/ScrollRenderObject.cpp @@ -189,13 +189,6 @@ Size ScrollRenderObject::OnMeasureContent(const MeasureRequirement& requirement, } } -Size ScrollRenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - auto child = GetChild(); - if (child == nullptr) return constraint.min; - auto child_size = child->Measure1(BoxConstraint::kNotLimit); - return constraint.Coerce(child_size); -} - void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) { if (auto child = GetChild()) { child->Layout(content_rect.GetLeftTop() - GetScrollOffset()); diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp index 3af08e49..b9ab8dd9 100644 --- a/src/ui/render/TextRenderObject.cpp +++ b/src/ui/render/TextRenderObject.cpp @@ -226,17 +226,6 @@ Size TextRenderObject::OnMeasureContent(const MeasureRequirement& requirement, return result; } -Size TextRenderObject::OnMeasureContent1(const BoxConstraint& constraint) { - text_layout_->SetMaxWidth(constraint.max.width); - text_layout_->SetMaxHeight(std::numeric_limits<float>::max()); - - const Size text_size( - text_layout_->GetTextBounds(is_measure_including_trailing_space_) - .GetRightBottom()); - - return constraint.Coerce(text_size); -} - void TextRenderObject::OnLayoutContent(const Rect& content_rect) { CRU_UNUSED(content_rect) } |
