forked from software-mansion/react-native-reanimated
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLayoutAnimationsProxy.cpp
More file actions
797 lines (696 loc) · 28.2 KB
/
Copy pathLayoutAnimationsProxy.cpp
File metadata and controls
797 lines (696 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#ifdef RCT_NEW_ARCH_ENABLED
#include <reanimated/LayoutAnimations/LayoutAnimationsProxy.h>
#include <reanimated/NativeModules/NativeReanimatedModule.h>
#include <react/renderer/animations/utils.h>
#include <react/renderer/mounting/ShadowViewMutation.h>
#include <set>
#include <utility>
namespace reanimated {
// We never modify the Shadow Tree, we just send some additional
// mutations to the mounting layer.
// When animations finish, the Host Tree will represent the most recent Shadow
// Tree
// On android this code will be sometimes executed on the JS thread.
// That's why we have to schedule some of animation manager function on the UI
// thread
std::optional<MountingTransaction> LayoutAnimationsProxy::pullTransaction(
SurfaceId surfaceId,
MountingTransaction::Number transactionNumber,
const TransactionTelemetry &telemetry,
ShadowViewMutationList mutations) const {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << std::endl;
LOG(INFO) << "pullTransaction " << std::this_thread::get_id() << " "
<< surfaceId << std::endl;
#endif
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
PropsParserContext propsParserContext{surfaceId, *contextContainer_};
ShadowViewMutationList filteredMutations;
std::vector<std::shared_ptr<MutationNode>> roots;
std::unordered_map<Tag, ShadowView> movedViews;
parseRemoveMutations(movedViews, mutations, roots);
handleRemovals(filteredMutations, roots);
handleUpdatesAndEnterings(
filteredMutations, movedViews, mutations, propsParserContext, surfaceId);
addOngoingAnimations(surfaceId, filteredMutations);
return MountingTransaction{
surfaceId, transactionNumber, std::move(filteredMutations), telemetry};
}
std::optional<SurfaceId> LayoutAnimationsProxy::progressLayoutAnimation(
int tag,
const jsi::Object &newStyle) {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "progress layout animation for tag " << tag << std::endl;
#endif
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
auto layoutAnimationIt = layoutAnimations_.find(tag);
if (layoutAnimationIt == layoutAnimations_.end()) {
return {};
}
auto &layoutAnimation = layoutAnimationIt->second;
maybeRestoreOpacity(layoutAnimation, newStyle);
auto rawProps =
std::make_shared<RawProps>(uiRuntime_, jsi::Value(uiRuntime_, newStyle));
PropsParserContext propsParserContext{
layoutAnimation.finalView->surfaceId, *contextContainer_};
#ifdef ANDROID
rawProps = std::make_shared<RawProps>(folly::dynamic::merge(
layoutAnimation.finalView->props->rawProps, (folly::dynamic)*rawProps));
#endif
auto newProps =
getComponentDescriptorForShadowView(*layoutAnimation.finalView)
.cloneProps(
propsParserContext,
layoutAnimation.finalView->props,
std::move(*rawProps));
auto &updateMap =
surfaceManager.getUpdateMap(layoutAnimation.finalView->surfaceId);
updateMap.insert_or_assign(
tag, UpdateValues{newProps, Frame(uiRuntime_, newStyle)});
return layoutAnimation.finalView->surfaceId;
}
std::optional<SurfaceId> LayoutAnimationsProxy::endLayoutAnimation(
int tag,
bool shouldRemove) {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "end layout animation for " << tag << " - should remove "
<< shouldRemove << std::endl;
#endif
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
auto layoutAnimationIt = layoutAnimations_.find(tag);
if (layoutAnimationIt == layoutAnimations_.end()) {
return {};
}
auto &layoutAnimation = layoutAnimationIt->second;
// multiple layout animations can be triggered for a view
// one after the other, so we need to keep count of how many
// were actually triggered, so that we don't cleanup necessary
// structures too early
if (layoutAnimation.count > 1) {
layoutAnimation.count--;
return {};
}
auto surfaceId = layoutAnimation.finalView->surfaceId;
auto &updateMap = surfaceManager.getUpdateMap(surfaceId);
layoutAnimations_.erase(tag);
updateMap.erase(tag);
if (!shouldRemove || !nodeForTag_.contains(tag)) {
return {};
}
auto node = nodeForTag_[tag];
auto mutationNode = std::static_pointer_cast<MutationNode>(node);
mutationNode->state = DEAD;
deadNodes.insert(mutationNode);
return surfaceId;
}
/**
Organizes removed views into a tree structure, allowing for convenient
traversals and index maintenance
*/
void LayoutAnimationsProxy::parseRemoveMutations(
std::unordered_map<Tag, ShadowView> &movedViews,
ShadowViewMutationList &mutations,
std::vector<std::shared_ptr<MutationNode>> &roots) const {
std::set<Tag> deletedViews;
std::unordered_map<Tag, std::vector<std::shared_ptr<MutationNode>>>
childrenForTag, unflattenedChildrenForTag;
std::vector<std::shared_ptr<MutationNode>> mutationNodes;
// iterate from the end, so that parents appear before children
for (auto it = mutations.rbegin(); it != mutations.rend(); it++) {
auto &mutation = *it;
if (mutation.type == ShadowViewMutation::Delete) {
deletedViews.insert(mutation.oldChildShadowView.tag);
}
if (mutation.type == ShadowViewMutation::Remove) {
updateIndexForMutation(mutation);
auto tag = mutation.oldChildShadowView.tag;
auto parentTag = mutation.parentTag;
auto unflattenedParentTag = parentTag; // temporary
std::shared_ptr<MutationNode> mutationNode;
std::shared_ptr<Node> node = nodeForTag_[tag],
parent = nodeForTag_[parentTag],
unflattenedParent =
nodeForTag_[unflattenedParentTag];
if (!node) {
mutationNode = std::make_shared<MutationNode>(mutation);
} else {
mutationNode =
std::make_shared<MutationNode>(mutation, std::move(*node));
for (auto &subNode : mutationNode->children) {
subNode->parent = mutationNode;
}
for (auto &subNode : mutationNode->unflattenedChildren) {
subNode->unflattenedParent = mutationNode;
}
}
if (!deletedViews.contains(mutation.oldChildShadowView.tag)) {
mutationNode->state = MOVED;
movedViews.insert_or_assign(
mutation.oldChildShadowView.tag, mutation.oldChildShadowView);
}
nodeForTag_[tag] = mutationNode;
if (!parent) {
parent = std::make_shared<Node>(parentTag);
nodeForTag_[parentTag] = parent;
}
if (!unflattenedParent) {
if (parentTag == unflattenedParentTag) {
unflattenedParent = parent;
} else {
unflattenedParent = std::make_shared<Node>(unflattenedParentTag);
nodeForTag_[unflattenedParentTag] = unflattenedParent;
}
}
mutationNodes.push_back(mutationNode);
childrenForTag[parentTag].push_back(mutationNode);
unflattenedChildrenForTag[unflattenedParentTag].push_back(mutationNode);
mutationNode->parent = parent;
mutationNode->unflattenedParent = unflattenedParent;
}
if (mutation.type == ShadowViewMutation::Update &&
movedViews.contains(mutation.newChildShadowView.tag)) {
auto node = nodeForTag_[mutation.newChildShadowView.tag];
auto mutationNode = std::static_pointer_cast<MutationNode>(node);
mutationNode->mutation.oldChildShadowView = mutation.oldChildShadowView;
movedViews[mutation.newChildShadowView.tag] = mutation.oldChildShadowView;
}
}
for (auto &[parentTag, children] : childrenForTag) {
auto &parent = nodeForTag_[parentTag];
parent->insertChildren(children);
for (auto &child : children) {
child->parent = parent;
}
}
for (auto &[unflattenedParentTag, children] : unflattenedChildrenForTag) {
auto &unflattenedParent = nodeForTag_[unflattenedParentTag];
unflattenedParent->insertUnflattenedChildren(children);
for (auto &child : children) {
child->unflattenedParent = unflattenedParent;
}
}
for (auto &mutationNode : mutationNodes) {
if (!mutationNode->unflattenedParent->isMutationMode()) {
roots.push_back(mutationNode);
}
}
}
void LayoutAnimationsProxy::handleRemovals(
ShadowViewMutationList &filteredMutations,
std::vector<std::shared_ptr<MutationNode>> &roots) const {
// iterate from the end, so that children
// with higher indices appear first in the mutations list
for (auto it = roots.rbegin(); it != roots.rend(); it++) {
auto &node = *it;
if (!startAnimationsRecursively(
node, true, true, false, filteredMutations)) {
filteredMutations.push_back(node->mutation);
node->unflattenedParent->removeChildFromUnflattenedTree(node); //???
if (node->state != MOVED) {
maybeCancelAnimation(node->tag);
filteredMutations.push_back(ShadowViewMutation::DeleteMutation(
node->mutation.oldChildShadowView));
nodeForTag_.erase(node->tag);
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "delete " << node->tag << std::endl;
#endif
}
}
}
for (auto node : deadNodes) {
if (node->state != DELETED) {
endAnimationsRecursively(node, filteredMutations);
maybeDropAncestors(node->unflattenedParent, node, filteredMutations);
}
}
deadNodes.clear();
}
void LayoutAnimationsProxy::handleUpdatesAndEnterings(
ShadowViewMutationList &filteredMutations,
const std::unordered_map<Tag, ShadowView> &movedViews,
ShadowViewMutationList &mutations,
const PropsParserContext &propsParserContext,
SurfaceId surfaceId) const {
std::unordered_map<Tag, ShadowView> oldShadowViewsForReparentings;
for (auto &mutation : mutations) {
maybeUpdateWindowDimensions(mutation, surfaceId);
Tag tag = mutation.type == ShadowViewMutation::Type::Create ||
mutation.type == ShadowViewMutation::Type::Insert
? mutation.newChildShadowView.tag
: mutation.oldChildShadowView.tag;
switch (mutation.type) {
case ShadowViewMutation::Type::Create: {
filteredMutations.push_back(mutation);
break;
}
case ShadowViewMutation::Type::Insert: {
updateIndexForMutation(mutation);
if (nodeForTag_.contains(mutation.parentTag)) {
nodeForTag_[mutation.parentTag]->applyMutationToIndices(
mutation);
}
if (movedViews.contains(tag)) {
auto layoutAnimationIt = layoutAnimations_.find(tag);
if (layoutAnimationIt == layoutAnimations_.end()) {
if (oldShadowViewsForReparentings.contains(tag)) {
filteredMutations.push_back(ShadowViewMutation::InsertMutation(
mutation.parentTag,
oldShadowViewsForReparentings[tag],
mutation.index));
} else {
filteredMutations.push_back(mutation);
}
continue;
}
auto oldView = *layoutAnimationIt->second.currentView;
filteredMutations.push_back(ShadowViewMutation::InsertMutation(
mutation.parentTag, oldView, mutation.index));
continue;
}
transferConfigFromNativeID(
mutation.newChildShadowView.props->nativeId,
mutation.newChildShadowView.tag);
if (!layoutAnimationsManager_->hasLayoutAnimation(tag, ENTERING)) {
filteredMutations.push_back(mutation);
continue;
}
startEnteringAnimation(tag, mutation);
filteredMutations.push_back(mutation);
// temporarily set opacity to 0 to prevent flickering on android
std::shared_ptr<ShadowView> newView =
cloneViewWithoutOpacity(mutation, propsParserContext);
filteredMutations.push_back(ShadowViewMutation::UpdateMutation(
mutation.newChildShadowView, *newView, mutation.parentTag));
break;
}
case ShadowViewMutation::Type::Update: {
auto shouldAnimate = hasLayoutChanged(mutation);
if (!layoutAnimationsManager_->hasLayoutAnimation(tag, LAYOUT) ||
(!shouldAnimate && !layoutAnimations_.contains(tag))) {
// We should cancel any ongoing animation here to ensure that the
// proper final state is reached for this view However, due to how
// RNSScreens handle adding headers (a second commit is triggered to
// offset all the elements by the header height) this would lead to
// all entering animations being cancelled when a screen with a header
// is pushed onto a stack
// TODO: find a better solution for this problem
filteredMutations.push_back(mutation);
continue;
} else if (!shouldAnimate) {
updateOngoingAnimationTarget(tag, mutation);
continue;
}
// store the oldChildShadowView, so that we can use this ShadowView when
// the view is inserted
oldShadowViewsForReparentings[tag] = mutation.oldChildShadowView;
startLayoutAnimation(tag, mutation);
break;
}
case ShadowViewMutation::Type::Remove:
case ShadowViewMutation::Type::Delete: {
break;
}
default:
filteredMutations.push_back(mutation);
}
}
}
void LayoutAnimationsProxy::addOngoingAnimations(
SurfaceId surfaceId,
ShadowViewMutationList &mutations) const {
auto &updateMap = surfaceManager.getUpdateMap(surfaceId);
for (auto &[tag, updateValues] : updateMap) {
auto layoutAnimationIt = layoutAnimations_.find(tag);
if (layoutAnimationIt == layoutAnimations_.end()) {
continue;
}
auto &layoutAnimation = layoutAnimationIt->second;
auto newView = std::make_shared<ShadowView>(*layoutAnimation.finalView);
newView->props = updateValues.newProps;
updateLayoutMetrics(newView->layoutMetrics, updateValues.frame);
mutations.push_back(ShadowViewMutation::UpdateMutation(
*layoutAnimation.currentView, *newView, layoutAnimation.parentTag));
layoutAnimation.currentView = newView;
}
updateMap.clear();
}
void LayoutAnimationsProxy::endAnimationsRecursively(
std::shared_ptr<MutationNode> node,
ShadowViewMutationList &mutations) const {
maybeCancelAnimation(node->tag);
node->state = DELETED;
// iterate from the end, so that children
// with higher indices appear first in the mutations list
for (auto it = node->unflattenedChildren.rbegin();
it != node->unflattenedChildren.rend();
it++) {
auto &subNode = *it;
if (subNode->state != DELETED) {
endAnimationsRecursively(subNode, mutations);
}
}
mutations.push_back(node->mutation);
nodeForTag_.erase(node->tag);
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "delete " << node->tag << std::endl;
#endif
mutations.push_back(
ShadowViewMutation::DeleteMutation(node->mutation.oldChildShadowView));
}
void LayoutAnimationsProxy::maybeDropAncestors(
std::shared_ptr<Node> parent,
std::shared_ptr<MutationNode> child,
ShadowViewMutationList &cleanupMutations) const {
parent->removeChildFromUnflattenedTree(child);
if (!parent->isMutationMode()) {
return;
}
auto node = std::static_pointer_cast<MutationNode>(parent);
node->animatedChildren.erase(child->tag);
if (node->animatedChildren.empty() && node->state != ANIMATING) {
nodeForTag_.erase(node->tag);
cleanupMutations.push_back(node->mutation);
maybeCancelAnimation(node->tag);
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "delete " << node->tag << std::endl;
#endif
cleanupMutations.push_back(
ShadowViewMutation::DeleteMutation(node->mutation.oldChildShadowView));
maybeDropAncestors(node->unflattenedParent, node, cleanupMutations);
}
}
const ComponentDescriptor &
LayoutAnimationsProxy::getComponentDescriptorForShadowView(
const ShadowView &shadowView) const {
return componentDescriptorRegistry_->at(shadowView.componentHandle);
}
bool LayoutAnimationsProxy::startAnimationsRecursively(
std::shared_ptr<MutationNode> node,
bool shouldRemoveSubviewsWithoutAnimations,
bool shouldAnimate,
bool isScreenPop,
ShadowViewMutationList &mutations) const {
if (isRNSScreen(node)) {
isScreenPop = true;
}
shouldAnimate = !isScreenPop &&
layoutAnimationsManager_->shouldAnimateExiting(node->tag, shouldAnimate);
bool hasExitAnimation = shouldAnimate &&
layoutAnimationsManager_->hasLayoutAnimation(
node->tag, LayoutAnimationType::EXITING);
bool hasAnimatedChildren = false;
shouldRemoveSubviewsWithoutAnimations =
shouldRemoveSubviewsWithoutAnimations && !hasExitAnimation;
std::vector<std::shared_ptr<MutationNode>> toBeRemoved;
// iterate from the end, so that children
// with higher indices appear first in the mutations list
for (auto it = node->unflattenedChildren.rbegin();
it != node->unflattenedChildren.rend();
it++) {
auto &subNode = *it;
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "child " << subNode->tag << " "
<< " " << shouldAnimate << " "
<< shouldRemoveSubviewsWithoutAnimations << std::endl;
#endif
if (subNode->state != UNDEFINED && subNode->state != MOVED) {
if (shouldAnimate && subNode->state != DEAD) {
node->animatedChildren.insert(subNode->tag);
hasAnimatedChildren = true;
} else {
endAnimationsRecursively(subNode, mutations);
toBeRemoved.push_back(subNode);
}
} else if (startAnimationsRecursively(
subNode,
shouldRemoveSubviewsWithoutAnimations,
shouldAnimate,
isScreenPop,
mutations)) {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "child " << subNode->tag
<< " start animations returned true " << std::endl;
#endif
node->animatedChildren.insert(subNode->tag);
hasAnimatedChildren = true;
} else if (subNode->state == MOVED) {
mutations.push_back(subNode->mutation);
toBeRemoved.push_back(subNode);
} else if (shouldRemoveSubviewsWithoutAnimations) {
maybeCancelAnimation(subNode->tag);
mutations.push_back(subNode->mutation);
toBeRemoved.push_back(subNode);
subNode->state = DELETED;
nodeForTag_.erase(subNode->tag);
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "delete " << subNode->tag << std::endl;
#endif
mutations.push_back(ShadowViewMutation::DeleteMutation(
subNode->mutation.oldChildShadowView));
} else {
subNode->state = WAITING;
}
}
for (auto &subNode : toBeRemoved) {
node->removeChildFromUnflattenedTree(subNode);
}
if (node->state == MOVED) {
auto replacement = std::make_shared<Node>(*node);
for (auto subNode : node->children) {
subNode->parent = replacement;
}
for (auto subNode : node->unflattenedChildren) {
subNode->unflattenedParent = replacement;
}
nodeForTag_[replacement->tag] = replacement;
return false;
}
bool wantAnimateExit = hasExitAnimation || hasAnimatedChildren;
if (hasExitAnimation) {
node->state = ANIMATING;
startExitingAnimation(node->tag, node->mutation);
} else {
layoutAnimationsManager_->clearLayoutAnimationConfig(node->tag);
}
if (!wantAnimateExit) {
return false;
}
return true;
}
void LayoutAnimationsProxy::updateIndexForMutation(
ShadowViewMutation &mutation) const {
if (mutation.index == -1) {
return;
}
if (!nodeForTag_.contains(mutation.parentTag)) {
return;
}
auto parent = nodeForTag_[mutation.parentTag];
int size = 0, prevIndex = -1, offset = 0;
for (auto &subNode : parent->children) {
size += subNode->mutation.index - prevIndex - 1;
if (mutation.index < size) {
break;
}
offset++;
prevIndex = subNode->mutation.index;
}
#ifdef LAYOUT_ANIMATIONS_LOGS
int tag = mutation.type == ShadowViewMutation::Insert
? mutation.newChildShadowView.tag
: mutation.oldChildShadowView.tag;
LOG(INFO) << "update index for " << tag << " in "
<< mutation.parentTag << ": " << mutation.index << " -> "
<< mutation.index + offset << std::endl;
#endif
mutation.index += offset;
}
bool LayoutAnimationsProxy::shouldOverridePullTransaction() const {
return true;
}
void LayoutAnimationsProxy::createLayoutAnimation(
const ShadowViewMutation &mutation,
ShadowView &oldView,
const SurfaceId &surfaceId,
const int tag) const {
int count = 1;
auto layoutAnimationIt = layoutAnimations_.find(tag);
if (layoutAnimationIt != layoutAnimations_.end()) {
auto &layoutAnimation = layoutAnimationIt->second;
oldView = *layoutAnimation.currentView;
count = layoutAnimation.count + 1;
}
auto finalView = std::make_shared<ShadowView>(
mutation.type == ShadowViewMutation::Remove
? mutation.oldChildShadowView
: mutation.newChildShadowView);
auto currentView = std::make_shared<ShadowView>(oldView);
layoutAnimations_.insert_or_assign(
tag, LayoutAnimation{finalView, currentView, mutation.parentTag, {}, count});
}
void LayoutAnimationsProxy::startEnteringAnimation(
const int tag,
ShadowViewMutation &mutation) const {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "start entering animation for tag " << tag << std::endl;
#endif
auto finalView = std::make_shared<ShadowView>(mutation.newChildShadowView);
auto current = std::make_shared<ShadowView>(mutation.newChildShadowView);
auto &viewProps =
static_cast<const ViewProps &>(*mutation.newChildShadowView.props);
auto opacity = viewProps.opacity;
uiScheduler_->scheduleOnUI(
[finalView, current, mutation, opacity, this, tag]() {
Rect window{};
{
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
layoutAnimations_.insert_or_assign(
tag, LayoutAnimation{finalView, current, mutation.parentTag, opacity});
window =
surfaceManager.getWindow(mutation.newChildShadowView.surfaceId);
}
Snapshot values(mutation.newChildShadowView, window);
jsi::Object yogaValues(uiRuntime_);
yogaValues.setProperty(uiRuntime_, "targetOriginX", values.x);
yogaValues.setProperty(uiRuntime_, "targetGlobalOriginX", values.x);
yogaValues.setProperty(uiRuntime_, "targetOriginY", values.y);
yogaValues.setProperty(uiRuntime_, "targetGlobalOriginY", values.y);
yogaValues.setProperty(uiRuntime_, "targetWidth", values.width);
yogaValues.setProperty(uiRuntime_, "targetHeight", values.height);
yogaValues.setProperty(uiRuntime_, "windowWidth", values.windowWidth);
yogaValues.setProperty(uiRuntime_, "windowHeight", values.windowHeight);
layoutAnimationsManager_->startLayoutAnimation(
uiRuntime_, tag, LayoutAnimationType::ENTERING, yogaValues);
});
}
void LayoutAnimationsProxy::startExitingAnimation(
const int tag,
ShadowViewMutation &mutation) const {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "start exiting animation for tag " << tag << std::endl;
#endif
auto surfaceId = mutation.oldChildShadowView.surfaceId;
uiScheduler_->scheduleOnUI([this, tag, mutation, surfaceId]() {
auto oldView = mutation.oldChildShadowView;
Rect window{};
{
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
createLayoutAnimation(mutation, oldView, surfaceId, tag);
window = surfaceManager.getWindow(surfaceId);
}
Snapshot values(oldView, window);
jsi::Object yogaValues(uiRuntime_);
yogaValues.setProperty(uiRuntime_, "currentOriginX", values.x);
yogaValues.setProperty(uiRuntime_, "currentGlobalOriginX", values.x);
yogaValues.setProperty(uiRuntime_, "currentOriginY", values.y);
yogaValues.setProperty(uiRuntime_, "currentGlobalOriginY", values.y);
yogaValues.setProperty(uiRuntime_, "currentWidth", values.width);
yogaValues.setProperty(uiRuntime_, "currentHeight", values.height);
yogaValues.setProperty(uiRuntime_, "windowWidth", values.windowWidth);
yogaValues.setProperty(uiRuntime_, "windowHeight", values.windowHeight);
layoutAnimationsManager_->startLayoutAnimation(
uiRuntime_, tag, LayoutAnimationType::EXITING, yogaValues);
layoutAnimationsManager_->clearLayoutAnimationConfig(tag);
});
}
void LayoutAnimationsProxy::startLayoutAnimation(
const int tag,
const ShadowViewMutation &mutation) const {
#ifdef LAYOUT_ANIMATIONS_LOGS
LOG(INFO) << "start layout animation for tag " << tag << std::endl;
#endif
auto surfaceId = mutation.oldChildShadowView.surfaceId;
uiScheduler_->scheduleOnUI([this, mutation, surfaceId, tag]() {
auto oldView = mutation.oldChildShadowView;
Rect window{};
{
auto lock = std::unique_lock<std::recursive_mutex>(mutex);
createLayoutAnimation(mutation, oldView, surfaceId, tag);
window = surfaceManager.getWindow(surfaceId);
}
Snapshot currentValues(oldView, window);
Snapshot targetValues(mutation.newChildShadowView, window);
jsi::Object yogaValues(uiRuntime_);
yogaValues.setProperty(uiRuntime_, "currentOriginX", currentValues.x);
yogaValues.setProperty(uiRuntime_, "currentGlobalOriginX", currentValues.x);
yogaValues.setProperty(uiRuntime_, "currentOriginY", currentValues.y);
yogaValues.setProperty(uiRuntime_, "currentGlobalOriginY", currentValues.y);
yogaValues.setProperty(uiRuntime_, "currentWidth", currentValues.width);
yogaValues.setProperty(uiRuntime_, "currentHeight", currentValues.height);
yogaValues.setProperty(uiRuntime_, "targetOriginX", targetValues.x);
yogaValues.setProperty(uiRuntime_, "targetGlobalOriginX", targetValues.x);
yogaValues.setProperty(uiRuntime_, "targetOriginY", targetValues.y);
yogaValues.setProperty(uiRuntime_, "targetGlobalOriginY", targetValues.y);
yogaValues.setProperty(uiRuntime_, "targetWidth", targetValues.width);
yogaValues.setProperty(uiRuntime_, "targetHeight", targetValues.height);
yogaValues.setProperty(uiRuntime_, "windowWidth", targetValues.windowWidth);
yogaValues.setProperty(
uiRuntime_, "windowHeight", targetValues.windowHeight);
layoutAnimationsManager_->startLayoutAnimation(
uiRuntime_, tag, LayoutAnimationType::LAYOUT, yogaValues);
});
}
void LayoutAnimationsProxy::updateOngoingAnimationTarget(
const int tag,
const ShadowViewMutation &mutation) const {
layoutAnimations_[tag].finalView =
std::make_shared<ShadowView>(mutation.newChildShadowView);
}
void LayoutAnimationsProxy::maybeCancelAnimation(const int tag) const {
if (!layoutAnimations_.contains(tag)) {
return;
}
layoutAnimations_.erase(tag);
uiScheduler_->scheduleOnUI([this, tag]() {
layoutAnimationsManager_->cancelLayoutAnimation(uiRuntime_, tag);
});
}
void LayoutAnimationsProxy::transferConfigFromNativeID(
const std::string nativeIdString,
const int tag) const {
if (nativeIdString.empty()) {
return;
}
try {
auto nativeId = stoi(nativeIdString);
layoutAnimationsManager_->transferConfigFromNativeID(nativeId, tag);
} catch (std::invalid_argument) {
} catch (std::out_of_range) {
}
}
// When entering animations start, we temporarily set opacity to 0
// so that we can immediately insert the view at the right position
// and schedule the animation on the UI thread
std::shared_ptr<ShadowView> LayoutAnimationsProxy::cloneViewWithoutOpacity(
facebook::react::ShadowViewMutation &mutation,
const PropsParserContext &propsParserContext) const {
auto newView = std::make_shared<ShadowView>(mutation.newChildShadowView);
folly::dynamic opacity = folly::dynamic::object("opacity", 0);
auto newProps = getComponentDescriptorForShadowView(*newView).cloneProps(
propsParserContext, newView->props, RawProps(opacity));
newView->props = newProps;
return newView;
}
void LayoutAnimationsProxy::maybeRestoreOpacity(
LayoutAnimation &layoutAnimation,
const jsi::Object &newStyle) const {
if (layoutAnimation.opacity && !newStyle.hasProperty(uiRuntime_, "opacity")) {
newStyle.setProperty(
uiRuntime_, "opacity", jsi::Value(*layoutAnimation.opacity));
layoutAnimation.opacity.reset();
}
}
void LayoutAnimationsProxy::maybeUpdateWindowDimensions(
facebook::react::ShadowViewMutation &mutation,
SurfaceId surfaceId) const {
if (mutation.type == ShadowViewMutation::Update &&
!std::strcmp(
mutation.oldChildShadowView.componentName, RootComponentName)) {
surfaceManager.updateWindow(
surfaceId,
mutation.newChildShadowView.layoutMetrics.frame.size.width,
mutation.newChildShadowView.layoutMetrics.frame.size.height);
}
}
} // namespace reanimated
#endif // RCT_NEW_ARCH_ENABLED