Repository navigation
Expand file tree
/
Copy pathscroll-view.android.ts
More file actions
262 lines (211 loc) · 8.69 KB
/
Copy pathscroll-view.android.ts
File metadata and controls
262 lines (211 loc) · 8.69 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
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
257
258
259
260
261
262
import dependencyObservable = require("ui/core/dependency-observable");
import view = require("ui/core/view");
import definition = require("ui/scroll-view");
import contentView = require("ui/content-view");
import common = require("ui/scroll-view/scroll-view-common");
import utils = require("utils/utils");
import enums = require("ui/enums");
// merge the exports of the common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);
var OWNER = "_owner";
var STATE = "_scrollViewState";
common.orientationProperty.metadata.onValueChanged = function scrollViewOrientationChanged(data: dependencyObservable.PropertyChangeData) {
(<ScrollView>data.object)._onOrientationChanged(data.oldValue, data.newValue);
}
interface ScrollViewState {
scrollX: number;
scrollY: number;
}
function onMeasureScrollView(widthMeasureSpec: number, heightMeasureSpec: number) {
var owner: view.View = this.owner;
owner.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.setMeasuredDimension(owner.getMeasuredWidth(), owner.getMeasuredHeight());
}
function onLayoutScrollView(changed: boolean, left: number, top: number, right: number, bottom: number) {
var owner: view.View = this.owner;
owner.onLayout(left, top, right, bottom);
// Restore scroll state on the first layout after native instance is recreated.
var state: ScrollViewState = owner[STATE];
if (state) {
this.scrollTo(state.scrollX, state.scrollY);
delete owner[STATE];
}
}
function onSaveInstanceStateNative(): android.os.Parcelable {
var state: ScrollViewState = {
scrollX: this.getScrollX(),
scrollY: this.getScrollY()
}
this.owner[STATE] = state;
return this.super.onSaveInstanceState();
}
class NativeVerticalScrollView extends android.widget.ScrollView {
constructor(ctx) {
super(ctx);
return global.__native(this);
}
get owner() {
return this[OWNER];
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
onMeasureScrollView.apply(this, [widthMeasureSpec, heightMeasureSpec]);
}
public onLayout(changed: boolean, left: number, top: number, right: number, bottom: number) {
onLayoutScrollView.apply(this, [changed, left, top, right, bottom]);
}
public onSaveInstanceState() {
onSaveInstanceStateNative.apply(this, []);
}
};
class NativeHorizontalScrollView extends android.widget.HorizontalScrollView {
constructor(ctx) {
super(ctx);
return global.__native(this);
}
get owner() {
return this[OWNER];
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
onMeasureScrollView.apply(this, [widthMeasureSpec, heightMeasureSpec]);
}
public onLayout(changed: boolean, left: number, top: number, right: number, bottom: number) {
onLayoutScrollView.apply(this, [changed, left, top, right, bottom]);
}
public onSaveInstanceState() {
onSaveInstanceStateNative.apply(this, []);
}
};
export class ScrollView extends contentView.ContentView implements definition.ScrollView {
private _android: android.widget.FrameLayout;
private _contentMeasuredWidth: number = 0;
private _contentMeasuredHeight: number = 0;
private _scrollableLength: number = 0;
private _androidViewId: number;
get android(): android.view.ViewGroup {
return this._android;
}
get _nativeView(): android.view.ViewGroup {
return this._android;
}
get orientation(): string {
return this._getValue(common.orientationProperty);
}
set orientation(value: string) {
this._setValue(common.orientationProperty, value);
}
get horizontalOffset(): number {
if (!this._android) {
return 0;
}
return this._android.getScrollX() / utils.layout.getDisplayDensity();
}
get verticalOffset(): number {
if (!this._android) {
return 0;
}
return this._android.getScrollY() / utils.layout.getDisplayDensity();
}
get scrollableWidth(): number {
if (!this._android || this.orientation !== enums.Orientation.horizontal) {
return 0;
}
return this._scrollableLength;
}
get scrollableHeight(): number {
if (!this._android || this.orientation !== enums.Orientation.vertical) {
return 0;
}
return this._scrollableLength;
}
public scrollToVerticalOffset(value: number, animated: boolean) {
if (this._android && this.orientation === enums.Orientation.vertical) {
value *= utils.layout.getDisplayDensity();
var scrollView: android.widget.ScrollView = (<android.widget.ScrollView>this._android);
if (animated) {
scrollView.smoothScrollTo(0, value);
}
else {
scrollView.scrollTo(0, value);
}
}
}
public scrollToHorizontalOffset(value: number, animated: boolean) {
if (this._android && this.orientation === enums.Orientation.horizontal) {
value *= utils.layout.getDisplayDensity();
var scrollView: android.widget.HorizontalScrollView = (<android.widget.HorizontalScrollView>this._android);
if (animated) {
scrollView.smoothScrollTo(value, 0);
}
else {
scrollView.scrollTo(value, 0);
}
}
}
public _createUI() {
if (this.orientation === enums.Orientation.horizontal) {
this._android = new NativeHorizontalScrollView(this._context);
}
else {
this._android = new NativeVerticalScrollView(this._context);
}
if (!this._androidViewId) {
this._androidViewId = android.view.View.generateViewId();
}
this._android.setId(this._androidViewId);
this._android[OWNER] = this;
}
public _onOrientationChanged(oldValue: string, newValue: string) {
if (this._android) {
var parent = this.parent;
if (parent) {
parent._removeView(this);
}
if (parent) {
parent._addView(this);
}
}
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
// Don't call measure because it will measure content twice.
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
var density = utils.layout.getDisplayDensity();
var child = this.content
if (!child) {
this._scrollableLength = 0;
this._contentMeasuredWidth = this.minWidth * density;
this._contentMeasuredHeight = this.minHeight * density;
}
else {
var childSize: { measuredWidth: number; measuredHeight: number };
if (this.orientation === enums.Orientation.vertical) {
childSize = view.View.measureChild(this, child, widthMeasureSpec, utils.layout.makeMeasureSpec(0, utils.layout.UNSPECIFIED));
}
else {
childSize = view.View.measureChild(this, child, utils.layout.makeMeasureSpec(0, utils.layout.UNSPECIFIED), heightMeasureSpec);
}
this._contentMeasuredWidth = Math.max(childSize.measuredWidth, this.minWidth * density);
this._contentMeasuredHeight = Math.max(childSize.measuredHeight, this.minHeight * density);
}
var widthAndState = view.View.resolveSizeAndState(this._contentMeasuredWidth, width, widthMode, 0);
var heightAndState = view.View.resolveSizeAndState(this._contentMeasuredHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
var width = (right - left);
var height = (bottom - top);
if (this.orientation === enums.Orientation.horizontal) {
this._scrollableLength = this._contentMeasuredWidth - width;
view.View.layoutChild(this, this.content, 0, 0, Math.max(this._contentMeasuredWidth, width), height);
}
else {
this._scrollableLength = this._contentMeasuredHeight - height;
view.View.layoutChild(this, this.content, 0, 0, width, Math.max(this._contentMeasuredHeight, height));
}
this._scrollableLength /= utils.layout.getDisplayDensity();
this._scrollableLength = Math.max(0, this._scrollableLength);
}
}