Skip to content

Commit 1f04469

Browse files
Nicuvtrifonov
andauthored
feat(css-bkg-pos): Added possibility to declare background pos by single numeric value (#7958)
* feat(css-bkg-pos): Added possibility to declare background pos by single numeric value * feat(css-bkg-pos): Implemented numeric bkg pos for iOS * feat(css-bkg-pos): removed unnecessary code Co-authored-by: Vasil Trifonov <v.trifonov@gmail.com>
1 parent 42fc4ac commit 1f04469

2 files changed

Lines changed: 54 additions & 20 deletions

File tree

nativescript-core/ui/styling/background.ios.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,23 @@ function parsePosition(pos: string): { x: CSSValue, y: CSSValue } {
204204
return { x: values[0], y: values[1] };
205205
}
206206

207-
if (values.length === 1 && values[0].type === "ident") {
208-
const val = values[0].string.toLocaleLowerCase();
209-
const center = { type: "ident", string: "center" };
210-
211-
// If you only one keyword is specified, the other value is "center"
212-
if (val === "left" || val === "right") {
213-
return { x: values[0], y: center };
214-
} else if (val === "top" || val === "bottom") {
215-
return { x: center, y: values[0] };
216-
} else if (val === "center") {
217-
return { x: center, y: center };
218-
}
207+
if (values.length === 1) {
208+
const center = { type: "ident", string: "center" };
209+
210+
if (values[0].type === "ident") {
211+
const val = values[0].string.toLocaleLowerCase();
212+
213+
// If you only one keyword is specified, the other value is "center"
214+
if (val === "left" || val === "right") {
215+
return { x: values[0], y: center };
216+
} else if (val === "top" || val === "bottom") {
217+
return { x: center, y: values[0] };
218+
} else if (val === "center") {
219+
return { x: center, y: center };
220+
}
221+
} else if (values[0].type === "number") {
222+
return {x: values[0], y: center};
223+
}
219224
}
220225

221226
return null;
@@ -322,6 +327,18 @@ function getDrawParams(this: void, image: UIImage, background: BackgroundDefinit
322327
} else if (v.y.string.toLowerCase() === "bottom") {
323328
res.posY = spaceY;
324329
}
330+
} else if (v.x.type === "number" && v.y.type === "ident") {
331+
if (v.x.unit === "%") {
332+
res.posX = spaceX * v.x.value / 100;
333+
} else if (v.x.unit === "px" || v.x.unit === "") {
334+
res.posX = v.x.value;
335+
}
336+
337+
if (v.y.string.toLowerCase() === "center") {
338+
res.posY = spaceY / 2;
339+
} else if (v.y.string.toLowerCase() === "bottom") {
340+
res.posY = spaceY;
341+
}
325342
}
326343
}
327344
}

tns-core-modules-widgets/android/widgets/src/main/java/org/nativescript/widgets/BorderDrawable.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,18 @@ private BackgroundDrawParams getDrawParams(float width, float height) {
713713
res.posX = spaceX;
714714
}
715715

716+
if ("center".equals(vy.getString().toLowerCase(Locale.ENGLISH))) {
717+
res.posY = spaceY / 2;
718+
} else if ("bottom".equals(vy.getString().toLowerCase(Locale.ENGLISH))) {
719+
res.posY = spaceY;
720+
}
721+
} else if ("number".equals(vx.getType()) && "ident".equals(vy.getType())) {
722+
if ("%".equals(vx.getUnit())) {
723+
res.posX = spaceX * vx.getValue() / 100;
724+
} else if ("px".equals(vx.getUnit()) || vx.getUnit() == null || vx.getUnit().isEmpty()) {
725+
res.posX = vx.getValue();
726+
}
727+
716728
if ("center".equals(vy.getString().toLowerCase(Locale.ENGLISH))) {
717729
res.posY = spaceY / 2;
718730
} else if ("bottom".equals(vy.getString().toLowerCase(Locale.ENGLISH))) {
@@ -731,17 +743,22 @@ private static CSSValue[] parsePosition(CSSValue[] values) {
731743
}
732744

733745
CSSValue[] result = null;
734-
if (values.length == 1 && "ident".equals(values[0].getType())) {
735-
String val = values[0].getString().toLowerCase(Locale.ENGLISH);
746+
if (values.length == 1) {
747+
// If you only one keyword is specified, the other value is "center"
736748
CSSValue center = new CSSValue("ident", "center", null, 0);
737749

738-
// If you only one keyword is specified, the other value is "center"
739-
if ("left".equals(val) || "right".equals(val)) {
750+
if ("ident".equals(values[0].getType())) {
751+
String val = values[0].getString().toLowerCase(Locale.ENGLISH);
752+
753+
if ("left".equals(val) || "right".equals(val)) {
754+
result = new CSSValue[]{values[0], center};
755+
} else if ("top".equals(val) || "bottom".equals(val)) {
756+
result = new CSSValue[]{center, values[0]};
757+
} else if ("center".equals(val)) {
758+
result = new CSSValue[]{center, center};
759+
}
760+
} else if ("number".equals(values[0].getType())) {
740761
result = new CSSValue[]{values[0], center};
741-
} else if ("top".equals(val) || "bottom".equals(val)) {
742-
result = new CSSValue[]{center, values[0]};
743-
} else if ("center".equals(val)) {
744-
result = new CSSValue[]{center, center};
745762
}
746763
}
747764

0 commit comments

Comments
 (0)