Skip to content

Commit

Permalink
[NavigationView] Disable drawing left and right insets if the nav bar…
Browse files Browse the repository at this point in the history
… is not edge to edge

resolves #3016

PiperOrigin-RevId: 484304490
  • Loading branch information
imhappi authored and hunterstich committed Oct 28, 2022
1 parent 5d261f5 commit cb384c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ScrimInsetsFrameLayout extends FrameLayout {
private Rect tempRect = new Rect();
private boolean drawTopInsetForeground = true;
private boolean drawBottomInsetForeground = true;
private boolean drawLeftInsetForeground = true;
private boolean drawRightInsetForeground = true;

public ScrimInsetsFrameLayout(@NonNull Context context) {
this(context, null);
Expand Down Expand Up @@ -104,6 +106,14 @@ public void setDrawBottomInsetForeground(boolean drawBottomInsetForeground) {
this.drawBottomInsetForeground = drawBottomInsetForeground;
}

public void setDrawLeftInsetForeground(boolean drawLeftInsetForeground) {
this.drawLeftInsetForeground = drawLeftInsetForeground;
}

public void setDrawRightInsetForeground(boolean drawRightInsetForeground) {
this.drawRightInsetForeground = drawRightInsetForeground;
}

@Override
public void draw(@NonNull Canvas canvas) {
super.draw(canvas);
Expand All @@ -129,14 +139,18 @@ public void draw(@NonNull Canvas canvas) {
}

// Left
tempRect.set(0, insets.top, insets.left, height - insets.bottom);
insetForeground.setBounds(tempRect);
insetForeground.draw(canvas);
if (drawLeftInsetForeground) {
tempRect.set(0, insets.top, insets.left, height - insets.bottom);
insetForeground.setBounds(tempRect);
insetForeground.draw(canvas);
}

// Right
tempRect.set(width - insets.right, insets.top, width, height - insets.bottom);
insetForeground.setBounds(tempRect);
insetForeground.draw(canvas);
if (drawRightInsetForeground) {
tempRect.set(width - insets.right, insets.top, width, height - insets.bottom);
insetForeground.setBounds(tempRect);
insetForeground.draw(canvas);
}

canvas.restoreToCount(sc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.appcompat.view.menu.MenuItemImpl;
import androidx.appcompat.widget.TintTypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
Expand Down Expand Up @@ -967,15 +968,30 @@ public void onGlobalLayout() {
presenter.setBehindStatusBar(isBehindStatusBar);
setDrawTopInsetForeground(isBehindStatusBar && isTopInsetScrimEnabled());

// The navigation view could be left aligned or just hidden out of view in a drawer
// layout when the global layout listener is called.
boolean isOnLeftSide = (tmpLocation[0] == 0) || (tmpLocation[0] + getWidth() == 0);
setDrawLeftInsetForeground(isOnLeftSide);

Activity activity = ContextUtils.getActivity(getContext());
if (activity != null && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);

boolean isBehindSystemNav =
activity.findViewById(android.R.id.content).getHeight() == getHeight();
displayMetrics.heightPixels - getHeight() == tmpLocation[1];
boolean hasNonZeroAlpha =
Color.alpha(activity.getWindow().getNavigationBarColor()) != 0;

setDrawBottomInsetForeground(
isBehindSystemNav && hasNonZeroAlpha && isBottomInsetScrimEnabled());

// The navigation view could be right aligned or just hidden out of view in a drawer
// layout when the global layout listener is called.
boolean isOnRightSide =
(displayMetrics.widthPixels == tmpLocation[0])
|| (displayMetrics.widthPixels - getWidth() == tmpLocation[0]);

setDrawRightInsetForeground(isOnRightSide);
}
}
};
Expand Down

0 comments on commit cb384c5

Please sign in to comment.