Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 982673b

Browse files
author
farfromrefuge
committedJan 22, 2024
fix(android): some improvements to make Label a bit faster
1 parent b8bfbad commit 982673b

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed
 

Diff for: ‎src/label.android.ts

+31-20
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ import {
5454
selectableProperty,
5555
textShadowProperty
5656
} from './label-common';
57+
import { SDK_VERSION } from '@nativescript/core/utils/constants';
5758

5859
export { createNativeAttributedString } from '@nativescript-community/text';
5960
export * from './label-common';
60-
const sdkVersion = lazy(() => parseInt(Device.sdkVersion, 10));
6161

6262
let TextView: typeof com.nativescript.text.TextView;
6363

@@ -262,6 +262,7 @@ export class Label extends LabelBase {
262262
}
263263
}
264264
[lineBreakProperty.setNative](value: string) {
265+
// TODO move it all to native
265266
const nativeView = this.nativeTextViewProtected;
266267
switch (value) {
267268
case 'end':
@@ -283,6 +284,7 @@ export class Label extends LabelBase {
283284
}
284285

285286
[whiteSpaceProperty.setNative](value: CoreTypes.WhiteSpaceType) {
287+
// TODO move it all to native
286288
if (!this.lineBreak) {
287289
const nativeView = this.nativeTextViewProtected;
288290
switch (value) {
@@ -299,16 +301,19 @@ export class Label extends LabelBase {
299301
}
300302
}
301303
[textShadowProperty.getDefault](value: number) {
302-
return {
303-
radius: this.nativeTextViewProtected.getShadowRadius(),
304-
offsetX: this.nativeTextViewProtected.getShadowDx(),
305-
offsetY: this.nativeTextViewProtected.getShadowDy(),
306-
color: this.nativeTextViewProtected.getShadowColor()
307-
};
304+
let defaultValue = Label[textShadowProperty.defaultValueKey];
305+
if (!defaultValue) {
306+
defaultValue = Label[textShadowProperty.defaultValueKey] = {
307+
radius: this.nativeTextViewProtected.getShadowRadius(),
308+
offsetX: this.nativeTextViewProtected.getShadowDx(),
309+
offsetY: this.nativeTextViewProtected.getShadowDy(),
310+
color: this.nativeTextViewProtected.getShadowColor()
311+
};
312+
}
313+
return defaultValue;
308314
}
309315

310316
[textShadowProperty.setNative](value: CSSShadow) {
311-
// prettier-ignore
312317
this.nativeViewProtected.setShadowLayer(
313318
Length.toDevicePixels(value.blurRadius, java.lang.Float.MIN_VALUE),
314319
Length.toDevicePixels(value.offsetX, 0),
@@ -318,6 +323,7 @@ export class Label extends LabelBase {
318323
}
319324

320325
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
326+
// TODO move it all to native
321327
const view = this.nativeTextViewProtected;
322328
view.setGravity(getHorizontalGravity(this.textAlignment) | getVerticalGravity(value));
323329
}
@@ -338,12 +344,13 @@ export class Label extends LabelBase {
338344
[textTransformProperty.setNative](value: CoreTypes.TextTransformType) {}
339345

340346
[textAlignmentProperty.setNative](value: CoreTypes.TextAlignmentType) {
347+
// TODO move it all to native
341348
const view = this.nativeTextViewProtected;
342-
if (android.os.Build.VERSION.SDK_INT >= 26) {
349+
if (SDK_VERSION >= 26) {
343350
if ((value as any) === 'justify') {
344-
view.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_INTER_WORD);
351+
view.setJustificationMode(1 /* android.text.Layout.JUSTIFICATION_MODE_INTER_WORD */);
345352
} else {
346-
view.setJustificationMode(android.text.Layout.JUSTIFICATION_MODE_NONE);
353+
view.setJustificationMode(0 /* android.text.Layout.JUSTIFICATION_MODE_NONE */);
347354
view.setGravity(getHorizontalGravity(value) | getVerticalGravity(this.verticalTextAlignment));
348355
}
349356
} else {
@@ -362,6 +369,7 @@ export class Label extends LabelBase {
362369
}
363370
}
364371
[fontSizeProperty.setNative](value: number | { nativeSize: number }) {
372+
// TODO move it all to native
365373
// setTextSize is ignored if autoFontSize is enabled
366374
// so we need to disable autoFontSize just to set textSize
367375
if (this.mAutoFontSize) {
@@ -370,15 +378,16 @@ export class Label extends LabelBase {
370378
if (typeof value === 'number') {
371379
this.nativeTextViewProtected.setTextSize(value);
372380
} else {
373-
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, value.nativeSize);
381+
this.nativeTextViewProtected.setTextSize(2 /* android.util.TypedValue.COMPLEX_UNIT_SP */, value.nativeSize);
374382
}
375383
if (this.mAutoFontSize) {
376384
this.enableAutoSize();
377385
}
378386
}
379387

380388
[lineHeightProperty.setNative](value: number) {
381-
if (sdkVersion() >= 28) {
389+
// TODO move it all to native
390+
if (SDK_VERSION >= 28) {
382391
this.nativeTextViewProtected.setLineHeight(value * Utils.layout.getDisplayDensity());
383392
} else {
384393
const fontHeight = this.nativeTextViewProtected.getPaint().getFontMetrics(null);
@@ -387,28 +396,30 @@ export class Label extends LabelBase {
387396
}
388397

389398
[fontInternalProperty.setNative](value: Font | android.graphics.Typeface) {
399+
// TODO move it all to native
390400
const androidFont: android.graphics.Typeface = value instanceof Font ? value.getAndroidTypeface() : value;
391401
this.nativeTextViewProtected.setTypeface(androidFont);
392-
if (this.lineHeight && sdkVersion() < 28) {
402+
if (SDK_VERSION < 28 && this.lineHeight) {
393403
const fontHeight = this.nativeTextViewProtected.getPaint().getFontMetrics(null);
394404
this.nativeTextViewProtected.setLineSpacing(this.lineHeight * Utils.layout.getDisplayDensity() - fontHeight, 1);
395405
}
396406
}
397407

398408
[textDecorationProperty.setNative](value: number | CoreTypes.TextDecorationType) {
409+
// TODO move it all to native
399410
switch (value) {
400411
case 'none':
401412
this.nativeTextViewProtected.setPaintFlags(0);
402413
break;
403414
case 'underline':
404-
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.UNDERLINE_TEXT_FLAG);
415+
this.nativeTextViewProtected.setPaintFlags(8 /* android.graphics.Paint.UNDERLINE_TEXT_FLAG */);
405416
break;
406417
case 'line-through':
407-
this.nativeTextViewProtected.setPaintFlags(android.graphics.Paint.STRIKE_THRU_TEXT_FLAG);
418+
this.nativeTextViewProtected.setPaintFlags(16 /* android.graphics.Paint.STRIKE_THRU_TEXT_FLAG */);
408419
break;
409420
case 'underline line-through':
410421
this.nativeTextViewProtected.setPaintFlags(
411-
android.graphics.Paint.UNDERLINE_TEXT_FLAG | android.graphics.Paint.STRIKE_THRU_TEXT_FLAG
422+
8 /* android.graphics.Paint.UNDERLINE_TEXT_FLAG */ | 16 /* android.graphics.Paint.STRIKE_THRU_TEXT_FLAG */
412423
);
413424
break;
414425
default:
@@ -466,7 +477,7 @@ export class Label extends LabelBase {
466477
this.minFontSize || 10,
467478
this.maxFontSize || 200,
468479
this.autoFontSizeStep || 1,
469-
android.util.TypedValue.COMPLEX_UNIT_DIP
480+
1 /* android.util.TypedValue.COMPLEX_UNIT_DIP */
470481
);
471482
}
472483
[maxFontSizeProperty.setNative](value) {
@@ -482,7 +493,7 @@ export class Label extends LabelBase {
482493
private disableAutoSize() {
483494
androidx.core.widget.TextViewCompat.setAutoSizeTextTypeWithDefaults(
484495
this.nativeView,
485-
androidx.core.widget.TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE
496+
0 /* androidx.core.widget.TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE */
486497
);
487498
}
488499
[autoFontSizeProperty.setNative](value: boolean) {
@@ -568,7 +579,7 @@ export class Label extends LabelBase {
568579
this,
569580
this.formattedText === null || this.formattedText === undefined ? '' : this.formattedText.toString()
570581
);
571-
} else if (this.text instanceof java.lang.CharSequence || this.text instanceof android.text.Spannable) {
582+
} else if (this.text instanceof java.lang.CharSequence || this.text instanceof android.text.Spannable) {
572583
transformedText = this.text;
573584
} else {
574585
const text = this.text;

0 commit comments

Comments
 (0)
Please sign in to comment.