Skip to content

Commit

Permalink
Improve support for RTL layout
Browse files Browse the repository at this point in the history
Do not consume insets for footer frame.
  • Loading branch information
pranavpandey committed Jul 22, 2022
1 parent a795b2e commit fcb9805
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
getBottomSheetBehavior();

if (isApplyFooterInsets()) {
DynamicViewUtils.applyWindowInsetsBottom(mFrameFooter, true);
DynamicViewUtils.applyWindowInsetsBottom(mFrameFooter);
}

setFrameVisibility(mBottomSheet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1166,25 +1166,23 @@ public void setNavigationBarColor(@ColorInt int color) {
if (isApplyEdgeToEdgeInsets() && getEdgeToEdgeView() != null) {
ViewCompat.setOnApplyWindowInsetsListener(getEdgeToEdgeView(),
new OnApplyWindowInsetsListener() {
@Override
public @NonNull WindowInsetsCompat onApplyWindowInsets(
@NonNull View v, @NonNull WindowInsetsCompat insets) {
if (!(v.getLayoutParams()
instanceof ViewGroup.MarginLayoutParams)) {
return insets;
}

final ViewGroup.MarginLayoutParams lp =
(ViewGroup.MarginLayoutParams) v.getLayoutParams();
lp.topMargin = insets.getInsets(
WindowInsetsCompat.Type.systemBars()).top;
v.setLayoutParams(lp);
@Override
public @NonNull WindowInsetsCompat onApplyWindowInsets(
@NonNull View v, @NonNull WindowInsetsCompat insets) {
if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
final ViewGroup.MarginLayoutParams lp =
(ViewGroup.MarginLayoutParams) v.getLayoutParams();
lp.topMargin = insets.getInsets(
WindowInsetsCompat.Type.systemBars()).top;
v.setLayoutParams(lp);

DynamicViewUtils.applyWindowInsetsBottom(
getEdgeToEdgeViewBottom(), true);
}

DynamicViewUtils.applyWindowInsetsBottom(
getEdgeToEdgeViewBottom(), true);
return insets;
}
});
return insets;
}
});
}

if (DynamicWindowUtils.isGestureNavigation(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,16 @@ private static void initializeEdgeEffectFields(@Nullable Object clazz) {
}

/**
* Set the edge effect or glow color for the navigation view.
* Returns the navigation menu view associated with the supplied navigation view.
*
* @param view The navigation view to be used.
* @param color The color to be set.
*
* @return The navigation menu view associated with the supplied navigation view.
*/
public static void setEdgeEffectColor(@Nullable NavigationView view, @ColorInt int color) {
public static @Nullable NavigationMenuView getNavigationMenuView(
@Nullable NavigationView view) {
if (view == null) {
return;
return null;
}

initializeNavigationViewFields(view);
Expand All @@ -220,9 +222,26 @@ public static void setEdgeEffectColor(@Nullable NavigationView view, @ColorInt i
F_NAVIGATION_VIEW_PRESENTER.get(view);
initializeNavigationViewFields(presenter);

NavigationMenuView navigationMenuView = (NavigationMenuView)
F_NAVIGATION_VIEW_RECYCLER_VIEW.get(presenter);
setEdgeEffectColor(navigationMenuView, color, null);
return (NavigationMenuView) F_NAVIGATION_VIEW_RECYCLER_VIEW.get(presenter);
} catch (Exception ignored) {
}

return null;
}

/**
* Set the edge effect or glow color for the navigation view.
*
* @param view The navigation view to be used.
* @param color The color to be set.
*/
public static void setEdgeEffectColor(@Nullable NavigationView view, @ColorInt int color) {
if (view == null) {
return;
}

try {
setEdgeEffectColor(getNavigationMenuView(view), color, null);
} catch (Exception ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,9 @@ public void applyWindowInsets() {
@Override
public @NonNull WindowInsetsCompat onApplyWindowInsets(
@NonNull View v, @NonNull WindowInsetsCompat insets) {
final boolean isRtl = DynamicViewUtils.isLayoutRtl(v);
v.setPadding(isRtl ? right : left
+ insets.getInsets(WindowInsetsCompat.Type.systemBars()).left,
v.setPadding(left + insets.getInsets(WindowInsetsCompat.Type.systemBars()).left,
top - insets.getInsets(WindowInsetsCompat.Type.systemBars()).top,
isRtl ? left : right
+ insets.getInsets(WindowInsetsCompat.Type.systemBars()).right,
right + insets.getInsets(WindowInsetsCompat.Type.systemBars()).right,
bottom + insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom);
/*
* Fix extra peek height when using top inset for coordinator layout and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ public void applyWindowInsets() {
insets.getInsets(WindowInsetsCompat.Type.systemBars()).right,
insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom);

final boolean isRtl = DynamicViewUtils.isLayoutRtl(v);
if (DynamicViewUtils.isRootLayout(v) || ViewCompat.getFitsSystemWindows(v)) {
v.setPadding(isRtl ? right : left + mInsets.left, top + mInsets.top,
isRtl ? left : right + mInsets.right, bottom);
v.setPadding(left + mInsets.left, top + mInsets.top,
right + mInsets.right, bottom);
} else {
v.setPadding(isRtl ? right : left + mInsets.left, top + mInsets.top,
isRtl ? left : right + mInsets.right, bottom + mInsets.bottom);
v.setPadding(left + mInsets.left, top + mInsets.top,
right + mInsets.right, bottom + mInsets.bottom);
}

setWillNotDraw(insets.getInsets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.internal.NavigationMenuView;
import com.google.android.material.internal.ScrimInsetsFrameLayout;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.shape.MaterialShapeDrawable;
Expand Down Expand Up @@ -325,16 +326,33 @@ public void initialize() {

@Override
public void applyWindowInsets() {
final NavigationMenuView menuView = DynamicScrollUtils.getNavigationMenuView(this);
final View header;
final int left = getPaddingLeft();
final int top = getPaddingTop();
final int right = getPaddingRight();
final int bottom = getPaddingBottom();
final int menuLeft;
final int menuTop;
final int menuRight;
final int menuBottom;
final int headerLeft;
final int headerTop;
final int headerRight;
final int headerBottom;

if (menuView != null) {
menuLeft = menuView.getPaddingLeft();
menuTop = menuView.getPaddingTop();
menuRight = menuView.getPaddingRight();
menuBottom = menuView.getPaddingBottom();
} else {
menuLeft = 0;
menuTop = 0;
menuRight = 0;
menuBottom = 0;
}

if (getHeaderCount() != 0) {
header = getHeaderView(0);
headerLeft = header.getPaddingLeft();
Expand All @@ -356,26 +374,31 @@ public void applyWindowInsets() {
@NonNull View v, @NonNull WindowInsetsCompat insets) {
final Rect rect = new Rect();
rect.set(insets.getInsets(WindowInsetsCompat.Type.systemBars()).left,
insets.getInsets(WindowInsetsCompat.Type.systemBars()).top, 0,
insets.getInsets(WindowInsetsCompat.Type.systemBars()).top,
insets.getInsets(WindowInsetsCompat.Type.systemBars()).right,
insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom);

final boolean isRtl = DynamicViewUtils.isLayoutRtl(v);
v.setPadding(isRtl ? right : left + rect.left, top,
isRtl ? left : right, bottom + rect.bottom);

try {
final Field fieldInsets =
ScrimInsetsFrameLayout.class.getDeclaredField("insets");
fieldInsets.setAccessible(true);
fieldInsets.set(DynamicNavigationView.this, rect);

if (header != null) {
header.setPadding(headerLeft, headerTop + rect.top,
headerRight, headerBottom);
}
} catch (Exception ignored) {
}

v.setPadding(left + rect.left, top,
right + rect.right, bottom + rect.bottom);

if (menuView != null) {
menuView.setPadding(menuLeft, header != null ? menuTop
: menuTop + rect.top, menuRight, menuBottom);
}

if (header != null) {
header.setPadding(headerLeft, headerTop + rect.top,
headerRight, headerBottom);
}

return insets;
}
});
Expand Down

0 comments on commit fcb9805

Please sign in to comment.