-
-
Notifications
You must be signed in to change notification settings - Fork 597
Fix: layout shift on orientation change #3295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kmichalikk
wants to merge
5
commits into
main
Choose a base branch
from
@kmichalikk/layout-shift-on-orientation-change
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+365
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b26bdfb
reproduce layout shift issue #2933
AndreiCalazans 922c9e7
Modify the provided example to highlight the bug even more
kmichalikk 2721301
Add a commit hook to detect the orientation change early
kmichalikk 60ed155
Add a feature flag to enable / disable the hook
kmichalikk f6ef130
Fix CR issues for c++
kmichalikk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,138 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import type { PropsWithChildren } from 'react'; | ||
| import { | ||
| StyleSheet, | ||
| Text, | ||
| useColorScheme, | ||
| View, | ||
| } from 'react-native'; | ||
|
|
||
| import { Screen, ScreenStack } from 'react-native-screens'; | ||
|
|
||
| type SectionProps = PropsWithChildren<{ | ||
| title: string; | ||
| }>; | ||
|
|
||
| function Section({ children, title }: SectionProps): React.JSX.Element { | ||
| return ( | ||
| <View style={styles.sectionContainer}> | ||
| <Text>{title}</Text> | ||
| <Text>{children}</Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function floodJsThread() { | ||
| setInterval(() => { | ||
| const end = Date.now() + 10; | ||
| while (Date.now() < end) { | ||
| // Intentionally do nothing; just burn CPU cycles. | ||
| Math.sqrt(Math.random()); | ||
| } | ||
| }, 12); | ||
| } | ||
|
|
||
| /* | ||
| * create artificial pressure in the JS thread to show off thep problem. | ||
| * */ | ||
| floodJsThread(); | ||
| floodJsThread(); | ||
|
|
||
| function AppMain(): React.JSX.Element { | ||
| const isDarkMode = useColorScheme() === 'dark'; | ||
|
|
||
| const backgroundStyle = { | ||
| flex: 1, | ||
| backgroundColor: 'white', | ||
| }; | ||
|
|
||
| const [num, setNum] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| let i = 0; | ||
| setInterval(() => { | ||
| i++; | ||
| setNum(i); | ||
| }, 2) | ||
| }, []); | ||
|
|
||
| /* | ||
| * To keep the template simple and small we're adding padding to prevent view | ||
| * from rendering under the System UI. | ||
| * For bigger apps the reccomendation is to use `react-native-safe-area-context`: | ||
| * https://github.com/AppAndFlow/react-native-safe-area-context | ||
| * | ||
| * You can read more about it here: | ||
| * https://github.com/react-native-community/discussions-and-proposals/discussions/827 | ||
| */ | ||
| const safePadding = '5%'; | ||
|
|
||
| return ( | ||
| <View style={backgroundStyle}> | ||
| {/* // TODO nowy task | ||
| <StatusBar | ||
| barStyle={isDarkMode ? 'light-content' : 'dark-content'} | ||
| backgroundColor={backgroundStyle.backgroundColor} | ||
| /> */} | ||
| <View | ||
| style={{ | ||
| flex: 1, | ||
| paddingHorizontal: safePadding, | ||
| paddingBottom: safePadding, | ||
| justifyContent: 'center', | ||
| alignContent: 'center', | ||
| }}> | ||
| <Section title="Step One"> | ||
| This test shows how the native layout update triggers a layout shift. | ||
| </Section> | ||
| <Section title="Step One"> | ||
| There is a view with a blue background. We don't expect to ever see | ||
| flashes of the blue background. | ||
| </Section> | ||
| <Section title="Orientation"> {num} </Section> | ||
| </View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| sectionContainer: { | ||
| marginTop: 32, | ||
| paddingHorizontal: 24, | ||
| }, | ||
| sectionTitle: { | ||
| fontSize: 24, | ||
| fontWeight: '600', | ||
| }, | ||
| sectionDescription: { | ||
| marginTop: 8, | ||
| fontSize: 18, | ||
| fontWeight: '400', | ||
| }, | ||
| highlight: { | ||
| fontWeight: '700', | ||
| }, | ||
| }); | ||
|
|
||
| function App() { | ||
| return ( | ||
| <ScreenStack style={{ flex: 1, backgroundColor: 'red' }}> | ||
| <Screen | ||
| style={{ | ||
| position: 'absolute', | ||
| top: 0, | ||
| left: 0, | ||
| bottom: 0, | ||
| right: 0, | ||
| backgroundColor: 'blue', | ||
| padding: 20, | ||
| }} | ||
| enabled | ||
| isNativeStack> | ||
| <AppMain /> | ||
| </Screen> | ||
| </ScreenStack> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
103 changes: 103 additions & 0 deletions
103
common/cpp/react/renderer/components/rnscreens/RNSScreenShadowNodeCommitHook.cpp
This file contains hidden or 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,103 @@ | ||
| #ifdef ANDROID | ||
|
|
||
| #include "RNSScreenShadowNodeCommitHook.h" | ||
| #include <stack> | ||
|
|
||
| namespace facebook { | ||
| namespace react { | ||
|
|
||
| RNSScreenShadowNodeCommitHook::RNSScreenShadowNodeCommitHook( | ||
| std::shared_ptr<const ContextContainer> contextContainer) | ||
| : contextContainer_(contextContainer) { | ||
| if (contextContainer_) { | ||
| auto fabricUIManager = | ||
| contextContainer_ | ||
| ->at<jni::alias_ref<facebook::react::JFabricUIManager::javaobject>>( | ||
| "FabricUIManager"); | ||
| fabricUIManager->getBinding() | ||
| ->getScheduler() | ||
| ->getUIManager() | ||
| ->registerCommitHook(*this); | ||
| } | ||
| } | ||
|
|
||
| RNSScreenShadowNodeCommitHook::~RNSScreenShadowNodeCommitHook() noexcept { | ||
| if (contextContainer_) { | ||
| auto fabricUIManager = | ||
| contextContainer_ | ||
| ->at<jni::alias_ref<facebook::react::JFabricUIManager::javaobject>>( | ||
| "FabricUIManager"); | ||
| fabricUIManager->getBinding() | ||
| ->getScheduler() | ||
| ->getUIManager() | ||
| ->unregisterCommitHook(*this); | ||
| } | ||
| } | ||
|
|
||
| RootShadowNode::Unshared RNSScreenShadowNodeCommitHook::shadowTreeWillCommit( | ||
| const facebook::react::ShadowTree &shadowTree, | ||
| const RootShadowNode::Shared &oldRootShadowNode, | ||
| const RootShadowNode::Unshared &newRootShadowNode, | ||
| const ShadowTreeCommitOptions &) noexcept { | ||
| auto oldRootProps = | ||
| std::static_pointer_cast<const RootProps>(oldRootShadowNode->getProps()); | ||
| auto newRootProps = | ||
| std::static_pointer_cast<const RootProps>(newRootShadowNode->getProps()); | ||
|
|
||
| const bool wasHorizontal = isHorizontal_(*oldRootProps.get()); | ||
| const bool willBeHorizontal = isHorizontal_(*newRootProps.get()); | ||
|
|
||
| const bool orientationDidChange = wasHorizontal != willBeHorizontal; | ||
|
|
||
| std::shared_ptr<ShadowNode> finalRootShadowNode = newRootShadowNode; | ||
| if (orientationDidChange) { | ||
| std::vector<const RNSScreenShadowNode *> screens; | ||
| findScreenNodes(newRootShadowNode, screens); | ||
|
|
||
| for (auto screen : screens) { | ||
| const auto rootShadowNodeClone = newRootShadowNode->cloneTree( | ||
| screen->getFamily(), [](const ShadowNode &oldShadowNode) { | ||
| auto clone = | ||
| oldShadowNode.clone({.state = oldShadowNode.getState()}); | ||
| auto screenNode = static_pointer_cast<RNSScreenShadowNode>(clone); | ||
| auto yogaNode = | ||
| static_pointer_cast<YogaLayoutableShadowNode>(clone); | ||
|
|
||
| screenNode->resetFrameSizeState(); | ||
| yogaNode->setSize({YGUndefined, YGUndefined}); | ||
|
|
||
| return clone; | ||
| }); | ||
|
|
||
| if (rootShadowNodeClone) { | ||
| finalRootShadowNode = rootShadowNodeClone; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return std::static_pointer_cast<RootShadowNode>(finalRootShadowNode); | ||
| } | ||
|
|
||
| void RNSScreenShadowNodeCommitHook::findScreenNodes( | ||
kmichalikk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const std::shared_ptr<const ShadowNode> &rootShadowNode, | ||
| std::vector<const RNSScreenShadowNode *> &screenNodes) { | ||
| std::stack<const ShadowNode *> shadowNodesToVisit; | ||
| shadowNodesToVisit.emplace(rootShadowNode.get()); | ||
|
|
||
| while (!shadowNodesToVisit.empty()) { | ||
| auto node = shadowNodesToVisit.top(); | ||
| shadowNodesToVisit.pop(); | ||
|
|
||
| for (auto const &child : node->getChildren()) { | ||
| if (node->getComponentHandle() == RNSScreenShadowNode::Handle()) { | ||
| screenNodes.push_back(static_cast<const RNSScreenShadowNode *>(node)); | ||
| } | ||
| shadowNodesToVisit.emplace(child.get()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace react | ||
| } // namespace facebook | ||
|
|
||
| #endif // ANDROID | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We never cleanup reference to the
contextContainer. Shall we do it here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we? doesn't the smart pointer handle this on its own?