blob: a9be49a3393fb4ef81dde8d73c605f733c5a8723 (
plain)
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
|
#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/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, Page, Line };
struct Scroll {
Direction direction;
ScrollKind kind;
float offset;
};
enum class ScrollBarAreaKind {
UpArrow, // Line up
DownArrow, // Line down
UpThumb, // Page up
DownThumb, // Page down
Thumb
};
class ScrollBar : public Object {
public:
explicit ScrollBar(gsl::not_null<ScrollRenderObject*> render_object);
CRU_DELETE_COPY(ScrollBar)
CRU_DELETE_MOVE(ScrollBar)
~ScrollBar() override = default;
public:
bool IsEnabled() const { return is_enabled_; }
void SetEnabled(bool value);
void Draw(platform::graphics::IPainter* painter);
virtual std::optional<ScrollBarAreaKind> HitTest(const Point& point) = 0;
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>>
GetCollapseThumbBrush() const;
protected:
virtual void OnDraw(platform::graphics::IPainter* painter, bool expand) = 0;
protected:
gsl::not_null<ScrollRenderObject*> render_object_;
private:
bool is_enabled_ = true;
bool is_expanded_ = false;
std::shared_ptr<platform::graphics::IBrush> collapse_thumb_brush_;
EventRevokerListGuard event_guard_;
Event<Scroll> scroll_attempt_event_;
};
class HorizontalScrollBar : public ScrollBar {
public:
explicit HorizontalScrollBar(
gsl::not_null<ScrollRenderObject*> render_object);
CRU_DELETE_COPY(HorizontalScrollBar)
CRU_DELETE_MOVE(HorizontalScrollBar)
~HorizontalScrollBar() override = default;
public:
std::optional<ScrollBarAreaKind> HitTest(const Point& point) override;
protected:
void OnDraw(platform::graphics::IPainter* painter, bool expand) 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;
public:
std::optional<ScrollBarAreaKind> HitTest(const Point& point) override;
protected:
void OnDraw(platform::graphics::IPainter* painter, bool expand) 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
|