Skip to content

Commit

Permalink
[Shared Element Transition] Animation restart (software-mansion#4043)
Browse files Browse the repository at this point in the history
## Summary

This change allows restarting of animation in two cases:
- when someone changes the current screen during transition animation
- when an update of layout comes for shared view during the transition
animation

**Known limitation**: If the parent of shared view updates owns the
layout during the shared transition it doesn't affect for shared view
layout. This issue will be fixed in the future.
  • Loading branch information
piaskowyk authored Feb 17, 2023
1 parent c86db64 commit 5bcc258
Show file tree
Hide file tree
Showing 24 changed files with 576 additions and 111 deletions.
24 changes: 24 additions & 0 deletions Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ void LayoutAnimationsManager::startLayoutAnimation(
config->getJSValue(rt));
}

void LayoutAnimationsManager::cancelLayoutAnimation(
jsi::Runtime &rt,
int tag,
const std::string &type,
bool cancelled = true,
bool removeView = true) {
jsi::Value layoutAnimationRepositoryAsValue =
rt.global()
.getPropertyAsObject(rt, "global")
.getProperty(rt, "LayoutAnimationsManager");
jsi::Function cancelLayoutAnimation =
layoutAnimationRepositoryAsValue.getObject(rt).getPropertyAsFunction(
rt, "stop");
std::shared_ptr<Shareable> config;
{
auto lock = std::unique_lock<std::mutex>(animationsMutex_);
config = sharedTransitionAnimations_[tag];
}
if (config != nullptr) {
cancelLayoutAnimation.call(
rt, jsi::Value(tag), config->getJSValue(rt), cancelled, removeView);
}
}

/*
The top screen on the stack triggers the animation, so we need to find
the sibling view registered in the past. This method finds view
Expand Down
6 changes: 6 additions & 0 deletions Common/cpp/LayoutAnimations/LayoutAnimationsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class LayoutAnimationsManager {
const std::string &type,
const jsi::Object &values);
void clearLayoutAnimationConfig(int tag);
void cancelLayoutAnimation(
jsi::Runtime &rt,
int tag,
const std::string &type,
bool cancelled /* = true */,
bool removeView /* = true */);
int findPrecedingViewTagForTransition(int tag);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public boolean hasAnimation(int tag, String type) {

@Override
public void clearAnimationConfig(int tag) {}

@Override
public void cancelAnimation(int tag, String type, boolean cancelled, boolean removeView) {}
};
}
}
15 changes: 15 additions & 0 deletions android/src/main/cpp/LayoutAnimations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ void LayoutAnimations::clearAnimationConfigForTag(int tag) {
clearAnimationConfigBlock_(tag);
}

void LayoutAnimations::setCancelAnimationForTag(
CancelAnimationConfigBlock cancelAnimationBlock) {
this->cancelAnimationBlock_ = cancelAnimationBlock;
}

void LayoutAnimations::cancelAnimationForTag(
int tag,
alias_ref<JString> type,
jboolean cancelled,
jboolean removeView) {
this->cancelAnimationBlock_(tag, type, cancelled, removeView);
}

bool LayoutAnimations::isLayoutAnimationEnabled() {
return FeaturesConfig::isLayoutAnimationEnabled();
}
Expand All @@ -89,6 +102,8 @@ void LayoutAnimations::registerNatives() {
makeNativeMethod(
"clearAnimationConfigForTag",
LayoutAnimations::clearAnimationConfigForTag),
makeNativeMethod(
"cancelAnimationForTag", LayoutAnimations::cancelAnimationForTag),
makeNativeMethod(
"isLayoutAnimationEnabled",
LayoutAnimations::isLayoutAnimationEnabled),
Expand Down
10 changes: 10 additions & 0 deletions android/src/main/cpp/LayoutAnimations.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class LayoutAnimations : public jni::HybridClass<LayoutAnimations> {
void(int, alias_ref<JString>, alias_ref<JMap<jstring, jstring>>)>;
using HasAnimationBlock = std::function<bool(int, const std::string &)>;
using ClearAnimationConfigBlock = std::function<void(int)>;
using CancelAnimationConfigBlock =
std::function<void(int, alias_ref<JString>, jboolean, jboolean)>;
using FindPrecedingViewTagForTransitionBlock = std::function<int(int)>;

public:
Expand All @@ -36,6 +38,8 @@ class LayoutAnimations : public jni::HybridClass<LayoutAnimations> {
void setHasAnimationBlock(HasAnimationBlock hasAnimationBlock);
void setClearAnimationConfigBlock(
ClearAnimationConfigBlock clearAnimationConfigBlock);
void setCancelAnimationForTag(
CancelAnimationConfigBlock cancelAnimationBlock);
void setFindPrecedingViewTagForTransition(
FindPrecedingViewTagForTransitionBlock
findPrecedingViewTagForTransitionBlock);
Expand All @@ -46,6 +50,11 @@ class LayoutAnimations : public jni::HybridClass<LayoutAnimations> {
bool isSharedTransition);
void endLayoutAnimation(int tag, bool cancelled, bool removeView);
void clearAnimationConfigForTag(int tag);
void cancelAnimationForTag(
int tag,
alias_ref<JString> type,
jboolean cancelled,
jboolean removeView);
int findPrecedingViewTagForTransition(int tag);

private:
Expand All @@ -54,6 +63,7 @@ class LayoutAnimations : public jni::HybridClass<LayoutAnimations> {
AnimationStartingBlock animationStartingBlock_;
HasAnimationBlock hasAnimationBlock_;
ClearAnimationConfigBlock clearAnimationConfigBlock_;
CancelAnimationConfigBlock cancelAnimationBlock_;
FindPrecedingViewTagForTransitionBlock
findPrecedingViewTagForTransitionBlock_;

Expand Down
15 changes: 15 additions & 0 deletions android/src/main/cpp/NativeProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,21 @@ void NativeProxy::installJSIBindings(
tag);
});

layoutAnimations->cthis()->setCancelAnimationForTag(
[wrt, weakModule](
int tag,
alias_ref<JString> type,
jboolean cancelled,
jboolean removeView) {
if (auto reaModule = weakModule.lock()) {
if (auto runtime = wrt.lock()) {
jsi::Runtime &rt = *runtime;
reaModule->layoutAnimationsManager().cancelLayoutAnimation(
rt, tag, type->toStdString(), cancelled, removeView);
}
}
});

layoutAnimations->cthis()->setFindPrecedingViewTagForTransition(
[weakModule](int tag) {
if (auto module = weakModule.lock()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ private void registerExitingAncestors(View view) {
}

private void maybeDropAncestors(View exitingView) {
if (!(exitingView.getParent() instanceof View)) {
return;
}
View parent = (View) exitingView.getParent();
while (parent != null && !(parent instanceof RootView)) {
View view = parent;
Expand Down Expand Up @@ -631,8 +634,12 @@ private static Point convertScreenLocationToViewCoordinates(Point fromPoint, Vie
return new Point(fromPoint.x - toPoint[0], fromPoint.y - toPoint[1]);
}

public void viewsDidLayout() {
mSharedTransitionManager.viewsDidLayout();
public void screenDidLayout() {
mSharedTransitionManager.screenDidLayout();
}

public void viewDidLayout(View view) {
mSharedTransitionManager.viewDidLayout(view);
}

public void notifyAboutViewsRemoval(int[] tagsToDelete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public LayoutAnimations(ReactApplicationContext context) {

public native void clearAnimationConfigForTag(int tag);

public native void cancelAnimationForTag(
int tag, String type, boolean cancelled, boolean removeView);

public native boolean isLayoutAnimationEnabled();

public native int findPrecedingViewTagForTransition(int tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface NativeMethodsHolder {

void clearAnimationConfig(int tag);

void cancelAnimation(int tag, String type, boolean cancelled, boolean removeView);

boolean isLayoutAnimationEnabled();

int findPrecedingViewTagForTransition(int tag);
Expand Down
Loading

0 comments on commit 5bcc258

Please sign in to comment.