Skip to content

Commit 2664ee9

Browse files
dsn5fthunterstich
authored andcommitted
[Bottom Sheet] Updated peek height gesture inset behavior to only add extra inset if necessary
Previously on Q the peek height would have the bottom gesture inset size added to it regardless of whether edge to edge mode or gesture navigation were enabled. Now the extra inset will only be added if it is needed due to a system gesture conflict, in order to give the user some space to drag the sheet. Resolves #1472 PiperOrigin-RevId: 320606747 (cherry picked from commit 7b62003)
1 parent d8a8569 commit 2664ee9

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.google.android.material.bottomsheet;
1818

19-
import com.google.android.material.R;
20-
2119
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2220

2321
import android.animation.ValueAnimator;
@@ -30,31 +28,34 @@
3028
import android.os.Build.VERSION_CODES;
3129
import android.os.Parcel;
3230
import android.os.Parcelable;
31+
import android.util.AttributeSet;
32+
import android.util.Log;
33+
import android.util.TypedValue;
34+
import android.view.MotionEvent;
35+
import android.view.VelocityTracker;
36+
import android.view.View;
37+
import android.view.ViewConfiguration;
38+
import android.view.ViewGroup;
39+
import android.view.ViewParent;
3340
import androidx.annotation.FloatRange;
3441
import androidx.annotation.IntDef;
3542
import androidx.annotation.NonNull;
3643
import androidx.annotation.Nullable;
3744
import androidx.annotation.RestrictTo;
3845
import androidx.annotation.VisibleForTesting;
46+
import androidx.coordinatorlayout.widget.CoordinatorLayout;
47+
import androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams;
3948
import androidx.core.math.MathUtils;
40-
import androidx.customview.view.AbsSavedState;
4149
import androidx.core.view.ViewCompat;
50+
import androidx.core.view.WindowInsetsCompat;
4251
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
4352
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
4453
import androidx.core.view.accessibility.AccessibilityViewCommand;
54+
import androidx.customview.view.AbsSavedState;
4555
import androidx.customview.widget.ViewDragHelper;
46-
import android.util.AttributeSet;
47-
import android.util.Log;
48-
import android.util.TypedValue;
49-
import android.view.MotionEvent;
50-
import android.view.VelocityTracker;
51-
import android.view.View;
52-
import android.view.ViewConfiguration;
53-
import android.view.ViewGroup;
54-
import android.view.ViewParent;
55-
import android.view.WindowInsets;
56-
import androidx.coordinatorlayout.widget.CoordinatorLayout;
57-
import androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams;
56+
import com.google.android.material.R;
57+
import com.google.android.material.internal.ViewUtils;
58+
import com.google.android.material.internal.ViewUtils.RelativePadding;
5859
import com.google.android.material.resources.MaterialResources;
5960
import com.google.android.material.shape.MaterialShapeDrawable;
6061
import com.google.android.material.shape.ShapeAppearanceModel;
@@ -370,7 +371,7 @@ public boolean onLayoutChild(
370371
// First layout with this behavior.
371372
peekHeightMin =
372373
parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
373-
setSystemGestureInsets(parent);
374+
setSystemGestureInsets(child);
374375
viewRef = new WeakReference<>(child);
375376
// Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
376377
// default to android:background declared in styles or layout.
@@ -1237,13 +1238,22 @@ public void onAnimationUpdate(@NonNull ValueAnimator animation) {
12371238
});
12381239
}
12391240

1240-
private void setSystemGestureInsets(@NonNull CoordinatorLayout parent) {
1241-
if (VERSION.SDK_INT >= VERSION_CODES.Q && !isGestureInsetBottomIgnored()) {
1242-
WindowInsets windowInsets = parent.getRootWindowInsets();
1243-
if (windowInsets != null) {
1244-
int systemMandatoryInsetsBottom = windowInsets.getSystemGestureInsets().bottom;
1245-
peekHeight += systemMandatoryInsetsBottom;
1246-
}
1241+
/**
1242+
* Ensure the peek height is at least as large as the bottom gesture inset size so that the sheet
1243+
* can always be dragged, but only when the inset is required by the system.
1244+
*/
1245+
private void setSystemGestureInsets(@NonNull View child) {
1246+
if (VERSION.SDK_INT >= VERSION_CODES.Q && !isGestureInsetBottomIgnored() && !peekHeightAuto) {
1247+
ViewUtils.doOnApplyWindowInsets(
1248+
child,
1249+
new ViewUtils.OnApplyWindowInsetsListener() {
1250+
@Override
1251+
public WindowInsetsCompat onApplyWindowInsets(
1252+
View view, WindowInsetsCompat insets, RelativePadding initialPadding) {
1253+
setPeekHeight(peekHeight + insets.getMandatorySystemGestureInsets().bottom);
1254+
return insets;
1255+
}
1256+
});
12471257
}
12481258
}
12491259

0 commit comments

Comments
 (0)