-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScrollView.cpp
More file actions
106 lines (91 loc) · 3 KB
/
ScrollView.cpp
File metadata and controls
106 lines (91 loc) · 3 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
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
#include "ScrollView.hpp"
#include "TextView.hpp"
ScrollView::ScrollView(ViewPtr t){
target = t.get();
TextViewPtr uparrow = TextView::make("^");
uparrow->setFontsize(10);
TextViewPtr downarrow = TextView::make("v");
downarrow->setFontsize(10);
addView(t);
addView(uparrow);
addView(downarrow);
singleStep = 5;
mRightPad = 10;
totalLength = target->getWidth();
LayoutParams::set(uparrow, 10, 10);
LayoutParams::set(downarrow, 10, 10);
// maxScrollX = totalLength - getWidth();
// maxScrollY = target->getHeight() - getHeight();
uparrow->doOnClick([this](View*){
target->scrollBy(0,-singleStep);
});
downarrow->doOnClick([this](View*){
target->scrollBy(0,singleStep);
});
this->interceptOnKey([this](View*, KeyEvent* event){
if(event->code == KeyCode::Up)
if(target->getScrollY() > 0)
target->scrollBy(0,-singleStep);
if(event->code == KeyCode::Down)
if(maxChildY - target->getHeight() - target->getScrollY() > 0)
target->scrollBy(0,singleStep);
if(event->code == KeyCode::Left)
if(target->getScrollX() > 0)
target->scrollBy(-singleStep,0);
if(event->code == KeyCode::Right){
if(maxChildX - target->getWidth() - target->getScrollX() > 0)
target->scrollBy(singleStep,0);
}
else
return false;
return true;
});
this->uparrow = uparrow.get();
this->downarrow = downarrow.get();
}
std::unique_ptr<LayoutParams> ScrollView::generateDefaultLayoutParams(){
auto lp = std::unique_ptr<LayoutParams>(new LayoutParams(LayoutParams::DimenType(LayoutParams::Params::MATCH_PARENT),
LayoutParams::DimenType(LayoutParams::Params::MATCH_PARENT)));
return lp;
}
void ScrollView::doMeasure(Measure wm, Measure hm){
/**
First measure what is the maximum the target view wants to be.
Save it for scrolling limits detection.
Then do the normal measurements
*/
Measure any;
any.spec = Measure::ANY;
measureChild(target,any, any);
maxChildX = target->getMeasuredWidth();
maxChildY = target->getMeasuredHeight();
measureChildren(wm, hm);
int maxWt = target->measuredWt.value + uparrow->measuredWt.value;
int maxHt = target->measuredHt.value;
maxWt += mLeftPad + mRightPad;
maxHt += mTopPad + mBottomPad;
measuredWt.value = resolveSize(maxWt, wm);
measuredHt.value = resolveSize(maxHt, hm);
}
void ScrollView::doLayout(bool , int ,int ,int ,int ){
int childL = mLeftPad;
int childT = mTopPad;
auto child = target;
child->layout(childL, childT,
childL + child->measuredWt.value,
childT + child->measuredHt.value);
child = uparrow;
childL = right - left - child->measuredWt.value;
child->layout(childL, childT,
childL + child->measuredWt.value,
childT + child->measuredHt.value);
child = downarrow;
childT = bottom -top - child->measuredHt.value;
child->layout(childL, childT,
childL + child->measuredWt.value,
childT + child->measuredHt.value);
}
std::unique_ptr<ScrollView> ScrollView::make(ViewPtr v){
auto tv = std::make_unique<ScrollView>(v);
return tv;
}