Commit 180abd10 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Commit Bot

Allow toolbars of different heights in the BottomSheet.

Bug: 933070
Change-Id: Ie1d0bd119a7a301b044ef5c28f63a9de9e77593e
Reviewed-on: https://chromium-review.googlesource.com/c/1475458
Commit-Queue: Jordan Demeulenaere <jdemeulenaere@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635712}
parent b2c588ab
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
<FrameLayout <FrameLayout
android:id="@+id/bottom_sheet_control_container" android:id="@+id/bottom_sheet_control_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content" >
android:minHeight="@dimen/bottom_sheet_peek_height" >
<ImageView <ImageView
android:id="@+id/bottom_sheet_toolbar_top_shadow" android:id="@+id/bottom_sheet_toolbar_top_shadow"
......
...@@ -141,12 +141,6 @@ public class BottomSheet ...@@ -141,12 +141,6 @@ public class BottomSheet
/** The desired height of a content that has just been shown or whose height was invalidated. */ /** The desired height of a content that has just been shown or whose height was invalidated. */
private static final float HEIGHT_UNSPECIFIED = -1.0f; private static final float HEIGHT_UNSPECIFIED = -1.0f;
/**
* Information about the different scroll states of the sheet. Order is important for these,
* they go from smallest to largest.
*/
private final float[] mStateRatios = new float[4];
/** The interpolator that the height animator uses. */ /** The interpolator that the height animator uses. */
private final Interpolator mInterpolator = new DecelerateInterpolator(1.0f); private final Interpolator mInterpolator = new DecelerateInterpolator(1.0f);
...@@ -180,9 +174,6 @@ public class BottomSheet ...@@ -180,9 +174,6 @@ public class BottomSheet
/** The animator set responsible for swapping the bottom sheet content. */ /** The animator set responsible for swapping the bottom sheet content. */
private AnimatorSet mContentSwapAnimatorSet; private AnimatorSet mContentSwapAnimatorSet;
/** The height of the toolbar. */
private float mToolbarHeight;
/** The width of the view that contains the bottom sheet. */ /** The width of the view that contains the bottom sheet. */
private float mContainerWidth; private float mContainerWidth;
...@@ -392,7 +383,7 @@ public class BottomSheet ...@@ -392,7 +383,7 @@ public class BottomSheet
} }
float startX = mVisibleViewportRect.left; float startX = mVisibleViewportRect.left;
float endX = mDefaultToolbarView.getWidth() + mVisibleViewportRect.left; float endX = getToolbarView().getWidth() + mVisibleViewportRect.left;
return currentEvent.getRawX() > startX && currentEvent.getRawX() < endX; return currentEvent.getRawX() > startX && currentEvent.getRawX() < endX;
} }
...@@ -528,8 +519,6 @@ public class BottomSheet ...@@ -528,8 +519,6 @@ public class BottomSheet
mToolbarHolder = mToolbarHolder =
(TouchRestrictingFrameLayout) findViewById(R.id.bottom_sheet_toolbar_container); (TouchRestrictingFrameLayout) findViewById(R.id.bottom_sheet_toolbar_container);
mDefaultToolbarView = mToolbarHolder.findViewById(R.id.bottom_sheet_toolbar); mDefaultToolbarView = mToolbarHolder.findViewById(R.id.bottom_sheet_toolbar);
mToolbarHeight =
activity.getResources().getDimensionPixelSize(R.dimen.bottom_sheet_peek_height);
mActivity = activity; mActivity = activity;
mActionBarDelegate = new ViewShiftingActionBarDelegate(mActivity, this); mActionBarDelegate = new ViewShiftingActionBarDelegate(mActivity, this);
...@@ -559,7 +548,9 @@ public class BottomSheet ...@@ -559,7 +548,9 @@ public class BottomSheet
mContainerHeight = bottom - top; mContainerHeight = bottom - top;
if (previousWidth != mContainerWidth || previousHeight != mContainerHeight) { if (previousWidth != mContainerWidth || previousHeight != mContainerHeight) {
updateSheetStateRatios(); if (mCurrentState == SheetState.HALF && shouldSkipHalfState()) {
setSheetState(SheetState.FULL, false);
}
invalidateContentDesiredHeight(); invalidateContentDesiredHeight();
} }
...@@ -794,9 +785,7 @@ public class BottomSheet ...@@ -794,9 +785,7 @@ public class BottomSheet
View newToolbar = content != null && content.getToolbarView() != null View newToolbar = content != null && content.getToolbarView() != null
? content.getToolbarView() ? content.getToolbarView()
: mDefaultToolbarView; : mDefaultToolbarView;
View oldToolbar = mSheetContent != null && mSheetContent.getToolbarView() != null View oldToolbar = getToolbarView();
? mSheetContent.getToolbarView()
: mDefaultToolbarView;
if (newToolbar != oldToolbar) { if (newToolbar != oldToolbar) {
// For the toolbar transition, make sure we don't detach the default toolbar view. // For the toolbar transition, make sure we don't detach the default toolbar view.
Animator transitionAnimator = getViewTransitionAnimator( Animator transitionAnimator = getViewTransitionAnimator(
...@@ -935,7 +924,7 @@ public class BottomSheet ...@@ -935,7 +924,7 @@ public class BottomSheet
if (detachOldView && oldView.getParent() != null) { if (detachOldView && oldView.getParent() != null) {
parent.removeView(oldView); parent.removeView(oldView);
} else { } else {
oldView.setVisibility(View.INVISIBLE); oldView.setVisibility(View.GONE);
} }
if (parent != newView.getParent()) parent.addView(newView); if (parent != newView.getParent()) parent.addView(newView);
} }
...@@ -994,28 +983,6 @@ public class BottomSheet ...@@ -994,28 +983,6 @@ public class BottomSheet
setContentDescription(null); setContentDescription(null);
} }
/**
* Updates the bottom sheet's state ratios and adjusts the sheet's state if necessary.
*/
private void updateSheetStateRatios() {
if (mContainerHeight <= 0) return;
// Though mStateRatios is a static constant, the peeking ratio is computed here because
// the correct toolbar height and container height are not know until those views are
// inflated. The other views are a specific DP distance from the top and bottom and are
// also updated.
mStateRatios[SheetState.HIDDEN] = 0;
mStateRatios[SheetState.PEEK] = (mToolbarHeight + mToolbarShadowHeight) / mContainerHeight;
mStateRatios[SheetState.HALF] = HALF_HEIGHT_RATIO;
// The max height ratio will be greater than 1 to account for the toolbar shadow.
mStateRatios[SheetState.FULL] =
(mContainerHeight + mToolbarShadowHeight) / mContainerHeight;
if (mCurrentState == SheetState.HALF && shouldSkipHalfState()) {
setSheetState(SheetState.FULL, false);
}
}
/** /**
* Cancels and nulls the height animation if it exists. * Cancels and nulls the height animation if it exists.
*/ */
...@@ -1151,14 +1118,31 @@ public class BottomSheet ...@@ -1151,14 +1118,31 @@ public class BottomSheet
*/ */
@VisibleForTesting @VisibleForTesting
float getHiddenRatio() { float getHiddenRatio() {
return mStateRatios[SheetState.HIDDEN]; return 0;
} }
/** /**
* @return The ratio of the height of the screen that the peeking state is. * @return The ratio of the height of the screen that the peeking state is.
*/ */
public float getPeekRatio() { public float getPeekRatio() {
return mStateRatios[SheetState.PEEK]; if (mContainerHeight <= 0) return 0;
View toolbarView = getToolbarView();
int toolbarHeight = toolbarView.getHeight();
if (toolbarHeight == 0) {
// If the toolbar is not laid out yet and has a fixed height layout parameter, we assume
// that the toolbar will have this height in the future.
ViewGroup.LayoutParams layoutParams = toolbarView.getLayoutParams();
if (layoutParams != null && layoutParams.height > 0) {
toolbarHeight = layoutParams.height;
}
}
return (toolbarHeight + mToolbarShadowHeight) / mContainerHeight;
}
private View getToolbarView() {
return mSheetContent != null && mSheetContent.getToolbarView() != null
? mSheetContent.getToolbarView()
: mDefaultToolbarView;
} }
/** /**
...@@ -1166,7 +1150,8 @@ public class BottomSheet ...@@ -1166,7 +1150,8 @@ public class BottomSheet
*/ */
@VisibleForTesting @VisibleForTesting
float getHalfRatio() { float getHalfRatio() {
return mStateRatios[SheetState.HALF]; if (mContainerHeight <= 0) return 0;
return HALF_HEIGHT_RATIO;
} }
/** /**
...@@ -1174,7 +1159,8 @@ public class BottomSheet ...@@ -1174,7 +1159,8 @@ public class BottomSheet
*/ */
@VisibleForTesting @VisibleForTesting
float getFullRatio() { float getFullRatio() {
return mStateRatios[SheetState.FULL]; if (mContainerHeight <= 0) return 0;
return (mContainerHeight + mToolbarShadowHeight) / mContainerHeight;
} }
/** /**
...@@ -1389,7 +1375,22 @@ public class BottomSheet ...@@ -1389,7 +1375,22 @@ public class BottomSheet
return mContentDesiredHeight; return mContentDesiredHeight;
} }
return mStateRatios[state] * mContainerHeight; return getRatioForState(state) * mContainerHeight;
}
private float getRatioForState(int state) {
switch (state) {
case SheetState.HIDDEN:
return getHiddenRatio();
case SheetState.PEEK:
return getPeekRatio();
case SheetState.HALF:
return getHalfRatio();
case SheetState.FULL:
return getFullRatio();
}
throw new IllegalArgumentException("Invalid state: " + state);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment