Skip to content

Commit 6783f9e

Browse files
dsn5ftymarian
authored andcommitted
Clean up naming of elevation overlay methods and attributes
PiperOrigin-RevId: 253283047
1 parent 0aabd78 commit 6783f9e

17 files changed

Lines changed: 102 additions & 103 deletions

File tree

catalog/java/io/material/catalog/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
</intent-filter>
4141
</activity>
4242
<activity
43-
android:name=".elevation.ElevationOverlaysDemoActivity"
43+
android:name=".elevation.ElevationOverlayDemoActivity"
4444
android:exported="false"
45-
android:label="@string/cat_elevation_overlays_title"
45+
android:label="@string/cat_elevation_overlay_title"
4646
android:theme="@style/Theme.Catalog.Dark"/>
4747
<activity
4848
android:name=".topappbar.TopAppBarActionBarDemoActivity"

catalog/java/io/material/catalog/elevation/ElevationFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ public Fragment createFragment() {
5757
@Override
5858
public List<Demo> getAdditionalDemos() {
5959
return Arrays.asList(
60-
new Demo(R.string.cat_elevation_overlays_title) {
60+
new Demo(R.string.cat_elevation_overlay_title) {
6161
@Override
6262
public Intent createActivityIntent() {
63-
return new Intent(getContext(), ElevationOverlaysDemoActivity.class);
63+
return new Intent(getContext(), ElevationOverlayDemoActivity.class);
6464
}
6565
},
6666
new Demo(R.string.cat_elevation_animation_title) {

catalog/java/io/material/catalog/elevation/ElevationOverlaysDemoActivity.java renamed to catalog/java/io/material/catalog/elevation/ElevationOverlayDemoActivity.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
import io.material.catalog.feature.DemoActivity;
3434
import java.util.Locale;
3535

36-
/** A fragment that displays the Elevation Overlays demo for the Catalog app. */
37-
public class ElevationOverlaysDemoActivity extends DemoActivity {
36+
/** A fragment that displays the Elevation Overlay demo for the Catalog app. */
37+
public class ElevationOverlayDemoActivity extends DemoActivity {
3838

3939
@Override
4040
public View onCreateDemoView(
4141
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
4242
View view =
4343
layoutInflater.inflate(
44-
R.layout.cat_elevation_overlays_activity, viewGroup, /* attachToRoot= */ false);
44+
R.layout.cat_elevation_overlay_activity, viewGroup, /* attachToRoot= */ false);
4545

4646
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
4747
recyclerView.setAdapter(new Adapter(getElevationDpValues()));
@@ -53,7 +53,7 @@ public View onCreateDemoView(
5353

5454
@Override
5555
public int getDemoTitleResId() {
56-
return R.string.cat_elevation_overlays_title;
56+
return R.string.cat_elevation_overlay_title;
5757
}
5858

5959
protected int[] getElevationDpValues() {
@@ -73,7 +73,7 @@ public Adapter(int[] elevationDpValues) {
7373
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
7474
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
7575
View view =
76-
inflater.inflate(R.layout.cat_elevation_overlays_item, parent, /* attachToRoot= */ false);
76+
inflater.inflate(R.layout.cat_elevation_overlay_item, parent, /* attachToRoot= */ false);
7777
return new ItemViewHolder(view);
7878
}
7979

@@ -103,8 +103,8 @@ private ItemViewHolder(View itemView) {
103103

104104
@SuppressWarnings("RestrictTo")
105105
private void bind(int elevationDp) {
106-
float elevation = ViewUtils.dpToPx(ElevationOverlaysDemoActivity.this, elevationDp);
107-
int color = overlayProvider.getSurfaceColorWithOverlayIfNeeded(elevation);
106+
float elevation = ViewUtils.dpToPx(ElevationOverlayDemoActivity.this, elevationDp);
107+
int color = overlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(elevation);
108108
int alphaPercent =
109109
Math.round(overlayProvider.calculateOverlayAlphaFraction(elevation) * 100);
110110

catalog/java/io/material/catalog/elevation/res/layout/cat_elevation_overlays_activity.xml renamed to catalog/java/io/material/catalog/elevation/res/layout/cat_elevation_overlay_activity.xml

File renamed without changes.

catalog/java/io/material/catalog/elevation/res/layout/cat_elevation_overlays_item.xml renamed to catalog/java/io/material/catalog/elevation/res/layout/cat_elevation_overlay_item.xml

File renamed without changes.

catalog/java/io/material/catalog/elevation/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
depicted (by default) using shadows. This demo illustrates the proper rendering of shadows at
2424
different elevations.
2525
</string>
26-
<string name="cat_elevation_overlays_title" translatable="false">Elevation Overlays Demo</string>
26+
<string name="cat_elevation_overlay_title" translatable="false">Elevation Overlay Demo</string>
2727
<string name="cat_elevation_animation_title" translatable="false">Elevation Animation Demo</string>
2828
<string name="cat_increase_elevation">Increase Elevation</string>
2929
<string name="cat_decrease_elevation">Decrease Elevation</string>

docs/theming/Dark.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ the following theme attributes:
143143

144144
Attribute Name |Description |Default Value
145145
-----------------------------|-------------------------------------------------------------------------------------|-------------
146-
`elevationOverlaysEnabled` |Whether the elevation overlay functionality is enabled. |`false` in `Light` themes, `true` in `Dark` themes
147-
`elevationOverlaysColor` |The color used for the elevation overlays, applied at an alpha based on elevation. |`colorOnSurface`
146+
`elevationOverlayEnabled` |Whether the elevation overlay functionality is enabled. |`false` in `Light` themes, `true` in `Dark` themes
147+
`elevationOverlayColor` |The color used for the elevation overlays, applied at an alpha based on elevation. |`colorOnSurface`
148148

149149
Note: If inheriting from the `Theme.MaterialComponents` theme or a descendant,
150150
you most likely do not have to set these attributes yourself because the
@@ -178,16 +178,16 @@ to get the corresponding dark theme overlay color (perhaps to color an existing
178178
view), then you can use `ElevationOverlayProvider`.
179179

180180
If elevation overlays are enabled at the theme level, the
181-
`ElevationOverlayProvider#getSurfaceColorWithOverlayIfNeeded` method will return
182-
`colorSurface` with the overlay color blended in at an alpha level based on the
183-
elevation passed in. Otherwise, it will simply return `colorSurface`, so that
184-
you can use the result of this method in both `Light` and `Dark` themes without
185-
needing any additional orchestration.
181+
`ElevationOverlayProvider#compositeOverlayWithThemeSurfaceColorIfNeeded` method
182+
will return `colorSurface` with the overlay color blended in at an alpha level
183+
based on the elevation passed in. Otherwise, it will simply return
184+
`colorSurface`, so that you can use the result of this method in both `Light`
185+
and `Dark` themes without needing any additional orchestration.
186186

187187
If you need to blend the overlays with an arbitrary color or an adjusted surface
188188
color, or get access to lower level values such as the overlay alpha
189189
percentages, take a look at the other `ElevationOverlayProvider` methods
190-
including `layerOverlayIfNeeded`, `layerOverlay`, and `calculateOverlayAlpha`.
190+
including `compositeOverlayIfNeeded`, `compositeOverlay`, and `calculateOverlayAlpha`.
191191

192192
[dark-theme-mdc-spec]: https://material.io/design/color/dark-theme.html
193193
[dark-theme-mdc-spec-ui-application]: https://material.io/design/color/dark-theme.html#ui-application

lib/java/com/google/android/material/dialog/res/values/themes_base_bridge.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
</item>
7373

7474
<!-- Elevation Overlays -->
75-
<item name="elevationOverlaysEnabled">true</item>
76-
<item name="elevationOverlaysColor">?attr/colorOnSurface</item>
75+
<item name="elevationOverlayEnabled">true</item>
76+
<item name="elevationOverlayColor">?attr/colorOnSurface</item>
7777
</style>
7878

7979
<style name="Base.V14.Theme.MaterialComponents.Light.Dialog.Bridge" parent="Platform.MaterialComponents.Light.Dialog">
@@ -118,8 +118,8 @@
118118
</item>
119119

120120
<!-- Elevation Overlays -->
121-
<item name="elevationOverlaysEnabled">false</item>
122-
<item name="elevationOverlaysColor">?attr/colorOnSurface</item>
121+
<item name="elevationOverlayEnabled">false</item>
122+
<item name="elevationOverlayColor">?attr/colorOnSurface</item>
123123
</style>
124124

125125
<!-- Themes in the "Base.Theme" family vary based on the current platform

lib/java/com/google/android/material/elevation/ElevationOverlayProvider.java

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,54 @@ public class ElevationOverlayProvider {
3131
private static final float FORMULA_MULTIPLIER = 4.5f;
3232
private static final float FORMULA_OFFSET = 2f;
3333

34-
private final boolean elevationOverlaysEnabled;
35-
private final int elevationOverlaysColor;
34+
private final boolean elevationOverlayEnabled;
35+
private final int elevationOverlayColor;
3636
private final int colorSurface;
3737
private final float displayDensity;
3838

3939
public ElevationOverlayProvider(Context context) {
40-
this.elevationOverlaysEnabled =
41-
MaterialAttributes.resolveBoolean(context, R.attr.elevationOverlaysEnabled, false);
42-
this.elevationOverlaysColor =
43-
MaterialColors.getColor(context, R.attr.elevationOverlaysColor, Color.TRANSPARENT);
40+
this.elevationOverlayEnabled =
41+
MaterialAttributes.resolveBoolean(context, R.attr.elevationOverlayEnabled, false);
42+
this.elevationOverlayColor =
43+
MaterialColors.getColor(context, R.attr.elevationOverlayColor, Color.TRANSPARENT);
4444
this.colorSurface = MaterialColors.getColor(context, R.attr.colorSurface, Color.TRANSPARENT);
4545
this.displayDensity = context.getResources().getDisplayMetrics().density;
4646
}
4747

4848
/**
49-
* Applies the calculated elevation overlay (@see #layerOverlay(int, float)) only if the current
50-
* theme's {@code R.attr.elevationOverlaysEnabled} is true and the {@code backgroundColor} matches
51-
* the theme's surface color ({@code R.attr.colorSurface}); otherwise returns the {@code
52-
* backgroundColor}.
49+
* Blends the calculated elevation overlay color (@see #compositeOverlayIfNeeded(int, float)) with
50+
* the current theme's color int value for {@code R.attr.colorSurface} if needed.
5351
*/
5452
@ColorInt
55-
public int layerOverlayIfNeeded(@ColorInt int backgroundColor, float elevation) {
56-
if (elevationOverlaysEnabled && isSurfaceColor(backgroundColor)) {
57-
return layerOverlay(backgroundColor, elevation);
53+
public int compositeOverlayWithThemeSurfaceColorIfNeeded(float elevation) {
54+
return compositeOverlayIfNeeded(colorSurface, elevation);
55+
}
56+
57+
/**
58+
* Blends the calculated elevation overlay color (@see #compositeOverlay(int, float)) with the
59+
* {@code backgroundColor}, only if the current theme's {@code R.attr.elevationOverlayEnabled} is
60+
* true and the {@code backgroundColor} matches the theme's surface color ({@code
61+
* R.attr.colorSurface}); otherwise returns the {@code backgroundColor}.
62+
*/
63+
@ColorInt
64+
public int compositeOverlayIfNeeded(@ColorInt int backgroundColor, float elevation) {
65+
if (elevationOverlayEnabled && isThemeSurfaceColor(backgroundColor)) {
66+
return compositeOverlay(backgroundColor, elevation);
5867
} else {
5968
return backgroundColor;
6069
}
6170
}
6271

6372
/**
64-
* Calculates a color that represents the layering of the current theme's {@code
65-
* R.attr.elevationOverlaysColor} on top of the {@code backgroundColor}.
73+
* Blends the calculated elevation overlay color with the provided {@code backgroundColor}.
6674
*
67-
* <p>An alpha level is applied to the {@code R.attr.elevationOverlaysColor} by using a formula
68-
* that is based on the provided {@code elevation} value.
75+
* <p>An alpha level is applied to the theme's {@code R.attr.elevationOverlayColor} by using a
76+
* formula that is based on the provided {@code elevation} value.
6977
*/
7078
@ColorInt
71-
public int layerOverlay(@ColorInt int backgroundColor, float elevation) {
79+
public int compositeOverlay(@ColorInt int backgroundColor, float elevation) {
7280
float overlayAlpha = calculateOverlayAlphaFraction(elevation);
73-
return MaterialColors.layer(backgroundColor, elevationOverlaysColor, overlayAlpha);
81+
return MaterialColors.layer(backgroundColor, elevationOverlayColor, overlayAlpha);
7482
}
7583

7684
/**
@@ -95,33 +103,24 @@ public float calculateOverlayAlphaFraction(float elevation) {
95103
return Math.min(alphaFraction, 1);
96104
}
97105

98-
/** Returns the current theme's boolean value for {@code R.attr.elevationOverlaysEnabled}. */
99-
public boolean isOverlaysEnabled() {
100-
return elevationOverlaysEnabled;
106+
/** Returns the current theme's boolean value for {@code R.attr.elevationOverlayEnabled}. */
107+
public boolean isThemeElevationOverlayEnabled() {
108+
return elevationOverlayEnabled;
101109
}
102110

103-
/** Returns the current theme's color int value for {@code R.attr.elevationOverlaysColor}. */
111+
/** Returns the current theme's color int value for {@code R.attr.elevationOverlayColor}. */
104112
@ColorInt
105-
public int getOverlaysColor() {
106-
return elevationOverlaysColor;
113+
public int getThemeElevationOverlayColor() {
114+
return elevationOverlayColor;
107115
}
108116

109117
/** Returns the current theme's color int value for {@code R.attr.colorSurface}. */
110118
@ColorInt
111-
public int getSurfaceColor() {
119+
public int getThemeSurfaceColor() {
112120
return colorSurface;
113121
}
114122

115-
/**
116-
* Returns the current theme's color int value for {@code R.attr.colorSurface}, with an elevation
117-
* overlay applied if needed (@see #layerOverlayIfNeeded(int, float)).
118-
*/
119-
@ColorInt
120-
public int getSurfaceColorWithOverlayIfNeeded(float elevation) {
121-
return layerOverlayIfNeeded(colorSurface, elevation);
122-
}
123-
124-
private boolean isSurfaceColor(@ColorInt int color) {
123+
private boolean isThemeSurfaceColor(@ColorInt int color) {
125124
return ColorUtils.setAlphaComponent(color, 255) == colorSurface;
126125
}
127126
}

lib/java/com/google/android/material/elevation/res-public/values/public.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
~ limitations under the License.
1616
-->
1717
<resources>
18-
<public name="elevationOverlaysEnabled" type="attr"/>
19-
<public name="elevationOverlaysColor" type="attr"/>
18+
<public name="elevationOverlayEnabled" type="attr"/>
19+
<public name="elevationOverlayColor" type="attr"/>
2020
</resources>

0 commit comments

Comments
 (0)