Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ensureInstance from '../../../src/private/__tests__/utilities/ensureInsta
import * as Fantom from '@react-native/fantom';
import {createRef} from 'react';
import {Animated, useAnimatedValue} from 'react-native';
import {allowStyleProp} from 'react-native/Libraries/Animated/NativeAnimatedAllowlist';
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';

test('animated opacity', () => {
Expand Down Expand Up @@ -73,3 +74,60 @@ test('animated opacity', () => {
<rn-view opacity="0" />,
);
});

test('animate layout props', () => {
const viewRef = createRef<HostInstance>();
allowStyleProp('height');

let _animatedHeight;
let _heightAnimation;

function MyApp() {
const animatedHeight = useAnimatedValue(0);
_animatedHeight = animatedHeight;
return (
<Animated.View
ref={viewRef}
style={[
{
width: 100,
height: animatedHeight,
},
]}
/>
);
}

const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(<MyApp />);
});

Fantom.runTask(() => {
_heightAnimation = Animated.timing(_animatedHeight, {
toValue: 100,
duration: 200,
useNativeDriver: true,
}).start();
});

Fantom.unstable_produceFramesForDuration(100);

// TODO: getFabricUpdateProps is not working with the cloneMutliple method
// expect(Fantom.unstable_getFabricUpdateProps(viewElement).height).toBe(100);
expect(root.getRenderedOutput({props: ['height']}).toJSX()).toEqual(
<rn-view height="50.000000" />,
);

Fantom.unstable_produceFramesForDuration(100);

// TODO: this shouldn't be neccessary since animation should be stopped after duration
Fantom.runTask(() => {
_heightAnimation?.stop();
});

expect(root.getRenderedOutput({props: ['height']}).toJSX()).toEqual(
<rn-view height="100.000000" />,
);
});
32 changes: 29 additions & 3 deletions packages/react-native/Libraries/Animated/nodes/AnimatedProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import type {AnimatedNodeConfig} from './AnimatedNode';
import type {AnimatedStyleAllowlist} from './AnimatedStyle';

import NativeAnimatedHelper from '../../../src/private/animated/NativeAnimatedHelper';
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
import {findNodeHandle} from '../../ReactNative/RendererProxy';
import {getNodeFromPublicInstance} from '../../ReactPrivate/ReactNativePrivateInterface';
import flattenStyle from '../../StyleSheet/flattenStyle';
import {AnimatedEvent} from '../AnimatedEvent';
import AnimatedNode from './AnimatedNode';
Expand Down Expand Up @@ -251,7 +253,9 @@ export default class AnimatedProps extends AnimatedNode {
super.__setPlatformConfig(platformConfig);

if (this._target != null) {
this.#connectAnimatedView(this._target);
const target = this._target;
this.#connectAnimatedView(target);
this.#connectShadowNode(target);
}
}
}
Expand All @@ -260,9 +264,10 @@ export default class AnimatedProps extends AnimatedNode {
if (this._target?.instance === instance) {
return;
}
this._target = {instance, connectedViewTag: null};
const target = (this._target = {instance, connectedViewTag: null});
if (this.__isNative) {
this.#connectAnimatedView(this._target);
this.#connectAnimatedView(target);
this.#connectShadowNode(target);
}
}

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

#connectShadowNode(target: TargetView): void {
if (
!ReactNativeFeatureFlags.cxxNativeAnimatedEnabled() ||
//eslint-disable-next-line
!ReactNativeFeatureFlags.useSharedAnimatedBackend()
) {
return;
}

invariant(this.__isNative, 'Expected node to be marked as "native"');
// $FlowExpectedError[incompatible-type] - target.instance may be an HTMLElement but we need ReactNativeElement for Fabric
const shadowNode = getNodeFromPublicInstance(target.instance);
if (shadowNode == null) {
return;
}
NativeAnimatedHelper.API.connectAnimatedNodeToShadowNodeFamily(
this.__getNativeTag(),
shadowNode,
);
}

#disconnectAnimatedView(target: TargetView): void {
invariant(this.__isNative, 'Expected node to be marked as "native"');
const viewTag = target.connectedViewTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTInitializing.h>
#import <React/RCTNativeAnimatedNodesManager.h>
#import <React/RCTNativeAnimatedTurboModule.h>
#import <react/debug/react_native_assert.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>

#import "RCTAnimationPlugins.h"
Expand Down Expand Up @@ -165,6 +166,12 @@ - (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
}];
}

RCT_EXPORT_METHOD(connectAnimatedNodeToShadowNodeFamily : (double)nodeTag shadowNode : (NSDictionary *)shadowNode)
{
// This method should only be called when using CxxNativeAnimated
react_native_assert(false);
}

RCT_EXPORT_METHOD(disconnectAnimatedNodeFromView : (double)nodeTag viewTag : (double)viewTag)
{
[self queueOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Pod::Spec.new do |s|
add_dependency(s, "ReactCommon", :subspec => "turbomodule/core", :additional_framework_paths => ["react/nativemodule/core"])
add_dependency(s, "React-NativeModulesApple")
add_dependency(s, "React-featureflags")
add_dependency(s, "React-debug")

add_rn_third_party_dependencies(s)
add_rncore_dependency(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

#include <glog/logging.h>
#include <jsi/JSIDynamic.h>
#include <react/renderer/bridging/bridging.h>

namespace facebook::react {

AnimatedModule::AnimatedModule(
std::shared_ptr<CallInvoker> jsInvoker,
std::shared_ptr<NativeAnimatedNodesManagerProvider> nodesManagerProvider)
Expand Down Expand Up @@ -160,6 +160,19 @@ void AnimatedModule::connectAnimatedNodeToView(
ConnectAnimatedNodeToViewOp{.nodeTag = nodeTag, .viewTag = viewTag});
}

void AnimatedModule::connectAnimatedNodeToShadowNodeFamily(
jsi::Runtime& rt,
Tag nodeTag,
jsi::Object shadowNodeObj) {
const auto& shadowNode = Bridging<std::shared_ptr<const ShadowNode>>::fromJs(
rt, jsi::Value(rt, shadowNodeObj));

operations_.emplace_back(
ConnectAnimatedNodeToShadowNodeFamilyOp{
.nodeTag = nodeTag,
.shadowNodeFamily = shadowNode->getFamilyShared()});
}

void AnimatedModule::disconnectAnimatedNodeFromView(
jsi::Runtime& /*rt*/,
Tag nodeTag,
Expand Down Expand Up @@ -282,6 +295,11 @@ void AnimatedModule::executeOperation(
DisconnectAnimatedNodeFromViewOp>) {
nodesManager->disconnectAnimatedNodeFromView(
op.nodeTag, op.viewTag);
} else if constexpr (std::is_same_v<
T,
ConnectAnimatedNodeToShadowNodeFamilyOp>) {
nodesManager->connectAnimatedNodeToShadowNodeFamily(
op.nodeTag, op.shadowNodeFamily);
} else if constexpr (std::is_same_v<T, RestoreDefaultValuesOp>) {
nodesManager->restoreDefaultValues(op.nodeTag);
} else if constexpr (std::is_same_v<T, DropAnimatedNodeOp>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
Tag viewTag{};
};

struct ConnectAnimatedNodeToShadowNodeFamilyOp {
Tag nodeTag{};
std::shared_ptr<const ShadowNodeFamily> shadowNodeFamily{};
};

struct DisconnectAnimatedNodeFromViewOp {
Tag nodeTag{};
Tag viewTag{};
Expand Down Expand Up @@ -124,6 +129,7 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
SetAnimatedNodeOffsetOp,
SetAnimatedNodeValueOp,
ConnectAnimatedNodeToViewOp,
ConnectAnimatedNodeToShadowNodeFamilyOp,
DisconnectAnimatedNodeFromViewOp,
RestoreDefaultValuesOp,
FlattenAnimatedNodeOffsetOp,
Expand Down Expand Up @@ -176,6 +182,8 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi

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

void connectAnimatedNodeToShadowNodeFamily(jsi::Runtime &rt, Tag nodeTag, jsi::Object shadowNode);

void disconnectAnimatedNodeFromView(jsi::Runtime &rt, Tag nodeTag, Tag viewTag);

void restoreDefaultValues(jsi::Runtime &rt, Tag nodeTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ void NativeAnimatedNodesManager::connectAnimatedNodeToView(
}
}

void NativeAnimatedNodesManager::connectAnimatedNodeToShadowNodeFamily(
Tag propsNodeTag,
std::shared_ptr<const ShadowNodeFamily> family) noexcept {
react_native_assert(propsNodeTag);
auto node = getAnimatedNode<PropsAnimatedNode>(propsNodeTag);
if (node != nullptr && family != nullptr) {
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
tagToShadowNodeFamily_[family->getTag()] = family;
} else {
LOG(WARNING)
<< "Cannot ConnectAnimatedNodeToShadowNodeFamily, animated node has to be props type";
}
}

void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
Tag propsNodeTag,
Tag viewTag) noexcept {
Expand All @@ -251,6 +265,10 @@ void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
std::lock_guard<std::mutex> lock(connectedAnimatedNodesMutex_);
connectedAnimatedNodes_.erase(viewTag);
}
{
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
tagToShadowNodeFamily_.erase(viewTag);
}
updatedNodeTags_.insert(node->tag());

onManagedPropsRemoved(viewTag);
Expand Down Expand Up @@ -989,15 +1007,47 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
}

for (auto& [tag, props] : updateViewPropsDirect_) {
// TODO: also handle layout props (updateViewProps_). It is skipped for
// now, because the backend requires shadowNodeFamilies to be able to
// commit to the ShadowTree
propsBuilder.storeDynamic(props);
mutations.push_back(
AnimationMutation{tag, nullptr, propsBuilder.get()});
containsChange = true;
}
updateViewPropsDirect_.clear();
{
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
for (auto& [tag, props] : updateViewProps_) {
auto familyIt = tagToShadowNodeFamily_.find(tag);
if (familyIt == tagToShadowNodeFamily_.end()) {
continue;
}
if (auto family = familyIt->second.lock()) {
// C++ Animated produces props in the form of a folly::dynamic, so
// it wouldn't make sense to unpack it here. However, for the
// purposes of testing, we want to be able to use the statically
// typed AnimationMutation. At a later stage we will instead just
// pass the dynamic directly to propsBuilder and the new API could
// be used by 3rd party libraries or in the fututre by Animated.
if (props.find("width") != props.items().end()) {
propsBuilder.setWidth(
yoga::Style::SizeLength::points(props["width"].asDouble()));
}
if (props.find("height") != props.items().end()) {
propsBuilder.setHeight(
yoga::Style::SizeLength::points(props["height"].asDouble()));
}
mutations.push_back(
AnimationMutation{
.tag = tag,
.family = family,
.props = propsBuilder.get(),
});
}
containsChange = true;
}
}
if (containsChange) {
updateViewPropsDirect_.clear();
updateViewProps_.clear();
}
}

if (!containsChange) {
Expand All @@ -1017,16 +1067,38 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
}
}

// Step 2: update all nodes that are connected to the finished animations.
// Step 2: update all nodes that are connected to the finished
// animations.
updateNodes(finishedAnimationValueNodes);

isEventAnimationInProgress_ = false;

for (auto& [tag, props] : updateViewPropsDirect_) {
// TODO: handle layout props
propsBuilder.storeDynamic(props);
mutations.push_back(
AnimationMutation{tag, nullptr, propsBuilder.get()});
AnimationMutation{
.tag = tag,
.family = nullptr,
.props = propsBuilder.get(),
});
}
{
std::lock_guard<std::mutex> lock(tagToShadowNodeFamilyMutex_);
for (auto& [tag, props] : updateViewProps_) {
auto familyIt = tagToShadowNodeFamily_.find(tag);
if (familyIt == tagToShadowNodeFamily_.end()) {
continue;
}
if (auto family = familyIt->second.lock()) {
propsBuilder.storeDynamic(props);
mutations.push_back(
AnimationMutation{
.tag = tag,
.family = family,
.props = propsBuilder.get(),
});
}
}
}
}
} else {
Expand Down Expand Up @@ -1107,7 +1179,8 @@ void NativeAnimatedNodesManager::onRender() {
}
}

// Step 2: update all nodes that are connected to the finished animations.
// Step 2: update all nodes that are connected to the finished
// animations.
updateNodes(finishedAnimationValueNodes);

isEventAnimationInProgress_ = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <react/renderer/animationbackend/AnimationBackend.h>
#endif
#include <react/renderer/core/ReactPrimitives.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
#include <chrono>
#include <memory>
Expand Down Expand Up @@ -101,6 +102,8 @@ class NativeAnimatedNodesManager {

void connectAnimatedNodeToView(Tag propsNodeTag, Tag viewTag) noexcept;

void connectAnimatedNodeToShadowNodeFamily(Tag propsNodeTag, std::shared_ptr<const ShadowNodeFamily> family) noexcept;

void disconnectAnimatedNodes(Tag parentTag, Tag childTag) noexcept;

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

mutable std::mutex tagToShadowNodeFamilyMutex_;
std::unordered_map<Tag, std::weak_ptr<const ShadowNodeFamily>> tagToShadowNodeFamily_{};

/*
* Sometimes a view is not longer connected to a PropsAnimatedNode, but
* NativeAnimated has previously changed the view's props via direct
Expand Down
Loading