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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#include "cru/ui/render/ScrollRenderObject.hpp"
#include "cru/platform/graphics/Painter.hpp"
#include "cru/platform/graphics/util/Painter.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/render/ScrollBar.hpp"
#include <algorithm>
#include <memory>
#include <optional>
namespace cru::ui::render {
constexpr float kLineHeight = 16;
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, max_scroll.x);
coerce(result.y, max_scroll.y);
return result;
}
} // namespace
ScrollRenderObject::ScrollRenderObject() : RenderObject(ChildMode::Single) {
scroll_bar_delegate_ = std::make_unique<ScrollBarDelegate>(this);
scroll_bar_delegate_->ScrollAttemptEvent()->AddHandler(
[this](const struct Scroll& scroll) { this->Scroll(scroll); });
}
void ScrollRenderObject::Scroll(const struct Scroll& scroll) {
auto direction = scroll.direction;
switch (scroll.kind) {
case ScrollKind::Absolute:
SetScrollOffset(direction, scroll.value);
break;
case ScrollKind::Relative:
SetScrollOffset(direction,
GetScrollOffset(scroll.direction) + scroll.value);
break;
case ScrollKind::Page:
SetScrollOffset(direction, GetScrollOffset(direction) +
(direction == Direction::Horizontal
? GetViewRect().width
: GetViewRect().height) *
scroll.value);
break;
case ScrollKind::Line:
SetScrollOffset(direction,
GetScrollOffset(direction) + kLineHeight * scroll.value);
break;
default:
break;
}
}
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
void ScrollRenderObject::OnDrawCore(platform::graphics::IPainter* painter) {
DefaultDrawContent(painter);
if (const auto child = GetSingleChild()) {
painter->PushLayer(this->GetContentRect());
const auto offset = child->GetOffset();
platform::graphics::util::WithTransform(
painter, Matrix::Translation(offset.x, offset.y),
[child](platform::graphics::IPainter* p) { child->Draw(p); });
painter->PopLayer();
}
scroll_bar_delegate_->DrawScrollBar(painter);
}
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();
}
void ScrollRenderObject::SetScrollOffset(std::optional<float> x,
std::optional<float> y) {
bool dirty = false;
if (x.has_value()) {
dirty = true;
scroll_offset_.x = *x;
}
if (y.has_value()) {
dirty = true;
scroll_offset_.y = *y;
}
if (dirty) InvalidateLayout();
}
void ScrollRenderObject::ScrollToContain(const Rect& rect,
const Thickness& margin) {
std::optional<float> new_scroll_x;
std::optional<float> new_scroll_y;
Rect real_rect = rect.Expand(margin);
Rect view_rect = GetViewRect();
// horizontal
if (real_rect.left < view_rect.left) {
new_scroll_x = real_rect.left;
} else if (real_rect.GetRight() > view_rect.GetRight()) {
new_scroll_x = real_rect.GetRight() - view_rect.width;
}
// vertical
if (real_rect.top < view_rect.top) {
new_scroll_y = real_rect.top;
} else if (real_rect.GetBottom() > view_rect.GetBottom()) {
new_scroll_y = real_rect.GetBottom() - view_rect.height;
}
SetScrollOffset(new_scroll_x, new_scroll_y);
}
Size ScrollRenderObject::OnMeasureContent(const MeasureRequirement& requirement,
const MeasureSize& preferred_size) {
if (const auto child = GetSingleChild()) {
child->Measure(MeasureRequirement{MeasureSize::NotSpecified(),
MeasureSize::NotSpecified()},
MeasureSize::NotSpecified());
Size result = requirement.Coerce(child->GetSize());
if (preferred_size.width.IsSpecified()) {
result.width = preferred_size.width.GetLengthOrUndefined();
}
if (preferred_size.height.IsSpecified()) {
result.height = preferred_size.height.GetLengthOrUndefined();
}
return result;
} else {
Size result{preferred_size.width.IsSpecified()
? preferred_size.width.GetLengthOrUndefined()
: requirement.min.width.GetLengthOr0(),
preferred_size.height.IsSpecified()
? preferred_size.height.GetLengthOrUndefined()
: requirement.min.height.GetLengthOr0()};
return result;
}
} // namespace cru::ui::render
void ScrollRenderObject::OnLayoutContent(const Rect& content_rect) {
if (const auto child = GetSingleChild()) {
child->Layout(content_rect.GetLeftTop() - GetScrollOffset());
}
}
void ScrollRenderObject::OnAttachedControlChanged(controls::Control* control) {
if (control) {
scroll_bar_delegate_->InstallHandlers(control);
} else {
scroll_bar_delegate_->UninstallHandlers();
}
}
} // namespace cru::ui::render
|