aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/render/ScrollBar.hpp
blob: ce6f1b9885aefc1b83f79fc8c02b5155731c2e68 (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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#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/Brush.hpp"
#include "cru/platform/graphics/Geometry.hpp"
#include "cru/platform/graphics/Painter.hpp"
#include "cru/platform/gui/Cursor.hpp"
#include "cru/platform/gui/TimerHelper.hpp"
#include "cru/platform/gui/UiApplication.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/helper/ClickDetector.hpp"


#include <gsl/pointers>
#include <memory>
#include <optional>
#include <unordered_map>

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
};

enum class ScrollBarBrushUsageKind { Arrow, ArrowBackground, Slot, Thumb };
enum class ScrollBarBrushStateKind { Normal, Hover, Press, Disable };

std::u16string GenerateScrollBarThemeColorKey(ScrollBarBrushUsageKind usage,
                                              ScrollBarBrushStateKind state);

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();
  // Brush could be nullptr to use the theme brush.
  void SetCollapsedThumbBrush(
      std::shared_ptr<platform::graphics::IBrush> brush);
  gsl::not_null<std::shared_ptr<platform::graphics::IBrush>> GetBrush(
      ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state);
  // Brush could be nullptr to use the theme brush.
  void SetBrush(ScrollBarBrushUsageKind usage, ScrollBarBrushStateKind state,
                std::shared_ptr<platform::graphics::IBrush> brush);

 protected:
  void OnDraw(platform::graphics::IPainter* painter, bool expand);

  virtual void DrawUpArrow(
      platform::graphics::IPainter* painter, const Rect& area,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) = 0;
  virtual void DrawDownArrow(
      platform::graphics::IPainter* painter, const Rect& area,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) = 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;

  virtual bool CanScrollUp() = 0;
  virtual bool CanScrollDown() = 0;

 private:
  void SetCursor();
  void RestoreCursor();

  void BeginAutoCollapseTimer();
  void StopAutoCollapseTimer();

  void OnMouseLeave();

  ScrollBarBrushStateKind GetState(ScrollBarAreaKind area);

 protected:
  gsl::not_null<ScrollRenderObject*> render_object_;

  std::unique_ptr<platform::graphics::IGeometry> arrow_geometry_;

 private:
  Direction direction_;

  bool is_enabled_ = true;

  bool is_expanded_ = false;

  std::shared_ptr<platform::graphics::IBrush> collapsed_thumb_brush_;
  std::unordered_map<
      ScrollBarBrushUsageKind,
      std::unordered_map<ScrollBarBrushStateKind,
                         std::shared_ptr<platform::graphics::IBrush>>>
      brushes_;

  Rect move_thumb_thumb_original_rect_;
  std::optional<Point> move_thumb_start_;

  std::optional<ScrollBarAreaKind> mouse_hover_;
  std::optional<ScrollBarAreaKind> mouse_press_;

  EventRevokerListGuard event_guard_;

  Event<Scroll> scroll_attempt_event_;

  bool cursor_overrided_ = false;

  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,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) override;
  void DrawDownArrow(
      platform::graphics::IPainter* painter, const Rect& area,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) 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;

  bool CanScrollUp() override;
  bool CanScrollDown() 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,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) override;
  void DrawDownArrow(
      platform::graphics::IPainter* painter, const Rect& area,
      gsl::not_null<platform::graphics::IBrush*> arrow_brush,
      gsl::not_null<platform::graphics::IBrush*> background_brush) 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;

  bool CanScrollUp() override;
  bool CanScrollDown() 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