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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#pragma once
#include "Base.hpp"
#include "cru/common/Base.hpp"
#include "cru/common/Event.hpp"
#include "cru/platform/graphics/Base.hpp"
#include "cru/platform/graphics/Painter.hpp"
#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/controls/Control.hpp"
#include <gsl/pointers>
#include <memory>
#include <optional>
namespace cru::ui::render {
class ScrollRenderObject;
enum class ScrollKind { Absolute, Relative, Page, Line };
struct Scroll {
Direction direction;
ScrollKind kind;
// For absolute, the new scroll position. Otherwise, offset.
float value;
};
enum class ScrollBarAreaKind {
UpArrow, // Line up
DownArrow, // Line down
UpSlot, // Page up
DownSlot, // Page down
Thumb
};
class ScrollBar : public Object {
public:
ScrollBar(gsl::not_null<ScrollRenderObject*> render_object,
Direction direction);
CRU_DELETE_COPY(ScrollBar)
CRU_DELETE_MOVE(ScrollBar)
~ScrollBar() override;
public:
Direction GetDirection() const { return direction_; }
bool IsEnabled() const { return is_enabled_; }
void SetEnabled(bool value);
bool IsExpanded() const { return is_expanded_; }
void SetExpanded(bool value);
void Draw(platform::graphics::IPainter* painter);
IEvent<Scroll>* ScrollAttemptEvent() { return &scroll_attempt_event_; }
void InstallHandlers(controls::Control* control);
void UninstallHandlers() { InstallHandlers(nullptr); }
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
GetCollapsedThumbBrush() const;
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
GetExpandedThumbBrush() const;
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
GetExpandedSlotBrush() const;
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
GetExpandedArrowBrush() const;
gsl::not_null<std::shared_ptr<platform::graphics::IBrush>>
GetExpandedArrowBackgroundBrush() const;
protected:
void OnDraw(platform::graphics::IPainter* painter, bool expand);
virtual void DrawUpArrow(platform::graphics::IPainter* painter,
const Rect& area) = 0;
virtual void DrawDownArrow(platform::graphics::IPainter* painter,
const Rect& area) = 0;
std::optional<ScrollBarAreaKind> ExpandedHitTest(const Point& point);
virtual bool IsShowBar() = 0;
virtual std::optional<Rect> GetExpandedAreaRect(
ScrollBarAreaKind area_kind) = 0;
virtual std::optional<Rect> GetCollapsedTriggerExpandAreaRect() = 0;
virtual std::optional<Rect> GetCollapsedThumbRect() = 0;
virtual float CalculateNewScrollPosition(const Rect& thumb_original_rect,
const Point& mouse_offset) = 0;
private:
void SetCursor();
void RestoreCursor();
void BeginAutoCollapseTimer();
void StopAutoCollapseTimer();
void OnMouseLeave();
protected:
gsl::not_null<ScrollRenderObject*> render_object_;
private:
Direction direction_;
bool is_enabled_ = true;
bool is_expanded_ = false;
std::shared_ptr<platform::graphics::IBrush> collapsed_thumb_brush_;
std::shared_ptr<platform::graphics::IBrush> expanded_thumb_brush_;
std::shared_ptr<platform::graphics::IBrush> expanded_slot_brush_;
std::shared_ptr<platform::graphics::IBrush> expanded_arrow_brush_;
std::shared_ptr<platform::graphics::IBrush> expanded_arrow_background_brush_;
Rect move_thumb_thumb_original_rect_;
std::optional<Point> move_thumb_start_;
EventRevokerListGuard event_guard_;
Event<Scroll> scroll_attempt_event_;
std::optional<std::shared_ptr<platform::gui::ICursor>> old_cursor_;
platform::gui::TimerAutoCanceler auto_collapse_timer_canceler_;
};
class HorizontalScrollBar : public ScrollBar {
public:
explicit HorizontalScrollBar(
gsl::not_null<ScrollRenderObject*> render_object);
CRU_DELETE_COPY(HorizontalScrollBar)
CRU_DELETE_MOVE(HorizontalScrollBar)
~HorizontalScrollBar() override = default;
protected:
void DrawUpArrow(platform::graphics::IPainter* painter,
const Rect& area) override;
void DrawDownArrow(platform::graphics::IPainter* painter,
const Rect& area) override;
bool IsShowBar() override;
std::optional<Rect> GetExpandedAreaRect(ScrollBarAreaKind area_kind) override;
std::optional<Rect> GetCollapsedTriggerExpandAreaRect() override;
std::optional<Rect> GetCollapsedThumbRect() override;
float CalculateNewScrollPosition(const Rect& thumb_original_rect,
const Point& mouse_offset) override;
};
class VerticalScrollBar : public ScrollBar {
public:
explicit VerticalScrollBar(gsl::not_null<ScrollRenderObject*> render_object);
CRU_DELETE_COPY(VerticalScrollBar)
CRU_DELETE_MOVE(VerticalScrollBar)
~VerticalScrollBar() override = default;
protected:
void DrawUpArrow(platform::graphics::IPainter* painter,
const Rect& area) override;
void DrawDownArrow(platform::graphics::IPainter* painter,
const Rect& area) override;
bool IsShowBar() override;
std::optional<Rect> GetExpandedAreaRect(ScrollBarAreaKind area_kind) override;
std::optional<Rect> GetCollapsedTriggerExpandAreaRect() override;
std::optional<Rect> GetCollapsedThumbRect() override;
float CalculateNewScrollPosition(const Rect& thumb_original_rect,
const Point& mouse_offset) override;
};
// A delegate to draw scrollbar and register related events.
class ScrollBarDelegate : public Object {
public:
explicit ScrollBarDelegate(gsl::not_null<ScrollRenderObject*> render_object);
CRU_DELETE_COPY(ScrollBarDelegate)
CRU_DELETE_MOVE(ScrollBarDelegate)
~ScrollBarDelegate() override = default;
public:
bool IsHorizontalBarEnabled() const { return horizontal_bar_.IsEnabled(); }
void SetHorizontalBarEnabled(bool value) {
horizontal_bar_.SetEnabled(value);
}
bool IsVerticalBarEnabled() const { return horizontal_bar_.IsEnabled(); }
void SetVerticalBarEnabled(bool value) { horizontal_bar_.SetEnabled(value); }
IEvent<Scroll>* ScrollAttemptEvent() { return &scroll_attempt_event_; }
void DrawScrollBar(platform::graphics::IPainter* painter);
void InstallHandlers(controls::Control* control);
void UninstallHandlers() { InstallHandlers(nullptr); }
private:
gsl::not_null<ScrollRenderObject*> render_object_;
HorizontalScrollBar horizontal_bar_;
VerticalScrollBar vertical_bar_;
Event<Scroll> scroll_attempt_event_;
};
} // namespace cru::ui::render
|