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
|
#pragma once
#include "RenderObject.hpp"
#include "cru/platform/graphics/util/Painter.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/render/ScrollBar.hpp"
#include <memory>
#include <optional>
namespace cru::ui::render {
// Measure logic:
// Measure child with unspecified min and max size.
// If parent's preferred size is specified, then it is used as measure result.
// Or child's size is coerced into requirement and then used as result.
// If no child, then use the preferred size if set or min size if set or 0.
// Layout logic:
// If child is smaller than content area, layout at lefttop.
// Or layout by scroll state.
class ScrollRenderObject : public RenderObject {
public:
ScrollRenderObject();
CRU_DELETE_COPY(ScrollRenderObject)
CRU_DELETE_MOVE(ScrollRenderObject)
~ScrollRenderObject() override = default;
RenderObject* HitTest(const Point& point) override;
// Return the coerced scroll offset.
Point GetScrollOffset();
float GetScrollOffset(Direction direction) {
return direction == Direction::Horizontal ? GetScrollOffset().x
: GetScrollOffset().y;
}
void SetScrollOffset(const Point& offset);
void SetScrollOffset(std::optional<float> x, std::optional<float> y);
void SetScrollOffset(Direction direction, std::optional<float> value) {
if (direction == Direction::Horizontal) {
SetScrollOffset(value, std::nullopt);
} else {
SetScrollOffset(std::nullopt, value);
}
}
void Scroll(const Scroll& scroll);
Point GetRawScrollOffset() const { return scroll_offset_; }
// Return the viewable area rect.
// Lefttop is scroll offset. Size is content size.
// If size exceeds view area, left and top is more important when calculate
// new scroll offset.
Rect GetViewRect() {
return Rect{GetScrollOffset(), GetContentRect().GetSize()};
}
// Rect lefttop relative to content rect.
// Param margin is just for convenience and it will just add to the rect.
void ScrollToContain(const Rect& rect, const Thickness& margin = Thickness{});
protected:
void OnDrawCore(platform::graphics::IPainter* painter) override;
// Logic:
// If available size is bigger than child's preferred size, then child's
// preferred size is taken.
// If not, all available size is taken while forming a scroll area.
Size OnMeasureContent(const MeasureRequirement& requirement,
const MeasureSize& preferred_size) override;
void OnLayoutContent(const Rect& content_rect) override;
void OnAttachedControlChanged(controls::Control* control) override;
private:
Point scroll_offset_;
std::unique_ptr<ScrollBarDelegate> scroll_bar_delegate_;
};
} // namespace cru::ui::render
|