mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-19 06:10:56 +08:00
Merge pull request #4167 from NativeScript/perf-flexbox-example
Improvements after profiling the android flexbox example
This commit is contained in:
@ -24,26 +24,33 @@ const contextKey = "context";
|
|||||||
const paramsRegex = /\[\s*(['"])*(\w*)\1\s*\]/;
|
const paramsRegex = /\[\s*(['"])*(\w*)\1\s*\]/;
|
||||||
const bc = bindingConstants;
|
const bc = bindingConstants;
|
||||||
const emptyArray = [];
|
const emptyArray = [];
|
||||||
|
const propertiesCache = {};
|
||||||
|
|
||||||
function getProperties(property: string): Array<string> {
|
function getProperties(property: string): Array<string> {
|
||||||
let result: Array<string> = emptyArray;
|
if (!property) {
|
||||||
if (property) {
|
return emptyArray;
|
||||||
// first replace all '$parents[..]' with a safe string
|
|
||||||
// second removes all ] since they are not important for property access and not needed
|
|
||||||
// then split properties either on '.' or '['
|
|
||||||
const parentsMatches = property.match(parentsRegex);
|
|
||||||
result = property.replace(parentsRegex, "parentsMatch")
|
|
||||||
.replace(/\]/g, "")
|
|
||||||
.split(/\.|\[/);
|
|
||||||
|
|
||||||
let parentsMatchesCounter = 0;
|
|
||||||
for (let i = 0, resultLength = result.length; i < resultLength; i++) {
|
|
||||||
if (result[i] === "parentsMatch") {
|
|
||||||
result[i] = parentsMatches[parentsMatchesCounter++];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let result: Array<string> = propertiesCache[property];
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// first replace all '$parents[..]' with a safe string
|
||||||
|
// second removes all ] since they are not important for property access and not needed
|
||||||
|
// then split properties either on '.' or '['
|
||||||
|
const parentsMatches = property.match(parentsRegex);
|
||||||
|
result = property.replace(parentsRegex, "parentsMatch")
|
||||||
|
.replace(/\]/g, "")
|
||||||
|
.split(/\.|\[/);
|
||||||
|
|
||||||
|
let parentsMatchesCounter = 0;
|
||||||
|
for (let i = 0, resultLength = result.length; i < resultLength; i++) {
|
||||||
|
if (result[i] === "parentsMatch") {
|
||||||
|
result[i] = parentsMatches[parentsMatchesCounter++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
propertiesCache[property] = result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,13 +2,15 @@
|
|||||||
ImageSource, ImageBase, stretchProperty, imageSourceProperty, srcProperty, tintColorProperty, Color,
|
ImageSource, ImageBase, stretchProperty, imageSourceProperty, srcProperty, tintColorProperty, Color,
|
||||||
isDataURI, isFileOrResourcePath, RESOURCE_PREFIX
|
isDataURI, isFileOrResourcePath, RESOURCE_PREFIX
|
||||||
} from "./image-common";
|
} from "./image-common";
|
||||||
import { path, knownFolders } from "../../file-system";
|
import { knownFolders } from "../../file-system";
|
||||||
|
|
||||||
export * from "./image-common";
|
export * from "./image-common";
|
||||||
|
|
||||||
const FILE_PREFIX = "file:///";
|
const FILE_PREFIX = "file:///";
|
||||||
const ASYNC = "async";
|
const ASYNC = "async";
|
||||||
|
|
||||||
|
let AndroidImageView: typeof org.nativescript.widgets.ImageView;
|
||||||
|
|
||||||
interface ImageLoadedListener {
|
interface ImageLoadedListener {
|
||||||
new (owner: Image): org.nativescript.widgets.image.Worker.OnImageLoadedListener;
|
new (owner: Image): org.nativescript.widgets.image.Worker.OnImageLoadedListener;
|
||||||
}
|
}
|
||||||
@ -45,9 +47,12 @@ export class Image extends ImageBase {
|
|||||||
public useCache = true;
|
public useCache = true;
|
||||||
|
|
||||||
public createNativeView() {
|
public createNativeView() {
|
||||||
|
if (!AndroidImageView) {
|
||||||
|
AndroidImageView = org.nativescript.widgets.ImageView;
|
||||||
|
}
|
||||||
initializeImageLoadedListener();
|
initializeImageLoadedListener();
|
||||||
|
|
||||||
const imageView = new org.nativescript.widgets.ImageView(this._context);
|
const imageView = new AndroidImageView(this._context);
|
||||||
const listener = new ImageLoadedListener(this);
|
const listener = new ImageLoadedListener(this);
|
||||||
imageView.setImageLoadedListener(listener);
|
imageView.setImageLoadedListener(listener);
|
||||||
(<any>imageView).listener = listener;
|
(<any>imageView).listener = listener;
|
||||||
@ -92,7 +97,7 @@ export class Image extends ImageBase {
|
|||||||
} else {
|
} else {
|
||||||
let fileName = value;
|
let fileName = value;
|
||||||
if (fileName.indexOf("~/") === 0) {
|
if (fileName.indexOf("~/") === 0) {
|
||||||
fileName = path.join(knownFolders.currentApp().path, fileName.replace("~/", ""));
|
fileName = knownFolders.currentApp().path + "/" + fileName.replace("~/", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
imageView.setUri(FILE_PREFIX + fileName, this.decodeWidth, this.decodeHeight, this.useCache, async);
|
imageView.setUri(FILE_PREFIX + fileName, this.decodeWidth, this.decodeHeight, this.useCache, async);
|
||||||
|
@ -3,6 +3,8 @@ import { TextBase, WhiteSpace, whiteSpaceProperty } from "../text-base";
|
|||||||
|
|
||||||
export * from "../text-base";
|
export * from "../text-base";
|
||||||
|
|
||||||
|
let TextView: typeof android.widget.TextView;
|
||||||
|
|
||||||
export class Label extends TextBase implements LabelDefinition {
|
export class Label extends TextBase implements LabelDefinition {
|
||||||
nativeView: android.widget.TextView;
|
nativeView: android.widget.TextView;
|
||||||
|
|
||||||
@ -14,7 +16,10 @@ export class Label extends TextBase implements LabelDefinition {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public createNativeView() {
|
public createNativeView() {
|
||||||
return new android.widget.TextView(this._context);
|
if (!TextView) {
|
||||||
|
TextView = android.widget.TextView;
|
||||||
|
}
|
||||||
|
return new TextView(this._context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public initNativeView(): void {
|
public initNativeView(): void {
|
||||||
|
@ -12,11 +12,14 @@ import {
|
|||||||
|
|
||||||
export * from "./flexbox-layout-common";
|
export * from "./flexbox-layout-common";
|
||||||
|
|
||||||
|
let widgetFlexboxLayout: typeof org.nativescript.widgets.FlexboxLayout;
|
||||||
|
let widgetLayoutParams: typeof org.nativescript.widgets.FlexboxLayout.LayoutParams;
|
||||||
|
|
||||||
function makeNativeSetter<T>(setter: (lp: org.nativescript.widgets.FlexboxLayout.LayoutParams, value: T) => void) {
|
function makeNativeSetter<T>(setter: (lp: org.nativescript.widgets.FlexboxLayout.LayoutParams, value: T) => void) {
|
||||||
return function(this: View, value: T) {
|
return function(this: View, value: T) {
|
||||||
const nativeView: android.view.View = this.nativeView;
|
const nativeView: android.view.View = this.nativeView;
|
||||||
const lp = nativeView.getLayoutParams() || new org.nativescript.widgets.FlexboxLayout.LayoutParams();
|
const lp = nativeView.getLayoutParams() || new widgetLayoutParams();
|
||||||
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
|
if (lp instanceof widgetLayoutParams) {
|
||||||
setter(lp, value);
|
setter(lp, value);
|
||||||
nativeView.setLayoutParams(lp);
|
nativeView.setLayoutParams(lp);
|
||||||
}
|
}
|
||||||
@ -80,7 +83,11 @@ export class FlexboxLayout extends FlexboxLayoutBase {
|
|||||||
nativeView: org.nativescript.widgets.FlexboxLayout;
|
nativeView: org.nativescript.widgets.FlexboxLayout;
|
||||||
|
|
||||||
public createNativeView() {
|
public createNativeView() {
|
||||||
return new org.nativescript.widgets.FlexboxLayout(this._context);
|
if (!widgetFlexboxLayout) {
|
||||||
|
widgetFlexboxLayout = org.nativescript.widgets.FlexboxLayout;
|
||||||
|
widgetLayoutParams = widgetFlexboxLayout.LayoutParams;
|
||||||
|
}
|
||||||
|
return new widgetFlexboxLayout(this._context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public disposeNativeView() {
|
public disposeNativeView() {
|
||||||
@ -127,29 +134,32 @@ export class FlexboxLayout extends FlexboxLayoutBase {
|
|||||||
super._updateNativeLayoutParams(child);
|
super._updateNativeLayoutParams(child);
|
||||||
|
|
||||||
const lp = <org.nativescript.widgets.FlexboxLayout.LayoutParams>child.nativeView.getLayoutParams();
|
const lp = <org.nativescript.widgets.FlexboxLayout.LayoutParams>child.nativeView.getLayoutParams();
|
||||||
lp.order = child.order;
|
const style = child.style;
|
||||||
lp.flexGrow = child.flexGrow;
|
lp.order = style.order;
|
||||||
lp.flexShrink = child.flexShrink;
|
lp.flexGrow = style.flexGrow;
|
||||||
lp.wrapBefore = child.flexWrapBefore;
|
lp.flexShrink = style.flexShrink;
|
||||||
lp.alignSelf = alignSelfMap[child.alignSelf];
|
lp.wrapBefore = style.flexWrapBefore;
|
||||||
|
lp.alignSelf = alignSelfMap[style.alignSelf];
|
||||||
child.nativeView.setLayoutParams(lp);
|
child.nativeView.setLayoutParams(lp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public _setChildMinWidthNative(child: View): void {
|
public _setChildMinWidthNative(child: View): void {
|
||||||
child._setMinWidthNative(0);
|
child._setMinWidthNative(0);
|
||||||
const lp = child.nativeView.getLayoutParams();
|
const nativeView = child.nativeView;
|
||||||
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
|
const lp = nativeView.getLayoutParams();
|
||||||
lp.minWidth = Length.toDevicePixels(child.minWidth, 0);
|
if (lp instanceof widgetLayoutParams) {
|
||||||
child.nativeView.setLayoutParams(lp);
|
lp.minWidth = Length.toDevicePixels(child.style.minWidth, 0);
|
||||||
|
nativeView.setLayoutParams(lp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public _setChildMinHeightNative(child: View): void {
|
public _setChildMinHeightNative(child: View): void {
|
||||||
child._setMinHeightNative(0);
|
child._setMinHeightNative(0);
|
||||||
const lp = child.nativeView.getLayoutParams();
|
const nativeView = child.nativeView;
|
||||||
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
|
const lp = nativeView.getLayoutParams();
|
||||||
lp.minHeight = Length.toDevicePixels(child.minHeight, 0);
|
if (lp instanceof widgetLayoutParams) {
|
||||||
child.nativeView.setLayoutParams(lp);
|
lp.minHeight = Length.toDevicePixels(child.style.minHeight, 0);
|
||||||
|
nativeView.setLayoutParams(lp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user