aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/StackLayoutRenderObject.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-28 21:25:09 +0800
committercrupest <crupest@outlook.com>2020-06-28 21:25:09 +0800
commitb3c0c076e5a1b1e7d76fa8d32af0bcbb2c1cd4cf (patch)
treedf90c6ae501508931fbd46ebc6340c90391e1ed5 /src/ui/render/StackLayoutRenderObject.cpp
parent698242c10cbef4ceaade848fa6aedac71baaf1f2 (diff)
downloadcru-b3c0c076e5a1b1e7d76fa8d32af0bcbb2c1cd4cf.tar.gz
cru-b3c0c076e5a1b1e7d76fa8d32af0bcbb2c1cd4cf.tar.bz2
cru-b3c0c076e5a1b1e7d76fa8d32af0bcbb2c1cd4cf.zip
...
Diffstat (limited to 'src/ui/render/StackLayoutRenderObject.cpp')
-rw-r--r--src/ui/render/StackLayoutRenderObject.cpp85
1 files changed, 39 insertions, 46 deletions
diff --git a/src/ui/render/StackLayoutRenderObject.cpp b/src/ui/render/StackLayoutRenderObject.cpp
index b953ae7e..a6ed7708 100644
--- a/src/ui/render/StackLayoutRenderObject.cpp
+++ b/src/ui/render/StackLayoutRenderObject.cpp
@@ -1,60 +1,53 @@
#include "cru/ui/render/StackLayoutRenderObject.hpp"
+#include "cru/common/Logger.hpp"
+#include "cru/ui/render/LayoutHelper.hpp"
+
#include <algorithm>
namespace cru::ui::render {
Size StackLayoutRenderObject::OnMeasureContent(
const MeasureRequirement& requirement, const MeasureSize& preferred_size) {
- // TODO: Rewrite this.
- CRU_UNUSED(requirement);
- CRU_UNUSED(preferred_size);
- throw std::runtime_error("Not implemented.");
+ Size max_size;
+ for (const auto child : GetChildren()) {
+ child->Measure(
+ MeasureRequirement{
+ MeasureSize{StackLayoutCalculateChildMaxLength(
+ preferred_size.width, requirement.max.width,
+ child->GetMinSize().width,
+ "StackLayoutRenderObject: Child's min width is "
+ "bigger than parent's max width."),
+ StackLayoutCalculateChildMaxLength(
+ preferred_size.height, requirement.max.height,
+ child->GetMinSize().height,
+ "StackLayoutRenderObject: Child's min height is "
+ "bigger than parent's max height.")},
+ MeasureSize::NotSpecified()},
+ MeasureSize::NotSpecified());
+ const auto size = child->GetSize();
+ max_size.width = std::max(max_size.width, size.width);
+ max_size.height = std::max(max_size.height, size.height);
+ }
+
+ max_size = Max(preferred_size.GetSizeOr0(), max_size);
+ max_size = Max(requirement.min.GetSizeOr0(), max_size);
- // throw std::runtime_error("Not implemented.");
- // auto size = Size{};
- // for (const auto child : GetChildren()) {
- // child->Measure(requirement);
- // const auto& measure_result = child->GetMeasuredSize();
- // size.width = std::max(size.width, measure_result.width);
- // size.height = std::max(size.height, measure_result.height);
- // }
- // return size;
+ return max_size;
}
void StackLayoutRenderObject::OnLayoutContent(const Rect& content_rect) {
- // TODO: Rewrite this.
- CRU_UNUSED(content_rect);
- throw std::runtime_error("Not implemented.");
-
- // auto calculate_anchor = [](int alignment, float start_point,
- // float total_length,
- // float content_length) -> float {
- // switch (alignment) {
- // case internal::align_start:
- // return start_point;
- // case internal::align_center:
- // return start_point + (total_length - content_length) / 2.0f;
- // case internal::align_end:
- // return start_point + total_length - content_length;
- // default:
- // return start_point;
- // }
- // };
-
- // const auto count = GetChildCount();
- // const auto& children = GetChildren();
+ const auto count = GetChildCount();
+ const auto& children = GetChildren();
- // for (int i = 0; i < count; i++) {
- // const auto layout_data = GetChildLayoutData(i);
- // const auto child = children[i];
- // const auto& size = child->GetMeasuredSize();
- // child->Layout(
- // Rect{calculate_anchor(static_cast<int>(layout_data->horizontal),
- // rect.left, rect.width, size.width),
- // calculate_anchor(static_cast<int>(layout_data->vertical),
- // rect.top,
- // rect.height, size.height),
- // size.width, size.height});
- // }
+ for (int i = 0; i < count; i++) {
+ const auto child = children[i];
+ const auto& layout_data = GetChildLayoutData(i);
+ const auto& size = child->GetSize();
+ child->Layout(Point{
+ CalculateAnchorByAlignment(layout_data.horizontal, content_rect.left,
+ content_rect.width, size.width),
+ CalculateAnchorByAlignment(layout_data.vertical, content_rect.top,
+ content_rect.height, size.height)});
+ }
}
} // namespace cru::ui::render