mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Refactor softInputMode check in ReactRootView.java (#40970)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/40970 This cleans up https://github.com/facebook/react-native/commit/94972039571e1f3b387e0f63227a6ad13740eaf3 a bit, after I did some debugging and looking through Android source code. 1. `getRootView()` gives us constant-time access to root hierarchy, and we don't need to do instanceof check once per level. It also, at least in the sample activity I tried, gives us the Window's `LayoutParams`. 2. The root of the hierarchy is documented in code to do what we want. https://github.com/facebook/react-native/commit/94972039571e1f3b387e0f63227a6ad13740eaf3 3. Calling `getRootView().getLayoutParams()`, then casting to `WindowManager.LayoutParams`, seems to show up in a lot of other widgets (inc Unity, RoboElectric), as a solution to getting this information. https://github.com/search?q=getRootView%28%29.getLayoutParams%28%29&type=code This still feels like not a 100% documented contract, so I added an assertion so we can catch if the contract isn't valid somewhere now or in the future, instead of silently breaking keyboard events. Note that this code only runs on SDK 30+ (Android 11+). Changelog: [Internal] Reviewed By: javache Differential Revision: D50297761 fbshipit-source-id: f97fb6ea1bcdb1b8e8dfcdcc178625efc0bb6b4a
This commit is contained in:
committed by
Facebook GitHub Bot
parent
43c9d475aa
commit
bb9cc0ccec
+4
-21
@@ -854,20 +854,6 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
checkForDeviceDimensionsChanges();
|
||||
}
|
||||
|
||||
private @Nullable WindowManager.LayoutParams getWindowLayoutParams() {
|
||||
View view = ReactRootView.this;
|
||||
if (view.getLayoutParams() instanceof WindowManager.LayoutParams) {
|
||||
return (WindowManager.LayoutParams) view.getLayoutParams();
|
||||
}
|
||||
while (view.getParent() instanceof View) {
|
||||
view = (View) view.getParent();
|
||||
if (view.getLayoutParams() instanceof WindowManager.LayoutParams) {
|
||||
return (WindowManager.LayoutParams) view.getLayoutParams();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.R)
|
||||
private void checkForKeyboardEvents() {
|
||||
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
|
||||
@@ -885,13 +871,10 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
|
||||
int height = imeInsets.bottom - barInsets.bottom;
|
||||
|
||||
int softInputMode;
|
||||
WindowManager.LayoutParams windowLayoutParams = getWindowLayoutParams();
|
||||
if (windowLayoutParams != null) {
|
||||
softInputMode = windowLayoutParams.softInputMode;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
ViewGroup.LayoutParams rootLayoutParams = getRootView().getLayoutParams();
|
||||
Assertions.assertCondition(rootLayoutParams instanceof WindowManager.LayoutParams);
|
||||
|
||||
int softInputMode = ((WindowManager.LayoutParams) rootLayoutParams).softInputMode;
|
||||
int screenY =
|
||||
softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
|
||||
? mVisibleViewArea.bottom - height
|
||||
|
||||
Reference in New Issue
Block a user