aboutsummaryrefslogtreecommitdiff
path: root/src/ui/render/ScrollRenderObject.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-28 00:22:34 +0800
committercrupest <crupest@outlook.com>2021-02-28 00:22:34 +0800
commit4b78e0b74f70bca2e24dc89b4fdca4dc9222c8b9 (patch)
tree257971a08ccb6bba0fa30857e0fcbd07902a0c91 /src/ui/render/ScrollRenderObject.cpp
parent4a0c86d94a06e72be0988062d49a19c05142434a (diff)
downloadcru-4b78e0b74f70bca2e24dc89b4fdca4dc9222c8b9.tar.gz
cru-4b78e0b74f70bca2e24dc89b4fdca4dc9222c8b9.tar.bz2
cru-4b78e0b74f70bca2e24dc89b4fdca4dc9222c8b9.zip
...
Diffstat (limited to 'src/ui/render/ScrollRenderObject.cpp')
-rw-r--r--src/ui/render/ScrollRenderObject.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/ui/render/ScrollRenderObject.cpp b/src/ui/render/ScrollRenderObject.cpp
index a9ec729d..0a81eaab 100644
--- a/src/ui/render/ScrollRenderObject.cpp
+++ b/src/ui/render/ScrollRenderObject.cpp
@@ -2,13 +2,17 @@
#include "cru/platform/graphics/Painter.hpp"
#include "cru/platform/graphics/util/Painter.hpp"
+#include "cru/ui/Base.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/render/ScrollBar.hpp"
#include <algorithm>
#include <memory>
+#include <optional>
namespace cru::ui::render {
+constexpr float kLineHeight = 16;
+
namespace {
// This method assumes margin offset is already considered.
// It promises that it won't return negetive value.
@@ -36,6 +40,35 @@ Point CoerceScroll(const Point& scroll_offset, const Size& content_size,
ScrollRenderObject::ScrollRenderObject() : RenderObject(ChildMode::Single) {
scroll_bar_delegate_ = std::make_unique<ScrollBarDelegate>(this);
+ scroll_bar_delegate_->ScrollAttemptEvent()->AddHandler(
+ [this](const struct Scroll& scroll) { this->Scroll(scroll); });
+}
+
+void ScrollRenderObject::Scroll(const struct Scroll& scroll) {
+ auto direction = scroll.direction;
+
+ switch (scroll.kind) {
+ case ScrollKind::Absolute:
+ SetScrollOffset(direction, scroll.value);
+ break;
+ case ScrollKind::Relative:
+ SetScrollOffset(direction,
+ GetScrollOffset(scroll.direction) + scroll.value);
+ break;
+ case ScrollKind::Page:
+ SetScrollOffset(direction, GetScrollOffset(direction) +
+ (direction == Direction::Horizontal
+ ? GetViewRect().width
+ : GetViewRect().height) *
+ scroll.value);
+ break;
+ case ScrollKind::Line:
+ SetScrollOffset(direction,
+ GetScrollOffset(direction) + kLineHeight * scroll.value);
+ break;
+ default:
+ break;
+ }
}
RenderObject* ScrollRenderObject::HitTest(const Point& point) {