Skip to content
Open
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 @@ -7,6 +7,7 @@
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -195,23 +196,28 @@ private void initWindowInsetsListener() {

Insets systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
Insets imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime());
boolean keyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime());
// With `adjustNothing` the keyboard overlays the WebView, so it is handled as if it is not there
boolean keyboardOverlaysWebView =
(getActivity().getWindow().getAttributes().softInputMode & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) ==
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
boolean keyboardVisible = !keyboardOverlaysWebView && insets.isVisible(WindowInsetsCompat.Type.ime());

if (shouldPassthroughInsets) {
// We need to correct for a possible shown IME
v.setPadding(0, 0, 0, keyboardVisible ? imeInsets.bottom : 0);

WindowInsetsCompat newInsets = new WindowInsetsCompat.Builder(insets)
.setInsets(
WindowInsetsCompat newInsets = withoutOverlayingKeyboard(
new WindowInsetsCompat.Builder(insets).setInsets(
WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout(),
Insets.of(
systemBarsInsets.left,
systemBarsInsets.top,
systemBarsInsets.right,
getBottomInset(systemBarsInsets, keyboardVisible)
)
)
.build();
),
keyboardOverlaysWebView
).build();

injectSafeAreaCSS(newInsets);

Expand All @@ -229,16 +235,33 @@ private void initWindowInsetsListener() {
// Returning `WindowInsetsCompat.CONSUMED` breaks recalculation of safe area insets
// So we have to explicitly set insets to `0`
// See: https://issues.chromium.org/issues/461332423
WindowInsetsCompat newInsets = new WindowInsetsCompat.Builder(insets)
.setInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout(), Insets.of(0, 0, 0, 0))
.build();
WindowInsetsCompat newInsets = withoutOverlayingKeyboard(
new WindowInsetsCompat.Builder(insets).setInsets(
WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout(),
Insets.of(0, 0, 0, 0)
),
keyboardOverlaysWebView
).build();

injectSafeAreaCSS(newInsets);

return newInsets;
});
}

private WindowInsetsCompat.Builder withoutOverlayingKeyboard(WindowInsetsCompat.Builder builder, boolean keyboardOverlaysWebView) {
if (!keyboardOverlaysWebView) {
return builder;
}

// WebView >= 139 shrinks its visual viewport for the IME insets it receives, which makes the page pannable above the keyboard.
// Unlike Chrome it ignores `VirtualKeyboard.overlaysContent` and `interactive-widget=overlays-content`,
// so not handing it the IME insets is the only way to opt out.
// Setting them to `0` rather than consuming them keeps the WebView's inset state up to date.
// See: https://developer.android.com/develop/ui/views/layout/webapps/understand-window-insets
return builder.setInsets(WindowInsetsCompat.Type.ime(), Insets.NONE).setVisible(WindowInsetsCompat.Type.ime(), false);
}

private void injectSafeAreaCSS(WindowInsetsCompat insets) {
if (!INSETS_HANDLING_CSS.equals(insetsHandling)) {
return;
Expand Down