|
19 | 19 | import com.google.android.material.R; |
20 | 20 |
|
21 | 21 | import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap; |
| 22 | +import static java.lang.Math.max; |
22 | 23 |
|
23 | 24 | import android.content.Context; |
24 | 25 | import android.content.res.ColorStateList; |
25 | 26 | import androidx.appcompat.widget.TintTypedArray; |
26 | 27 | import android.util.AttributeSet; |
| 28 | +import android.util.DisplayMetrics; |
| 29 | +import android.util.Log; |
| 30 | +import android.view.View; |
| 31 | +import android.view.ViewGroup; |
27 | 32 | import android.widget.FrameLayout; |
28 | 33 | import androidx.annotation.AttrRes; |
29 | 34 | import androidx.annotation.ColorInt; |
30 | 35 | import androidx.annotation.NonNull; |
31 | 36 | import androidx.annotation.Nullable; |
32 | 37 | import androidx.annotation.StyleRes; |
33 | 38 | import androidx.coordinatorlayout.widget.CoordinatorLayout.AttachedBehavior; |
| 39 | +import androidx.core.graphics.Insets; |
| 40 | +import androidx.core.view.ViewCompat; |
| 41 | +import androidx.core.view.WindowInsetsCompat; |
34 | 42 | import com.google.android.material.behavior.HideViewOnScrollBehavior; |
35 | 43 | import com.google.android.material.internal.ThemeEnforcement; |
36 | 44 | import com.google.android.material.shape.MaterialShapeDrawable; |
|
47 | 55 | * {@link android.view.ViewGroup}. |
48 | 56 | */ |
49 | 57 | public class FloatingToolbarLayout extends FrameLayout implements AttachedBehavior { |
| 58 | + |
| 59 | + private static final String TAG = FloatingToolbarLayout.class.getSimpleName(); |
50 | 60 | private static final int DEF_STYLE_RES = R.style.Widget_Material3_FloatingToolbar; |
51 | 61 | @Nullable private Behavior behavior; |
52 | 62 |
|
| 63 | + private boolean marginLeftSystemWindowInsets; |
| 64 | + private boolean marginTopSystemWindowInsets; |
| 65 | + private boolean marginRightSystemWindowInsets; |
| 66 | + private boolean marginBottomSystemWindowInsets; |
| 67 | + |
| 68 | + private int topInset = 0; |
| 69 | + private int leftInset = 0; |
| 70 | + private int rightInset = 0; |
| 71 | + private int bottomInset = 0; |
| 72 | + |
| 73 | + private final Runnable insetsRunnable = |
| 74 | + () -> { |
| 75 | + ViewGroup.LayoutParams lp = getLayoutParams(); |
| 76 | + if (!(lp instanceof MarginLayoutParams)) { |
| 77 | + Log.w(TAG, "Unable to update margins because layout params are not MarginLayoutParams"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + int[] coords = new int[2]; |
| 82 | + getLocationInWindow(coords); |
| 83 | + int x = coords[0]; |
| 84 | + int y = coords[1]; |
| 85 | + |
| 86 | + DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 87 | + if (getDisplay() == null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + getDisplay().getMetrics(displayMetrics); |
| 91 | + |
| 92 | + MarginLayoutParams marginLp = (MarginLayoutParams) lp; |
| 93 | + |
| 94 | + if (marginLeftSystemWindowInsets && x < leftInset) { |
| 95 | + marginLp.leftMargin = max(leftInset, marginLp.leftMargin); |
| 96 | + } |
| 97 | + |
| 98 | + if (marginRightSystemWindowInsets && x + getWidth() > displayMetrics.widthPixels - rightInset) { |
| 99 | + marginLp.rightMargin = max(rightInset, marginLp.rightMargin); |
| 100 | + } |
| 101 | + |
| 102 | + if (marginTopSystemWindowInsets && y < topInset) { |
| 103 | + marginLp.topMargin = max(topInset, marginLp.topMargin); |
| 104 | + } |
| 105 | + |
| 106 | + if (marginBottomSystemWindowInsets && y + getHeight() > displayMetrics.heightPixels - bottomInset) { |
| 107 | + marginLp.bottomMargin = max(bottomInset, marginLp.bottomMargin); |
| 108 | + } |
| 109 | + requestLayout(); |
| 110 | + }; |
| 111 | + |
53 | 112 | public FloatingToolbarLayout(@NonNull Context context) { |
54 | 113 | this(context, null); |
55 | 114 | } |
@@ -91,6 +150,34 @@ public FloatingToolbarLayout( |
91 | 150 | setBackground(materialShapeDrawable); |
92 | 151 | } |
93 | 152 |
|
| 153 | + // Reading out if we are handling inset margins, so we can apply it to the content. |
| 154 | + marginLeftSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginLeftSystemWindowInsets, true); |
| 155 | + marginTopSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginTopSystemWindowInsets, true); |
| 156 | + marginRightSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginRightSystemWindowInsets, true); |
| 157 | + marginBottomSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginBottomSystemWindowInsets, true); |
| 158 | + |
| 159 | + ViewCompat.setOnApplyWindowInsetsListener( |
| 160 | + this, |
| 161 | + new androidx.core.view.OnApplyWindowInsetsListener() { |
| 162 | + @NonNull |
| 163 | + @Override |
| 164 | + public WindowInsetsCompat onApplyWindowInsets( |
| 165 | + @NonNull View v, @NonNull WindowInsetsCompat insets) { |
| 166 | + if (!marginLeftSystemWindowInsets && !marginRightSystemWindowInsets |
| 167 | + && !marginTopSystemWindowInsets && !marginBottomSystemWindowInsets) { |
| 168 | + return insets; |
| 169 | + } |
| 170 | + Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars()); |
| 171 | + bottomInset = systemBarInsets.bottom; |
| 172 | + topInset = systemBarInsets.top; |
| 173 | + rightInset = systemBarInsets.right; |
| 174 | + leftInset = systemBarInsets.left; |
| 175 | + v.removeCallbacks(insetsRunnable); |
| 176 | + v.post(insetsRunnable); |
| 177 | + return insets; |
| 178 | + } |
| 179 | + }); |
| 180 | + |
94 | 181 | attributes.recycle(); |
95 | 182 | } |
96 | 183 |
|
|
0 commit comments