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
|
#include "cru/ui/render/ScrollBar.hpp"
#include "../Helper.hpp"
#include "cru/common/Base.hpp"
#include "cru/platform/GraphBase.hpp"
#include "cru/platform/graphics/Factory.hpp"
#include "cru/platform/graphics/Painter.hpp"
#include "cru/ui/render/ScrollRenderObject.hpp"
#include <gsl/pointers>
#include <optional>
namespace cru::ui::render {
constexpr float kScrollBarCollapseThumbWidth = 2;
ScrollBar::ScrollBar(gsl::not_null<ScrollRenderObject*> render_object)
: render_object_(render_object) {
// TODO: Use theme resource and delete this.
auto collapse_thumb_brush = GetUiApplication()
->GetInstance()
->GetGraphFactory()
->CreateSolidColorBrush();
collapse_thumb_brush->SetColor(colors::gray.WithAlpha(128));
collapse_thumb_brush_ = std::move(collapse_thumb_brush);
}
void ScrollBar::SetEnabled(bool value) {
CRU_UNUSED(value)
// TODO: Implement this.
}
void ScrollBar::Draw(platform::graphics::IPainter* painter) {
if (is_enabled_) {
OnDraw(painter, is_expanded_);
}
}
void ScrollBar::InstallHandlers(controls::Control* control) {
CRU_UNUSED(control);
// TODO: Implement this.
}
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
ScrollBar::GetCollapseThumbBrush() const {
// TODO: Read theme resource.
return collapse_thumb_brush_;
}
HorizontalScrollBar::HorizontalScrollBar(
gsl::not_null<ScrollRenderObject*> render_object)
: ScrollBar(render_object) {}
std::optional<ScrollBarAreaKind> HorizontalScrollBar::HitTest(
const Point& point) {
// TODO: Implement this.
CRU_UNUSED(point);
return std::nullopt;
}
void HorizontalScrollBar::OnDraw(platform::graphics::IPainter* painter,
bool expand) {
const auto child = render_object_->GetFirstChild();
if (child == nullptr) return;
const auto view_rect = render_object_->GetViewRect();
const auto padding_rect = render_object_->GetPaddingRect();
const auto child_size = child->GetSize();
if (view_rect.width >= child_size.width) return;
const float start_percentage = view_rect.left / child_size.width;
const float length_percentage = view_rect.width / child_size.width;
// const float end_percentage = start_percentage + length_percentage;
if (expand) {
// TODO: Implement this.
} else {
Rect thumb_rect{padding_rect.left + padding_rect.width * start_percentage,
padding_rect.GetBottom() - kScrollBarCollapseThumbWidth,
padding_rect.width * length_percentage,
kScrollBarCollapseThumbWidth};
painter->FillRectangle(thumb_rect, GetCollapseThumbBrush().get().get());
}
}
VerticalScrollBar::VerticalScrollBar(
gsl::not_null<ScrollRenderObject*> render_object)
: ScrollBar(render_object) {}
std::optional<ScrollBarAreaKind> VerticalScrollBar::HitTest(
const Point& point) {
// TODO: Implement this.
CRU_UNUSED(point);
return std::nullopt;
}
void VerticalScrollBar::OnDraw(platform::graphics::IPainter* painter,
bool expand) {
const auto child = render_object_->GetFirstChild();
if (child == nullptr) return;
const auto view_rect = render_object_->GetViewRect();
const auto padding_rect = render_object_->GetPaddingRect();
const auto child_size = child->GetSize();
if (view_rect.height >= child_size.height) return;
const float start_percentage = view_rect.top / child_size.height;
const float length_percentage = view_rect.height / child_size.height;
// const float end_percentage = start_percentage + length_percentage;
if (expand) {
// TODO: Implement this.
} else {
Rect thumb_rect{padding_rect.GetRight() - kScrollBarCollapseThumbWidth,
padding_rect.top + padding_rect.height * start_percentage,
kScrollBarCollapseThumbWidth,
padding_rect.height * length_percentage};
painter->FillRectangle(thumb_rect, GetCollapseThumbBrush().get().get());
}
}
ScrollBarDelegate::ScrollBarDelegate(
gsl::not_null<ScrollRenderObject*> render_object)
: render_object_(render_object),
horizontal_bar_(render_object),
vertical_bar_(render_object) {
horizontal_bar_.ScrollAttemptEvent()->AddHandler(
[this](auto scroll) { this->scroll_attempt_event_.Raise(scroll); });
vertical_bar_.ScrollAttemptEvent()->AddHandler(
[this](auto scroll) { this->scroll_attempt_event_.Raise(scroll); });
}
void ScrollBarDelegate::DrawScrollBar(platform::graphics::IPainter* painter) {
horizontal_bar_.Draw(painter);
vertical_bar_.Draw(painter);
}
void ScrollBarDelegate::InstallHandlers(controls::Control* control) {
horizontal_bar_.InstallHandlers(control);
vertical_bar_.InstallHandlers(control);
}
} // namespace cru::ui::render
|