Skip to content

Commit 63aecfa

Browse files
committed
fix: faster properties
1 parent 6d56513 commit 63aecfa

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

src/label.android.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ export const htmlProperty = new Property<Label, string>({ name: 'html', defaultV
9292

9393
type ClickableSpan = new (owner: Span) => android.text.style.ClickableSpan;
9494

95+
function getHorizontalGravity(textAlignment: TextAlignment) {
96+
switch (textAlignment) {
97+
case 'initial':
98+
case 'left':
99+
return 8388611; //Gravity.START
100+
case 'center':
101+
return 1; //Gravity.CENTER_HORIZONTAL
102+
case 'right':
103+
return 8388613; //Gravity.END
104+
}
105+
}
106+
function getVerticalGravity(textAlignment: VerticalTextAlignment) {
107+
switch (textAlignment) {
108+
case 'initial':
109+
case 'top':
110+
return 48; //Gravity.TOP
111+
case 'middle':
112+
case 'center':
113+
return 16; //Gravity.CENTER_VERTICAL
114+
115+
case 'bottom':
116+
return 80; //Gravity.BOTTOM
117+
}
118+
}
119+
95120
// eslint-disable-next-line no-redeclare
96121
let ClickableSpan: ClickableSpan;
97122

@@ -297,10 +322,6 @@ export class Label extends LabelBase {
297322
return new TextView(this._context);
298323
}
299324

300-
@needFormattedStringComputation
301-
[htmlProperty.setNative](value: string) {
302-
this._setNativeText();
303-
}
304325

305326
[maxLinesProperty.setNative](value: number | string) {
306327
// this.nativeViewProtected.setMinLines(1);
@@ -360,57 +381,33 @@ export class Label extends LabelBase {
360381
}
361382

362383
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
363-
const horizontalGravity = this.nativeTextViewProtected.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
364-
switch (value) {
365-
case 'initial':
366-
case 'top':
367-
this.nativeTextViewProtected.setGravity(android.view.Gravity.TOP | horizontalGravity);
368-
break;
369-
case 'middle':
370-
case 'center':
371-
this.nativeTextViewProtected.setGravity(android.view.Gravity.CENTER_VERTICAL | horizontalGravity);
372-
break;
373-
374-
case 'bottom':
375-
this.nativeTextViewProtected.setGravity(android.view.Gravity.BOTTOM | horizontalGravity);
376-
break;
377-
}
384+
const view = this.nativeTextViewProtected;
385+
view.setGravity(getHorizontalGravity(this.textAlignment) | getVerticalGravity(value));
378386
}
379387

380388
[textProperty.getDefault](): symbol | number {
381389
return resetSymbol;
382390
}
383391

384-
@needFormattedStringComputation
385392
[textProperty.setNative](value: string | number | symbol) {
386393
this._setNativeText();
387394
}
388395

389-
@needFormattedStringComputation
390396
[formattedTextProperty.setNative](value: FormattedString) {
391397
this._setNativeText();
392398
}
393399

394-
@needFormattedStringComputation
395-
[textTransformProperty.setNative](value: TextTransform) {
400+
[htmlProperty.setNative](value: string) {
396401
this._setNativeText();
397402
}
398-
[textAlignmentProperty.setNative](value: TextAlignment) {
399-
const verticalGravity = this.nativeTextViewProtected.getGravity() & android.view.Gravity.VERTICAL_GRAVITY_MASK;
400-
switch (value) {
401-
case 'initial':
402-
case 'left':
403-
this.nativeTextViewProtected.setGravity(android.view.Gravity.START | verticalGravity);
404-
break;
405403

406-
case 'center':
407-
this.nativeTextViewProtected.setGravity(android.view.Gravity.CENTER_HORIZONTAL | verticalGravity);
408-
break;
404+
[textTransformProperty.setNative](value: TextTransform) {
405+
this._setNativeText();
406+
}
409407

410-
case 'right':
411-
this.nativeTextViewProtected.setGravity(android.view.Gravity.END | verticalGravity);
412-
break;
413-
}
408+
[textAlignmentProperty.setNative](value: TextAlignment) {
409+
const view = this.nativeTextViewProtected;
410+
view.setGravity(getHorizontalGravity(value) | getVerticalGravity(this.verticalTextAlignment));
414411
}
415412

416413
[colorProperty.setNative](value: Color | android.content.res.ColorStateList) {
@@ -559,6 +556,10 @@ export class Label extends LabelBase {
559556

560557
@profile
561558
_setNativeText(reset: boolean = false): void {
559+
if (!this._canChangeText) {
560+
this._needFormattedStringComputation = true;
561+
return;
562+
}
562563
if (reset) {
563564
this.nativeTextViewProtected.setText(null);
564565
return;

0 commit comments

Comments
 (0)