Skip to content

Commit 2377b6a

Browse files
authored
chore(core): setNative type handling (NativeScript#10768)
1 parent cc18909 commit 2377b6a

File tree

8 files changed

+140
-155
lines changed

8 files changed

+140
-155
lines changed

packages/core/ui/core/properties/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class Property<T extends ViewBase, U> implements TypedPropertyDescriptor<
196196
public readonly key: symbol;
197197

198198
public readonly getDefault: symbol;
199-
public readonly setNative: any;
199+
public readonly setNative: symbol;
200200

201201
public readonly defaultValueKey: symbol;
202202
public readonly defaultValue: U;
@@ -397,7 +397,7 @@ export class CoercibleProperty<T extends ViewBase, U> extends Property<T, U> imp
397397
const propertyName = options.name;
398398
const key = this.key;
399399
const getDefault: symbol = this.getDefault;
400-
const setNative: any = this.setNative;
400+
const setNative: symbol = this.setNative;
401401
const defaultValueKey = this.defaultValueKey;
402402
const defaultValue: U = this.defaultValue;
403403

@@ -612,7 +612,7 @@ export class CssProperty<T extends Style, U> {
612612

613613
public readonly key: symbol;
614614
public readonly getDefault: symbol;
615-
public readonly setNative: any;
615+
public readonly setNative: symbol;
616616
public readonly sourceKey: symbol;
617617
public readonly defaultValueKey: symbol;
618618
public readonly defaultValue: U;
@@ -879,7 +879,7 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro
879879
public readonly cssLocalName: string;
880880

881881
public readonly getDefault: symbol;
882-
public readonly setNative: any;
882+
public readonly setNative: symbol;
883883

884884
public readonly register: (cls: { prototype }) => void;
885885

packages/core/ui/core/view/index.android.ts

Lines changed: 115 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,117 @@ export class View extends ViewCommon {
807807
this.nativeViewProtected.setAlpha(float(value));
808808
}
809809

810+
[accessibilityRoleProperty.setNative](value: AccessibilityRole): void {
811+
this.accessibilityRole = value as AccessibilityRole;
812+
updateAccessibilityProperties(this);
813+
814+
if (SDK_VERSION >= 28) {
815+
this.nativeViewProtected?.setAccessibilityHeading(value === AccessibilityRole.Header);
816+
}
817+
}
818+
819+
[accessibilityLiveRegionProperty.setNative](value: AccessibilityLiveRegion): void {
820+
switch (value as AccessibilityLiveRegion) {
821+
case AccessibilityLiveRegion.Assertive: {
822+
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
823+
break;
824+
}
825+
case AccessibilityLiveRegion.Polite: {
826+
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_POLITE);
827+
break;
828+
}
829+
default: {
830+
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_NONE);
831+
break;
832+
}
833+
}
834+
}
835+
836+
[accessibilityStateProperty.setNative](value: AccessibilityState): void {
837+
this.accessibilityState = value as AccessibilityState;
838+
updateAccessibilityProperties(this);
839+
}
840+
841+
[horizontalAlignmentProperty.getDefault](): CoreTypes.HorizontalAlignmentType {
842+
return <CoreTypes.HorizontalAlignmentType>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeViewProtected);
843+
}
844+
[horizontalAlignmentProperty.setNative](value: CoreTypes.HorizontalAlignmentType) {
845+
const nativeView = this.nativeViewProtected;
846+
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
847+
const gravity = lp.gravity;
848+
const weight = lp.weight;
849+
// Set only if params gravity exists.
850+
if (gravity !== undefined) {
851+
switch (value) {
852+
case 'left':
853+
lp.gravity = GRAVITY_LEFT | (gravity & VERTICAL_GRAVITY_MASK);
854+
if (weight < 0) {
855+
lp.weight = -2;
856+
}
857+
break;
858+
case 'center':
859+
lp.gravity = GRAVITY_CENTER_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
860+
if (weight < 0) {
861+
lp.weight = -2;
862+
}
863+
break;
864+
case 'right':
865+
lp.gravity = GRAVITY_RIGHT | (gravity & VERTICAL_GRAVITY_MASK);
866+
if (weight < 0) {
867+
lp.weight = -2;
868+
}
869+
break;
870+
case 'stretch':
871+
lp.gravity = GRAVITY_FILL_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
872+
if (weight < 0) {
873+
lp.weight = -1;
874+
}
875+
break;
876+
}
877+
nativeView.setLayoutParams(lp);
878+
}
879+
}
880+
881+
[verticalAlignmentProperty.getDefault](): CoreTypes.VerticalAlignmentType {
882+
return <CoreTypes.VerticalAlignmentType>org.nativescript.widgets.ViewHelper.getVerticalAlignment(this.nativeViewProtected);
883+
}
884+
[verticalAlignmentProperty.setNative](value: CoreTypes.VerticalAlignmentType) {
885+
const nativeView = this.nativeViewProtected;
886+
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
887+
const gravity = lp.gravity;
888+
const height = lp.height;
889+
// Set only if params gravity exists.
890+
if (gravity !== undefined) {
891+
switch (value) {
892+
case 'top':
893+
lp.gravity = GRAVITY_TOP | (gravity & HORIZONTAL_GRAVITY_MASK);
894+
if (height < 0) {
895+
lp.height = -2;
896+
}
897+
break;
898+
case 'middle':
899+
lp.gravity = GRAVITY_CENTER_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
900+
if (height < 0) {
901+
lp.height = -2;
902+
}
903+
break;
904+
case 'bottom':
905+
lp.gravity = GRAVITY_BOTTOM | (gravity & HORIZONTAL_GRAVITY_MASK);
906+
if (height < 0) {
907+
lp.height = -2;
908+
}
909+
break;
910+
case 'stretch':
911+
lp.gravity = GRAVITY_FILL_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
912+
if (height < 0) {
913+
lp.height = -1;
914+
}
915+
break;
916+
}
917+
nativeView.setLayoutParams(lp);
918+
}
919+
}
920+
810921
[testIDProperty.setNative](value: string) {
811922
this.setAccessibilityIdentifier(this.nativeViewProtected, value);
812923
}
@@ -833,27 +944,17 @@ export class View extends ViewCommon {
833944
this.setAccessibilityIdentifier(this.nativeViewProtected, value);
834945
}
835946

836-
// @ts-expect-error
837-
[accessibilityRoleProperty.setNative](value: AccessibilityRole): void {
838-
this.accessibilityRole = value;
839-
updateAccessibilityProperties(this);
840-
841-
if (SDK_VERSION >= 28) {
842-
this.nativeViewProtected?.setAccessibilityHeading(value === AccessibilityRole.Header);
843-
}
844-
}
845-
846-
[accessibilityValueProperty.setNative](): void {
947+
[accessibilityValueProperty.setNative](value: string): void {
847948
this._androidContentDescriptionUpdated = true;
848949
updateContentDescription(this);
849950
}
850951

851-
[accessibilityLabelProperty.setNative](): void {
952+
[accessibilityLabelProperty.setNative](value: string): void {
852953
this._androidContentDescriptionUpdated = true;
853954
updateContentDescription(this);
854955
}
855956

856-
[accessibilityHintProperty.setNative](): void {
957+
[accessibilityHintProperty.setNative](value: string): void {
857958
this._androidContentDescriptionUpdated = true;
858959
updateContentDescription(this);
859960
}
@@ -866,31 +967,7 @@ export class View extends ViewCommon {
866967
}
867968
}
868969

869-
// @ts-expect-error
870-
[accessibilityLiveRegionProperty.setNative](value: AccessibilityLiveRegion): void {
871-
switch (value) {
872-
case AccessibilityLiveRegion.Assertive: {
873-
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
874-
break;
875-
}
876-
case AccessibilityLiveRegion.Polite: {
877-
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_POLITE);
878-
break;
879-
}
880-
default: {
881-
this.nativeViewProtected.setAccessibilityLiveRegion(android.view.View.ACCESSIBILITY_LIVE_REGION_NONE);
882-
break;
883-
}
884-
}
885-
}
886-
887-
// @ts-expect-error
888-
[accessibilityStateProperty.setNative](value: AccessibilityState): void {
889-
this.accessibilityState = value;
890-
updateAccessibilityProperties(this);
891-
}
892-
893-
[accessibilityMediaSessionProperty.setNative](): void {
970+
[accessibilityMediaSessionProperty.setNative](value: string): void {
894971
updateAccessibilityProperties(this);
895972
}
896973

@@ -974,88 +1051,6 @@ export class View extends ViewCommon {
9741051
nativeView.setStateListAnimator(stateListAnimator);
9751052
}
9761053

977-
[horizontalAlignmentProperty.getDefault](): CoreTypes.HorizontalAlignmentType {
978-
return <CoreTypes.HorizontalAlignmentType>org.nativescript.widgets.ViewHelper.getHorizontalAlignment(this.nativeViewProtected);
979-
}
980-
// @ts-expect-error
981-
[horizontalAlignmentProperty.setNative](value: CoreTypes.HorizontalAlignmentType) {
982-
const nativeView = this.nativeViewProtected;
983-
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
984-
const gravity = lp.gravity;
985-
const weight = lp.weight;
986-
// Set only if params gravity exists.
987-
if (gravity !== undefined) {
988-
switch (value) {
989-
case 'left':
990-
lp.gravity = GRAVITY_LEFT | (gravity & VERTICAL_GRAVITY_MASK);
991-
if (weight < 0) {
992-
lp.weight = -2;
993-
}
994-
break;
995-
case 'center':
996-
lp.gravity = GRAVITY_CENTER_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
997-
if (weight < 0) {
998-
lp.weight = -2;
999-
}
1000-
break;
1001-
case 'right':
1002-
lp.gravity = GRAVITY_RIGHT | (gravity & VERTICAL_GRAVITY_MASK);
1003-
if (weight < 0) {
1004-
lp.weight = -2;
1005-
}
1006-
break;
1007-
case 'stretch':
1008-
lp.gravity = GRAVITY_FILL_HORIZONTAL | (gravity & VERTICAL_GRAVITY_MASK);
1009-
if (weight < 0) {
1010-
lp.weight = -1;
1011-
}
1012-
break;
1013-
}
1014-
nativeView.setLayoutParams(lp);
1015-
}
1016-
}
1017-
1018-
[verticalAlignmentProperty.getDefault](): CoreTypes.VerticalAlignmentType {
1019-
return <CoreTypes.VerticalAlignmentType>org.nativescript.widgets.ViewHelper.getVerticalAlignment(this.nativeViewProtected);
1020-
}
1021-
// @ts-expect-error
1022-
[verticalAlignmentProperty.setNative](value: CoreTypes.VerticalAlignmentType) {
1023-
const nativeView = this.nativeViewProtected;
1024-
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
1025-
const gravity = lp.gravity;
1026-
const height = lp.height;
1027-
// Set only if params gravity exists.
1028-
if (gravity !== undefined) {
1029-
switch (value) {
1030-
case 'top':
1031-
lp.gravity = GRAVITY_TOP | (gravity & HORIZONTAL_GRAVITY_MASK);
1032-
if (height < 0) {
1033-
lp.height = -2;
1034-
}
1035-
break;
1036-
case 'middle':
1037-
lp.gravity = GRAVITY_CENTER_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
1038-
if (height < 0) {
1039-
lp.height = -2;
1040-
}
1041-
break;
1042-
case 'bottom':
1043-
lp.gravity = GRAVITY_BOTTOM | (gravity & HORIZONTAL_GRAVITY_MASK);
1044-
if (height < 0) {
1045-
lp.height = -2;
1046-
}
1047-
break;
1048-
case 'stretch':
1049-
lp.gravity = GRAVITY_FILL_VERTICAL | (gravity & HORIZONTAL_GRAVITY_MASK);
1050-
if (height < 0) {
1051-
lp.height = -1;
1052-
}
1053-
break;
1054-
}
1055-
nativeView.setLayoutParams(lp);
1056-
}
1057-
}
1058-
10591054
[rotateProperty.setNative](value: number) {
10601055
org.nativescript.widgets.ViewHelper.setRotate(this.nativeViewProtected, float(value));
10611056
}

packages/core/ui/image/index.ios.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ImageBase, stretchProperty, imageSourceProperty, tintColorProperty, srcProperty, iosSymbolEffectProperty, ImageSymbolEffect, ImageSymbolEffects, iosSymbolScaleProperty } from './image-common';
2-
import { ImageSource, iosSymbolScaleType } from '../../image-source';
2+
import { ImageSource } from '../../image-source';
33
import { ImageAsset } from '../../image-asset';
44
import { Color } from '../../color';
55
import { Trace } from '../../trace';
@@ -219,8 +219,7 @@ export class Image extends ImageBase {
219219
}
220220
}
221221

222-
// @ts-expect-error
223-
[iosSymbolScaleProperty.setNative](value: iosSymbolScaleType) {
222+
[iosSymbolScaleProperty.setNative](value: string) {
224223
// reset src to configure scale
225224
this._setSrc(this.src);
226225
}

packages/core/ui/search-bar/index.android.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isUserInteractionEnabledProperty, isEnabledProperty } from '../core/vie
44
import { ad } from '../../utils';
55
import { Color } from '../../color';
66
import { colorProperty, backgroundColorProperty, backgroundInternalProperty, fontInternalProperty, fontSizeProperty } from '../styling/style-properties';
7+
import { Background } from '../styling/background';
78

89
export * from './search-bar-common';
910

@@ -227,22 +228,20 @@ export class SearchBar extends SearchBarBase {
227228
[backgroundInternalProperty.getDefault](): any {
228229
return null;
229230
}
230-
[backgroundInternalProperty.setNative](value: any) {
231+
[backgroundInternalProperty.setNative](value: android.graphics.drawable.Drawable | Background) {
231232
//
232233
}
233234

234235
[textProperty.getDefault](): string {
235236
return '';
236237
}
237-
// @ts-expect-error
238238
[textProperty.setNative](value: string) {
239239
const text = value === null || value === undefined ? '' : value.toString();
240240
this.nativeViewProtected.setQuery(text, false);
241241
}
242242
[hintProperty.getDefault](): string {
243243
return null;
244244
}
245-
// @ts-expect-error
246245
[hintProperty.setNative](value: string) {
247246
if (value === null || value === undefined) {
248247
this.nativeViewProtected.setQueryHint(null);

packages/core/ui/search-bar/index.ios.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,13 @@ export class SearchBar extends SearchBarBase {
158158
[backgroundInternalProperty.getDefault](): any {
159159
return null;
160160
}
161-
[backgroundInternalProperty.setNative](value: any) {
161+
[backgroundInternalProperty.setNative](value: UIColor) {
162162
//
163163
}
164164

165165
[textProperty.getDefault](): string {
166166
return '';
167167
}
168-
// @ts-expect-error
169168
[textProperty.setNative](value: string) {
170169
const text = value === null || value === undefined ? '' : value.toString();
171170
this.ios.text = text;
@@ -174,7 +173,6 @@ export class SearchBar extends SearchBarBase {
174173
[hintProperty.getDefault](): string {
175174
return '';
176175
}
177-
// @ts-expect-error
178176
[hintProperty.setNative](value: string) {
179177
this._updateAttributedPlaceholder();
180178
}

packages/core/ui/styling/style-properties.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export const minWidthProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.Leng
8787
export const minHeightProperty: CssProperty<Style, CoreTypes.dip | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit>;
8888
export const widthProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
8989
export const heightProperty: CssAnimationProperty<Style, CoreTypes.PercentLengthType>;
90-
export const lineHeightProperty: CssProperty<Style, number>;
9190
export const marginProperty: ShorthandProperty<Style, string | CoreTypes.PercentLengthType>;
9291
export const marginLeftProperty: CssProperty<Style, CoreTypes.PercentLengthType>;
9392
export const marginRightProperty: CssProperty<Style, CoreTypes.PercentLengthType>;

0 commit comments

Comments
 (0)