Skip to content

Commit d2732dc

Browse files
Bartlomiej Bloniarzfacebook-github-bot
authored andcommitted
Pass families to Native Animated (#54613)
Summary: This PR allows C++ Native Animated to use `ShadowNodeFamily` instances to use the `cloneMultiple` method when pushing updates through the `ShadowTree` in `AnimationBackend` # Changelog [General] [Added] - Add `connectAnimatedNodeToShadowNodeFamily` method to `NativeAnimatedModule` and `NativeAnimatedTurboModule` Reviewed By: sammy-SC, zeyap Differential Revision: D84055752
1 parent 49267d4 commit d2732dc

13 files changed

Lines changed: 245 additions & 15 deletions

File tree

packages/react-native/Libraries/Animated/__tests__/AnimatedBackend-itest.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ensureInstance from '../../../src/private/__tests__/utilities/ensureInsta
1717
import * as Fantom from '@react-native/fantom';
1818
import {createRef} from 'react';
1919
import {Animated, useAnimatedValue} from 'react-native';
20+
import {allowStyleProp} from 'react-native/Libraries/Animated/NativeAnimatedAllowlist';
2021
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
2122

2223
test('animated opacity', () => {
@@ -73,3 +74,60 @@ test('animated opacity', () => {
7374
<rn-view opacity="0" />,
7475
);
7576
});
77+
78+
test('animate layout props', () => {
79+
const viewRef = createRef<HostInstance>();
80+
allowStyleProp('height');
81+
82+
let _animatedHeight;
83+
let _heightAnimation;
84+
85+
function MyApp() {
86+
const animatedHeight = useAnimatedValue(0);
87+
_animatedHeight = animatedHeight;
88+
return (
89+
<Animated.View
90+
ref={viewRef}
91+
style={[
92+
{
93+
width: 100,
94+
height: animatedHeight,
95+
},
96+
]}
97+
/>
98+
);
99+
}
100+
101+
const root = Fantom.createRoot();
102+
103+
Fantom.runTask(() => {
104+
root.render(<MyApp />);
105+
});
106+
107+
Fantom.runTask(() => {
108+
_heightAnimation = Animated.timing(_animatedHeight, {
109+
toValue: 100,
110+
duration: 200,
111+
useNativeDriver: true,
112+
}).start();
113+
});
114+
115+
Fantom.unstable_produceFramesForDuration(100);
116+
117+
// TODO: getFabricUpdateProps is not working with the cloneMutliple method
118+
// expect(Fantom.unstable_getFabricUpdateProps(viewElement).height).toBe(100);
119+
expect(root.getRenderedOutput({props: ['height']}).toJSX()).toEqual(
120+
<rn-view height="50.000000" />,
121+
);
122+
123+
Fantom.unstable_produceFramesForDuration(100);
124+
125+
// TODO: this shouldn't be neccessary since animation should be stopped after duration
126+
Fantom.runTask(() => {
127+
_heightAnimation?.stop();
128+
});
129+
130+
expect(root.getRenderedOutput({props: ['height']}).toJSX()).toEqual(
131+
<rn-view height="100.000000" />,
132+
);
133+
});

packages/react-native/Libraries/Animated/nodes/AnimatedProps.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import type {AnimatedNodeConfig} from './AnimatedNode';
1414
import type {AnimatedStyleAllowlist} from './AnimatedStyle';
1515

1616
import NativeAnimatedHelper from '../../../src/private/animated/NativeAnimatedHelper';
17+
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
1718
import {findNodeHandle} from '../../ReactNative/RendererProxy';
19+
import {getNodeFromPublicInstance} from '../../ReactPrivate/ReactNativePrivateInterface';
1820
import flattenStyle from '../../StyleSheet/flattenStyle';
1921
import {AnimatedEvent} from '../AnimatedEvent';
2022
import AnimatedNode from './AnimatedNode';
@@ -251,7 +253,9 @@ export default class AnimatedProps extends AnimatedNode {
251253
super.__setPlatformConfig(platformConfig);
252254

253255
if (this._target != null) {
254-
this.#connectAnimatedView(this._target);
256+
const target = this._target;
257+
this.#connectAnimatedView(target);
258+
this.#connectShadowNode(target);
255259
}
256260
}
257261
}
@@ -260,9 +264,10 @@ export default class AnimatedProps extends AnimatedNode {
260264
if (this._target?.instance === instance) {
261265
return;
262266
}
263-
this._target = {instance, connectedViewTag: null};
267+
const target = (this._target = {instance, connectedViewTag: null});
264268
if (this.__isNative) {
265-
this.#connectAnimatedView(this._target);
269+
this.#connectAnimatedView(target);
270+
this.#connectShadowNode(target);
266271
}
267272
}
268273

@@ -283,6 +288,27 @@ export default class AnimatedProps extends AnimatedNode {
283288
target.connectedViewTag = viewTag;
284289
}
285290

291+
#connectShadowNode(target: TargetView): void {
292+
if (
293+
!ReactNativeFeatureFlags.cxxNativeAnimatedEnabled() ||
294+
//eslint-disable-next-line
295+
!ReactNativeFeatureFlags.useSharedAnimatedBackend()
296+
) {
297+
return;
298+
}
299+
300+
invariant(this.__isNative, 'Expected node to be marked as "native"');
301+
// $FlowExpectedError[incompatible-type] - target.instance may be an HTMLElement but we need ReactNativeElement for Fabric
302+
const shadowNode = getNodeFromPublicInstance(target.instance);
303+
if (shadowNode == null) {
304+
return;
305+
}
306+
NativeAnimatedHelper.API.connectAnimatedNodeToShadowNodeFamily(
307+
this.__getNativeTag(),
308+
shadowNode,
309+
);
310+
}
311+
286312
#disconnectAnimatedView(target: TargetView): void {
287313
invariant(this.__isNative, 'Expected node to be marked as "native"');
288314
const viewTag = target.connectedViewTag;

packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <React/RCTInitializing.h>
1111
#import <React/RCTNativeAnimatedNodesManager.h>
1212
#import <React/RCTNativeAnimatedTurboModule.h>
13+
#import <react/debug/react_native_assert.h>
1314
#import <react/featureflags/ReactNativeFeatureFlags.h>
1415

1516
#import "RCTAnimationPlugins.h"
@@ -165,6 +166,12 @@ - (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
165166
}];
166167
}
167168

169+
RCT_EXPORT_METHOD(connectAnimatedNodeToShadowNodeFamily : (double)nodeTag shadowNode : (NSDictionary *)shadowNode)
170+
{
171+
// This method should only be called when using CxxNativeAnimated
172+
react_native_assert(false);
173+
}
174+
168175
RCT_EXPORT_METHOD(disconnectAnimatedNodeFromView : (double)nodeTag viewTag : (double)viewTag)
169176
{
170177
[self queueOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {

packages/react-native/Libraries/NativeAnimation/React-RCTAnimation.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Pod::Spec.new do |s|
4848
add_dependency(s, "ReactCommon", :subspec => "turbomodule/core", :additional_framework_paths => ["react/nativemodule/core"])
4949
add_dependency(s, "React-NativeModulesApple")
5050
add_dependency(s, "React-featureflags")
51+
add_dependency(s, "React-debug")
5152

5253
add_rn_third_party_dependencies(s)
5354
add_rncore_dependency(s)

packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#include <glog/logging.h>
1111
#include <jsi/JSIDynamic.h>
12+
#include <react/renderer/bridging/bridging.h>
1213

1314
namespace facebook::react {
14-
1515
AnimatedModule::AnimatedModule(
1616
std::shared_ptr<CallInvoker> jsInvoker,
1717
std::shared_ptr<NativeAnimatedNodesManagerProvider> nodesManagerProvider)
@@ -160,6 +160,19 @@ void AnimatedModule::connectAnimatedNodeToView(
160160
ConnectAnimatedNodeToViewOp{.nodeTag = nodeTag, .viewTag = viewTag});
161161
}
162162

163+
void AnimatedModule::connectAnimatedNodeToShadowNodeFamily(
164+
jsi::Runtime& rt,
165+
Tag nodeTag,
166+
jsi::Object shadowNodeObj) {
167+
const auto& shadowNode = Bridging<std::shared_ptr<const ShadowNode>>::fromJs(
168+
rt, jsi::Value(rt, shadowNodeObj));
169+
170+
operations_.emplace_back(
171+
ConnectAnimatedNodeToShadowNodeFamilyOp{
172+
.nodeTag = nodeTag,
173+
.shadowNodeFamily = shadowNode->getFamilyShared()});
174+
}
175+
163176
void AnimatedModule::disconnectAnimatedNodeFromView(
164177
jsi::Runtime& /*rt*/,
165178
Tag nodeTag,
@@ -282,6 +295,11 @@ void AnimatedModule::executeOperation(
282295
DisconnectAnimatedNodeFromViewOp>) {
283296
nodesManager->disconnectAnimatedNodeFromView(
284297
op.nodeTag, op.viewTag);
298+
} else if constexpr (std::is_same_v<
299+
T,
300+
ConnectAnimatedNodeToShadowNodeFamilyOp>) {
301+
nodesManager->connectAnimatedNodeToShadowNodeFamily(
302+
op.nodeTag, op.shadowNodeFamily);
285303
} else if constexpr (std::is_same_v<T, RestoreDefaultValuesOp>) {
286304
nodesManager->restoreDefaultValues(op.nodeTag);
287305
} else if constexpr (std::is_same_v<T, DropAnimatedNodeOp>) {

packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
8787
Tag viewTag{};
8888
};
8989

90+
struct ConnectAnimatedNodeToShadowNodeFamilyOp {
91+
Tag nodeTag{};
92+
std::shared_ptr<const ShadowNodeFamily> shadowNodeFamily{};
93+
};
94+
9095
struct DisconnectAnimatedNodeFromViewOp {
9196
Tag nodeTag{};
9297
Tag viewTag{};
@@ -124,6 +129,7 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
124129
SetAnimatedNodeOffsetOp,
125130
SetAnimatedNodeValueOp,
126131
ConnectAnimatedNodeToViewOp,
132+
ConnectAnimatedNodeToShadowNodeFamilyOp,
127133
DisconnectAnimatedNodeFromViewOp,
128134
RestoreDefaultValuesOp,
129135
FlattenAnimatedNodeOffsetOp,
@@ -176,6 +182,8 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
176182

177183
void connectAnimatedNodeToView(jsi::Runtime &rt, Tag nodeTag, Tag viewTag);
178184

185+
void connectAnimatedNodeToShadowNodeFamily(jsi::Runtime &rt, Tag nodeTag, jsi::Object shadowNode);
186+
179187
void disconnectAnimatedNodeFromView(jsi::Runtime &rt, Tag nodeTag, Tag viewTag);
180188

181189
void restoreDefaultValues(jsi::Runtime &rt, Tag nodeTag);

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ void NativeAnimatedNodesManager::connectAnimatedNodeToView(
238238
}
239239
}
240240

241+
void NativeAnimatedNodesManager::connectAnimatedNodeToShadowNodeFamily(
242+
Tag propsNodeTag,
243+
std::shared_ptr<const ShadowNodeFamily> family) noexcept {
244+
react_native_assert(propsNodeTag);
245+
auto node = getAnimatedNode<PropsAnimatedNode>(propsNodeTag);
246+
if (node != nullptr && family != nullptr) {
247+
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
248+
tagToShadowNodeFamily_[family->getTag()] = family;
249+
} else {
250+
LOG(WARNING)
251+
<< "Cannot ConnectAnimatedNodeToShadowNodeFamily, animated node has to be props type";
252+
}
253+
}
254+
241255
void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
242256
Tag propsNodeTag,
243257
Tag viewTag) noexcept {
@@ -251,6 +265,10 @@ void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
251265
std::lock_guard<std::mutex> lock(connectedAnimatedNodesMutex_);
252266
connectedAnimatedNodes_.erase(viewTag);
253267
}
268+
{
269+
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
270+
tagToShadowNodeFamily_.erase(viewTag);
271+
}
254272
updatedNodeTags_.insert(node->tag());
255273

256274
onManagedPropsRemoved(viewTag);
@@ -989,15 +1007,47 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
9891007
}
9901008

9911009
for (auto& [tag, props] : updateViewPropsDirect_) {
992-
// TODO: also handle layout props (updateViewProps_). It is skipped for
993-
// now, because the backend requires shadowNodeFamilies to be able to
994-
// commit to the ShadowTree
9951010
propsBuilder.storeDynamic(props);
9961011
mutations.push_back(
9971012
AnimationMutation{tag, nullptr, propsBuilder.get()});
9981013
containsChange = true;
9991014
}
1000-
updateViewPropsDirect_.clear();
1015+
{
1016+
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
1017+
for (auto& [tag, props] : updateViewProps_) {
1018+
auto familyIt = tagToShadowNodeFamily_.find(tag);
1019+
if (familyIt == tagToShadowNodeFamily_.end()) {
1020+
continue;
1021+
}
1022+
if (auto family = familyIt->second.lock()) {
1023+
// C++ Animated produces props in the form of a folly::dynamic, so
1024+
// it wouldn't make sense to unpack it here. However, for the
1025+
// purposes of testing, we want to be able to use the statically
1026+
// typed AnimationMutation. At a later stage we will instead just
1027+
// pass the dynamic directly to propsBuilder and the new API could
1028+
// be used by 3rd party libraries or in the fututre by Animated.
1029+
if (props.find("width") != props.items().end()) {
1030+
propsBuilder.setWidth(
1031+
yoga::Style::SizeLength::points(props["width"].asDouble()));
1032+
}
1033+
if (props.find("height") != props.items().end()) {
1034+
propsBuilder.setHeight(
1035+
yoga::Style::SizeLength::points(props["height"].asDouble()));
1036+
}
1037+
mutations.push_back(
1038+
AnimationMutation{
1039+
.tag = tag,
1040+
.family = family,
1041+
.props = propsBuilder.get(),
1042+
});
1043+
}
1044+
containsChange = true;
1045+
}
1046+
}
1047+
if (containsChange) {
1048+
updateViewPropsDirect_.clear();
1049+
updateViewProps_.clear();
1050+
}
10011051
}
10021052

10031053
if (!containsChange) {
@@ -1017,16 +1067,38 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
10171067
}
10181068
}
10191069

1020-
// Step 2: update all nodes that are connected to the finished animations.
1070+
// Step 2: update all nodes that are connected to the finished
1071+
// animations.
10211072
updateNodes(finishedAnimationValueNodes);
10221073

10231074
isEventAnimationInProgress_ = false;
10241075

10251076
for (auto& [tag, props] : updateViewPropsDirect_) {
1026-
// TODO: handle layout props
10271077
propsBuilder.storeDynamic(props);
10281078
mutations.push_back(
1029-
AnimationMutation{tag, nullptr, propsBuilder.get()});
1079+
AnimationMutation{
1080+
.tag = tag,
1081+
.family = nullptr,
1082+
.props = propsBuilder.get(),
1083+
});
1084+
}
1085+
{
1086+
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
1087+
for (auto& [tag, props] : updateViewProps_) {
1088+
auto familyIt = tagToShadowNodeFamily_.find(tag);
1089+
if (familyIt == tagToShadowNodeFamily_.end()) {
1090+
continue;
1091+
}
1092+
if (auto family = familyIt->second.lock()) {
1093+
propsBuilder.storeDynamic(props);
1094+
mutations.push_back(
1095+
AnimationMutation{
1096+
.tag = tag,
1097+
.family = family,
1098+
.props = propsBuilder.get(),
1099+
});
1100+
}
1101+
}
10301102
}
10311103
}
10321104
} else {
@@ -1107,7 +1179,8 @@ void NativeAnimatedNodesManager::onRender() {
11071179
}
11081180
}
11091181

1110-
// Step 2: update all nodes that are connected to the finished animations.
1182+
// Step 2: update all nodes that are connected to the finished
1183+
// animations.
11111184
updateNodes(finishedAnimationValueNodes);
11121185

11131186
isEventAnimationInProgress_ = false;

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <react/renderer/animationbackend/AnimationBackend.h>
2222
#endif
2323
#include <react/renderer/core/ReactPrimitives.h>
24+
#include <react/renderer/core/ShadowNode.h>
2425
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
2526
#include <chrono>
2627
#include <memory>
@@ -101,6 +102,8 @@ class NativeAnimatedNodesManager {
101102

102103
void connectAnimatedNodeToView(Tag propsNodeTag, Tag viewTag) noexcept;
103104

105+
void connectAnimatedNodeToShadowNodeFamily(Tag propsNodeTag, std::shared_ptr<const ShadowNodeFamily> family) noexcept;
106+
104107
void disconnectAnimatedNodes(Tag parentTag, Tag childTag) noexcept;
105108

106109
void disconnectAnimatedNodeFromView(Tag propsNodeTag, Tag viewTag) noexcept;
@@ -258,6 +261,9 @@ class NativeAnimatedNodesManager {
258261
std::unordered_map<Tag, folly::dynamic> updateViewProps_{};
259262
std::unordered_map<Tag, folly::dynamic> updateViewPropsDirect_{};
260263

264+
mutable std::mutex tagToShadowNodeFamilyMutex_;
265+
std::unordered_map<Tag, std::weak_ptr<const ShadowNodeFamily>> tagToShadowNodeFamily_{};
266+
261267
/*
262268
* Sometimes a view is not longer connected to a PropsAnimatedNode, but
263269
* NativeAnimated has previously changed the view's props via direct

0 commit comments

Comments
 (0)