aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/StackLayoutRenderObject.cpp
blob: 168ff379f94652c82f24d3f629f320c07d2be040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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) {
  Size max_size;
  for (const auto child : GetChildren()) {
    child->Measure(
        MeasureRequirement{
            MeasureSize{StackLayoutCalculateChildMaxLength(
                            preferred_size.width, requirement.max.width,
                            child->GetMinSize().width, log_tag,
                            "(Measure) Child's min width is bigger than "
                            "parent's max width."),
                        StackLayoutCalculateChildMaxLength(
                            preferred_size.height, requirement.max.height,
                            child->GetMinSize().height, log_tag,
                            "(Measure) 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);

  return max_size;
}

void StackLayoutRenderObject::OnLayoutContent(const Rect& content_rect) {
  const auto count = GetChildCount();
  const auto& children = GetChildren();

  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