aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/StackLayoutRenderObject.cpp
blob: 7dce1a83e90a8e799d83c19a6f7b32cc320568fc (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
#include "cru/ui/render/StackLayoutRenderObject.hpp"

#include <algorithm>

namespace cru::ui::render {
Size StackLayoutRenderObject::OnMeasureContent(
    const MeasureRequirement& requirement) {
  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;
}

void StackLayoutRenderObject::OnLayoutContent(const Rect& rect) {
  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();

  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});
  }
}

}  // namespace cru::ui::render