Skip to content

Commit

Permalink
[BottomSheetBehavior] Add margin flags to BottomSheetBehavior for sys…
Browse files Browse the repository at this point in the history
…tem inset bars.

Updates bottom sheet styles to default to left and right margins, top and bottom padding.

Resolves: #2221
PiperOrigin-RevId: 409212345
  • Loading branch information
josefigueroa168 authored and drchen committed Nov 16, 2021
1 parent b878fc3 commit 022a05f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
Expand Up @@ -32,6 +32,7 @@
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
Expand Down Expand Up @@ -231,6 +232,9 @@ public abstract static class BottomSheetCallback {
private boolean paddingLeftSystemWindowInsets;
private boolean paddingRightSystemWindowInsets;
private boolean paddingTopSystemWindowInsets;
private boolean marginLeftSystemWindowInsets;
private boolean marginRightSystemWindowInsets;
private boolean marginTopSystemWindowInsets;

private int insetBottom;
private int insetTop;
Expand Down Expand Up @@ -375,6 +379,12 @@ public BottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attr
// this is a breaking change from the old behavior the default is true.
paddingTopSystemWindowInsets =
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_paddingTopSystemWindowInsets, true);
marginLeftSystemWindowInsets =
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_marginLeftSystemWindowInsets, false);
marginRightSystemWindowInsets =
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_marginRightSystemWindowInsets, false);
marginTopSystemWindowInsets =
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_marginTopSystemWindowInsets, false);

a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
Expand Down Expand Up @@ -1462,6 +1472,10 @@ private void setWindowInsetsListener(@NonNull View child) {
if (!paddingBottomSystemWindowInsets
&& !paddingLeftSystemWindowInsets
&& !paddingRightSystemWindowInsets
&& !paddingTopSystemWindowInsets
&& !marginLeftSystemWindowInsets
&& !marginRightSystemWindowInsets
&& !marginTopSystemWindowInsets
&& !shouldHandleGestureInsets) {
return;
}
Expand All @@ -1471,7 +1485,11 @@ private void setWindowInsetsListener(@NonNull View child) {
@Override
public WindowInsetsCompat onApplyWindowInsets(
View view, WindowInsetsCompat insets, RelativePadding initialPadding) {
insetTop = insets.getSystemWindowInsetTop();
Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Insets mandatoryGestureInsets =
insets.getInsets(WindowInsetsCompat.Type.mandatorySystemGestures());

insetTop = systemBarInsets.top;

boolean isRtl = ViewUtils.isLayoutRtl(view);

Expand All @@ -1480,24 +1498,38 @@ public WindowInsetsCompat onApplyWindowInsets(
int rightPadding = view.getPaddingRight();

if (paddingBottomSystemWindowInsets) {
insetBottom = insets.getSystemWindowInsetBottom();
insetBottom = systemBarInsets.bottom;
bottomPadding = initialPadding.bottom + insetBottom;
}

if (paddingLeftSystemWindowInsets) {
leftPadding = isRtl ? initialPadding.end : initialPadding.start;
leftPadding += insets.getSystemWindowInsetLeft();
leftPadding += systemBarInsets.left;
}

if (paddingRightSystemWindowInsets) {
rightPadding = isRtl ? initialPadding.start : initialPadding.end;
rightPadding += insets.getSystemWindowInsetRight();
}

MarginLayoutParams mlp = (MarginLayoutParams) view.getLayoutParams();
if (marginLeftSystemWindowInsets) {
mlp.leftMargin = systemBarInsets.left;
}

if (marginRightSystemWindowInsets) {
mlp.rightMargin = systemBarInsets.right;
}

if (marginTopSystemWindowInsets) {
mlp.topMargin = systemBarInsets.top;
}

view.setLayoutParams(mlp);
view.setPadding(leftPadding, view.getPaddingTop(), rightPadding, bottomPadding);

if (shouldHandleGestureInsets) {
gestureInsetBottom = insets.getMandatorySystemGestureInsets().bottom;
gestureInsetBottom = mandatoryGestureInsets.bottom;
}

// Don't update the peek height to be above the navigation bar or gestures if these
Expand Down
Expand Up @@ -81,6 +81,9 @@
<attr name="paddingLeftSystemWindowInsets" />
<attr name="paddingRightSystemWindowInsets" />
<attr name="paddingTopSystemWindowInsets" />
<attr name="marginLeftSystemWindowInsets" />
<attr name="marginRightSystemWindowInsets" />
<attr name="marginTopSystemWindowInsets" />
<attr name="android:elevation"/>
<attr name="android:maxWidth"/>
<attr name="android:maxHeight"/>
Expand Down
Expand Up @@ -67,6 +67,12 @@
<!-- Style for the M3 Bottom Sheet. -->
<style name="Widget.Material3.BottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="android:elevation" tools:ignore="NewApi">@dimen/m3_bottom_sheet_elevation</item>
<item name="marginLeftSystemWindowInsets">true</item>
<item name="marginRightSystemWindowInsets">true</item>
<item name="paddingBottomSystemWindowInsets">true</item>
<item name="paddingTopSystemWindowInsets">true</item>
<item name="paddingLeftSystemWindowInsets">false</item>
<item name="paddingRightSystemWindowInsets">false</item>
</style>

<style name="Widget.Material3.BottomSheet.Modal">
Expand Down
Expand Up @@ -35,10 +35,6 @@
<item name="android:windowAnimationStyle">@style/Animation.MaterialComponents.BottomSheetDialog</item>
<item name="bottomSheetStyle">@style/Widget.Material3.BottomSheet.Modal</item>
<item name="enableEdgeToEdge">true</item>
<item name="paddingBottomSystemWindowInsets">true</item>
<item name="paddingLeftSystemWindowInsets">true</item>
<item name="paddingRightSystemWindowInsets">true</item>
<item name="paddingTopSystemWindowInsets">true</item>
</style>

<style name="Base.Theme.Material3.Light.BottomSheetDialog" parent="Base.V14.Theme.Material3.Light.BottomSheetDialog"/>
Expand Down
Expand Up @@ -22,5 +22,8 @@
<public name="paddingLeftSystemWindowInsets" type="attr"/>
<public name="paddingRightSystemWindowInsets" type="attr"/>
<public name="paddingTopSystemWindowInsets" type="attr"/>
<public name="marginLeftSystemWindowInsets" type="attr"/>
<public name="marginRightSystemWindowInsets" type="attr"/>
<public name="marginTopSystemWindowInsets" type="attr"/>
<public name="Widget.Design.ScrimInsetsFrameLayout" type="style"/>
</resources>
Expand Up @@ -59,6 +59,9 @@
<attr name="paddingLeftSystemWindowInsets" format="boolean"/>
<attr name="paddingRightSystemWindowInsets" format="boolean"/>
<attr name="paddingTopSystemWindowInsets" format="boolean"/>
<attr name="marginLeftSystemWindowInsets" format="boolean"/>
<attr name="marginRightSystemWindowInsets" format="boolean"/>
<attr name="marginTopSystemWindowInsets" format="boolean"/>
</declare-styleable>

</resources>

0 comments on commit 022a05f

Please sign in to comment.