forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ColorAnimatedNode convert Java->Kotlin (facebook#45752)
Summary: Pull Request resolved: facebook#45752 # Changelog: [Internal] - As in the title. Differential Revision: D60336724
- Loading branch information
1 parent
054e992
commit 751b60a
Showing
2 changed files
with
111 additions
and
131 deletions.
There are no files selected for viewing
131 changes: 0 additions & 131 deletions
131
...eact-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
.../react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.animated | ||
|
||
import android.content.Context | ||
import android.graphics.Color | ||
import com.facebook.react.bridge.ColorPropConverter | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.views.view.ColorUtil.normalize | ||
|
||
/** Animated node that represents a color. */ | ||
internal class ColorAnimatedNode( | ||
config: ReadableMap, | ||
private val nativeAnimatedNodesManager: NativeAnimatedNodesManager, | ||
private val reactApplicationContext: ReactApplicationContext | ||
) : AnimatedNode(), AnimatedNodeWithUpdateableConfig { | ||
private var rNodeId = 0 | ||
private var gNodeId = 0 | ||
private var bNodeId = 0 | ||
private var aNodeId = 0 | ||
private var nativeColor: ReadableMap? = null | ||
private var nativeColorApplied = false | ||
|
||
init { | ||
onUpdateConfig(config) | ||
} | ||
|
||
val color: Int | ||
get() { | ||
tryApplyNativeColor() | ||
val rNode = nativeAnimatedNodesManager.getNodeById(rNodeId) as ValueAnimatedNode? | ||
val gNode = nativeAnimatedNodesManager.getNodeById(gNodeId) as ValueAnimatedNode? | ||
val bNode = nativeAnimatedNodesManager.getNodeById(bNodeId) as ValueAnimatedNode? | ||
val aNode = nativeAnimatedNodesManager.getNodeById(aNodeId) as ValueAnimatedNode? | ||
val r = rNode?.nodeValue ?: 0.0 | ||
val g = gNode?.nodeValue ?: 0.0 | ||
val b = bNode?.nodeValue ?: 0.0 | ||
val a = aNode?.nodeValue ?: 0.0 | ||
return normalize(r, g, b, a) | ||
} | ||
|
||
override fun onUpdateConfig(config: ReadableMap?) { | ||
if (config != null) { | ||
rNodeId = config.getInt("r") | ||
gNodeId = config.getInt("g") | ||
bNodeId = config.getInt("b") | ||
aNodeId = config.getInt("a") | ||
nativeColor = config.getMap("nativeColor") | ||
nativeColorApplied = false | ||
tryApplyNativeColor() | ||
} else { | ||
rNodeId = 0 | ||
gNodeId = 0 | ||
bNodeId = 0 | ||
aNodeId = 0 | ||
nativeColor = null | ||
nativeColorApplied = false | ||
} | ||
} | ||
|
||
override fun prettyPrint(): String = | ||
"ColorAnimatedNode[$tag]: r: $rNodeId g: $gNodeId b: $bNodeId a: $aNodeId" | ||
|
||
private fun tryApplyNativeColor() { | ||
if (nativeColor == null || nativeColorApplied) { | ||
return | ||
} | ||
val context = context ?: return | ||
val color = ColorPropConverter.getColor(nativeColor, context) | ||
val rNode = nativeAnimatedNodesManager.getNodeById(rNodeId) as ValueAnimatedNode? | ||
val gNode = nativeAnimatedNodesManager.getNodeById(gNodeId) as ValueAnimatedNode? | ||
val bNode = nativeAnimatedNodesManager.getNodeById(bNodeId) as ValueAnimatedNode? | ||
val aNode = nativeAnimatedNodesManager.getNodeById(aNodeId) as ValueAnimatedNode? | ||
rNode?.nodeValue = Color.red(color).toDouble() | ||
gNode?.nodeValue = Color.green(color).toDouble() | ||
bNode?.nodeValue = Color.blue(color).toDouble() | ||
aNode?.nodeValue = Color.alpha(color) / 255.0 | ||
nativeColorApplied = true | ||
} | ||
|
||
private val context: Context? | ||
get() { | ||
// There are cases where the activity may not exist (such as for VRShell panel apps). In this | ||
// case we will search for a view associated with a PropsAnimatedNode to get the context. | ||
return reactApplicationContext.currentActivity ?: getContextHelper(this) | ||
} | ||
|
||
companion object { | ||
private fun getContextHelper(node: AnimatedNode): Context? { | ||
// Search children depth-first until we get to a PropsAnimatedNode, from which we can | ||
// get the view and its context | ||
node.children?.let { children -> | ||
for (child in children) { | ||
return if (child is PropsAnimatedNode) { | ||
val view = child.connectedView | ||
view?.context | ||
} else { | ||
getContextHelper(child) | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
} | ||
} |