Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class KeyboardAvoidingView extends React.Component<
];
} else {
this._subscriptions = [
Keyboard.addListener('keyboardDidHide', this._onKeyboardChange),
Keyboard.addListener('keyboardDidHide', this._onKeyboardHide),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solves a crazy render loop (with the UI jumping back and forth) when using KeyboardAvoidingView with behavior="height"

Keyboard.addListener('keyboardDidShow', this._onKeyboardChange),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,25 @@
import android.content.Context;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Insets;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.DisplayCutout;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.ThreadConfined;
Expand Down Expand Up @@ -801,11 +800,7 @@ public void runApplication() {

@VisibleForTesting
/* package */ void simulateCheckForKeyboardForTesting() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getCustomGlobalLayoutListener().checkForKeyboardEvents();
} else {
getCustomGlobalLayoutListener().checkForKeyboardEventsLegacy();
}
getCustomGlobalLayoutListener().checkForKeyboardEvents();
}

private CustomGlobalLayoutListener getCustomGlobalLayoutListener() {
Expand Down Expand Up @@ -932,16 +927,13 @@ public boolean isViewAttachedToReactInstance() {

private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final Rect mVisibleViewArea;
private final int mMinKeyboardHeightDetected;

private boolean mKeyboardIsVisible = false;
private int mKeyboardHeight = 0; // Only used in checkForKeyboardEventsLegacy path
private int mDeviceRotation = 0;

/* package */ CustomGlobalLayoutListener() {
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
mVisibleViewArea = new Rect();
mMinKeyboardHeightDetected = (int) PixelUtil.toPixelFromDIP(60);
}

@Override
Expand All @@ -950,31 +942,25 @@ public void onGlobalLayout() {
return;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
checkForKeyboardEvents();
} else {
checkForKeyboardEventsLegacy();
}

checkForKeyboardEvents();
checkForDeviceOrientationChanges();
checkForDeviceDimensionsChanges();
}

@RequiresApi(api = Build.VERSION_CODES.R)
private void checkForKeyboardEvents() {
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
WindowInsets rootInsets = getRootView().getRootWindowInsets();
WindowInsetsCompat rootInsets = ViewCompat.getRootWindowInsets(getRootView());
if (rootInsets == null) {
return;
}

boolean keyboardIsVisible = rootInsets.isVisible(WindowInsets.Type.ime());
boolean keyboardIsVisible = rootInsets.isVisible(WindowInsetsCompat.Type.ime());
if (keyboardIsVisible != mKeyboardIsVisible) {
mKeyboardIsVisible = keyboardIsVisible;
Insets barInsets = rootInsets.getInsets(WindowInsetsCompat.Type.systemBars());

if (keyboardIsVisible) {
Insets imeInsets = rootInsets.getInsets(WindowInsets.Type.ime());
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
Insets imeInsets = rootInsets.getInsets(WindowInsetsCompat.Type.ime());
int height = imeInsets.bottom - barInsets.bottom;

ViewGroup.LayoutParams rootLayoutParams = getRootView().getLayoutParams();
Expand All @@ -997,62 +983,14 @@ private void checkForKeyboardEvents() {
sendEvent(
"keyboardDidHide",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.height()),
PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom + barInsets.bottom),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mVisibleViewArea height doesn't include system UI height, we cannot rely on it when the app is edge-to-edge:

Image

0,
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
0));
}
}
}

private void checkForKeyboardEventsLegacy() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to simplify the codebase here, checkForKeyboardEventsLegacy is removed and checkForKeyboardEvents now uses AndroidX.

getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);

int notchHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowInsets insets = getRootView().getRootWindowInsets();
if (insets != null) {
DisplayCutout displayCutout = insets.getDisplayCutout();
if (displayCutout != null) {
notchHeight = displayCutout.getSafeInsetTop();
}
}
}
final int heightDiff =
DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels
- mVisibleViewArea.bottom
+ notchHeight;

boolean isKeyboardShowingOrKeyboardHeightChanged =
mKeyboardHeight != heightDiff && heightDiff > mMinKeyboardHeightDetected;

if (isKeyboardShowingOrKeyboardHeightChanged) {
mKeyboardHeight = heightDiff;
mKeyboardIsVisible = true;
sendEvent(
"keyboardDidShow",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom),
PixelUtil.toDIPFromPixel(mVisibleViewArea.left),
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
PixelUtil.toDIPFromPixel(mKeyboardHeight)));
return;
}

boolean isKeyboardHidden = mKeyboardHeight != 0 && heightDiff <= mMinKeyboardHeightDetected;
if (isKeyboardHidden) {
mKeyboardHeight = 0;
mKeyboardIsVisible = false;
sendEvent(
"keyboardDidHide",
createKeyboardEventPayload(
PixelUtil.toDIPFromPixel(mVisibleViewArea.height()),
0,
PixelUtil.toDIPFromPixel(mVisibleViewArea.width()),
0));
}
}

private void checkForDeviceOrientationChanges() {
final int rotation =
((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
Expand Down
Loading