aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/ScrollRenderObject.cpp
blob: f1df9d8b635585a348b6fbba00c68c7a83978659 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "cru/ui/render/ScrollRenderObject.hpp"

#include "cru/platform/graph/Painter.hpp"
#include "cru/platform/graph/util/Painter.hpp"

#include <algorithm>

namespace cru::ui::render {
namespace {
// This method assumes margin offset is already considered.
// It promises that it won't return negetive value.
Point CalculateChildOffsetOfScroll(const Point& scroll_offset,
                                   const Size& content_area_size,
                                   const Thickness& padding,
                                   const Size& child_size) {
  Point result(scroll_offset);

  Point max_scroll(child_size - content_area_size);
  max_scroll.x = std::max(max_scroll.x, 0.f);
  max_scroll.y = std::max(max_scroll.y, 0.f);

  auto coerce = [](float& n, float max) {
    if (n < 0)
      n = 0;
    else if (n > max)
      n = max;
  };

  coerce(result.x, scroll_offset.x);
  coerce(result.y, scroll_offset.y);

  result.x += padding.left;
  result.y += padding.top;

  return result;
}
}  // namespace

void ScrollRenderObject::Draw(platform::graph::IPainter* painter) {
  for (auto child : this->GetChildren()) {
    painter->PushLayer(this->GetPaddingRect());
    const auto true_offset =
        CalculateChildOffsetOfScroll(scroll_offset_, GetContentRect().GetSize(),
                                     GetPadding(), child->GetSize());
    platform::graph::util::WithTransform(
        painter, Matrix::Translation(true_offset.x, true_offset.y),
        [child](platform::graph::IPainter* p) { child->Draw(p); });
    painter->PopLayer();
  }
}

RenderObject* ScrollRenderObject::HitTest(const Point& point) {
  const auto rect = GetPaddingRect();
  return rect.IsPointInside(point) ? this : nullptr;
}

void ScrollRenderObject::SetScrollOffset(const Point& offset) {
  scroll_offset_ = offset;
  InvalidatePaint();
}

Size ScrollRenderObject::OnMeasureContent(
    const MeasureRequirement& requirement) {
  CRU_UNUSED(requirement);
  // TODO!
  // const auto& children = GetChildren();
  // if (children.empty()) return Size{};
  // const auto child = children.front();
  // child->Measure(MeasureRequirement::Infinate());
  // const auto preferred_size = child->GetMeasuredSize();
  return Size{};
}  // namespace cru::ui::render

void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) {
  CRU_UNUSED(content_rect);
  // TODO!
}
}  // namespace cru::ui::render