Skip to content

Commit 82c8c97

Browse files
andreicoman11Facebook Github Bot 4
authored andcommitted
Support orientation change on modals
Summary: This automatically changes the size of the modal by listening to dialog size changes and propagating those changes through UIManager. In detail: I've looked into three ways of doing this: 1. Send `onSizeChanged` events/info from the View to the CSSNode directly. This is kinda hacky because you would need to hold a reference to the CSSNode somewhere, either in the View or in the ViewManager. But then you'll have to take care of the lifecycle of the CSSNode, so that you don't update it after it has been dismissed. Not great. 2. The version we went for, is to just update the size of the corresponding CSSNode in the same way we do it for root nodes: we inform the UIManager that the size of the root node has changed, and it will propagate that change, triggering a `dispatchViewUpdates` if none is underway, so that the layout is updated. 3. The other solution we thought of is to treat the Modal as a root view. This would mean rendering an application with the tag of the Modal as the root of the application. That tag would be received by calling some method into UIManager and ReactModalHostManager to create a new RootView, create a Dialog and plop the root view in it. The idea was to maintain the JS API that we now have, but make the implementation more correct (ie. since both RootView and the Modal must deal with touch handling), and could have other benefits (ie. no hacks necessary for making the inspector work on top of modals). However, the change is not trivial and I don't know just how much code would have to be changed to make this work correctly. We might revisit this at a later stage, after we've done more work on having several root views at the same time in the app. Reviewed By: foghina Differential Revision: D3841379 fbshipit-source-id: f5e363e27041b785cf44eb59da04bc789306ddb9
1 parent 178407d commit 82c8c97

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,17 @@ public void removeRootView(int rootViewTag) {
130130
}
131131

132132
/**
133-
* Invoked when native view that corresponds to a root node has its size changed.
133+
* Invoked when native view that corresponds to a root node, or acts as a root view (ie. Modals)
134+
* has its size changed.
134135
*/
135-
public void updateRootNodeSize(
136-
int rootViewTag,
136+
public void updateNodeSize(
137+
int nodeViewTag,
137138
int newWidth,
138139
int newHeight,
139140
EventDispatcher eventDispatcher) {
140-
ReactShadowNode rootCSSNode = mShadowNodeRegistry.getNode(rootViewTag);
141-
rootCSSNode.setStyleWidth(newWidth);
142-
rootCSSNode.setStyleHeight(newHeight);
141+
ReactShadowNode cssNode = mShadowNodeRegistry.getNode(nodeViewTag);
142+
cssNode.setStyleWidth(newWidth);
143+
cssNode.setStyleHeight(newHeight);
143144

144145
// If we're in the middle of a batch, the change will automatically be dispatched at the end of
145146
// the batch. As all batches are executed as a single runnable on the event queue this should

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void onSizeChanged(final int width, final int height, int oldW, int oldH)
192192
new Runnable() {
193193
@Override
194194
public void run() {
195-
updateRootNodeSize(tag, width, height);
195+
updateNodeSize(tag, width, height);
196196
}
197197
});
198198
}
@@ -206,10 +206,10 @@ public void removeRootView(int rootViewTag) {
206206
mUIImplementation.removeRootView(rootViewTag);
207207
}
208208

209-
private void updateRootNodeSize(int rootViewTag, int newWidth, int newHeight) {
209+
public void updateNodeSize(int nodeViewTag, int newWidth, int newHeight) {
210210
getReactApplicationContext().assertOnNativeModulesQueueThread();
211211

212-
mUIImplementation.updateRootNodeSize(rootViewTag, newWidth, newHeight, mEventDispatcher);
212+
mUIImplementation.updateNodeSize(nodeViewTag, newWidth, newHeight, mEventDispatcher);
213213
}
214214

215215
@ReactMethod

ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ public DialogRootViewGroup(Context context) {
264264
super(context);
265265
}
266266

267+
@Override
268+
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
269+
super.onSizeChanged(w, h, oldw, oldh);
270+
if (getChildCount() > 0) {
271+
((ReactContext) getContext()).runOnNativeModulesQueueThread(
272+
new Runnable() {
273+
@Override
274+
public void run() {
275+
((ReactContext) getContext()).getNativeModule(UIManagerModule.class)
276+
.updateNodeSize(getChildAt(0).getId(), w, h);
277+
}
278+
});
279+
}
280+
}
281+
267282
@Override
268283
public boolean onInterceptTouchEvent(MotionEvent event) {
269284
mJSTouchDispatcher.handleTouchEvent(event, getEventDispatcher());

0 commit comments

Comments
 (0)