Skip to content

Commit

Permalink
[NavigationRail] Added support for opting in/out of the NavigationRai…
Browse files Browse the repository at this point in the history
…l automatically adding system top and bottom window insets.

PiperOrigin-RevId: 424424554
  • Loading branch information
hunterstich authored and pekingme committed Jan 27, 2022
1 parent 0c8fc41 commit c66633b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
Expand Up @@ -20,5 +20,6 @@
android:id="@+id/cat_navigation_rail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_rail_animated_menu"/>
app:menu="@menu/navigation_rail_animated_menu"
android:fitsSystemWindows="false"/>

Expand Up @@ -16,8 +16,8 @@
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -26,7 +26,8 @@
android:id="@+id/cat_navigation_rail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_rail_menu"/>
app:menu="@menu/navigation_rail_menu"
android:fitsSystemWindows="false"/>

<ScrollView
android:layout_width="0dp"
Expand Down
Expand Up @@ -103,6 +103,8 @@ public class NavigationRailView extends NavigationBarView {

private final int topMargin;
@Nullable private View headerView;
@Nullable private Boolean paddingTopSystemWindowInsets = null;
@Nullable private Boolean paddingBottomSystemWindowInsets = null;

public NavigationRailView(@NonNull Context context) {
this(context, null);
Expand Down Expand Up @@ -146,6 +148,17 @@ public NavigationRailView(
R.styleable.NavigationRailView_itemMinHeight, NO_ITEM_MINIMUM_HEIGHT));
}

if (attributes.hasValue(R.styleable.NavigationRailView_paddingTopSystemWindowInsets)) {
paddingTopSystemWindowInsets =
attributes.getBoolean(
R.styleable.NavigationRailView_paddingTopSystemWindowInsets, false);
}
if (attributes.hasValue(R.styleable.NavigationRailView_paddingBottomSystemWindowInsets)) {
paddingBottomSystemWindowInsets =
attributes.getBoolean(
R.styleable.NavigationRailView_paddingBottomSystemWindowInsets, false);
}

attributes.recycle();

applyWindowInsets();
Expand All @@ -163,8 +176,12 @@ public WindowInsetsCompat onApplyWindowInsets(
@NonNull RelativePadding initialPadding) {
// Apply the top, bottom, and start padding for a start edge aligned
// NavigationRailView to dodge the system status and navigation bars
initialPadding.top += insets.getSystemWindowInsetTop();
initialPadding.bottom += insets.getSystemWindowInsetBottom();
if (shouldApplyWindowInsetPadding(paddingTopSystemWindowInsets)) {
initialPadding.top += insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
}
if (shouldApplyWindowInsetPadding(paddingBottomSystemWindowInsets)) {
initialPadding.top += insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
}

boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
Expand All @@ -176,6 +193,16 @@ public WindowInsetsCompat onApplyWindowInsets(
});
}

/**
* Whether the top or bottom of this view should be padded in to avoid the system window insets.
*
* If the {@code paddingInsetFlag} is set, that value will take precedent. Otherwise,
* fitsSystemWindow will be used.
*/
private boolean shouldApplyWindowInsetPadding(Boolean paddingInsetFlag) {
return paddingInsetFlag != null ? paddingInsetFlag : ViewCompat.getFitsSystemWindows(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minWidthSpec = makeMinWidthSpec(widthMeasureSpec);
Expand Down
Expand Up @@ -36,6 +36,12 @@
<!-- Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL -->
<enum name="bottom" value="81"/>
</attr>
<!-- Whether the navigation rail should apply padding to have its menu
items and optional header below the top window insets. -->
<attr name="paddingTopSystemWindowInsets"/>
<!-- Whether the navigation rail should apply padding to have its menu
items above the bottom window insets. -->
<attr name="paddingBottomSystemWindowInsets"/>
</declare-styleable>

</resources>
Expand Up @@ -38,6 +38,7 @@
<item name="itemTextColor">@color/mtrl_navigation_bar_item_tint</item>
<item name="labelVisibilityMode">auto</item>
<item name="menuGravity">top</item>
<item name="android:fitsSystemWindows">true</item>
</style>

<style name="Widget.MaterialComponents.NavigationRailView.Compact">
Expand Down

0 comments on commit c66633b

Please sign in to comment.