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
79
80
81
82
83
84
85
86
87
88
89
|
#include "cru/ui/render/ScrollRenderObject.hpp"
#include "cru/platform/graph/Painter.hpp"
#include "cru/platform/graph/util/Painter.hpp"
#include "cru/ui/render/LayoutUtility.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 CoerceScroll(const Point& scroll_offset, const Size& content_size,
const Size& child_size) {
Point result(scroll_offset);
Point max_scroll(child_size - content_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);
return result;
}
} // namespace
void ScrollRenderObject::Draw(platform::graph::IPainter* painter) {
if (const auto child = GetSingleChild()) {
painter->PushLayer(this->GetPaddingRect());
const auto offset = child->GetOffset();
platform::graph::util::WithTransform(
painter, Matrix::Translation(offset.x, offset.y),
[child](platform::graph::IPainter* p) { child->Draw(p); });
painter->PopLayer();
}
}
RenderObject* ScrollRenderObject::HitTest(const Point& point) {
if (const auto child = GetSingleChild()) {
const auto offset = child->GetOffset();
const auto r = child->HitTest(point - offset);
if (r != nullptr) return r;
}
const auto rect = GetPaddingRect();
return rect.IsPointInside(point) ? this : nullptr;
} // namespace cru::ui::render
Point ScrollRenderObject::GetScrollOffset() {
if (const auto child = GetSingleChild())
return CoerceScroll(scroll_offset_, GetContentRect().GetSize(),
child->GetSize());
else
return scroll_offset_;
}
void ScrollRenderObject::SetScrollOffset(const Point& offset) {
scroll_offset_ = offset;
InvalidateLayout();
}
Size ScrollRenderObject::OnMeasureContent(
const MeasureRequirement& requirement) {
if (const auto child = GetSingleChild()) {
child->Measure(MeasureRequirement::Infinate());
const auto preferred_size = child->GetMeasuredSize();
return Min(preferred_size, requirement.GetMaxSize());
} else {
return Size{};
}
} // namespace cru::ui::render
void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) {
if (const auto child = GetSingleChild()) {
const auto child_size = child->GetMeasuredSize();
const auto true_scroll =
CoerceScroll(scroll_offset_, content_rect.GetSize(), child_size);
child->Layout(Rect{content_rect.GetLeftTop() - true_scroll, child_size});
}
}
} // namespace cru::ui::render
|