forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android: Enable views to be nested within <Text>
Potential breaking change: The signature of ReactShadowNode's onBeforeLayout method was changed - Before: public void onBeforeLayout() - After: public void onBeforeLayout(NativeViewHierarchyOptimizer nativeViewHierarchyOptimizer) Implements same feature as this iOS PR: facebook#7304 Previously, only Text and Image could be nested within Text. Now, any view can be nested within Text. One restriction of this feature is that developers must give inline views a width and a height via the style prop. Previously, inline Images were supported via FrescoBasedReactTextInlineImageSpan. To get support for nesting views within Text, we create one special kind of span per inline view. This span is called TextInlineViewPlaceholderSpan. It is the same size as the inline view. Its job is just to occupy space -- it doesn't render any visual. After the text is rendered, we query the Android Layout object associated with the TextView to find out where it has positioned each TextInlineViewPlaceholderSpan. We then position the views to be at those locations. One tricky aspect of the implementation is that the Text component needs to be able to render native children (the inline views) but the Android TextView cannot have children. This is solved by having the native parent of the ReactTextView also host the inline views. Implementation-wise, this was accomplished by extending the NativeViewHierarchyOptimizer to handle this case. The optimizer now handles these cases: - Node is not in the native tree. An ancestor must host its children. - Node is in the native tree and it can host its own children. - (new) Node is in the native tree but it cannot host its own children. An ancestor must host both this node and its children. Limitation: Clipping ---------- If Text's height/width is small such that an inline view doesn't completely fit, the inline view may still be fully visible due to hoisting (the inline view isn't actually parented to the Text which has the limited size. It is parented to an ancestor which may have a different clipping rectangle.). Prior to this change, layout-only views had a similar limitation. Test Plan: ---------- Created a test app which tested a variety of cases including: - An inline view that is smaller than the line height - An inline view that is bigger than the line height - An inline view that is in text that wraps to multiple lines - An inline view that is in text that gets truncated and the inline view occurs past the truncation point. - An inline view that is nested under 1 and 2 layers of Text I considered each of the above test cases under these variations: - Type of inline element: image or view - Type of hosting text component: Text or TextInput All of the inline image cases behave identically before and after my change. The inline view behaviors are similar to the inline image behaviors. Note that TextInput doesn't support inline views (only inline images). Lastly, we've been using a change like this in Skype for the past couple of years. Changelog: ---------- [Android] [Added] - Enable views to be nested within <Text>
- Loading branch information
Adam Comella
committed
Feb 22, 2019
1 parent
81860c5
commit 773d9ce
Showing
20 changed files
with
668 additions
and
133 deletions.
There are no files selected for viewing
This file contains 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 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 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
24 changes: 24 additions & 0 deletions
24
ReactAndroid/src/main/java/com/facebook/react/uimanager/IViewManagerWithChildren.java
This file contains 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,24 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.uimanager; | ||
|
||
public interface IViewManagerWithChildren { | ||
/** | ||
* Returns whether this View type needs to handle laying out its own children instead of | ||
* deferring to the standard css-layout algorithm. | ||
* Returns true for the layout to *not* be automatically invoked. Instead onLayout will be | ||
* invoked as normal and it is the View instance's responsibility to properly call layout on its | ||
* children. | ||
* Returns false for the default behavior of automatically laying out children without going | ||
* through the ViewGroup's onLayout method. In that case, onLayout for this View type must *not* | ||
* call layout on its children. | ||
*/ | ||
public boolean needsCustomLayoutForChildren(); | ||
} |
27 changes: 27 additions & 0 deletions
27
ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeKind.java
This file contains 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,27 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.uimanager; | ||
|
||
// Common conditionals: | ||
// - `kind == PARENT` checks whether the node can host children in the native tree. | ||
// - `kind != NONE` checks whether the node appears in the native tree. | ||
|
||
public enum NativeKind { | ||
// Node is in the native hierarchy and the HierarchyOptimizer should assume it can host children | ||
// (e.g. because it's a ViewGroup). Note that it's okay if the node doesn't support children. When | ||
// the HierarchyOptimizer generates children manipulation commands for that node, the | ||
// HierarchyManager will catch this case and throw an exception. | ||
PARENT, | ||
// Node is in the native hierarchy, it may have children, but it cannot host them itself (e.g. | ||
// because it isn't a ViewGroup). Consequently, its children need to be hosted by an ancestor. | ||
LEAF, | ||
// Node is not in the native hierarchy. | ||
NONE | ||
} |
This file contains 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 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
Oops, something went wrong.