diff --git a/apps/app/ui-tests-app/animation/animation-army-100.ts b/apps/app/ui-tests-app/animation/animation-army-100.ts
new file mode 100644
index 000000000..7f74f1c2d
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/animation-army-100.ts
@@ -0,0 +1,114 @@
+import * as view from "tns-core-modules/ui/core/view";
+import { View } from "tns-core-modules/ui/core/view";
+import * as pages from "tns-core-modules/ui/page";
+import { Button } from "tns-core-modules/ui/button";
+import { SegmentedBar, SegmentedBarItem } from "tns-core-modules/ui/segmented-bar";
+import { Label } from "tns-core-modules/ui/label";
+import { Animation, AnimationDefinition } from "tns-core-modules/ui/animation";
+import * as fpsMeter from "tns-core-modules/fps-meter";
+
+let fpsCallbackId;
+export function onLoaded(args) {
+ const page = args.object;
+ const fpsLabel = view.getViewById(page, "fps") as Label;
+ fpsCallbackId = fpsMeter.addCallback((fps: number, minFps: number) => {
+ fpsLabel.text = `${fps.toFixed(2)}/${minFps.toFixed(2)}`;
+ });
+ fpsMeter.start();
+}
+
+export function onUnloaded() {
+ fpsMeter.removeCallback(fpsCallbackId);
+ fpsMeter.stop();
+}
+
+export function getBoxPropertyAnimationData(property: string,
+ animateEase: string,
+ target: View,
+ extentX: number = 128,
+ extentY: number = 128) {
+ let animateKey;
+ let animateValueTo;
+ let animateValueFrom;
+ let animateDuration = animateEase === "spring" ? 800 : 500;
+ let animateReturnDelay = animateEase === "spring" ? 0 : 200;
+
+ // Determine the full animation property name (some are shortened in UI), and the demo to/from values
+ switch (property) {
+ case "height":
+ target.originX = target.originY = 0.5;
+ animateKey = "height";
+ animateValueTo = 0;
+ animateValueFrom = extentY;
+ break;
+ case "width":
+ target.originX = target.originY = 0.5;
+ animateKey = "width";
+ animateValueTo = 0;
+ animateValueFrom = extentX;
+ break;
+ case "opacity":
+ animateKey = "opacity";
+ animateValueTo = 0;
+ animateValueFrom = 1;
+ break;
+ case "color":
+ animateKey = "backgroundColor";
+ animateValueTo = "blue";
+ animateValueFrom = "purple";
+ break;
+ case "rotate":
+ target.originX = target.originY = 0.5;
+ animateKey = "rotate";
+ animateValueTo = 180;
+ animateValueFrom = 0;
+ break;
+ case "scale":
+ target.originX = target.originY = 0.5;
+ animateKey = "scale";
+ animateValueTo = {x: 0.1, y: 0.1};
+ animateValueFrom = {x: 1, y: 1};
+ break;
+ default:
+ throw new Error(`demo animation for "${property}" is not implemented`);
+ }
+
+ return {
+ animateEase,
+ animateKey,
+ animateValueTo,
+ animateValueFrom,
+ animateReturnDelay,
+ animateDuration
+ };
+}
+
+export function easeAnimate(args) {
+ const clicked = args.object as Button;
+ const page: pages.Page = clicked.page;
+ const select = view.getViewById(page, "select") as SegmentedBar;
+ const item: SegmentedBarItem = select.items[select.selectedIndex];
+ const animsIn: AnimationDefinition[] = [];
+ const animsOut: AnimationDefinition[] = [];
+ for (let i = 0; i < 100; i++) {
+ const box = view.getViewById(page, "el-" + i) as Label;
+ const prop = getBoxPropertyAnimationData(item.title, clicked.text, box, 32, 24);
+ animsIn.push({
+ [prop.animateKey]: prop.animateValueTo,
+ delay: 15 * i,
+ target: box,
+ duration: prop.animateDuration,
+ curve: prop.animateEase
+ });
+ animsOut.push({
+ [prop.animateKey]: prop.animateValueFrom,
+ target: box,
+ delay: prop.animateReturnDelay + (5 * (Math.abs(i - 100))),
+ duration: prop.animateDuration,
+ curve: prop.animateEase
+ });
+ }
+ new Animation(animsIn, false).play()
+ .then(() => new Animation(animsOut, false).play())
+ .catch((e) => console.log(e));
+}
diff --git a/apps/app/ui-tests-app/animation/animation-army-100.xml b/apps/app/ui-tests-app/animation/animation-army-100.xml
new file mode 100644
index 000000000..d56f1558b
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/animation-army-100.xml
@@ -0,0 +1,157 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/apps/app/ui-tests-app/animation/animation-curves.ts b/apps/app/ui-tests-app/animation/animation-curves.ts
new file mode 100644
index 000000000..c90abe64d
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/animation-curves.ts
@@ -0,0 +1,72 @@
+import * as view from "tns-core-modules/ui/core/view";
+import * as pages from "tns-core-modules/ui/page";
+import { Button } from "tns-core-modules/ui/button";
+import { SegmentedBar, SegmentedBarItem } from "tns-core-modules/ui/segmented-bar";
+import { Label } from "tns-core-modules/ui/label";
+
+export function easeAnimate(args) {
+ const clicked = args.object as Button;
+ const page: pages.Page = clicked.page;
+ const target = view.getViewById(page, "target") as Label;
+ const select = view.getViewById(page, "select") as SegmentedBar;
+ const item: SegmentedBarItem = select.items[select.selectedIndex];
+ const easeType: string = clicked.text;
+ const extent = 128;
+ let duration = easeType === "spring" ? 800 : 500;
+ let delay = easeType === "spring" ? 0 : 200;
+ let animateKey: string = null;
+ let animateValueTo: any = null;
+ let animateValueFrom: any = null;
+
+ switch (item.title) {
+ case "height":
+ animateKey = "height";
+ target.originX = target.originY = 0;
+ animateValueTo = 0;
+ animateValueFrom = extent;
+ break;
+ case "width":
+ animateKey = "width";
+ target.originX = target.originY = 0;
+ animateValueTo = 0;
+ animateValueFrom = extent;
+ break;
+ case "opacity":
+ animateKey = "opacity";
+ animateValueTo = 0;
+ animateValueFrom = 1;
+ break;
+ case "color":
+ animateKey = "backgroundColor";
+ animateValueTo = "blue";
+ animateValueFrom = "purple";
+ break;
+ case "rotate":
+ animateKey = "rotate";
+ target.originX = target.originY = 0.5;
+ animateValueTo = 180;
+ animateValueFrom = 0;
+ break;
+ case "scale":
+ animateKey = "scale";
+ target.originX = target.originY = 0.5;
+ animateValueTo = {x: 1.5, y: 1.5};
+ animateValueFrom = {x: 1, y: 1};
+ break;
+ }
+ target
+ .animate({
+ [animateKey]: animateValueTo,
+ duration,
+ curve: easeType
+ })
+ .then(() => {
+ return target.animate({
+ [animateKey]: animateValueFrom,
+ delay,
+ duration,
+ curve: easeType
+ });
+ })
+ .catch((e) => console.log(e));
+}
diff --git a/apps/app/ui-tests-app/animation/animation-curves.xml b/apps/app/ui-tests-app/animation/animation-curves.xml
new file mode 100644
index 000000000..58f58d686
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/animation-curves.xml
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/apps/app/ui-tests-app/animation/effect-summary-details.ts b/apps/app/ui-tests-app/animation/effect-summary-details.ts
new file mode 100644
index 000000000..fafdf515a
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/effect-summary-details.ts
@@ -0,0 +1,79 @@
+import * as view from "tns-core-modules/ui/core/view";
+import * as pages from "tns-core-modules/ui/page";
+import * as platform from "tns-core-modules/platform";
+import { Animation } from "tns-core-modules/ui/animation";
+import { TextView } from "tns-core-modules/ui/text-view";
+import { isIOS } from "tns-core-modules/platform";
+
+let toggle = false;
+
+export function pageLoaded(args) {
+ const page = args.object;
+ const screenHeight = platform.screen.mainScreen.heightDIPs;
+ const screenYCenter = (screenHeight / 2);
+ page.bindingContext = {
+ screenHeight,
+ screenYCenter,
+ detailsHeight: 96,
+ summary: "Space! 🌌",
+ ipsum: `Houston, Tranquillity Base here. The Eagle has landed.
+
+For those who have seen the Earth from space, and for the hundreds and perhaps thousands more who will, the experience most certainly changes your perspective. The things that we share in our world are far more valuable than those which divide us.
+
+As we got further and further away, it [the Earth] diminished in size. Finally it shrank to the size of a marble, the most beautiful you can imagine. That beautiful, warm, living object looked so fragile, so delicate, that if you touched it with a finger it would crumble and fall apart. Seeing this has to change a man.
+
+What was most significant about the lunar voyage was not that man set foot on the Moon but that they set eye on the earth.
+
+Spaceflights cannot be stopped. This is not the work of any one man or even a group of men. It is a historical process which mankind is carrying out in accordance with the natural laws of human development.
+
+NASA is not about the ‘Adventure of Human Space Exploration’…We won’t be doing it just to get out there in space – we’ll be doing it because the things we learn out there will be making life better for a lot of people who won’t be able to go.
+
+Science has not yet mastered prophecy. We predict too much for the next year and yet far too little for the next 10.
+
+Science cuts two ways, of course; its products can be used for both good and evil. But there"s no turning back from science. The early warnings about technological dangers also come from science.
+
+Here men from the planet Earth first set foot upon the Moon. July 1969 AD. We came in peace for all mankind.
+
+When I orbited the Earth in a spaceship, I saw for the first time how beautiful our planet is. Mankind, let us preserve and increase this beauty, and not destroy it!
+
+http://spaceipsum.com`
+ };
+}
+
+export function theFinalFrontier(args) {
+ const clicked = args.object as view.View;
+ const page: pages.Page = clicked.page;
+ const details = view.getViewById(page, "details") as TextView;
+ const ctx = page.bindingContext;
+ const detailHeaderHeight: number = ctx.detailsHeight;
+
+ let statusBar = 0;
+ if (isIOS) {
+ const {ios} = require("tns-core-modules/ui/utils");
+ statusBar = ios.getStatusBarHeight();
+ }
+
+ const textViewHeight: number = ctx.screenHeight - statusBar - detailHeaderHeight;
+ const transitions = [
+ {
+ target: clicked,
+ height: toggle ? "100%" : detailHeaderHeight,
+ duration: 200,
+ curve: "ease"
+ },
+ {
+ target: details,
+ opacity: toggle ? 0 : 1,
+ height: textViewHeight,
+ translate: {
+ x: 0,
+ y: toggle ? 50 : 0,
+ },
+ duration: 200,
+ curve: "easeIn"
+ }
+ ];
+ const animationSet = new Animation(transitions, false);
+ animationSet.play();
+ toggle = !toggle;
+}
diff --git a/apps/app/ui-tests-app/animation/effect-summary-details.xml b/apps/app/ui-tests-app/animation/effect-summary-details.xml
new file mode 100644
index 000000000..41db9265c
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/effect-summary-details.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
diff --git a/apps/app/ui-tests-app/animation/height-basic.ts b/apps/app/ui-tests-app/animation/height-basic.ts
new file mode 100644
index 000000000..8cd68627a
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/height-basic.ts
@@ -0,0 +1,17 @@
+import { Label } from "tns-core-modules/ui/label";
+
+let toggle = false;
+
+export function animateHeight(args) {
+ const clicked = args.object as Label;
+ clicked
+ .animate({
+ height: toggle ? 128 : "100%",
+ duration: 200,
+ curve: "easeInOut"
+ })
+ .then(() => {
+ clicked.text = toggle ? "Cool." : "Tap here";
+ });
+ toggle = !toggle;
+}
diff --git a/apps/app/ui-tests-app/animation/height-basic.xml b/apps/app/ui-tests-app/animation/height-basic.xml
new file mode 100644
index 000000000..566873a12
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/height-basic.xml
@@ -0,0 +1,10 @@
+
+
+
+
+
diff --git a/apps/app/ui-tests-app/animation/layout-stack-height.ts b/apps/app/ui-tests-app/animation/layout-stack-height.ts
new file mode 100644
index 000000000..09c5135df
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/layout-stack-height.ts
@@ -0,0 +1,12 @@
+import * as view from "tns-core-modules/ui/core/view";
+
+export function tapLabel(args) {
+ const clicked: view.View = args.object;
+ const graffiti = clicked as any;
+ clicked.animate({
+ height: graffiti.toggle ? 64 : 128,
+ duration: 200,
+ curve: "easeOut"
+ });
+ graffiti.toggle = !graffiti.toggle;
+}
diff --git a/apps/app/ui-tests-app/animation/layout-stack-height.xml b/apps/app/ui-tests-app/animation/layout-stack-height.xml
new file mode 100644
index 000000000..f33b85e3b
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/layout-stack-height.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
diff --git a/apps/app/ui-tests-app/animation/main-page.ts b/apps/app/ui-tests-app/animation/main-page.ts
new file mode 100644
index 000000000..56d2ee64e
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/main-page.ts
@@ -0,0 +1,20 @@
+import { EventData } from "tns-core-modules/data/observable";
+import { SubMainPageViewModel } from "../sub-main-page-view-model";
+import { WrapLayout } from "tns-core-modules/ui/layouts/wrap-layout";
+import { Page } from "tns-core-modules/ui/page";
+
+export function pageLoaded(args: EventData) {
+ const page = args.object;
+ const wrapLayout = page.getViewById("wrapLayoutWithExamples");
+ page.bindingContext = new SubMainPageViewModel(wrapLayout, loadExamples());
+}
+
+export function loadExamples() {
+ const examples = new Map();
+ examples.set("animation-curves", "animation/animation-curves");
+ examples.set("animation-army-100", "animation/animation-army-100");
+ examples.set("height-basic", "animation/height-basic");
+ examples.set("layout-stack-height", "animation/layout-stack-height");
+ examples.set("effect-summary-details", "animation/effect-summary-details");
+ return examples;
+}
diff --git a/apps/app/ui-tests-app/animation/main-page.xml b/apps/app/ui-tests-app/animation/main-page.xml
new file mode 100644
index 000000000..33306f0d0
--- /dev/null
+++ b/apps/app/ui-tests-app/animation/main-page.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apps/app/ui-tests-app/main-page.ts b/apps/app/ui-tests-app/main-page.ts
index 2e6c4fd49..314fd0073 100644
--- a/apps/app/ui-tests-app/main-page.ts
+++ b/apps/app/ui-tests-app/main-page.ts
@@ -8,6 +8,7 @@ export function pageLoaded(args: EventData) {
const page = args.object;
const wrapLayout = page.getViewById("wrapLayoutWithExamples");
const examples: Map = new Map();
+ examples.set("animation", "animation/main-page");
examples.set("action-bar", "action-bar/main-page");
examples.set("bindings", "bindings/main-page");
examples.set("button", "button/main-page");
@@ -47,4 +48,4 @@ export function pageLoaded(args: EventData) {
} else {
parent.style.marginBottom = 10;
}
-}
\ No newline at end of file
+}
diff --git a/apps/app/ui-tests-app/test-page-main-view-model.ts b/apps/app/ui-tests-app/test-page-main-view-model.ts
index 8ba190bfb..bdd6faa1e 100644
--- a/apps/app/ui-tests-app/test-page-main-view-model.ts
+++ b/apps/app/ui-tests-app/test-page-main-view-model.ts
@@ -35,6 +35,7 @@ export class TestPageMainViewModel extends Observable {
try {
frame.topmost().navigate(TestPageMainViewModel.APP_NAME + "/" + exampleFullPath);
} catch (error) {
+ console.log("EXAMPLE LOAD FAILED:" + error);
alert("Cannot find example: " + exampleFullPath);
}
diff --git a/tests/app/ui/animation/animation-tests.ts b/tests/app/ui/animation/animation-tests.ts
index 4274bdf69..2652cfc8e 100644
--- a/tests/app/ui/animation/animation-tests.ts
+++ b/tests/app/ui/animation/animation-tests.ts
@@ -9,14 +9,22 @@ import { AnimationPromise } from "tns-core-modules/ui/animation";
// >> animation-require
import * as animation from "tns-core-modules/ui/animation";
+import { PercentLength } from "tns-core-modules/ui/styling/style-properties";
// << animation-require
-function prepareTest(): Label {
+function prepareTest(parentHeight?: number, parentWidth?: number): Label {
let mainPage = helper.getCurrentPage();
let label = new Label();
label.text = "label";
let stackLayout = new StackLayout();
+ // optionally size the parent extent to make assertions about percentage values
+ if (parentHeight !== undefined) {
+ stackLayout.height = PercentLength.parse(parentHeight + "");
+ }
+ if (parentWidth !== undefined) {
+ stackLayout.width = PercentLength.parse(parentWidth + "");
+ }
stackLayout.addChild(label);
mainPage.content = stackLayout;
TKUnit.waitUntilReady(() => label.isLoaded);
@@ -395,6 +403,110 @@ export function test_AnimateRotate(done) {
});
}
+function animateExtentAndAssertExpected(along: "height" | "width", value: PercentLength, pixelExpected: PercentLength): Promise {
+ function pretty(val) {
+ return JSON.stringify(val, null, 2);
+ }
+ const parentExtent = 200;
+ const height = along === "height" ? parentExtent : undefined;
+ const width = along === "height" ? undefined : parentExtent;
+ const label = prepareTest(height, width);
+ const props = {
+ duration: 5,
+ [along]: value
+ };
+ return label.animate(props).then(() => {
+ const observedString: string = PercentLength.convertToString(label[along]);
+ const inputString: string = PercentLength.convertToString(value);
+ TKUnit.assertEqual(
+ observedString,
+ inputString,
+ `PercentLength.convertToString(${pretty(value)}) should be "${inputString}" but is "${observedString}"`
+ );
+ // assert that the animated view"s calculated pixel extent matches the expected pixel value
+ const observedNumber: number = PercentLength.toDevicePixels(label[along], parentExtent, parentExtent);
+ const expectedNumber: number = PercentLength.toDevicePixels(pixelExpected, parentExtent, parentExtent);
+ TKUnit.assertEqual(
+ observedNumber,
+ expectedNumber,
+ `PercentLength.toDevicePixels(${inputString}) should be "${expectedNumber}" but is "${observedNumber}"`
+ );
+ assertIOSNativeTransformIsCorrect(label);
+ });
+}
+
+export function test_AnimateExtent_Should_ResolvePercentageStrings(done) {
+ let promise: Promise = Promise.resolve();
+ const pairs: [string, string][] = [
+ ["100%", "200px"],
+ ["50%", "100px"],
+ ["25%", "50px"],
+ ["-25%", "-50px"],
+ ["-50%", "-100px"],
+ ["-100%", "-200px"],
+ ];
+ pairs.forEach((pair) => {
+ const input = PercentLength.parse(pair[0]);
+ const expected = PercentLength.parse(pair[1]);
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("height", input, expected);
+ });
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("width", input, expected);
+ });
+ });
+ promise.then(() => done()).catch(done);
+}
+
+export function test_AnimateExtent_Should_AcceptStringPixelValues(done) {
+ let promise: Promise = Promise.resolve();
+ const pairs: [string, number][] = [
+ ["100px", 100],
+ ["50px", 50]
+ ];
+ pairs.forEach((pair) => {
+ const input = PercentLength.parse(pair[0]);
+ const expected = {
+ unit: "px",
+ value: pair[1]
+ } as PercentLength;
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("height", input, expected);
+ });
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("width", input, expected);
+ });
+ });
+ promise.then(() => done()).catch(done);
+}
+
+export function test_AnimateExtent_Should_AcceptNumberValuesAsDip(done) {
+ let promise: Promise = Promise.resolve();
+ const inputs: any[] = [200, 150, 100, 50, 0];
+ inputs.forEach((value) => {
+ const parsed = PercentLength.parse(value);
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("height", parsed, parsed);
+ });
+ promise = promise.then(() => {
+ return animateExtentAndAssertExpected("width", parsed, parsed);
+ });
+ });
+ promise.then(() => done()).catch(done);
+}
+
+export function test_AnimateExtent_Should_ThrowIfCannotParsePercentLength() {
+ const label = new Label();
+ helper.buildUIAndRunTest(label, (views: Array) => {
+ TKUnit.assertThrows(() => {
+ label.animate({width: "-frog%"});
+ }, "Invalid percent string should throw");
+ TKUnit.assertThrows(() => {
+ label.animate({height: "-frog%"});
+ }, "Invalid percent string should throw");
+ });
+}
+
export function test_AnimateTranslateScaleAndRotateSimultaneously(done) {
let label = prepareTest();
diff --git a/tests/app/ui/styling/style-properties-tests.ts b/tests/app/ui/styling/style-properties-tests.ts
index 1c2b7f19f..83f6dc66f 100644
--- a/tests/app/ui/styling/style-properties-tests.ts
+++ b/tests/app/ui/styling/style-properties-tests.ts
@@ -10,6 +10,7 @@ import { isAndroid, isIOS } from "tns-core-modules/platform";
import { View } from "tns-core-modules/ui/core/view";
import { Length, PercentLength } from "tns-core-modules/ui/core/view";
import * as fontModule from "tns-core-modules/ui/styling/font";
+import { LengthPercentUnit, LengthPxUnit } from "tns-core-modules/ui/styling/style-properties";
export function test_setting_textDecoration_property_from_CSS_is_applied_to_Style() {
test_property_from_CSS_is_applied_to_style("textDecoration", "text-decoration", "underline");
@@ -862,3 +863,57 @@ export function test_border_radius() {
TKUnit.assertTrue(Length.equals(testView.style.borderBottomRightRadius, expected), "bottom");
TKUnit.assertTrue(Length.equals(testView.style.borderBottomLeftRadius, expected), "left");
}
+
+function assertPercentLengthParseInputOutputPairs(pairs: [string, any][]) {
+ pairs.forEach((pair: [string, any]) => {
+ const output = PercentLength.parse(pair[0]) as LengthPxUnit | LengthPercentUnit;
+ TKUnit.assertEqual(pair[1].unit, output.unit,
+ `PercentLength.parse('${pair[0]}') should return unit '${pair[1].unit}' but returned '${output.unit}'`
+ );
+ TKUnit.assertEqual(pair[1].value.toFixed(2), output.value.toFixed(2),
+ `PercentLength.parse('${pair[0]}') should return value '${pair[1].value}' but returned '${output.value}'`
+ );
+ });
+}
+
+export function test_PercentLength_parses_pixel_values_from_string_input() {
+ assertPercentLengthParseInputOutputPairs([
+ ["4px", {unit: "px", value: 4}],
+ ["-4px", {unit: "px", value: -4}],
+ ]);
+}
+
+export function test_PercentLength_parses_percentage_values_from_string_input() {
+ assertPercentLengthParseInputOutputPairs([
+ ["4%", {unit: "%", value: 0.04}],
+ ["17%", {unit: "%", value: 0.17}],
+ ["-27%", {unit: "%", value: -0.27}],
+ ]);
+}
+
+export function test_PercentLength_parse_throws_given_string_input_it_cannot_parse() {
+ const inputs: any[] = [
+ "-l??%",
+ "qre%",
+ "undefinedpx",
+ "undefined",
+ "-frog%"
+ ];
+ inputs.forEach((input) => {
+ TKUnit.assertThrows(() => {
+ PercentLength.parse(input);
+ }, `PercentLength.parse('${input}') should throw.`);
+ });
+}
+
+export function test_PercentLength_returns_unsupported_types_untouched() {
+ const inputs: any[] = [
+ null,
+ undefined,
+ {baz: true}
+ ];
+ inputs.forEach((input) => {
+ const result = PercentLength.parse(input);
+ TKUnit.assertEqual(input, result, `PercentLength.parse(${input}) should return input value`);
+ });
+}
diff --git a/tns-core-modules/ui/animation/animation-common.ts b/tns-core-modules/ui/animation/animation-common.ts
index bf021e52e..d88e3f00d 100644
--- a/tns-core-modules/ui/animation/animation-common.ts
+++ b/tns-core-modules/ui/animation/animation-common.ts
@@ -11,6 +11,7 @@ import { View } from "../core/view";
// Types.
import { Color } from "../../color";
import { isEnabled as traceEnabled, write as traceWrite, categories as traceCategories, messageType as traceType } from "../../trace";
+import { PercentLength } from "../styling/style-properties";
export { Color, traceEnabled, traceWrite, traceCategories, traceType };
export { AnimationPromise } from ".";
@@ -21,6 +22,8 @@ export module Properties {
export const translate = "translate";
export const rotate = "rotate";
export const scale = "scale";
+ export const height = "height";
+ export const width = "width";
}
export interface PropertyAnimation {
@@ -166,6 +169,9 @@ export abstract class AnimationBase implements AnimationBaseDefinition {
throw new Error(`Property ${item} must be valid Pair. Value: ${animationDefinition[item]}`);
} else if (item === Properties.backgroundColor && !Color.isValid(animationDefinition.backgroundColor)) {
throw new Error(`Property ${item} must be valid color. Value: ${animationDefinition[item]}`);
+ } else if (item === Properties.width || item === Properties.height) {
+ // Coerce input into a PercentLength object in case it's a string.
+ animationDefinition[item] = PercentLength.parse(animationDefinition[item]);
}
}
@@ -237,8 +243,34 @@ export abstract class AnimationBase implements AnimationBaseDefinition {
});
}
+ // height
+ if (animationDefinition.height !== undefined) {
+ propertyAnimations.push({
+ target: animationDefinition.target,
+ property: Properties.height,
+ value: animationDefinition.height,
+ duration: animationDefinition.duration,
+ delay: animationDefinition.delay,
+ iterations: animationDefinition.iterations,
+ curve: animationDefinition.curve
+ });
+ }
+
+ // width
+ if (animationDefinition.width !== undefined) {
+ propertyAnimations.push({
+ target: animationDefinition.target,
+ property: Properties.width,
+ value: animationDefinition.width,
+ duration: animationDefinition.duration,
+ delay: animationDefinition.delay,
+ iterations: animationDefinition.iterations,
+ curve: animationDefinition.curve
+ });
+ }
+
if (propertyAnimations.length === 0) {
- throw new Error("No animation property specified.");
+ throw new Error("No known animation properties specified");
}
return propertyAnimations;
@@ -255,4 +287,4 @@ export abstract class AnimationBase implements AnimationBaseDefinition {
curve: animation.curve
});
}
-}
\ No newline at end of file
+}
diff --git a/tns-core-modules/ui/animation/animation.android.ts b/tns-core-modules/ui/animation/animation.android.ts
index b536c6937..549be00e9 100644
--- a/tns-core-modules/ui/animation/animation.android.ts
+++ b/tns-core-modules/ui/animation/animation.android.ts
@@ -5,13 +5,13 @@ import { View } from "../core/view";
import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, Color, traceWrite, traceEnabled, traceCategories, traceType } from "./animation-common";
import {
opacityProperty, backgroundColorProperty, rotateProperty,
- translateXProperty, translateYProperty, scaleXProperty, scaleYProperty
+ translateXProperty, translateYProperty, scaleXProperty, scaleYProperty,
+ heightProperty, widthProperty, PercentLength
} from "../styling/style-properties";
import { layout } from "../../utils/utils";
-import { device } from "../../platform";
+import { device, screen } from "../../platform";
import lazy from "../../utils/lazy";
-
export * from "./animation-common";
interface AnimationDefinitionInternal extends AnimationDefinition {
@@ -38,6 +38,8 @@ propertyKeys[Properties.opacity] = Symbol(keyPrefix + Properties.opacity);
propertyKeys[Properties.rotate] = Symbol(keyPrefix + Properties.rotate);
propertyKeys[Properties.scale] = Symbol(keyPrefix + Properties.scale);
propertyKeys[Properties.translate] = Symbol(keyPrefix + Properties.translate);
+propertyKeys[Properties.height] = Symbol(keyPrefix + Properties.height);
+propertyKeys[Properties.width] = Symbol(keyPrefix + Properties.width);
export function _resolveAnimationCurve(curve: string | CubicBezierAnimationCurve | android.view.animation.Interpolator | android.view.animation.LinearInterpolator): android.view.animation.Interpolator {
switch (curve) {
@@ -224,7 +226,7 @@ export class Animation extends AnimationBase {
}
}
- private _onAndroidAnimationCancel() { // tslint:disable-line
+ private _onAndroidAnimationCancel() { // tslint:disable-line
this._propertyResetCallbacks.forEach(v => v());
this._rejectAnimationFinishedPromise();
@@ -450,9 +452,48 @@ export class Animation extends AnimationBase {
}));
animators.push(android.animation.ObjectAnimator.ofFloat(nativeView, "rotation", nativeArray));
break;
+ case Properties.width:
+ case Properties.height: {
+ const isVertical: boolean = propertyAnimation.property === "height";
+ const extentProperty = isVertical ? heightProperty : widthProperty;
+
+ extentProperty._initDefaultNativeValue(style);
+ nativeArray = Array.create("float", 2);
+ let toValue = propertyAnimation.value;
+ let parent = propertyAnimation.target.parent as View;
+ if (!parent) {
+ throw new Error(`cannot animate ${propertyAnimation.property} on root view`);
+ }
+ const parentExtent: number = isVertical ? parent.getMeasuredHeight() : parent.getMeasuredWidth();
+ toValue = PercentLength.toDevicePixels(toValue, parentExtent, parentExtent) / screen.mainScreen.scale;
+ const nativeHeight: number = isVertical ? nativeView.getHeight() : nativeView.getWidth();
+ const targetStyle: string = setLocal ? extentProperty.name : extentProperty.keyframe;
+ originalValue1 = nativeHeight / screen.mainScreen.scale;
+ nativeArray[0] = originalValue1;
+ nativeArray[1] = toValue;
+ let extentAnimator = android.animation.ValueAnimator.ofFloat(nativeArray);
+ extentAnimator.addUpdateListener(new android.animation.ValueAnimator.AnimatorUpdateListener({
+ onAnimationUpdate(animator: android.animation.ValueAnimator) {
+ const argb = (animator.getAnimatedValue()).floatValue();
+ propertyAnimation.target.style[setLocal ? extentProperty.name : extentProperty.keyframe] = argb;
+ }
+ }));
+ propertyUpdateCallbacks.push(checkAnimation(() => {
+ propertyAnimation.target.style[targetStyle] = propertyAnimation.value;
+ }));
+ propertyResetCallbacks.push(checkAnimation(() => {
+ propertyAnimation.target.style[targetStyle] = originalValue1;
+ if (propertyAnimation.target.nativeViewProtected) {
+ const setter = propertyAnimation.target[extentProperty.setNative];
+ setter(propertyAnimation.target.style[propertyAnimation.property]);
+ }
+ }));
+ animators.push(extentAnimator);
+ break;
+ }
default:
- throw new Error("Cannot animate " + propertyAnimation.property);
+ throw new Error(`Animating property '${propertyAnimation.property}' is unsupported`);
}
for (let i = 0, length = animators.length; i < length; i++) {
diff --git a/tns-core-modules/ui/animation/animation.d.ts b/tns-core-modules/ui/animation/animation.d.ts
index 02e42000c..06f8acd25 100644
--- a/tns-core-modules/ui/animation/animation.d.ts
+++ b/tns-core-modules/ui/animation/animation.d.ts
@@ -3,6 +3,7 @@
*/ /** */
import { View, Color } from "../core/view";
+import { PercentLength } from "../styling/style-properties";
/**
* Defines animation options for the View.animate method.
@@ -33,6 +34,16 @@ export interface AnimationDefinition {
*/
scale?: Pair;
+ /**
+ * Animates the height of a view.
+ */
+ height?: PercentLength | string;
+
+ /**
+ * Animates the width of a view.
+ */
+ width?: PercentLength | string;
+
/**
* Animates the rotate affine transform of the view. Value should be a number specifying the rotation amount in degrees.
*/
diff --git a/tns-core-modules/ui/animation/animation.ios.ts b/tns-core-modules/ui/animation/animation.ios.ts
index 2007b637e..33b08333a 100644
--- a/tns-core-modules/ui/animation/animation.ios.ts
+++ b/tns-core-modules/ui/animation/animation.ios.ts
@@ -4,9 +4,12 @@ import { View } from "../core/view";
import { AnimationBase, Properties, PropertyAnimation, CubicBezierAnimationCurve, AnimationPromise, traceWrite, traceEnabled, traceCategories, traceType } from "./animation-common";
import {
opacityProperty, backgroundColorProperty, rotateProperty,
- translateXProperty, translateYProperty, scaleXProperty, scaleYProperty
+ translateXProperty, translateYProperty, scaleXProperty, scaleYProperty,
+ heightProperty, widthProperty, PercentLength
} from "../styling/style-properties";
+import { screen } from "../../platform";
+
export * from "./animation-common";
let _transform = "_transform";
@@ -78,6 +81,12 @@ class AnimationDelegateImpl extends NSObject implements CAAnimationDelegate {
targetStyle[setLocal ? translateXProperty.name : translateXProperty.keyframe] = value.x;
targetStyle[setLocal ? translateYProperty.name : translateYProperty.keyframe] = value.y;
break;
+ case Properties.height:
+ targetStyle[setLocal ? heightProperty.name : heightProperty.keyframe] = value;
+ break;
+ case Properties.width:
+ targetStyle[setLocal ? widthProperty.name : widthProperty.keyframe] = value;
+ break;
case Properties.scale:
targetStyle[setLocal ? scaleXProperty.name : scaleXProperty.keyframe] = value.x === 0 ? 0.001 : value.x;
targetStyle[setLocal ? scaleYProperty.name : scaleYProperty.keyframe] = value.y === 0 ? 0.001 : value.y;
@@ -263,8 +272,10 @@ export class Animation extends AnimationBase {
let nativeView = animation.target.nativeViewProtected;
let propertyNameToAnimate = animation.property;
- let value = animation.value;
- let originalValue;
+ let toValue = animation.value;
+ let fromValue;
+ const parent = animation.target.parent as View;
+ const screenScale: number = screen.mainScreen.scale;
let tempRotate = (animation.target.rotate || 0) * Math.PI / 180;
let abs;
@@ -277,18 +288,18 @@ export class Animation extends AnimationBase {
animation._propertyResetCallback = (value, valueSource) => {
animation.target.style[setLocal ? backgroundColorProperty.name : backgroundColorProperty.keyframe] = value;
};
- originalValue = nativeView.layer.backgroundColor;
+ fromValue = nativeView.layer.backgroundColor;
if (nativeView instanceof UILabel) {
nativeView.setValueForKey(UIColor.clearColor, "backgroundColor");
}
- value = value.CGColor;
+ toValue = toValue.CGColor;
break;
case Properties.opacity:
animation._originalValue = animation.target.opacity;
animation._propertyResetCallback = (value, valueSource) => {
animation.target.style[setLocal ? opacityProperty.name : opacityProperty.keyframe] = value;
};
- originalValue = nativeView.layer.opacity;
+ fromValue = nativeView.layer.opacity;
break;
case Properties.rotate:
animation._originalValue = animation.target.rotate !== undefined ? animation.target.rotate : 0;
@@ -296,44 +307,44 @@ export class Animation extends AnimationBase {
animation.target.style[setLocal ? rotateProperty.name : rotateProperty.keyframe] = value;
};
propertyNameToAnimate = "transform.rotation";
- originalValue = nativeView.layer.valueForKeyPath("transform.rotation");
- if (animation.target.rotate !== undefined && animation.target.rotate !== 0 && Math.floor(value / 360) - value / 360 === 0) {
- originalValue = animation.target.rotate * Math.PI / 180;
+ fromValue = nativeView.layer.valueForKeyPath("transform.rotation");
+ if (animation.target.rotate !== undefined && animation.target.rotate !== 0 && Math.floor(toValue / 360) - toValue / 360 === 0) {
+ fromValue = animation.target.rotate * Math.PI / 180;
}
- value = value * Math.PI / 180;
- abs = fabs(originalValue - value);
- if (abs < 0.001 && originalValue !== tempRotate) {
- originalValue = tempRotate;
+ toValue = toValue * Math.PI / 180;
+ abs = fabs(fromValue - toValue);
+ if (abs < 0.001 && fromValue !== tempRotate) {
+ fromValue = tempRotate;
}
break;
case Properties.translate:
- animation._originalValue = { x: animation.target.translateX, y: animation.target.translateY };
+ animation._originalValue = {x: animation.target.translateX, y: animation.target.translateY};
animation._propertyResetCallback = (value, valueSource) => {
animation.target.style[setLocal ? translateXProperty.name : translateXProperty.keyframe] = value.x;
animation.target.style[setLocal ? translateYProperty.name : translateYProperty.keyframe] = value.y;
};
propertyNameToAnimate = "transform";
- originalValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
- value = NSValue.valueWithCATransform3D(CATransform3DTranslate(nativeView.layer.transform, value.x, value.y, 0));
+ fromValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
+ toValue = NSValue.valueWithCATransform3D(CATransform3DTranslate(nativeView.layer.transform, toValue.x, toValue.y, 0));
break;
case Properties.scale:
- if (value.x === 0) {
- value.x = 0.001;
+ if (toValue.x === 0) {
+ toValue.x = 0.001;
}
- if (value.y === 0) {
- value.y = 0.001;
+ if (toValue.y === 0) {
+ toValue.y = 0.001;
}
- animation._originalValue = { x: animation.target.scaleX, y: animation.target.scaleY };
+ animation._originalValue = {x: animation.target.scaleX, y: animation.target.scaleY};
animation._propertyResetCallback = (value, valueSource) => {
animation.target.style[setLocal ? scaleXProperty.name : scaleXProperty.keyframe] = value.x;
animation.target.style[setLocal ? scaleYProperty.name : scaleYProperty.keyframe] = value.y;
};
propertyNameToAnimate = "transform";
- originalValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
- value = NSValue.valueWithCATransform3D(CATransform3DScale(nativeView.layer.transform, value.x, value.y, 1));
+ fromValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
+ toValue = NSValue.valueWithCATransform3D(CATransform3DScale(nativeView.layer.transform, toValue.x, toValue.y, 1));
break;
case _transform:
- originalValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
+ fromValue = NSValue.valueWithCATransform3D(nativeView.layer.transform);
animation._originalValue = {
xs: animation.target.scaleX, ys: animation.target.scaleY,
xt: animation.target.translateX, yt: animation.target.translateY
@@ -345,10 +356,33 @@ export class Animation extends AnimationBase {
animation.target.style[setLocal ? scaleYProperty.name : scaleYProperty.keyframe] = value.ys;
};
propertyNameToAnimate = "transform";
- value = NSValue.valueWithCATransform3D(Animation._createNativeAffineTransform(animation));
+ toValue = NSValue.valueWithCATransform3D(Animation._createNativeAffineTransform(animation));
+ break;
+ case Properties.width:
+ case Properties.height:
+ const direction: string = animation.property;
+ const isHeight: boolean = direction === "height";
+ propertyNameToAnimate = "bounds";
+ if (!parent) {
+ throw new Error(`cannot animate ${direction} on root view`);
+ }
+ const parentExtent: number = isHeight ? parent.getMeasuredHeight() : parent.getMeasuredWidth();
+ const asNumber = PercentLength.toDevicePixels(PercentLength.parse(toValue), parentExtent, parentExtent) / screenScale;
+ let currentBounds = nativeView.layer.bounds;
+ let extentX = isHeight ? currentBounds.size.width : asNumber;
+ let extentY = isHeight ? asNumber : currentBounds.size.height;
+ fromValue = NSValue.valueWithCGRect(currentBounds);
+ toValue = NSValue.valueWithCGRect(
+ CGRectMake(currentBounds.origin.x, currentBounds.origin.y, extentX, extentY)
+ );
+ animation._originalValue = animation.target.height;
+ animation._propertyResetCallback = (value, valueSource) => {
+ const prop = isHeight ? heightProperty : widthProperty;
+ animation.target.style[setLocal ? prop.name : prop.keyframe] = value;
+ };
break;
default:
- throw new Error("Cannot animate " + animation.property);
+ throw new Error(`Animating property '${animation.property}' is unsupported`);
}
let duration = 0.3;
@@ -373,8 +407,8 @@ export class Animation extends AnimationBase {
return {
propertyNameToAnimate: propertyNameToAnimate,
- fromValue: originalValue,
- toValue: value,
+ fromValue: fromValue,
+ toValue: toValue,
duration: duration,
repeatCount: repeatCount,
delay: delay
@@ -450,6 +484,14 @@ export class Animation extends AnimationBase {
case Properties.opacity:
animation.target.opacity = args.toValue;
break;
+ case Properties.height:
+ case Properties.width:
+ animation._originalValue = animation.target[animation.property];
+ nativeView.layer.setValueForKey(args.toValue, args.propertyNameToAnimate);
+ animation._propertyResetCallback = function (value) {
+ animation.target[animation.property] = value;
+ };
+ break;
case Properties.rotate:
nativeView.layer.setValueForKey(args.toValue, args.propertyNameToAnimate);
break;
@@ -458,11 +500,11 @@ export class Animation extends AnimationBase {
nativeView.layer.setValueForKey(args.toValue, args.propertyNameToAnimate);
animation._propertyResetCallback = function (value) {
nativeView.layer.transform = value;
- }
+ };
break;
}
- }, function (finished: boolean) {
- if (finished) {
+ }, function (animationDidFinish: boolean) {
+ if (animationDidFinish) {
if (animation.property === _transform) {
if (animation.value[Properties.translate] !== undefined) {
animation.target.translateX = animation.value[Properties.translate].x;
@@ -480,10 +522,10 @@ export class Animation extends AnimationBase {
}
}
if (finishedCallback) {
- let cancelled = !finished;
+ let cancelled = !animationDidFinish;
finishedCallback(cancelled);
}
- if (finished && nextAnimation) {
+ if (animationDidFinish && nextAnimation) {
nextAnimation();
}
});
diff --git a/tns-core-modules/ui/animation/keyframe-animation.ts b/tns-core-modules/ui/animation/keyframe-animation.ts
index 49e6887bc..b95d83863 100644
--- a/tns-core-modules/ui/animation/keyframe-animation.ts
+++ b/tns-core-modules/ui/animation/keyframe-animation.ts
@@ -21,7 +21,8 @@ import {
backgroundColorProperty,
scaleXProperty, scaleYProperty,
translateXProperty, translateYProperty,
- rotateProperty, opacityProperty
+ rotateProperty, opacityProperty,
+ widthProperty, heightProperty, PercentLength
} from "../styling/style-properties";
export class Keyframes implements KeyframesDefinition {
@@ -62,6 +63,8 @@ interface Keyframe {
translate?: { x: number, y: number };
rotate?: number;
opacity?: number;
+ width?: PercentLength;
+ height?: PercentLength;
valueSource?: "keyframe" | "animation";
duration?: number;
curve?: any;
@@ -213,6 +216,12 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
if ("opacity" in animation) {
view.style[opacityProperty.keyframe] = animation.opacity;
}
+ if ("height" in animation) {
+ view.style[heightProperty.keyframe] = animation.height;
+ }
+ if ("width" in animation) {
+ view.style[widthProperty.keyframe] = animation.width;
+ }
setTimeout(() => this.animate(view, 1, iterations), 1);
}
@@ -287,5 +296,11 @@ export class KeyframeAnimation implements KeyframeAnimationDefinition {
if ("opacity" in animation) {
view.style[opacityProperty.keyframe] = unsetValue;
}
+ if ("height" in animation) {
+ view.style[heightProperty.keyframe] = unsetValue;
+ }
+ if ("width" in animation) {
+ view.style[widthProperty.keyframe] = unsetValue;
+ }
}
}
diff --git a/tns-core-modules/ui/styling/style-properties.d.ts b/tns-core-modules/ui/styling/style-properties.d.ts
index 163df0cdd..e0b907ace 100644
--- a/tns-core-modules/ui/styling/style-properties.d.ts
+++ b/tns-core-modules/ui/styling/style-properties.d.ts
@@ -85,8 +85,8 @@ export const opacityProperty: CssAnimationProperty