forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.ios.ts
More file actions
242 lines (186 loc) · 8.57 KB
/
view.ios.ts
File metadata and controls
242 lines (186 loc) · 8.57 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
import viewCommon = require("ui/core/view-common");
import trace = require("trace");
import utils = require("utils/utils");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
// merge the exports of the common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(viewCommon, exports);
function onIdPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var view = <View>data.object;
if (!view._nativeView) {
return;
}
view._nativeView.accessibilityIdentifier = data.newValue;
}
(<proxy.PropertyMetadata>viewCommon.View.idProperty.metadata).onSetNativeValue = onIdPropertyChanged;
function onIsEnabledPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var view = <View>data.object;
if (!view._nativeView) {
return;
}
if (view._nativeView instanceof UIControl) {
(<UIControl>view._nativeView).enabled = data.newValue;
}
}
(<proxy.PropertyMetadata>viewCommon.View.isEnabledProperty.metadata).onSetNativeValue = onIsEnabledPropertyChanged;
function onIsUserInteractionEnabledPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var view = <View>data.object;
if (!view._nativeView) {
return;
}
view._nativeView.userInteractionEnabled = data.newValue;
}
(<proxy.PropertyMetadata>viewCommon.View.isUserInteractionEnabledProperty.metadata).onSetNativeValue = onIsUserInteractionEnabledPropertyChanged;
var PFLAG_FORCE_LAYOUT = 1;
var PFLAG_MEASURED_DIMENSION_SET = 1 << 1;
var PFLAG_LAYOUT_REQUIRED = 1 << 2;
export class View extends viewCommon.View {
private _privateFlags: number;
constructor() {
super();
this._privateFlags = PFLAG_LAYOUT_REQUIRED | PFLAG_FORCE_LAYOUT;
}
public _addViewCore(view: viewCommon.View) {
super._addViewCore(view);
this.requestLayout();
}
public _removeViewCore(view: viewCommon.View) {
super._removeViewCore(view);
// TODO: Detach from the context?
view._onDetached();
this.requestLayout();
}
public onLoaded() {
super.onLoaded();
// TODO: It is very late to work with options here in the onLoaded method.
// We should not do anything that affects UI AFTER the widget has been loaded.
utils.copyFrom(this._options, this);
delete this._options;
// We do not need to call this in iOS, since the native instance is created immediately
// and any props that you set on our instance are immediately synced onto the native one.
// _syncNativeProperties makes sense for Android only, where widgets are created later.
// this._syncNativeProperties();
}
get _nativeView(): UIView {
return this.ios;
}
public _prepareNativeView(view: UIView) {
// For UILabel and UIImage.
view.userInteractionEnabled = true;
}
public requestLayout(): void {
super.requestLayout();
this._privateFlags |= PFLAG_FORCE_LAYOUT;
if (this.parent) {
this.parent.requestLayout();
}
}
public measure(widthMeasureSpec: number, heightMeasureSpec: number): void {
var measureSpecsChanged = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec);
var forceLayout = (this._privateFlags & PFLAG_FORCE_LAYOUT) === PFLAG_FORCE_LAYOUT;
if (forceLayout || measureSpecsChanged) {
// first clears the measured dimension flag
this._privateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
// measure ourselves, this should set the measured dimension flag back
this.onMeasure(widthMeasureSpec, heightMeasureSpec);
this._privateFlags |= PFLAG_LAYOUT_REQUIRED;
// flag not set, setMeasuredDimension() was not invoked, we raise
// an exception to warn the developer
if ((this._privateFlags & PFLAG_MEASURED_DIMENSION_SET) !== PFLAG_MEASURED_DIMENSION_SET) {
throw new Error("onMeasure() did not set the measured dimension by calling setMeasuredDimension()");
}
}
}
public layout(left: number, top: number, right: number, bottom: number): void {
var changed: boolean = this._setCurrentLayoutBounds(left, top, right, bottom);
this.layoutNativeView(left, top, right, bottom);
if (changed || (this._privateFlags & PFLAG_LAYOUT_REQUIRED) === PFLAG_LAYOUT_REQUIRED) {
this.onLayout(left, top, right, bottom);
this._privateFlags &= ~PFLAG_LAYOUT_REQUIRED;
}
this._privateFlags &= ~PFLAG_FORCE_LAYOUT;
}
public setMeasuredDimension(measuredWidth: number, measuredHeight: number): void {
super.setMeasuredDimension(measuredWidth, measuredHeight);
this._privateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
var view = this._nativeView;
if (view) {
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
if (widthMode === utils.layout.UNSPECIFIED) {
width = Number.POSITIVE_INFINITY;
}
if (heightMode === utils.layout.UNSPECIFIED) {
height = Number.POSITIVE_INFINITY;
}
trace.write(this + " :onMeasure: " + utils.layout.getMode(widthMode) + " " + width + ", " + utils.layout.getMode(heightMode) + " " + height, trace.categories.Layout);
var nativeSize = view.sizeThatFits(CGSizeMake(width, height));
var measureWidth = Math.max(nativeSize.width, this.minWidth);
var measureHeight = Math.max(nativeSize.height, this.minHeight);
var widthAndState = View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
trace.write(this + " :onLayout: " + left + ", " + top + ", " + (right - left) + ", " + (bottom - top), trace.categories.Layout);
}
public layoutNativeView(left: number, top: number, right: number, bottom: number): void {
var frame = CGRectMake(left, top, right - left, bottom - top);
if (!CGRectEqualToRect(this._nativeView.frame, frame)) {
trace.write(this + ", Native setFrame: " + NSStringFromCGRect(frame), trace.categories.Layout);
this._nativeView.frame = frame;
}
}
public _updateLayout() {
var oldBounds = this._getCurrentLayoutBounds();
this.layoutNativeView(oldBounds.left, oldBounds.top, oldBounds.right, oldBounds.bottom);
}
public focus(): boolean {
if (this.ios) {
return this.ios.becomeFirstResponder();
}
return false;
}
}
export class CustomLayoutView extends View {
private _view: UIView;
constructor() {
super();
this._view = new UIView();
this._view.autoresizesSubviews = false;
}
get ios(): UIView {
return this._view;
}
get _nativeView(): UIView {
return this._view;
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
// Don't call super because it will trigger measure again.
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
trace.write(this + " :onMeasure: " + utils.layout.getMode(widthMode) + " " + width + ", " + utils.layout.getMode(heightMode) + " " + height, trace.categories.Layout);
}
public _addViewToNativeVisualTree(child: View): boolean {
super._addViewToNativeVisualTree(child);
if (this._nativeView && child._nativeView) {
this._nativeView.addSubview(child._nativeView);
return true;
}
return false;
}
public _removeViewFromNativeVisualTree(child: View): void {
super._removeViewFromNativeVisualTree(child);
if (child._nativeView) {
child._nativeView.removeFromSuperview();
}
}
}