Skip to content

Commit d6d8296

Browse files
committed
chore: refactor for faster native calls
1 parent f1bfcfe commit d6d8296

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

src/button/button.android.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
22
import { dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty, shapeProperty, themer } from '@nativescript-community/ui-material-core';
3-
import { createStateListAnimator, getColorStateList, getLayout, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
3+
import { createStateListAnimator, getColorStateList, getHorizontalGravity, getLayout, getVerticalGravity, isPostLollipop } from '@nativescript-community/ui-material-core/android/utils';
44
import {
55
Background,
66
Color,
@@ -14,8 +14,10 @@ import {
1414
backgroundInternalProperty,
1515
colorProperty,
1616
profile,
17+
textAlignmentProperty,
1718
textTransformProperty
1819
} from '@nativescript/core';
20+
import { TextAlignment } from '@nativescript/core/ui/text-base';
1921
import { ButtonBase, imageSourceProperty, srcProperty } from './button-common';
2022

2123
let LayoutInflater: typeof android.view.LayoutInflater;
@@ -169,22 +171,11 @@ export class Button extends ButtonBase {
169171
}
170172
}
171173

174+
[textAlignmentProperty.setNative](value: TextAlignment) {
175+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(value) | getVerticalGravity(this.verticalTextAlignment));
176+
}
172177
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
173-
const view = this.nativeTextViewProtected;
174-
const horizontalGravity = view.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
175-
switch (value) {
176-
case 'initial':
177-
case 'top':
178-
view.setGravity(android.view.Gravity.TOP | horizontalGravity);
179-
break;
180-
case 'middle':
181-
view.setGravity(android.view.Gravity.CENTER_VERTICAL | horizontalGravity);
182-
break;
183-
184-
case 'bottom':
185-
view.setGravity(android.view.Gravity.BOTTOM | horizontalGravity);
186-
break;
187-
}
178+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(this.textAlignment) | getVerticalGravity(value));
188179
}
189180

190181
[imageSourceProperty.setNative](value: ImageSource) {

src/core/android/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { VerticalTextAlignment } from '@nativescript-community/text';
12
import { Application, Color, Utils, ViewBase, profile } from '@nativescript/core';
3+
import { TextAlignment } from '@nativescript/core/ui/text-base';
24

35
let isPostLollipopVar: boolean;
46
export function isPostLollipop() {
@@ -250,3 +252,28 @@ export function getAttr(id: string) {
250252
const context: android.content.Context = Application.android.context;
251253
return context.getResources().getIdentifier(id, 'attr', context.getPackageName());
252254
}
255+
256+
export function getHorizontalGravity(textAlignment: TextAlignment) {
257+
switch (textAlignment) {
258+
case 'initial':
259+
case 'left':
260+
return 8388611; //Gravity.START
261+
case 'center':
262+
return 1; //Gravity.CENTER_HORIZONTAL
263+
case 'right':
264+
return 8388613; //Gravity.END
265+
}
266+
}
267+
export function getVerticalGravity(textAlignment: VerticalTextAlignment) {
268+
switch (textAlignment) {
269+
case 'initial':
270+
case 'top':
271+
return 48; //Gravity.TOP
272+
case 'middle':
273+
case 'center':
274+
return 16; //Gravity.CENTER_VERTICAL
275+
276+
case 'bottom':
277+
return 80; //Gravity.BOTTOM
278+
}
279+
}

src/textfield/textfield.android.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
22
import { themer } from '@nativescript-community/ui-material-core';
3-
import { getFullColorStateList, getLayout } from '@nativescript-community/ui-material-core/android/utils';
3+
import { getFullColorStateList, getHorizontalGravity, getLayout, getVerticalGravity } from '@nativescript-community/ui-material-core/android/utils';
44
import {
55
counterMaxLengthProperty,
66
digitsProperty,
@@ -30,8 +30,10 @@ import {
3030
paddingRightProperty,
3131
paddingTopProperty,
3232
placeholderColorProperty,
33-
profile
33+
profile,
34+
textAlignmentProperty
3435
} from '@nativescript/core';
36+
import { TextAlignment } from '@nativescript/core/ui/text-base';
3537
import { secureProperty } from '@nativescript/core/ui/text-field';
3638
import { TextFieldBase } from './textfield.common';
3739

@@ -299,23 +301,12 @@ export class TextField extends TextFieldBase {
299301
[paddingLeftProperty.setNative](value: Length) {
300302
org.nativescript.widgets.ViewHelper.setPaddingLeft(this.nativeViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderLeftWidth, 0));
301303
}
304+
[textAlignmentProperty.setNative](value: TextAlignment) {
305+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(value) | getVerticalGravity(this.verticalTextAlignment));
306+
}
302307
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
303308
// TODO: not working for now
304-
const view = this.nativeTextViewProtected;
305-
const horizontalGravity = view.getGravity() & android.view.Gravity.HORIZONTAL_GRAVITY_MASK;
306-
switch (value) {
307-
case 'initial':
308-
case 'top':
309-
view.setGravity(android.view.Gravity.TOP | horizontalGravity);
310-
break;
311-
case 'middle':
312-
view.setGravity(android.view.Gravity.CENTER_VERTICAL | horizontalGravity);
313-
break;
314-
315-
case 'bottom':
316-
view.setGravity(android.view.Gravity.BOTTOM | horizontalGravity);
317-
break;
318-
}
309+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(this.textAlignment) | getVerticalGravity(value));
319310
}
320311
}
321312
//

src/textview/textview.android.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ import {
2525
paddingLeftProperty,
2626
paddingRightProperty,
2727
paddingTopProperty,
28-
placeholderColorProperty
28+
placeholderColorProperty,
29+
textAlignmentProperty
2930
} from '@nativescript/core';
3031
import { TextViewBase } from './textview.common';
31-
import { getFullColorStateList, getLayout } from '@nativescript-community/ui-material-core/android/utils';
32+
import { getFullColorStateList, getHorizontalGravity, getLayout, getVerticalGravity } from '@nativescript-community/ui-material-core/android/utils';
3233
import { themer } from '@nativescript-community/ui-material-core';
34+
import { VerticalTextAlignment, verticalTextAlignmentProperty } from '@nativescript-community/text';
35+
import { TextAlignment } from '@nativescript/core/ui/text-base';
3336

3437
let LayoutInflater: typeof android.view.LayoutInflater;
3538
let FrameLayoutLayoutParams: typeof android.widget.FrameLayout.LayoutParams;
@@ -292,5 +295,11 @@ export class TextView extends TextViewBase {
292295
[paddingLeftProperty.setNative](value: Length) {
293296
org.nativescript.widgets.ViewHelper.setPaddingLeft(this.nativeViewProtected, Length.toDevicePixels(value, 0) + Length.toDevicePixels(this.style.borderLeftWidth, 0));
294297
}
298+
[textAlignmentProperty.setNative](value: TextAlignment) {
299+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(value) | getVerticalGravity(this.verticalTextAlignment));
300+
}
301+
[verticalTextAlignmentProperty.setNative](value: VerticalTextAlignment) {
302+
this.nativeTextViewProtected.setGravity(getHorizontalGravity(this.textAlignment) | getVerticalGravity(value));
303+
}
295304
}
296305
//

src/textview/textview.common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { VerticalTextAlignment } from '@nativescript-community/text';
12
import { cssProperty } from '@nativescript-community/ui-material-core';
23
import { CSSType, Color, TextView as NSTextView } from '@nativescript/core';
34

@@ -20,4 +21,5 @@ export abstract class TextViewBase extends NSTextView {
2021
@cssProperty floatingColor: Color;
2122
@cssProperty floatingInactiveColor: Color;
2223
@cssProperty buttonColor: Color;
24+
@cssProperty verticalTextAlignment: VerticalTextAlignment;
2325
}

0 commit comments

Comments
 (0)