mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
190 lines
6.4 KiB
TypeScript
190 lines
6.4 KiB
TypeScript
import {
|
|
ImageSource, ImageAsset, ImageBase, stretchProperty, imageSourceProperty, srcProperty, tintColorProperty, Color,
|
|
isDataURI, isFontIconURI, isFileOrResourcePath, RESOURCE_PREFIX, Length
|
|
} from "./image-common";
|
|
import { knownFolders } from "../../file-system";
|
|
|
|
import * as platform from "../../platform";
|
|
export * from "./image-common";
|
|
|
|
const FILE_PREFIX = "file:///";
|
|
const ASYNC = "async";
|
|
|
|
let AndroidImageView: typeof org.nativescript.widgets.ImageView;
|
|
|
|
interface ImageLoadedListener {
|
|
new(owner: Image): org.nativescript.widgets.image.Worker.OnImageLoadedListener;
|
|
}
|
|
|
|
let ImageLoadedListener: ImageLoadedListener;
|
|
function initializeImageLoadedListener() {
|
|
if (ImageLoadedListener) {
|
|
return;
|
|
}
|
|
|
|
@Interfaces([org.nativescript.widgets.image.Worker.OnImageLoadedListener])
|
|
class ImageLoadedListenerImpl extends java.lang.Object implements org.nativescript.widgets.image.Worker.OnImageLoadedListener {
|
|
constructor(public owner: Image) {
|
|
super();
|
|
|
|
return global.__native(this);
|
|
}
|
|
|
|
onImageLoaded(success: boolean): void {
|
|
const owner = this.owner;
|
|
if (owner) {
|
|
owner.isLoading = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
ImageLoadedListener = ImageLoadedListenerImpl;
|
|
}
|
|
|
|
export class Image extends ImageBase {
|
|
nativeViewProtected: org.nativescript.widgets.ImageView;
|
|
|
|
public useCache = true;
|
|
|
|
public createNativeView() {
|
|
if (!AndroidImageView) {
|
|
AndroidImageView = org.nativescript.widgets.ImageView;
|
|
}
|
|
|
|
return new AndroidImageView(this._context);
|
|
}
|
|
|
|
public initNativeView(): void {
|
|
super.initNativeView();
|
|
initializeImageLoadedListener();
|
|
const nativeView = this.nativeViewProtected;
|
|
const listener = new ImageLoadedListener(this);
|
|
nativeView.setImageLoadedListener(listener);
|
|
(<any>nativeView).listener = listener;
|
|
}
|
|
|
|
public disposeNativeView() {
|
|
(<any>this.nativeViewProtected).listener.owner = null;
|
|
super.disposeNativeView();
|
|
}
|
|
|
|
public resetNativeView(): void {
|
|
super.resetNativeView();
|
|
this.nativeViewProtected.setImageMatrix(new android.graphics.Matrix());
|
|
}
|
|
|
|
public _createImageSourceFromSrc(value: string | ImageSource | ImageAsset) {
|
|
const imageView = this.nativeViewProtected;
|
|
if (!imageView) {
|
|
return;
|
|
}
|
|
|
|
if (!value) {
|
|
imageView.setUri(null, 0, 0, false, false, true);
|
|
|
|
return;
|
|
}
|
|
|
|
let screen = platform.screen.mainScreen;
|
|
|
|
let decodeWidth = Math.min(Length.toDevicePixels(this.decodeWidth, 0), screen.widthPixels);
|
|
let decodeHeight = Math.min(Length.toDevicePixels(this.decodeHeight, 0), screen.heightPixels);
|
|
let keepAspectRatio = this._calculateKeepAspectRatio();
|
|
if (value instanceof ImageAsset) {
|
|
if (value.options) {
|
|
decodeWidth = value.options.width || decodeWidth;
|
|
decodeHeight = value.options.height || decodeHeight;
|
|
keepAspectRatio = !!value.options.keepAspectRatio;
|
|
}
|
|
|
|
// handle assets as file paths natively
|
|
value = value.android;
|
|
}
|
|
|
|
const async = this.loadMode === ASYNC;
|
|
if (typeof value === "string" || value instanceof String) {
|
|
value = value.trim();
|
|
this.isLoading = true;
|
|
|
|
if (isFontIconURI(value) || isDataURI(value)) {
|
|
// TODO: Check with runtime what should we do in case of base64 string.
|
|
super._createImageSourceFromSrc(value);
|
|
} else if (isFileOrResourcePath(value)) {
|
|
if (value.indexOf(RESOURCE_PREFIX) === 0) {
|
|
imageView.setUri(value, decodeWidth, decodeHeight, keepAspectRatio, this.useCache, async);
|
|
} else {
|
|
let fileName = value;
|
|
if (fileName.indexOf("~/") === 0) {
|
|
fileName = knownFolders.currentApp().path + "/" + fileName.replace("~/", "");
|
|
}
|
|
|
|
imageView.setUri(FILE_PREFIX + fileName, decodeWidth, decodeHeight, keepAspectRatio, this.useCache, async);
|
|
}
|
|
} else {
|
|
// For backwards compatibility http always use async loading.
|
|
imageView.setUri(value, decodeWidth, decodeHeight, keepAspectRatio, this.useCache, true);
|
|
}
|
|
} else {
|
|
super._createImageSourceFromSrc(value);
|
|
}
|
|
}
|
|
|
|
private _calculateKeepAspectRatio(): boolean {
|
|
return this.stretch === "fill" ? false : true;
|
|
}
|
|
|
|
[stretchProperty.getDefault](): "aspectFit" {
|
|
return "aspectFit";
|
|
}
|
|
[stretchProperty.setNative](value: "none" | "aspectFill" | "aspectFit" | "fill") {
|
|
switch (value) {
|
|
case "aspectFit":
|
|
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER);
|
|
break;
|
|
case "aspectFill":
|
|
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);
|
|
break;
|
|
case "fill":
|
|
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
|
|
break;
|
|
case "none":
|
|
default:
|
|
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.MATRIX);
|
|
break;
|
|
}
|
|
}
|
|
|
|
[tintColorProperty.getDefault](): Color {
|
|
return undefined;
|
|
}
|
|
[tintColorProperty.setNative](value: Color) {
|
|
if (value === undefined) {
|
|
this.nativeViewProtected.clearColorFilter();
|
|
} else {
|
|
this.nativeViewProtected.setColorFilter(value.android);
|
|
}
|
|
}
|
|
|
|
[imageSourceProperty.getDefault](): ImageSource {
|
|
return undefined;
|
|
}
|
|
[imageSourceProperty.setNative](value: ImageSource) {
|
|
const nativeView = this.nativeViewProtected;
|
|
if (value && value.android) {
|
|
const rotation = value.rotationAngle ? value.rotationAngle : 0;
|
|
nativeView.setRotationAngle(rotation);
|
|
nativeView.setImageBitmap(value.android);
|
|
} else {
|
|
nativeView.setRotationAngle(0);
|
|
nativeView.setImageBitmap(null);
|
|
}
|
|
}
|
|
|
|
[srcProperty.getDefault](): any {
|
|
return undefined;
|
|
}
|
|
[srcProperty.setNative](value: any) {
|
|
this._createImageSourceFromSrc(value);
|
|
}
|
|
}
|