From 6997a5a71948574c42728d6f3e4a70cc68bcc2e2 Mon Sep 17 00:00:00 2001 From: hariroshan Date: Sat, 25 Feb 2023 17:18:54 +0530 Subject: [PATCH] added cubicBezier for pageTransition --- elm-native-js/src/Native/Native.res | 9 ++++ elm-native-js/src/Native/NativescriptCore.res | 52 +++++++++++++++++++ elm-native/src/Native/Frame.elm | 12 +++++ 3 files changed, 73 insertions(+) diff --git a/elm-native-js/src/Native/Native.res b/elm-native-js/src/Native/Native.res index b5b2994..3ef350a 100644 --- a/elm-native-js/src/Native/Native.res +++ b/elm-native-js/src/Native/Native.res @@ -10,6 +10,15 @@ module Frame = { ) => current.navigationOptions ->Js.Nullable.toOption + ->Belt.Option.map(x => { + ...x, + transition: x.transition + ->Js.Nullable.toOption + ->Belt.Option.map(transition => + transition->NativescriptCore.AnimationCurve.convertTransitionCurve->Js.Nullable.return + ) + ->Belt.Option.getWithDefault(x.transition), + }) ->Belt.Option.flatMap(x => fx(x)->Js.Nullable.toOption) ->Belt.Option.forEach(value => { config->Obj.magic->Js.Dict.set(key, value) diff --git a/elm-native-js/src/Native/NativescriptCore.res b/elm-native-js/src/Native/NativescriptCore.res index 79e8232..8427062 100644 --- a/elm-native-js/src/Native/NativescriptCore.res +++ b/elm-native-js/src/Native/NativescriptCore.res @@ -1,5 +1,57 @@ type rootLayout +module AnimationCurve = { + type animationCurve = {cubicBezier: (. float, float, float, float) => string} + %%private( + let cubicBezierParamsRE = %re(`/cubicBezier\(([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*),([0-9]+\.?[0-9]*)\)/g`) + + let cubicBezierParams = value => { + cubicBezierParamsRE + ->Js.Re.exec_(value) + ->Belt.Option.map(Js.Re.captures) + ->Belt.Option.flatMap(captures => { + let arr = captures->Js.Array2.sliceFrom(1) + let (x1, y1) = ( + Belt.Array.get(arr, 0)->Belt.Option.flatMap(Js.Nullable.toOption), + Belt.Array.get(arr, 1)->Belt.Option.flatMap(Js.Nullable.toOption), + ) + let (x2, y2) = ( + Belt.Array.get(arr, 2)->Belt.Option.flatMap(Js.Nullable.toOption), + Belt.Array.get(arr, 3)->Belt.Option.flatMap(Js.Nullable.toOption), + ) + switch (x1, y1, x2, y2) { + | (Some(a1), Some(b1), Some(a2), Some(b2)) => Some(((a1, b1), (a2, b2))) + | _ => None + } + }) + } + @module("@nativescript/core") @scope("CoreTypes") + external animationCurve: animationCurve = "AnimationCurve" + ) + + let convertTransitionCurve = (transition: Types.transition): Types.transition => { + { + ...transition, + curve: switch transition.curve->Js.Nullable.toOption { + | Some(curve) if !Js.String2.startsWith(curve, "cubicBezier") => Js.Nullable.return(curve) + | Some(curve) => + curve + ->cubicBezierParams + ->Belt.Option.map((((x1, y1), (x2, y2))) => { + animationCurve.cubicBezier(. + Js.Float.fromString(x1), + Js.Float.fromString(y1), + Js.Float.fromString(x2), + Js.Float.fromString(y2), + )->Js.Nullable.return + }) + ->Belt.Option.getWithDefault(Js.Nullable.return(curve)) + | None => Js.Nullable.null + }, + } + } +} + module Application = { type config = {create: unit => rootLayout} diff --git a/elm-native/src/Native/Frame.elm b/elm-native/src/Native/Frame.elm index b443146..f37c766 100644 --- a/elm-native/src/Native/Frame.elm +++ b/elm-native/src/Native/Frame.elm @@ -49,6 +49,7 @@ type TransitionCurve | EaseOut | Linear | Spring + | CubicBezier ( Float, Float ) ( Float, Float ) type alias Transition = @@ -80,6 +81,17 @@ encodeTransitionCurve curve = Spring -> "spring" + + CubicBezier ( x1, y1 ) ( x2, y2 ) -> + "cubicBezier(" + ++ String.fromFloat x1 + ++ "," + ++ String.fromFloat y1 + ++ "," + ++ String.fromFloat x2 + ++ "," + ++ String.fromFloat y2 + ++ ")" )