Commit Graph

9142 Commits

Author SHA1 Message Date
Andrei Coman 65f4988ddf Support multi sources for images
Summary:
@public
This adds support for specifying multiple sources for an image component, so that native can choose the best one based on the flexbox-computed size of the image.
The API is as follows: the image component receives in the `source` prop an array of objects of the type `{uri, width, height}`. On the native side, the native component will wait for the layout pass to receive the width and height of the image, and then parse the array to find the best fitting one. For now, this does not support local resources, but it will be added soon.
To see how this works and play with it, there's an example called `MultipleSourcesExample` under `ImageExample` In UIExplorer.

Reviewed By: foghina

Differential Revision: D3364550
2016-12-19 13:40:26 -08:00
Ahmed El-Helw e1ece03593 Improve removeClippedSubviews for overflowing views
Summary:
Historically, removeClippedSubviews for Nodes would not clip views
that overflowed their parent. This patch changes that, so that Nodes can
properly clip views when they are off screen (even if they have descendants
that overflow the bounds of their parent).

This is done by calculating a set of offsets from the actual width and
height of the view, and using those in the clipping calculations.

Reviewed By: sriramramani

Differential Revision: D3409859
2016-12-19 13:40:26 -08:00
Ahmed El-Helw 4bb33f93cd Promote grandchildren of certain views are Views
Summary:
As an optimization, for something like a ScrollView which contains
a FlatViewGroup containing posts, make sure that each post is explicitly
mounted to a View. This may help improve performance, especially when said
Views are otherwise inlined as DrawCommands instead of actual Views.

Reviewed By: astreet

Differential Revision: D3161232
2016-12-19 13:40:26 -08:00
Andrei Coman 0adc390a19 Align inline image in nodes text
Summary:
This is similar to https://github.com/facebook/react-native/pull/6528
for the non-nodes inline images.

Reviewed By: dmmiller

Differential Revision: D3358530
2016-12-19 13:40:26 -08:00
Ahmed El-Helw efb65e5665 Fix leak in Nodes due to removeClippedSubviews
Summary:
The removeClippedSubviews optimization often detaches views while
maintaining strong references to them (so they can be attached again later
on). However, when removing the parent view, any detached views end up not
being cleaned up or removed, thus leaking memory. This fixes this by
explicitly dropping detached views when the parent is removed.

Reviewed By: astreet

Differential Revision: D3337513
2016-12-19 13:40:26 -08:00
Ahmed El-Helw c62840c7a8 RCTVirtualText wasn't clickable in certain cases
Summary:
Text in Nodes is squashed into a single DrawCommand for drawing a
Boring or StaticLayout. Touch is handled using a TextNodeRegion subclass of
NodeRegion that knows how to identify pieces of text inside of the DrawCommand
based on spans in the text. However, we only use a TextNodeRegion on the
second call for updateNodeRegion for an RCTText. If there is only one call,
the NodeRegion will just be a normal one. This patch ensures that the
NodeRegion for an RCTText is always a TextNodeRegion, allowing for null
Layouts that are set when the DrawCommand is made.

Reviewed By: astreet

Differential Revision: D3291682
2016-12-19 13:40:26 -08:00
Ahmed El-Helw 9d67989001 Don't clip overflowing Nodes
Summary:
As of D3235050, Nodes supports the optimization of removing clipped
subviews from the hierarchy. However, because Nodes supports overflow:visible,
this could cause issues when DrawCommands overflow the bounds of their parent
container. This patch fixes this by not clipping any overflowing Nodes.

Reviewed By: astreet

Differential Revision: D3235072
2016-12-19 13:40:26 -08:00
Ahmed El-Helw 5f162ca119 Implement RemoveClippedSubviews for Nodes
Summary:
RN has an optimization in which a ScrollView (or similar ViewGroups)
can ask to remove clipped subviews from the View hierarchy. This patch
implements this optimization for Nodes, but instead of adding and removing the
Views, it attaches and detaches Views instead.

Note that this patch does not handle overflow: visible. This is addressed in a
stacked patch on top of this patch (to simplify the review process).

Reviewed By: astreet

Differential Revision: D3235050
2016-12-19 13:40:25 -08:00
Ahmed El-Helw 96cb8165c8 Fix setJSResponder Nodes crash
Summary:
Nodes crashed when setJSResponder was called on a virtual (non-View)
node, because a View could not be found using that react tag. The solution is
two fold - first, to figure out the View parent and pass that to
setJSResponder in addition to that of the virtual tag. Secondly, we weren't
mounting views that had animation properties (transform, for example) to
Views, which caused related code to fail.

Reviewed By: sriramramani

Differential Revision: D3301310
2016-12-19 13:40:25 -08:00
Ahmed El-Helw 273c2e539c Improves RCTImage loading handlers to mimic iOS behavior
Summary: This is a port of D3283011 to Nodes.

Reviewed By: sahrens

Differential Revision: D3290662
2016-12-19 13:40:25 -08:00
Dave Miller fede138786 Add setChildren to FlatUIImplementation
Summary: Add support in FlatUIImplementation for the setChildren call added in D3235369

Reviewed By: astreet

Differential Revision: D3240883
2016-12-19 13:40:25 -08:00
Ahmed El-Helw 0d21baf604 Don't save matrix when using canvas.save
Summary:
Canvas.save by default saves both the matrix (for translations,
scaling, etc) and the clip (clipRect) - in most of our cases, we really only
care to save and restore the clip, not the matrix.

Reviewed By: sriramramani

Differential Revision: D3235698
2016-12-19 13:40:25 -08:00
Emil Sjolander 118cc4b9b7 Import css-layout measure mode changes from pull request #163
Summary: See https://github.com/facebook/css-layout/pull/163

Reviewed By: lucasr

Differential Revision: D3167760
2016-12-19 13:40:25 -08:00
Emil Sjolander cd06228f7a backout update of css-layout. breaks ios <9
Summary: Backed out changeset 29fa5aef3dbcc12d63c01d211b32bb598caadd0d and 56cc13ffbc4805aa0789df62a04e19f23a438bd3.

Reviewed By: javache

Differential Revision: D3164127
2016-12-19 13:40:25 -08:00
Ahmed El-Helw 72eb547f22 Cleanup React comments and unused imports
Summary: @public Fix some typos in comments.

Reviewed By: astreet

Differential Revision: D3161278
2016-12-19 13:40:25 -08:00
Ahmed El-Helw a099869edd Don't intercept touch for Nodes after down event
Summary:
With nodes, it's possible for a touchable region to not be
explicitly mounted to a View. To work around this (and allow the region to
be the handler of the touch event), FlatViewGroup intercepts touch events
when the touch lies within any of the virtual NodeRegions.

This can sometimes be wrong - the canonical example is when touch starts
outside of a particular FlatViewGroup (so someone else, for example a
sibling) intercepts the touch event, and then the person moves over a
different FlatViewGroup, causing it to intercept the touch event when it
shouldn't. To fix this, we only allow intercepting touch events due to
NodeRegions on the down event.

Reviewed By: astreet

Differential Revision: D3160152
2016-12-19 13:40:25 -08:00
Ahmed El-Helw cbf195e165 Do less work in Nodes when clipping isn't needed
Summary:
By default, Nodes causes views to not be clipped, unless overflow is
explicitly set to hidden. Consequently, Nodes sets all the clipping bounds to
negative infinity, and does some extra work (saving the canvas layer,
clipping, etc) before drawing. This optimization skips the extra work when
it's not needed.

Reviewed By: sriramramani

Differential Revision: D3161268
2016-12-19 13:40:25 -08:00
Ahmed El-Helw 5d6e73d0eb Don't mount nodes to Views on click
Summary:
Initially, we used to mount nodes to Views anytime a node was
clicked. This was not useful, since we could still not handle touch when
a touch event was already dispatched. Later, a fix was pushed that
supported handling touch events for non-View NodeRegions. Part of the
intention was to remove this code, but it was forgotten.

Reviewed By: sriramramani

Differential Revision: D3160532
2016-12-19 13:40:25 -08:00
Emil Sjolander c1474cc671 Import css-layout measure mode changes from pull request #163
Summary: See https://github.com/facebook/css-layout/pull/163

Reviewed By: astreet

Differential Revision: D3059432
2016-12-19 13:40:24 -08:00
Emil Sjolander 7a31ca29a4 Migrate text layout code to use TextDirectionHeuristicCompat instead of forceLTR flag
Summary: This allows users of the API to have greater control over handling RTL. One example is Components which needs this greater control to correctly handle RTL.

Differential Revision: D3120721
2016-12-19 13:40:24 -08:00
Denis Koroskin f4690ef8de Make sure UIImplementation methods that touch native View has the Views created
Summary:
UIImplementation has a few methods that in the end touch native Views, such as dispatchViewManagerCommand, addAnimation, sendAccessibilityEvent etc. There are 2 cases where it is possible to have those methods called on shadow nodes that don't have backing Views created:
- backing view is scheduled to be created but not commited yet (StateBuilder accumulates createView commands into a queue and flushes it in the very end)
- shadow node doesn't mount to a View so there is no backing View

Touching View in UI thread in these 2 cases will either lead to silent error (e.g. failure callback will execute), or a native crash.

This diff is overriding all UIImplementation methods that touch Views in UI thread and makes sure that backing View is created before we do so.

Reviewed By: ahmedre

Differential Revision: D3046392
2016-12-19 13:40:24 -08:00
Denis Koroskin 68580fcab6 Fix RCTText crashing when negative width is passed to measure
Summary: In some rare cases, RCTText.measure can receive negative width which will crash with an assertion in android.text.Layout because it expects a positive value. This is a temporary fix to treat negative values as unconstrained width until the original bug is fixed.

Reviewed By: sriramramani

Differential Revision: D3038767
2016-12-19 13:40:24 -08:00
Ahmed El-Helw aa609a46ad Revert nodes touch workaround
Summary:
Since we now have the correct fix in place, this patch is to revert
the hack we put in place to not be launch blocking.

Reviewed By: sriramramani

Differential Revision: D3019883
2016-12-19 13:40:24 -08:00
Denis Koroskin 89cf5c49b9 Make FlatViewGroup implement ReactCompoundViewGroup interface
Summary: In FlatViewGroup, we flatten some react nodes into parent while mounting others into child Views. This is causing touch events being dispatched to wrong targets because child Views are \"stealing\" touch events from flattened Views. To fix the issue, implement ReactCompoundViewGroup to provide information about both virtual and non-virtual nodes.

Reviewed By: ahmedre

Differential Revision: D3018054
2016-12-19 13:40:24 -08:00
Denis Koroskin b52928c484 Include non-virtual nodes when collecting NodeRegions
Summary: Before this patch, we only collected virtual nodes in NodeRegions, because NodeRegions are only needed to implement ReactCompoundView.reactTargetForTouch() which is only interested in virtual nodes. In the next patch, FlatViewGroup will implement ReactCompoundViewGroup interface which requires knowledge of both virtual and non-virtual children. As a step towards that, we need to include non-virtual nodes in NodeRegions. This patch is implementing that. By itself, it should have not cause any changes in application behavior: we add non-virtual nodes to NodeRegions and mark them as non-virtual, then skip all non-virtual nodes in reactTagForTouch().

Reviewed By: ahmedre

Differential Revision: D3018047
2016-12-19 13:40:24 -08:00
Denis Koroskin 8014147013 Fix order of NodeRegions
Summary: Before the patch, order of NodeRegions was inconsistent. Given parent A and 3 children B, C and D, we collect DrawCommands like this: A, B, C, D but NodeRegions were collected as B, C, D, A which neither matches draw order, nor a reverse of it. This patch changes it so that NodeRegions are collected in drawing order (A, B, C, D) and we iterate backwards to find correct touch target (in case they overlap).

Reviewed By: ahmedre

Differential Revision: D3018034
2016-12-19 13:40:24 -08:00
Ahmed El-Helw 4d25624a62 Work around nodes touch bug for Groups
Summary:
This is a hack to fix the Groups dialog bug so that we can get some
real data in production from using Nodes. This should be replaced with a
better solution in the near future.

Differential Revision: D3016859
2016-12-19 13:40:24 -08:00
Denis Koroskin c75f3b7d9f Make sure we call notifyChanged(true) when a child is added to RCTVirtualText
Summary: CSSNode.addChildAt() calls dirty() to invalidate the node and propagate dirty flag up to the root. However, ReactShadowNode overrides dirty() for virtual nodes so it does nothing. This results in bugs where an added text doesn't trigger a measure pass because RCTText is never dirtied. To fix the bug, override addChildAt() in RCTVirtualText and explicitly call notifyChanged(true) to make sure hosting RCTText is dirtied and re-measured/re-laid out.

Reviewed By: ahmedre

Differential Revision: D3016827
2016-12-19 13:40:24 -08:00
Ahmed El-Helw 272d99ed56 Revert workaround for Nodes detach
Summary:
This code reverts D3004541, since it fixes the symptom instead
of the root cause. Root cause fix is in D3011191.

Differential Revision: D3011291
2016-12-19 13:40:24 -08:00
Ahmed El-Helw 1a4d40df6c Fix commit order of operations in Nodes
Summary: @public Split dispatchViewUpdates into two methods, which enables subclasses to commit pending ui operations, even when no root node is present.

Differential Revision: D3011191
2016-12-19 13:40:23 -08:00
Ahmed El-Helw a541384621 Simplify width calculation in RCTText
Summary:
getLayoutWidth is the same as right - left, and since we have right
and left, we can save a method call.

Reviewed By: sriramramani

Differential Revision: D3010697
2016-12-19 13:40:23 -08:00
Ahmed El-Helw 9498f22439 Use layout width for BoringLayout in Nodes
Summary:
In D2980358, alignment was implemented for nodes. This unfortunately
introduced a bug, which is that when we have a BoringLayout that is set to
ALIGN_CENTER, it can't be seen. This is a result of the fact that the width
passed in to BoringLayout is Integer.MAX_VALUE. Since measure has already
been called at this point, we can just pass the layout's width as the width
of the BoringLayout.

Reviewed By: sriramramani

Differential Revision: D3004971
2016-12-19 13:40:23 -08:00
Ahmed El-Helw 45472fe400 Fix detaching dropped views in Nodes
Summary:
Groups had a crash when running with React with Nodes when returning
from a search screen. This was due to the fact that the node representing a
ShimmerFrameLayout was being dropped, and then later we were trying to detach
it. Since the view was already dropped, we shouldn't try to detach it since
it's already dropped and removed from the view hierarchy.

Reviewed By: sriramramani

Differential Revision: D3004541
2016-12-19 13:40:23 -08:00
Denis Koroskin 0b560d3d3d Don't try to remove Views that about to be removed with the root View
Summary:
We keep a list of FlatShadowNodes that mount to Views that we want to delete, and only flush it at the end of an update cycle. This results in a situation where a root view is being removed before children are removed, which results in a crash in NativeViewHierarchyManager because a View that we are trying to remove no longer exists. There are a few approaches to fix the issue:
a) make a check if a View exists before removing it. While works, it removes a bug protection when we erroneously trying to remove a View that no longer exists (such as this). I'd prefer to keep the check in place
b) flush the views-to-drop queue. This works, but does some extra work in UI thread (namely, removing Views that would be removed anyway)
c) trim the views-to-drop queue to remove any Views that will be removed anyway. This does a tiny bit of extra work in BG thread, but less work in UI thread.

This diff implements option c).

Reviewed By: ahmedre

Differential Revision: D2990105
2016-12-19 13:40:23 -08:00
Ahmed El-Helw 75117fc91a Minor code improvements for RCTTextInput
Summary: @public Expose hasUnseenUpdates in ReactShadowNode. Various text input cleanup and fixes.

Differential Revision: D2975870
2016-12-19 13:40:23 -08:00
Ahmed El-Helw df382e986c Implement alignment for nodes Text
Summary: Alignment wasn't implemented for Text, this patch supports it.

Differential Revision: D2980358
2016-12-19 13:40:23 -08:00
Denis Koroskin 848bae2e95 Fix FlatUIImplementation.manageChildren() failing when moveFrom is not sorted
Summary:
Code in FlatUIImplementation.manageChildren() incorrectly assumed that moveFrom is sorted and moveTo is not, which is actually reverse: moveFrom is not sorted, and moveTo is. This means that we need to sort moveFrom before we can traverse it, and that we no longer need to sort moveTo (we did it by moving nodes to mNodesToMove first and then sorting it).

The sorting algorithm used is borrowed from Android implementation of insertion sort used in DualPivotQuicksort.doSort() when number of elements < INSERTION_SORT_THRESHOLD(32) which is 99.999% the case in UIImplementation.manageChildren() (most of the time this array is either empty or only contains 1 element).

Another (very rare) bug this is fixing is that the code only worked for FlatShadowNodes, but not all shadow nodes are FlatShadowNodes (there are rare exceptions, such as ARTShape, ARTGroup etc). New code works with all types of shadow nodes.

Reviewed By: ahmedre

Differential Revision: D2975787
2016-12-19 13:40:23 -08:00
Denis Koroskin 42fb9a3d91 Only collect state from nodes that have been updated
Summary: We are currently traversing entire tree every time there is any update to any of the nodes, which is hugely inefficient. Instead, we can early out for nodes that are known to contain no updates. This saves a lof of CPU. This optimization can be turned off, and an Exception will be thrown if there was an unexpected update.

Reviewed By: ahmedre

Differential Revision: D2975706
2016-12-19 13:40:23 -08:00
Ahmed El-Helw 7055c52288 Fix screenshot tests for React with nodes
Summary:
Fix screenshot tests for React with nodes. It was broken due to
calling clipRect with bounds of [-∞, ∞], which, due to a bug in Canvas that
appeared in screenshot tests, caused the view not to draw. Since this is a
no-op anyway, this patch just doesn't call clipRect when we have infinite
bounds.

Differential Revision: D2975494
2016-12-19 13:40:23 -08:00
Denis Koroskin b9e3ef2d5f Add @Nullable annotation to FlatShadowNode.obtainLayoutEvent()
Summary: Lint tool raised a warning. This diff is fixing it.

Reviewed By: sriramramani

Differential Revision: D2977649
2016-12-19 13:40:23 -08:00
Denis Koroskin ca7a3519cf Store \"dirty\" and \"dirty descendant\" flags in every node instead of only marking root node as invalid
Summary: Right now invalidate always tell the root node that the tree is dirty, and next update will traverse the entire tree in search of changes. While this works correctly, it's not the most efficient implementation. It is more efficient to store dirty flag in every node, and skip entire subtrees if this node and all descendants are already up to date. This diff is a first step towards that optimization.

Reviewed By: ahmedre

Differential Revision: D2955197
2016-12-19 13:40:22 -08:00
Ahmed El-Helw 4c92a0b962 Support setText in RCTTextInput
Summary:
Examples that use defaultValue as part of their declaration (such as
those examples under UIExplorerApp's TextInput) were being ignored. This patch
supports having text set directly on RCTTextInput and handles it properly.
Note that this patch depends on D2962643 and D2964847, without which the
TextInput example will look wrong.

Differential Revision: D2968002
2016-12-19 13:40:22 -08:00
Denis Koroskin a9c88bce14 Fix 2 bugs in FlatUIImplementation.removeChild()
Summary:
There are 2 issues in removeChild() implementation.

a) There is a chance that a node that we are removing will be mounting to a View, but the create view request has not be created  so there is no backing View for it yet. In that case, FlatNativeViewHierarchyManager.dropView() will throw an Exception failing to find the View. The fix is to only dropView() if one was (requested to be) created.
b) If the shadow node that we are removing doesn't mount to a View, but one or more of its children do, those Views will leak because noone is removing them. The fix is to recursively call removeChild() until we find a node that mounts to a View.

Reviewed By: ahmedre

Differential Revision: D2974938
2016-12-19 13:40:22 -08:00
Denis Koroskin 5305f3b68e Fix OnLayoutEvent dispatching wrong x/y coordinates
Summary:
OnLayoutEvent was incorrectly dispatching x/y coordinates relative to native View instead of relative to parent. This was causing issues in many cases, such as when Node was mounting to a View, in which case x/y would always be 0. This diff is fixing it.

This diff also caches last OnLayoutEvent to avoid dispatching the event on every layout even when layout didn't chance.

Reviewed By: ahmedre

Differential Revision: D2955087
2016-12-19 13:40:22 -08:00
Denis Koroskin c36d4593fd Revert \"[react-native][nodes] Split StateBuilder.ensureBackingViewIsCreated into 2 different methods\"
Summary: Reverts D2916697 that landed twice.

Differential Revision: D2974199
2016-12-19 13:40:22 -08:00
Ahmed El-Helw 827989f572 Fix TextInput text color not applying
Summary:
Setting the color for a TextInput with nodes was broken. The text color was
not being applied due to an optimization that prevented setting spans if
begin and end were the same (which is the case for an empty TextInput).
This patch depends on D2962643.

Differential Revision: D2962394
2016-12-19 13:40:22 -08:00
Ahmed El-Helw b4100ef246 Fix spannable flags in RCTVirtualText
Summary:
The spannable flags for RCTVirtualText were always being set to
INCLUSIVE_EXCLUSIVE - this is different than the behavior that is found in
ReactTextShadowNode, which sets EXCLUSIVE_INCLUSIVE as the default flag
unless the text is at the beginning.

This is needed to fix various problems with TextInput, including the handling
of empty spans (see also D2962394 which depends on this patch), and making the
behavior consistent when styled children of a TextInput are changed.

Differential Revision: D2962643
2016-12-19 13:40:22 -08:00
Ahmed El-Helw 055b31a165 Implement measure for nodes TextInput
Summary:
Implement measure for RCTTextInput. This is (almost) the same as
the one implemented for ReactTextInputShadowNode.

Differential Revision: D2964847
2016-12-19 13:40:22 -08:00
Denis Koroskin 64e050793e Split StateBuilder.ensureBackingViewIsCreated into 2 different methods
Summary:
There are 2 reasons why someone would call StateBuilder.ensureBackingViewIsCreated():
1) to make sure a View is created, because we are going to use it NOW
2) make sure react styles are applied to View, which doesn't really need the View to be created immediately

This diff is splitting the method into 2, without changing behavior. Difference between the methods' signatures is coming from the fact that 1) never takes styles and 2) possibly takes styles.

This is a pure refactoring diff and should have no change in functionality or behavior.

Reviewed By: ahmedre

Differential Revision: D2916697
2016-12-19 13:40:22 -08:00
Denis Koroskin ada5234b0e Lazily create/update views
Summary:
There is a rare case in React Native when a hierarchy is created and then *immediately* discarded all in one transaction. This is causing a View leak, because a View is created and then never attached to a hierarchy because it shadow node got detached. Since we never attached the View to the hierarchy, we cannot do a detach either and the View is kept in NativeViewHierarchyManager forever, causing a View leak. To fix the issue, don't create a backing View whenever a shadow node mounts to a View. Instead, put the create request into a queue and ONLY execute it if a shadow node is still attached to a hierarchy. This is fixing the View leak because will not create a View unless it's going to be attached somewhere.

The same logic applies to View style update (we don't want to update a View that is going to be detached from a hierarchy).

Reviewed By: ahmedre

Differential Revision: D2916826
2016-12-19 13:40:22 -08:00