mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
iOS layout positioning now respects native properties like automaticallyAdjustsScrollViewInsets, edgesForExtendedLayout, extendedLayoutIncludesOpaqueBars, navigationBar.translucent, tabBar.translucent Removed frame-tests.ios.ts - those tests are now invalid Added new layout tests inside page-tests.ios.ts Commented few asserts in scroll-view-tests View now expose ios namespace with layoutView method and UILayoutViewController used by page, tab-view and application module ViewBase now expose viewController property that should be set from all widgets that are using viewcontrollers internally (like Page, Frame, TabView) ViewBase now sets ios property to either the view returned from createNativeView or to nativeViewProptected fragment.transitions now use animation/transition start to add fragments to waitingQueue. Before we did it manually in navigate/goBack. This way we can reuse the fragment.transition when calling showDialog. Also when animation/transition ends we check the animation/transition to see if this fragment should be set as current. Frame expose new loadViewFromEntry method (to load a view from URI) Frame navigation happens once frame is loaded Frame now supports Page as a child in XML Fixed GridLayout row, rowSpan, column, columnSpan properties type Fixed bug in GridLayout where add/remove of columns/rows won't update the internal state of the grid (backport from android when GridLayout is recycled) ListView will no longer invalidate layout when cell is removed Fixed bug in ScrollView ios where effectiveMinWidth/Height was multiplied to density (it is already on device pixels so no need to multiply) TabView android now calls loaded only on the selected child (not all) Core refactoring
155 lines
6.1 KiB
TypeScript
155 lines
6.1 KiB
TypeScript
import {
|
|
ImageSource, ImageBase, stretchProperty, imageSourceProperty, tintColorProperty, srcProperty, layout, Color,
|
|
traceEnabled, traceWrite, traceCategories
|
|
} from "./image-common";
|
|
|
|
export * from "./image-common";
|
|
|
|
export class Image extends ImageBase {
|
|
nativeViewProtected: UIImageView;
|
|
private _imageSourceAffectsLayout: boolean = true;
|
|
private _templateImageWasCreated: boolean;
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
//TODO: Think of unified way of setting all the default values.
|
|
const imageView = UIImageView.new();
|
|
imageView.contentMode = UIViewContentMode.ScaleAspectFit;
|
|
imageView.userInteractionEnabled = true;
|
|
this.nativeViewProtected = imageView;
|
|
this._setNativeClipToBounds();
|
|
}
|
|
|
|
private setTintColor(value: Color) {
|
|
if (value && this.nativeViewProtected.image && !this._templateImageWasCreated) {
|
|
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
|
|
this._templateImageWasCreated = true;
|
|
} else if (this.nativeViewProtected.image && this._templateImageWasCreated) {
|
|
this._templateImageWasCreated = false;
|
|
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.Automatic);
|
|
}
|
|
this.nativeViewProtected.tintColor = value ? value.ios : null;
|
|
}
|
|
|
|
public _setNativeImage(nativeImage: UIImage) {
|
|
this.nativeViewProtected.image = nativeImage;
|
|
this._templateImageWasCreated = false;
|
|
this.setTintColor(this.style.tintColor);
|
|
|
|
if (this._imageSourceAffectsLayout) {
|
|
this.requestLayout();
|
|
}
|
|
}
|
|
|
|
_setNativeClipToBounds() {
|
|
// Always set clipsToBounds for images
|
|
this.nativeViewProtected.clipsToBounds = true;
|
|
}
|
|
|
|
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
|
// We don't call super because we measure native view with specific size.
|
|
const width = layout.getMeasureSpecSize(widthMeasureSpec);
|
|
const widthMode = layout.getMeasureSpecMode(widthMeasureSpec);
|
|
|
|
const height = layout.getMeasureSpecSize(heightMeasureSpec);
|
|
const heightMode = layout.getMeasureSpecMode(heightMeasureSpec);
|
|
|
|
const nativeWidth = this.imageSource ? layout.toDevicePixels(this.imageSource.width) : 0;
|
|
const nativeHeight = this.imageSource ? layout.toDevicePixels(this.imageSource.height) : 0;
|
|
|
|
let measureWidth = Math.max(nativeWidth, this.effectiveMinWidth);
|
|
let measureHeight = Math.max(nativeHeight, this.effectiveMinHeight);
|
|
|
|
const finiteWidth: boolean = widthMode !== layout.UNSPECIFIED;
|
|
const finiteHeight: boolean = heightMode !== layout.UNSPECIFIED;
|
|
|
|
this._imageSourceAffectsLayout = widthMode !== layout.EXACTLY || heightMode !== layout.EXACTLY;
|
|
|
|
if (nativeWidth !== 0 && nativeHeight !== 0 && (finiteWidth || finiteHeight)) {
|
|
const scale = Image.computeScaleFactor(width, height, finiteWidth, finiteHeight, nativeWidth, nativeHeight, this.stretch);
|
|
const resultW = Math.round(nativeWidth * scale.width);
|
|
const resultH = Math.round(nativeHeight * scale.height);
|
|
|
|
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
|
|
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;
|
|
|
|
if (traceEnabled()) {
|
|
traceWrite("Image stretch: " + this.stretch +
|
|
", nativeWidth: " + nativeWidth +
|
|
", nativeHeight: " + nativeHeight, traceCategories.Layout);
|
|
}
|
|
}
|
|
|
|
const widthAndState = Image.resolveSizeAndState(measureWidth, width, widthMode, 0);
|
|
const heightAndState = Image.resolveSizeAndState(measureHeight, height, heightMode, 0);
|
|
|
|
this.setMeasuredDimension(widthAndState, heightAndState);
|
|
}
|
|
|
|
private static computeScaleFactor(measureWidth: number, measureHeight: number, widthIsFinite: boolean, heightIsFinite: boolean, nativeWidth: number, nativeHeight: number, imageStretch: string): { width: number; height: number } {
|
|
let scaleW = 1;
|
|
let scaleH = 1;
|
|
|
|
if ((imageStretch === "aspectFill" || imageStretch === "aspectFit" || imageStretch === "fill") &&
|
|
(widthIsFinite || heightIsFinite)) {
|
|
|
|
scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0;
|
|
scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0;
|
|
|
|
if (!widthIsFinite) {
|
|
scaleW = scaleH;
|
|
}
|
|
else if (!heightIsFinite) {
|
|
scaleH = scaleW;
|
|
}
|
|
else {
|
|
// No infinite dimensions.
|
|
switch (imageStretch) {
|
|
case "aspectFit":
|
|
scaleH = scaleW < scaleH ? scaleW : scaleH;
|
|
scaleW = scaleH;
|
|
break;
|
|
case "aspectFill":
|
|
scaleH = scaleW > scaleH ? scaleW : scaleH;
|
|
scaleW = scaleH;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return { width: scaleW, height: scaleH };
|
|
}
|
|
|
|
[stretchProperty.setNative](value: "none" | "aspectFill" | "aspectFit" | "fill") {
|
|
switch (value) {
|
|
case "aspectFit":
|
|
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFit;
|
|
break;
|
|
|
|
case "aspectFill":
|
|
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFill;
|
|
break;
|
|
|
|
case "fill":
|
|
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleToFill;
|
|
break;
|
|
|
|
case "none":
|
|
default:
|
|
this.nativeViewProtected.contentMode = UIViewContentMode.TopLeft;
|
|
break;
|
|
}
|
|
}
|
|
|
|
[tintColorProperty.setNative](value: Color) {
|
|
this.setTintColor(value);
|
|
}
|
|
|
|
[imageSourceProperty.setNative](value: ImageSource) {
|
|
this._setNativeImage(value ? value.ios : null);
|
|
}
|
|
|
|
[srcProperty.setNative](value: any) {
|
|
this._createImageSourceFromSrc(value);
|
|
}
|
|
} |