-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Fix Keyboard events and KeyboardAvoidingView on Android (edge-to-edge) #55855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zoontek
wants to merge
5
commits into
facebook:main
Choose a base branch
from
zoontek:fix-keyboard-avoiding-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+11
−73
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1779b1
Remove checkForKeyboardEventsLegacy
zoontek 0179410
Optimize imports
zoontek 8423a45
Fix keyboardDidHide event values
zoontek 59e783c
Set mKeyboardIsVisible = keyboardIsVisible first
zoontek 82c14b2
Fix render loop in edge-to-edge when using KeyboardAvoidingView with …
zoontek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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() { | ||
|
|
@@ -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 | ||
|
|
@@ -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(); | ||
|
|
@@ -997,62 +983,14 @@ private void checkForKeyboardEvents() { | |
| sendEvent( | ||
| "keyboardDidHide", | ||
| createKeyboardEventPayload( | ||
| PixelUtil.toDIPFromPixel(mVisibleViewArea.height()), | ||
| PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom + barInsets.bottom), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 0, | ||
| PixelUtil.toDIPFromPixel(mVisibleViewArea.width()), | ||
| 0)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void checkForKeyboardEventsLegacy() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In order to simplify the codebase here, |
||
| 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)) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
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
KeyboardAvoidingViewwithbehavior="height"