Skip to content

Commit 72dafae

Browse files
committed
fix: shape allows dip and px units
1 parent ea159b1 commit 72dafae

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

src/core/index.android.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Background, Button, Color, Length, View, androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty, profile } from '@nativescript/core';
1+
import { Application, Background, Button, Color, Length, View, androidDynamicElevationOffsetProperty, androidElevationProperty, backgroundInternalProperty, profile, PercentLength } from '@nativescript/core';
22
import { createRippleDrawable, createStateListAnimator, getAttrColor, getColorStateList, handleClearFocus, isPostLollipop, isPostLollipopMR1, isPostMarshmallow } from './android/utils';
33
import { CornerFamily, applyMixins } from './index.common';
44
import { cssProperty, dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty } from './cssproperties';
@@ -127,35 +127,55 @@ export class Themer {
127127
}
128128
if (options.cornerSize !== undefined) {
129129
if (typeof options.cornerSize === 'object') {
130-
builder.setAllCornerSizes(new com.google.android.material.shape.RelativeCornerSize(options.cornerSize.value));
130+
if (options.cornerSize.unit === '%') {
131+
builder.setAllCornerSizes(new com.google.android.material.shape.RelativeCornerSize(options.cornerSize.value));
132+
} else {
133+
builder.setAllCornerSizes(PercentLength.toDevicePixels(options.cornerSize));
134+
}
131135
} else {
132136
builder.setAllCornerSizes(options.cornerSize);
133137
}
134138
}
135139
if (options.cornerSizeBottomLeft !== undefined) {
136140
if (typeof options.cornerSizeBottomLeft === 'object') {
137-
builder.setBottomLeftCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeBottomLeft.value));
141+
if (options.cornerSizeBottomLeft.unit === '%') {
142+
builder.setBottomLeftCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeBottomLeft.value));
143+
} else {
144+
builder.setBottomLeftCornerSize(PercentLength.toDevicePixels(options.cornerSizeBottomLeft));
145+
}
138146
} else {
139147
builder.setBottomLeftCornerSize(options.cornerSizeBottomLeft);
140148
}
141149
}
142150
if (options.cornerSizeBottomRight !== undefined) {
143151
if (typeof options.cornerSizeBottomRight === 'object') {
144-
builder.setBottomRightCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeBottomRight.value));
152+
if (options.cornerSizeBottomRight.unit === '%') {
153+
builder.setBottomRightCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeBottomRight.value));
154+
} else {
155+
builder.setBottomRightCornerSize(PercentLength.toDevicePixels(options.cornerSizeBottomRight));
156+
}
145157
} else {
146158
builder.setBottomRightCornerSize(options.cornerSizeBottomRight);
147159
}
148160
}
149161
if (options.cornerSizeTopRight !== undefined) {
150162
if (typeof options.cornerSizeTopRight === 'object') {
151-
builder.setTopRightCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeTopRight.value));
163+
if (options.cornerSizeTopRight.unit === '%') {
164+
builder.setTopRightCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeTopRight.value));
165+
} else {
166+
builder.setTopRightCornerSize(PercentLength.toDevicePixels(options.cornerSizeTopRight));
167+
}
152168
} else {
153169
builder.setTopRightCornerSize(options.cornerSizeTopRight);
154170
}
155171
}
156172
if (options.cornerSizeTopLeft !== undefined) {
157173
if (typeof options.cornerSizeTopLeft === 'object') {
158-
builder.setTopLeftCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeTopLeft.value));
174+
if (options.cornerSizeTopLeft.unit === '%') {
175+
builder.setTopLeftCornerSize(new com.google.android.material.shape.RelativeCornerSize(options.cornerSizeTopLeft.value));
176+
} else {
177+
builder.setTopLeftCornerSize(PercentLength.toDevicePixels(options.cornerSizeTopLeft));
178+
}
159179
} else {
160180
builder.setTopLeftCornerSize(options.cornerSizeTopLeft);
161181
}

src/core/index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export interface TypographyOptions {
2121
import { CornerFamily } from './index.common';
2222
export { CornerFamily };
2323
export interface ShapeProperties {
24-
cornerSize?: number | CoreTypes.LengthPercentUnit;
25-
cornerSizeTopRight?: number | CoreTypes.LengthPercentUnit;
26-
cornerSizeBottomLeft?: number | CoreTypes.LengthPercentUnit;
27-
cornerSizeTopLeft?: number | CoreTypes.LengthPercentUnit;
28-
cornerSizeBottomRight?: number | CoreTypes.LengthPercentUnit;
24+
cornerSize?: number | CoreTypes.LengthPercentUnit | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit;
25+
cornerSizeTopRight?: number | CoreTypes.LengthPercentUnit | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit;
26+
cornerSizeBottomLeft?: number | CoreTypes.LengthPercentUnit | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit;
27+
cornerSizeTopLeft?: number | CoreTypes.LengthPercentUnit | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit;
28+
cornerSizeBottomRight?: number | CoreTypes.LengthPercentUnit | CoreTypes.LengthDipUnit | CoreTypes.LengthPxUnit;
2929
cornerFamily?: CornerFamily;
3030
cornerFamilyTopLeft?: CornerFamily;
3131
cornerFamilyTopRight?: CornerFamily;

src/core/index.ios.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
import { Background, Button, Color, ControlStateChangeListener, CoreTypes, GestureTypes, TouchAction, TouchGestureEventData, Utils, View, backgroundInternalProperty } from '@nativescript/core';
1+
import {
2+
Background,
3+
Button,
4+
Color,
5+
ControlStateChangeListener,
6+
CoreTypes,
7+
GestureTypes,
8+
PercentLength,
9+
TouchAction,
10+
TouchGestureEventData,
11+
Utils,
12+
View,
13+
backgroundInternalProperty
14+
} from '@nativescript/core';
215
import { ShapeProperties, TypographyOptions } from '.';
316
import { CornerFamily, applyMixins } from './index.common';
417
import { cssProperty, dynamicElevationOffsetProperty, elevationProperty, rippleColorProperty } from './cssproperties';
@@ -18,9 +31,17 @@ function cornerTreatment(cornerFamily: CornerFamily, cornerSize: number | CoreTy
1831
let corner: MDCCornerTreatment;
1932
if (typeof cornerSize === 'object') {
2033
if (cornerFamily === CornerFamily.CUT) {
21-
corner = MDCCornerTreatment.cornerWithCutValueType(cornerSize.value, MDCCornerTreatmentValueType.Percentage);
34+
if (cornerSize.unit === '%') {
35+
corner = MDCCornerTreatment.cornerWithCutValueType(cornerSize.value, MDCCornerTreatmentValueType.Percentage);
36+
} else {
37+
corner = MDCCornerTreatment.cornerWithCutValueType(PercentLength.toDevicePixels(cornerSize.value), MDCCornerTreatmentValueType.Absolute);
38+
}
2239
} else {
23-
corner = MDCCornerTreatment.cornerWithRadiusValueType(cornerSize.value, MDCCornerTreatmentValueType.Percentage);
40+
if (cornerSize.unit === '%') {
41+
corner = MDCCornerTreatment.cornerWithCutValueType(cornerSize.value, MDCCornerTreatmentValueType.Percentage);
42+
} else {
43+
corner = MDCCornerTreatment.cornerWithCutValueType(PercentLength.toDevicePixels(cornerSize.value), MDCCornerTreatmentValueType.Absolute);
44+
}
2445
}
2546
} else {
2647
if (cornerFamily === CornerFamily.ROUNDED) {

0 commit comments

Comments
 (0)