-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathFloatingToolbarLayout.java
160 lines (134 loc) · 6.48 KB
/
FloatingToolbarLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.floatingtoolbar;
import com.google.android.material.R;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import android.content.Context;
import android.content.res.ColorStateList;
import androidx.appcompat.widget.TintTypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
/**
* Provides an implementation of a floating toolbar.
*
* <p>Floating toolbars float above the body content and can be used to display contextual actions
* relevant to the body content or the specific page.
*
* <p>The floating toolbar supports a custom {@link android.view.ViewGroup} child. The toolbar
* provides styling for the provided child to give a uniform "floating toolbar" appearance to the
* {@link android.view.ViewGroup}.
*/
public class FloatingToolbarLayout extends FrameLayout {
private static final String TAG = FloatingToolbarLayout.class.getSimpleName();
private static final int DEF_STYLE_RES = R.style.Widget_Material3_FloatingToolbar;
private boolean marginLeftSystemWindowInsets;
private boolean marginTopSystemWindowInsets;
private boolean marginRightSystemWindowInsets;
private boolean marginBottomSystemWindowInsets;
public FloatingToolbarLayout(@NonNull Context context) {
this(context, null);
}
public FloatingToolbarLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.floatingToolbarStyle);
}
public FloatingToolbarLayout(
@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
this(context, attrs, defStyleAttr, DEF_STYLE_RES);
}
public FloatingToolbarLayout(
@NonNull Context context,
@Nullable AttributeSet attrs,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes) {
super(wrap(context, attrs, defStyleAttr, defStyleRes), attrs, defStyleAttr);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
/* Custom attributes */
TintTypedArray attributes =
ThemeEnforcement.obtainTintedStyledAttributes(
context, attrs, R.styleable.FloatingToolbar, defStyleAttr, defStyleRes);
// Add a MaterialShapeDrawable as a background that supports tinting in every API level.
if (attributes.hasValue(R.styleable.FloatingToolbar_backgroundTint)) {
@ColorInt
int backgroundColor = attributes.getColor(R.styleable.FloatingToolbar_backgroundTint, 0);
ShapeAppearanceModel shapeAppearanceModel =
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, defStyleRes).build();
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
materialShapeDrawable.setFillColor(ColorStateList.valueOf(backgroundColor));
setBackground(materialShapeDrawable);
}
// Reading out if we are handling inset margins, so we can apply it to the content.
marginLeftSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginLeftSystemWindowInsets, true);
// Top-aligned floating toolbars are not recommended, so a top inset margin is turned off by default
marginTopSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginTopSystemWindowInsets, false);
marginRightSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginRightSystemWindowInsets, true);
marginBottomSystemWindowInsets = attributes.getBoolean(R.styleable.FloatingToolbar_marginBottomSystemWindowInsets, true);
ViewCompat.setOnApplyWindowInsetsListener(
this,
new androidx.core.view.OnApplyWindowInsetsListener() {
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(
@NonNull View v, @NonNull WindowInsetsCompat insets) {
if (!marginLeftSystemWindowInsets && !marginRightSystemWindowInsets
&& !marginTopSystemWindowInsets && !marginBottomSystemWindowInsets) {
return insets;
}
Insets systemBarInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Insets cutoutInsets = insets.getInsets(WindowInsetsCompat.Type.displayCutout());
int bottomInset = systemBarInsets.bottom + cutoutInsets.bottom;
int topInset = systemBarInsets.top + cutoutInsets.top;
int rightInset = systemBarInsets.right + cutoutInsets.right;
int leftInset = systemBarInsets.left + cutoutInsets.left;
ViewGroup.LayoutParams lp = getLayoutParams();
if (!(lp instanceof MarginLayoutParams)) {
Log.w(TAG, "Unable to update margins because layout params are not MarginLayoutParams");
return insets;
}
MarginLayoutParams marginLp = (MarginLayoutParams) lp;
if (marginLeftSystemWindowInsets) {
marginLp.leftMargin += leftInset;
}
if (marginRightSystemWindowInsets) {
marginLp.rightMargin += rightInset;
}
if (marginTopSystemWindowInsets) {
marginLp.topMargin += topInset;
}
if (marginBottomSystemWindowInsets) {
marginLp.bottomMargin += bottomInset;
}
requestLayout();
return insets;
}
});
attributes.recycle();
}
}