Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Added option to force always applying syste…
Browse files Browse the repository at this point in the history
…m window inset top regardless of layout_height

PiperOrigin-RevId: 382286923
  • Loading branch information
dsn5ft authored and hunterstich committed Jul 1, 2021
1 parent 86f8c51 commit 9ebf1a1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Expand Up @@ -27,14 +27,14 @@
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/cat_toc_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/cat_toc_tall_toolbar_height"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.Catalog.AppBarLayout"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/cat_toc_collapsingtoolbarlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="@dimen/cat_toc_tall_toolbar_height"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">

Expand Down
Expand Up @@ -16,8 +16,7 @@
-->

<resources>
<dimen name="cat_toc_tall_toolbar_height">128dp</dimen>
<dimen name="cat_toc_short_toolbar_height">128dp</dimen>
<dimen name="cat_toc_tall_toolbar_height">112dp</dimen>
<dimen name="cat_toc_toolbar_image_offset">16dp</dimen>
<dimen name="cat_toc_grid_divider_size">1dp</dimen>
<dimen name="cat_toc_item_size">180dp</dimen>
Expand Down
Expand Up @@ -174,6 +174,8 @@ public class CollapsingToolbarLayout extends FrameLayout {
@TitleCollapseMode private int titleCollapseMode;

@Nullable WindowInsetsCompat lastInsets;
private int topInsetApplied = 0;
private boolean forceApplySystemWindowInsetTop = false;

public CollapsingToolbarLayout(@NonNull Context context) {
this(context, null);
Expand Down Expand Up @@ -285,6 +287,9 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet

toolbarId = a.getResourceId(R.styleable.CollapsingToolbarLayout_toolbarId, -1);

forceApplySystemWindowInsetTop =
a.getBoolean(R.styleable.CollapsingToolbarLayout_forceApplySystemWindowInsetTop, false);

a.recycle();

setWillNotDraw(false);
Expand Down Expand Up @@ -528,9 +533,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

final int mode = MeasureSpec.getMode(heightMeasureSpec);
final int topInset = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;
if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
// If we have a top inset and we're set to wrap_content height we need to make sure
// we add the top inset to our height, therefore we re-measure
if ((mode == MeasureSpec.UNSPECIFIED || forceApplySystemWindowInsetTop) && topInset > 0) {
// If we have a top inset and we're set to wrap_content height or force apply,
// we need to make sure we add the top inset to our height, therefore we re-measure
topInsetApplied = topInset;
heightMeasureSpec =
MeasureSpec.makeMeasureSpec(getMeasuredHeight() + topInset, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -1322,6 +1328,24 @@ public boolean isRtlTextDirectionHeuristicsEnabled() {
return collapsingTextHelper.isRtlTextDirectionHeuristicsEnabled();
}

/**
* Sets whether the top system window inset should be respected regardless of what the
* {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
*/
@RestrictTo(LIBRARY_GROUP)
public void setForceApplySystemWindowInsetTop(boolean forceApplySystemWindowInsetTop) {
this.forceApplySystemWindowInsetTop = forceApplySystemWindowInsetTop;
}

/**
* Gets whether the top system window inset should be respected regardless of what the
* {@code layout_height} of the {@code CollapsingToolbarLayout} is set to. Experimental Feature.
*/
@RestrictTo(LIBRARY_GROUP)
public boolean isForceApplySystemWindowInsetTop() {
return forceApplySystemWindowInsetTop;
}

/**
* Set the amount of visible height in pixels used to define when to trigger a scrim visibility
* change.
Expand Down Expand Up @@ -1350,7 +1374,7 @@ public void setScrimVisibleHeightTrigger(@IntRange(from = 0) final int height) {
public int getScrimVisibleHeightTrigger() {
if (scrimVisibleHeightTrigger >= 0) {
// If we have one explicitly set, return it
return scrimVisibleHeightTrigger;
return scrimVisibleHeightTrigger + topInsetApplied;
}

// Otherwise we'll use the default computed value
Expand Down
Expand Up @@ -221,6 +221,9 @@
</attr>
<!-- The maximum number of lines to display in the expanded state. Experimental Feature. -->
<attr name="maxLines" format="integer" />
<!-- Whether the system window inset top should be applied regardless of
what the layout_height is set to. Experimental Feature. -->
<attr name="forceApplySystemWindowInsetTop" format="boolean" />
</declare-styleable>

<declare-styleable name="CollapsingToolbarLayout_Layout">
Expand Down

0 comments on commit 9ebf1a1

Please sign in to comment.