Files
Alexander Vakrilov 23757e5dfc Enable recycling of nativeView 2 (#4467)
* enable recycling of nativeView

* backgroundInternal is reset if setting new value leads to background.isEmpty() == true.

* android background.getDefault always return copy of the background. Now all controls that mutate the background can be reset to initial state (e.g. Button & ActionBar)
passing resources to copied background so it respect density.
fix properties initNativeView

* reset padding when backgroundInternal is reset.

* Fix text reset
Fix padding reset

* fix tsc errors

* fix ugly text rendering.

* Add unit tests for recycling native views
Fix several issues that came from the above tests
Fix maxLength property missing a converter callback
Remove old files

* Remove old files

* Revert backgroundInternal setter

* change the order of tests so that appium can work again

* Remove suggestion on every TextView & TextField init (strangely it is enabled after view is recycled....)

* Fix function to get parent layout if specified

* Button stateListAnimator restored when button is recycled
zIndex defaultValue is now undefined instead of NaN

* revert zIndex.setNative to always clear stateListAnimator because it was breaking one UI test (setting value=0 was returning the previous stateListAnimator)

* fix search-bar backgound-color recycling

* Fix alignments setters

* Fix imageView recycling
Fix button recycling
Fix edit-text recycling
resetNativeView is called only if recycleNativeView flag is true

* Fix incorrect merge

* Fix text-view & text-field textTransform

* Fix EditText text reset

* Fix runtime crash on ARM emulator API 21

* Fix text-base minHeight. maxHeight reset
Fix reset of isUserInteractionEnabled
2017-06-29 18:01:22 +03:00

153 lines
5.3 KiB
TypeScript

import { ScrollEventData } from ".";
import { View, layout, ScrollViewBase } from "./scroll-view-common";
export * from "./scroll-view-common";
class UIScrollViewDelegateImpl extends NSObject implements UIScrollViewDelegate {
private _owner: WeakRef<ScrollView>;
public static initWithOwner(owner: WeakRef<ScrollView>): UIScrollViewDelegateImpl {
let impl = <UIScrollViewDelegateImpl>UIScrollViewDelegateImpl.new();
impl._owner = owner;
return impl;
}
public scrollViewDidScroll(sv: UIScrollView): void {
let owner = this._owner.get();
if (owner) {
owner.notify(<ScrollEventData>{
object: owner,
eventName: ScrollViewBase.scrollEvent,
scrollX: owner.horizontalOffset,
scrollY: owner.verticalOffset
});
}
}
public static ObjCProtocols = [UIScrollViewDelegate];
}
export class ScrollView extends ScrollViewBase {
public nativeView: UIScrollView;
private _contentMeasuredWidth: number = 0;
private _contentMeasuredHeight: number = 0;
private _delegate: UIScrollViewDelegateImpl;
constructor() {
super();
this.nativeView = UIScrollView.new();
this._setNativeClipToBounds();
}
_setNativeClipToBounds() {
// Always set clipsToBounds for scroll-view
this.nativeView.clipsToBounds = true;
}
protected attachNative() {
this._delegate = UIScrollViewDelegateImpl.initWithOwner(new WeakRef(this));
this.nativeView.delegate = this._delegate;
}
protected dettachNative() {
this.nativeView.delegate = null;
}
get horizontalOffset(): number {
return this.nativeView.contentOffset.x;
}
get verticalOffset(): number {
return this.nativeView.contentOffset.y;
}
get scrollableWidth(): number {
if (this.orientation !== "horizontal") {
return 0;
}
return Math.max(0, this.nativeView.contentSize.width - this.nativeView.bounds.size.width);
}
get scrollableHeight(): number {
if (this.orientation !== "vertical") {
return 0;
}
return Math.max(0, this.nativeView.contentSize.height - this.nativeView.bounds.size.height);
}
get ios(): UIView {
return this.nativeView;
}
public scrollToVerticalOffset(value: number, animated: boolean) {
if (this.orientation === "vertical") {
const bounds = this.nativeView.bounds.size;
this.nativeView.scrollRectToVisibleAnimated(CGRectMake(0, value, bounds.width, bounds.height), animated);
}
}
public scrollToHorizontalOffset(value: number, animated: boolean) {
if (this.orientation === "horizontal") {
const bounds = this.nativeView.bounds.size;
this.nativeView.scrollRectToVisibleAnimated(CGRectMake(value, 0, bounds.width, bounds.height), animated);
}
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
// Don't call measure because it will measure content twice.
const width = layout.getMeasureSpecSize(widthMeasureSpec);
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
const height = layout.getMeasureSpecSize(heightMeasureSpec);
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
const density = layout.getDisplayDensity();
const child = this.layoutView;
if (!child) {
this._contentMeasuredWidth = this.effectiveMinWidth * density;
this._contentMeasuredHeight = this.effectiveMinHeight * density;
}
else {
let childSize: { measuredWidth: number; measuredHeight: number };
if (this.orientation === "vertical") {
childSize = View.measureChild(this, child, widthMeasureSpec, layout.makeMeasureSpec(0, layout.UNSPECIFIED));
}
else {
childSize = View.measureChild(this, child, layout.makeMeasureSpec(0, layout.UNSPECIFIED), heightMeasureSpec);
}
let w = layout.toDeviceIndependentPixels(childSize.measuredWidth);
let h = layout.toDeviceIndependentPixels(childSize.measuredHeight);
this.nativeView.contentSize = CGSizeMake(w, h);
this._contentMeasuredWidth = Math.max(childSize.measuredWidth, this.effectiveMinWidth * density);
this._contentMeasuredHeight = Math.max(childSize.measuredHeight, this.effectiveMinHeight * density);
}
const widthAndState = View.resolveSizeAndState(this._contentMeasuredWidth, width, widthMode, 0);
const heightAndState = View.resolveSizeAndState(this._contentMeasuredHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
const width = (right - left);
const height = (bottom - top);
if (this.orientation === "horizontal") {
View.layoutChild(this, this.layoutView, 0, 0, Math.max(this._contentMeasuredWidth, width), height);
}
else {
View.layoutChild(this, this.layoutView, 0, 0, width, Math.max(this._contentMeasuredHeight, height));
}
}
public _onOrientationChanged() {
// NOOP
}
}
ScrollView.prototype.recycleNativeView = true;