mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Workaround chip compositing bug that dropped stated_pressed and state_focused.
PiperOrigin-RevId: 260577585
This commit is contained in:
parent
24a3fd4bb0
commit
d60fb3fc22
@ -162,18 +162,7 @@ public class ChipDrawable extends MaterialShapeDrawable
|
||||
private static final boolean DEBUG = false;
|
||||
private static final int[] DEFAULT_STATE = new int[] {android.R.attr.state_enabled};
|
||||
private static final String NAMESPACE_APP = "http://schemas.android.com/apk/res-auto";
|
||||
private static final int[][] states =
|
||||
new int[][] {
|
||||
new int[] {
|
||||
android.R.attr.state_enabled, android.R.attr.state_selected,
|
||||
},
|
||||
new int[] {
|
||||
android.R.attr.state_enabled, android.R.attr.state_checked,
|
||||
},
|
||||
new int[] {android.R.attr.state_enabled, android.R.attr.state_checkable},
|
||||
new int[] {android.R.attr.state_enabled},
|
||||
new int[] {} // default
|
||||
};
|
||||
|
||||
private static final ShapeDrawable closeIconRippleMask = new ShapeDrawable(new OvalShape());
|
||||
|
||||
// Visuals
|
||||
@ -254,6 +243,7 @@ public class ChipDrawable extends MaterialShapeDrawable
|
||||
|
||||
@ColorInt private int currentChipSurfaceColor;
|
||||
@ColorInt private int currentChipBackgroundColor;
|
||||
@ColorInt private int currentCompositeSurfaceBackgroundColor;
|
||||
@ColorInt private int currentChipStrokeColor;
|
||||
@ColorInt private int currentCompatRippleColor;
|
||||
@ColorInt private int currentTextColor;
|
||||
@ -996,6 +986,14 @@ public class ChipDrawable extends MaterialShapeDrawable
|
||||
invalidate = true;
|
||||
}
|
||||
|
||||
int newCompositeSurfaceBackgroundColor =
|
||||
MaterialColors.layer(newChipSurfaceColor, newChipBackgroundColor);
|
||||
if (currentCompositeSurfaceBackgroundColor != newCompositeSurfaceBackgroundColor) {
|
||||
currentCompositeSurfaceBackgroundColor = newCompositeSurfaceBackgroundColor;
|
||||
setFillColor(ColorStateList.valueOf(currentCompositeSurfaceBackgroundColor));
|
||||
invalidate = true;
|
||||
}
|
||||
|
||||
int newChipStrokeColor =
|
||||
chipStrokeColor != null
|
||||
? chipStrokeColor.getColorForState(chipState, currentChipStrokeColor)
|
||||
@ -1294,11 +1292,6 @@ public class ChipDrawable extends MaterialShapeDrawable
|
||||
private void setChipSurfaceColor(@Nullable ColorStateList chipSurfaceColor) {
|
||||
if (this.chipSurfaceColor != chipSurfaceColor) {
|
||||
this.chipSurfaceColor = chipSurfaceColor;
|
||||
if (isShapeThemingEnabled) {
|
||||
if (chipSurfaceColor != null && chipBackgroundColor != null) {
|
||||
setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor));
|
||||
}
|
||||
}
|
||||
onStateChange(getState());
|
||||
}
|
||||
}
|
||||
@ -1356,21 +1349,10 @@ public class ChipDrawable extends MaterialShapeDrawable
|
||||
public void setChipBackgroundColor(@Nullable ColorStateList chipBackgroundColor) {
|
||||
if (this.chipBackgroundColor != chipBackgroundColor) {
|
||||
this.chipBackgroundColor = chipBackgroundColor;
|
||||
if (isShapeThemingEnabled) {
|
||||
if (chipSurfaceColor != null && chipBackgroundColor != null) {
|
||||
setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor));
|
||||
}
|
||||
}
|
||||
onStateChange(getState());
|
||||
}
|
||||
}
|
||||
|
||||
private ColorStateList compositeSurfaceBackgroundColor(
|
||||
@NonNull ColorStateList backgroundColor, @NonNull ColorStateList surfaceColor) {
|
||||
return MaterialColors.layer(
|
||||
surfaceColor, currentChipSurfaceColor, backgroundColor, currentChipBackgroundColor, states);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this chip's minimum height.
|
||||
*
|
||||
|
||||
@ -18,21 +18,15 @@ package com.google.android.material.color;
|
||||
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import android.graphics.Color;
|
||||
import androidx.annotation.AttrRes;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.FloatRange;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RestrictTo;
|
||||
import com.google.android.material.resources.MaterialAttributes;
|
||||
import androidx.core.graphics.ColorUtils;
|
||||
import android.util.StateSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A utility class for common color variants used in Material themes.
|
||||
@ -144,119 +138,4 @@ public class MaterialColors {
|
||||
public static int layer(@ColorInt int backgroundColor, @ColorInt int overlayColor) {
|
||||
return ColorUtils.compositeColors(overlayColor, backgroundColor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a color state list that represents the layering of the {@code overlayColor} on top
|
||||
* of the {@code backgroundColor} for the given set of {@code states}. CAUTION: More specific
|
||||
* states that have the same color value as a more generic state may be dropped, see example
|
||||
* below:
|
||||
* <p>states:
|
||||
* <pre>
|
||||
* {selected, enabled},
|
||||
* {checked, enabled},
|
||||
* {enabled},
|
||||
* default
|
||||
* </pre>
|
||||
* <p>Overlay CSL:
|
||||
* <pre>
|
||||
* "" TRANSPARENT
|
||||
* </pre>
|
||||
* <p>Scenario 1
|
||||
* <p>Background CSL:
|
||||
* <pre>
|
||||
* checked RED
|
||||
* "" GREEN
|
||||
* </pre>
|
||||
*
|
||||
* <p>Current result:
|
||||
* <pre>
|
||||
* enabled, checked RED
|
||||
* enabled GREEN
|
||||
* </pre>
|
||||
*
|
||||
* <p>Color for state {enabled, checked, selected} --> returns RED # correct
|
||||
*
|
||||
* <p>Result if iterating top down through CSL to composite each state color:
|
||||
* <pre>
|
||||
* enabled, selected GREEN
|
||||
* enabled, checked RED
|
||||
* enabled GREEN
|
||||
* </pre>
|
||||
*
|
||||
* <p>Color for state {enabled, checked, selected} --> returns GREEN #incorrect
|
||||
*
|
||||
* <p>Scenario 2
|
||||
* Background CSL:
|
||||
* <pre>
|
||||
* selected GREEN
|
||||
* checked RED
|
||||
* "" GREEN
|
||||
* </pre>
|
||||
* <p>Current result:
|
||||
* <pre>
|
||||
* enabled, checked RED
|
||||
* enabled GREEN
|
||||
* </pre>
|
||||
* <p>Color for state {enabled, checked, selected} --> returns RED # incorrect
|
||||
*
|
||||
* <p>Result if iterating top down through CSL to composite each state color:
|
||||
* <pre>
|
||||
* enabled, selected GREEN
|
||||
* enabled, checked RED
|
||||
* enabled GREEN
|
||||
* </pre>
|
||||
* <p>Color for state {enabled, checked, selected} --> returns GREEN # correct
|
||||
*/
|
||||
public static ColorStateList layer(
|
||||
ColorStateList backgroundColor,
|
||||
@ColorInt int defaultBackgroundColor,
|
||||
ColorStateList overlayColor,
|
||||
@ColorInt int defaultOverlayColor,
|
||||
int[][] states) {
|
||||
List<Integer> uniqueColors = new ArrayList<>();
|
||||
List<int[]> uniqueStateSet = new ArrayList<>();
|
||||
|
||||
// Iterates bottom to top, from least to most specific states.
|
||||
for (int i = states.length - 1; i >= 0; i--) {
|
||||
int[] curState = states[i];
|
||||
int layeredStateColor =
|
||||
MaterialColors.layer(
|
||||
backgroundColor.getColorForState(curState, defaultBackgroundColor),
|
||||
overlayColor.getColorForState(curState, defaultOverlayColor));
|
||||
|
||||
if (shouldAddColorForState(uniqueColors, layeredStateColor, uniqueStateSet, curState)) {
|
||||
// Add to the front of the list in original CSL order.
|
||||
uniqueColors.add(0, layeredStateColor);
|
||||
uniqueStateSet.add(0, curState);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert lists to arrays.
|
||||
int numStates = uniqueColors.size();
|
||||
int[] colors = new int[numStates];
|
||||
int[][] colorStates = new int[numStates][];
|
||||
for (int i = 0; i < numStates; i++) {
|
||||
colors[i] = uniqueColors.get(i);
|
||||
colorStates[i] = uniqueStateSet.get(i);
|
||||
}
|
||||
return new ColorStateList(colorStates, colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the specified @{code color} should be added to a ColorStateList for the
|
||||
* specified {@code state} or if the existing color set and state set already cover it.
|
||||
*/
|
||||
private static boolean shouldAddColorForState(
|
||||
List<Integer> colorSet, @ColorInt int color, List<int[]> stateSet, @Nullable int[] state) {
|
||||
new HashSet<Integer>(colorSet);
|
||||
if (!colorSet.contains(color)) {
|
||||
return true;
|
||||
}
|
||||
for (int[] stateSetItem : stateSet) {
|
||||
if (StateSet.stateSetMatches(stateSetItem, state)) {
|
||||
return (colorSet.get(stateSet.indexOf(stateSetItem)) != color);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 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.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.google.android.material.color">
|
||||
|
||||
<uses-sdk
|
||||
tools:overrideLibrary="androidx.test.core"/>
|
||||
|
||||
<application/>
|
||||
</manifest>
|
||||
@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.color;
|
||||
|
||||
import com.google.android.material.R;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.ColorStateList;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.internal.DoNotInstrument;
|
||||
|
||||
/** Tests for {@link com.google.android.material.color.MaterialColors}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@DoNotInstrument
|
||||
public class MaterialColorsTest {
|
||||
|
||||
private static final int[][] states =
|
||||
new int[][] {
|
||||
new int[] {
|
||||
android.R.attr.state_enabled, android.R.attr.state_selected,
|
||||
},
|
||||
new int[] {
|
||||
android.R.attr.state_enabled, android.R.attr.state_checked,
|
||||
},
|
||||
new int[] {android.R.attr.state_enabled},
|
||||
new int[] {},
|
||||
};
|
||||
|
||||
private static final int[] state_empty = {};
|
||||
private static final int[] state_enabled = {android.R.attr.state_enabled};
|
||||
|
||||
private final Context context = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Before
|
||||
public void themeApplicationContext() {
|
||||
context.setTheme(R.style.Theme_MaterialComponents_Light);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLayerColorStateLists() {
|
||||
ColorStateList overlayColor =
|
||||
AppCompatResources.getColorStateList(context, R.color.test_background_state);
|
||||
ColorStateList backgroundColor =
|
||||
AppCompatResources.getColorStateList(context, R.color.test_surface_state);
|
||||
ColorStateList compositeColor =
|
||||
MaterialColors.layer(backgroundColor, -1, overlayColor, 0, states);
|
||||
|
||||
List<int[][]> testStatesList = generateTestStatesList();
|
||||
for (int[][] testStates : testStatesList) {
|
||||
for (int[] state : testStates) {
|
||||
compareLayeredColorsForStates(compositeColor, backgroundColor, overlayColor, state, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLayerColorStateLists_repeatedColors_unsupported() {
|
||||
ColorStateList overlayColor =
|
||||
AppCompatResources.getColorStateList(
|
||||
context, R.color.test_background_state_unsupported_case);
|
||||
ColorStateList backgroundColor =
|
||||
AppCompatResources.getColorStateList(context, R.color.test_surface_state);
|
||||
ColorStateList compositeColor =
|
||||
MaterialColors.layer(backgroundColor, -1, overlayColor, 0, states);
|
||||
|
||||
int[] unsupportedTestState =
|
||||
new int[] {
|
||||
android.R.attr.state_enabled, android.R.attr.state_selected, android.R.attr.state_checked,
|
||||
};
|
||||
compareLayeredColorsForStates(
|
||||
compositeColor, backgroundColor, overlayColor, unsupportedTestState, false);
|
||||
}
|
||||
|
||||
private static List<int[][]> generateTestStatesList() {
|
||||
List<int[][]> testStatesList = new ArrayList<>();
|
||||
int[][] testStates1 =
|
||||
new int[][] {
|
||||
new int[] {
|
||||
android.R.attr.state_enabled,
|
||||
android.R.attr.state_selected,
|
||||
android.R.attr.state_checked,
|
||||
},
|
||||
state_enabled,
|
||||
state_empty,
|
||||
};
|
||||
testStatesList.add(testStates1);
|
||||
int[][] testState2 = {
|
||||
{android.R.attr.state_enabled, android.R.attr.state_selected}, state_enabled, state_empty
|
||||
};
|
||||
testStatesList.add(testState2);
|
||||
int[][] testState3 = {
|
||||
{android.R.attr.state_checked, android.R.attr.state_enabled}, state_enabled, state_empty
|
||||
};
|
||||
testStatesList.add(testState3);
|
||||
int[][] testState4 = {
|
||||
{android.R.attr.state_pressed, android.R.attr.state_checked, android.R.attr.state_enabled},
|
||||
state_enabled,
|
||||
state_empty
|
||||
};
|
||||
testStatesList.add(testState4);
|
||||
return testStatesList;
|
||||
}
|
||||
|
||||
private static void compareLayeredColorsForStates(
|
||||
ColorStateList compositeColor,
|
||||
ColorStateList backgroundColor,
|
||||
ColorStateList overlayColor,
|
||||
int[] state,
|
||||
boolean expectedResult) {
|
||||
int composite = compositeColor.getColorForState(state, -1);
|
||||
int background = backgroundColor.getColorForState(state, -1);
|
||||
int overlay = overlayColor.getColorForState(state, -1);
|
||||
int expectedColor = MaterialColors.layer(background, overlay);
|
||||
assertColorsAreEqual(expectedColor, composite, expectedResult);
|
||||
}
|
||||
|
||||
private static void assertColorsAreEqual(
|
||||
@ColorInt int expectedColor, @ColorInt int color, boolean expectedResult) {
|
||||
if (expectedResult) {
|
||||
Assert.assertEquals(expectedColor, color);
|
||||
} else {
|
||||
Assert.assertNotEquals(expectedColor, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:state_checked="true" android:state_enabled="true" android:color="#ffadff" />
|
||||
<item android:state_pressed="true" android:color="#ffadff" />
|
||||
<item android:alpha="0.10" android:color="?attr/colorOnSurface" android:state_enabled="true"/>
|
||||
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
|
||||
</selector>
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:state_selected="true" android:state_enabled="true" android:color="#ff0000" />
|
||||
<item android:state_checked="true" android:state_enabled="true" android:color="#ffadff" />
|
||||
<item android:state_enabled="true" android:color="#ff0000" />
|
||||
<item android:color="?attr/colorOnSurface"/>
|
||||
</selector>
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2019 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
|
||||
|
||||
https://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.
|
||||
-->
|
||||
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item android:color="@android:color/transparent"/>
|
||||
|
||||
</selector>
|
||||
Loading…
x
Reference in New Issue
Block a user