Skip to content

Commit

Permalink
[NavigationRailView] Added attribute to control the application of st…
Browse files Browse the repository at this point in the history
…art window inset padding.

PiperOrigin-RevId: 516904093
  • Loading branch information
hunterstich authored and paulfthomas committed Mar 15, 2023
1 parent 4e907f2 commit 3f99392
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
Expand Up @@ -21,6 +21,7 @@
<public name="paddingBottomSystemWindowInsets" type="attr"/>
<public name="paddingLeftSystemWindowInsets" type="attr"/>
<public name="paddingRightSystemWindowInsets" type="attr"/>
<public name="paddingStartSystemWindowInsets" type="attr"/>
<public name="paddingTopSystemWindowInsets" type="attr"/>
<public name="marginLeftSystemWindowInsets" type="attr"/>
<public name="marginRightSystemWindowInsets" type="attr"/>
Expand Down
Expand Up @@ -58,6 +58,7 @@
<attr name="paddingBottomSystemWindowInsets" format="boolean"/>
<attr name="paddingLeftSystemWindowInsets" format="boolean"/>
<attr name="paddingRightSystemWindowInsets" format="boolean"/>
<attr name="paddingStartSystemWindowInsets" format="boolean"/>
<attr name="paddingTopSystemWindowInsets" format="boolean"/>
<attr name="marginLeftSystemWindowInsets" format="boolean"/>
<attr name="marginRightSystemWindowInsets" format="boolean"/>
Expand Down
Expand Up @@ -35,6 +35,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.internal.ThemeEnforcement;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class NavigationRailView extends NavigationBarView {
@Nullable private View headerView;
@Nullable private Boolean paddingTopSystemWindowInsets = null;
@Nullable private Boolean paddingBottomSystemWindowInsets = null;
@Nullable private Boolean paddingStartSystemWindowInsets = null;

public NavigationRailView(@NonNull Context context) {
this(context, null);
Expand Down Expand Up @@ -150,14 +152,18 @@ public NavigationRailView(

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

attributes.recycle();

Expand All @@ -176,18 +182,17 @@ 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
Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
if (shouldApplyWindowInsetPadding(paddingTopSystemWindowInsets)) {
initialPadding.top += insets.getInsets(WindowInsetsCompat.Type.systemBars()).top;
initialPadding.top += systemBarInsets.top;
}
if (shouldApplyWindowInsetPadding(paddingBottomSystemWindowInsets)) {
initialPadding.bottom +=
insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom;
initialPadding.bottom += systemBarInsets.bottom;
}
if (shouldApplyWindowInsetPadding(paddingStartSystemWindowInsets)) {
initialPadding.start +=
ViewUtils.isLayoutRtl(view) ? systemBarInsets.right : systemBarInsets.left;
}

boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
int systemWindowInsetRight = insets.getSystemWindowInsetRight();
initialPadding.start += isRtl ? systemWindowInsetRight : systemWindowInsetLeft;
initialPadding.applyToView(view);
return insets;
}
Expand All @@ -197,7 +202,7 @@ 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,
* <p>If the {@code paddingInsetFlag} is set, that value will take precedent. Otherwise,
* fitsSystemWindow will be used.
*/
private boolean shouldApplyWindowInsetPadding(Boolean paddingInsetFlag) {
Expand Down Expand Up @@ -311,9 +316,7 @@ public int getMenuGravity() {
return getNavigationRailMenuView().getMenuGravity();
}

/**
* Get the minimum height each item in the navigation rail's menu should be.
*/
/** Get the minimum height each item in the navigation rail's menu should be. */
public int getItemMinimumHeight() {
NavigationRailMenuView menuView = (NavigationRailMenuView) getMenuView();
return menuView.getItemMinimumHeight();
Expand All @@ -322,7 +325,7 @@ public int getItemMinimumHeight() {
/**
* Set the minimum height each item in the navigation rail's menu should use.
*
* If this is unset (-1), each item will be at least as tall as the navigation rail is wide.
* <p>If this is unset (-1), each item will be at least as tall as the navigation rail is wide.
*/
public void setItemMinimumHeight(@Px int minHeight) {
NavigationRailMenuView menuView = (NavigationRailMenuView) getMenuView();
Expand Down
Expand Up @@ -42,6 +42,9 @@
<!-- Whether the navigation rail should apply padding to have its menu
items above the bottom window insets. -->
<attr name="paddingBottomSystemWindowInsets"/>
<!-- Whether the navigation rail should apply any padding to its start to
account for any system window insets. -->
<attr name="paddingStartSystemWindowInsets"/>
</declare-styleable>

</resources>

0 comments on commit 3f99392

Please sign in to comment.