scrollable_view.cpp
2.14 KB
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
#include <escher/scrollable_view.h>
#include <escher/metric.h>
#include <assert.h>
ScrollableView::ScrollableView(Responder * parentResponder, View * view, ScrollViewDataSource * dataSource) :
Responder(parentResponder),
ScrollView(view, dataSource),
m_manualScrollingOffset(KDPointZero)
{
setShowsIndicators(false);
setColorsBackground(false);
}
bool ScrollableView::handleEvent(Ion::Events::Event event) {
KDPoint translation = KDPointZero;
if (event == Ion::Events::Left) {
KDCoordinate movementToEdge = m_manualScrollingOffset.x();
if (movementToEdge > 0) {
translation = KDPoint(-min(Metric::ScrollStep, movementToEdge), 0);
}
}
if (event == Ion::Events::Right) {
KDCoordinate movementToEdge = m_contentView->minimalSizeForOptimalDisplay().width() - bounds().width() - m_manualScrollingOffset.x();
if (movementToEdge > 0) {
translation = KDPoint(min(Metric::ScrollStep, movementToEdge), 0);
}
}
if (event == Ion::Events::Up) {
KDCoordinate movementToEdge = m_manualScrollingOffset.y();
if (movementToEdge > 0) {
translation = KDPoint(0, -min(Metric::ScrollStep, movementToEdge));
}
}
if (event == Ion::Events::Down) {
KDCoordinate movementToEdge = m_contentView->minimalSizeForOptimalDisplay().height() - bounds().height() - m_manualScrollingOffset.y();
if (movementToEdge > 0) {
translation = KDPoint(0, min(Metric::ScrollStep, movementToEdge));
}
}
if (translation != KDPointZero) {
m_manualScrollingOffset = m_manualScrollingOffset.translatedBy(translation);
setContentOffset(m_manualScrollingOffset);
return true;
}
return false;
}
void ScrollableView::reloadScroll(bool forceReLayout) {
m_manualScrollingOffset = KDPointZero;
setContentOffset(m_manualScrollingOffset, forceReLayout);
}
void ScrollableView::layoutSubviews() {
KDSize viewSize = contentSize();
KDCoordinate viewWidth = max(viewSize.width(), bounds().width() - leftMargin() - rightMargin());
KDCoordinate viewHeight = max(viewSize.height(), bounds().height() - topMargin() - bottomMargin());
m_contentView->setSize(KDSize(viewWidth, viewHeight));
ScrollView::layoutSubviews();
}