From 245ffe72368be98d73909227732299b869a6a36a Mon Sep 17 00:00:00 2001 From: dniz Date: Wed, 30 Jun 2021 06:01:23 -0700 Subject: [PATCH] [CollapsingToolbarLayout] Added option to force always applying system window inset top regardless of layout_height PiperOrigin-RevId: 382286923 --- .../res/layout/cat_toc_fragment.xml | 4 +-- .../tableofcontents/res/values/dimens.xml | 3 +- .../appbar/CollapsingToolbarLayout.java | 32 ++++++++++++++++--- .../material/appbar/res/values/attrs.xml | 3 ++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/catalog/java/io/material/catalog/tableofcontents/res/layout/cat_toc_fragment.xml b/catalog/java/io/material/catalog/tableofcontents/res/layout/cat_toc_fragment.xml index 36f0677b482..a91932d1317 100644 --- a/catalog/java/io/material/catalog/tableofcontents/res/layout/cat_toc_fragment.xml +++ b/catalog/java/io/material/catalog/tableofcontents/res/layout/cat_toc_fragment.xml @@ -27,14 +27,14 @@ diff --git a/catalog/java/io/material/catalog/tableofcontents/res/values/dimens.xml b/catalog/java/io/material/catalog/tableofcontents/res/values/dimens.xml index ffc4f12be0d..33c5c7c075c 100644 --- a/catalog/java/io/material/catalog/tableofcontents/res/values/dimens.xml +++ b/catalog/java/io/material/catalog/tableofcontents/res/values/dimens.xml @@ -16,8 +16,7 @@ --> - 128dp - 128dp + 112dp 16dp 1dp 180dp diff --git a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java index 58d529e312b..73473b82b11 100644 --- a/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java +++ b/lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java @@ -173,6 +173,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); @@ -273,6 +275,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); @@ -516,9 +521,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); @@ -1310,6 +1316,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. @@ -1338,7 +1362,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 diff --git a/lib/java/com/google/android/material/appbar/res/values/attrs.xml b/lib/java/com/google/android/material/appbar/res/values/attrs.xml index b55b11d770f..b74698cec83 100644 --- a/lib/java/com/google/android/material/appbar/res/values/attrs.xml +++ b/lib/java/com/google/android/material/appbar/res/values/attrs.xml @@ -206,6 +206,9 @@ + +