Skip to content

Commit

Permalink
[ButtonToggleGroup] Renamed insideCorner to innerCorner.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 639864920
  • Loading branch information
pekingme committed Jun 5, 2024
1 parent 99e8044 commit 63bb499
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.android.material.shape.RelativeCornerSize;
import com.google.android.material.slider.Slider;
import com.google.android.material.snackbar.Snackbar;
import io.material.catalog.feature.DemoFragment;
Expand Down Expand Up @@ -98,11 +99,11 @@ public View onCreateDemoView(
});
}

Slider insideCornerSizeSlider = view.findViewById(R.id.insideCornerSizeSlider);
insideCornerSizeSlider.addOnChangeListener(
Slider innerCornerSizeSlider = view.findViewById(R.id.innerCornerSizeSlider);
innerCornerSizeSlider.addOnChangeListener(
(slider, value, fromUser) -> {
for (MaterialButtonToggleGroup toggleGroup : toggleGroups) {
toggleGroup.setInsideCornerSizeInFraction(value / 100f);
toggleGroup.setInnerCornerSize(new RelativeCornerSize(value / 100f));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cat_inside_corner_size_label"/>
android:text="@string/cat_inner_corner_size_label"/>

<com.google.android.material.slider.Slider
android:id="@+id/insideCornerSizeSlider"
android:id="@+id/innerCornerSizeSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:valueFrom="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
<string name="cat_icon_only">Icon only</string>
<string description="A label for a spacing slider [CHAR LIMIT=NONE]"
name="cat_spacing_label">Spacing (dp)</string>
<string description="A label for an inside corner size slider [CHAR LIMIT=NONE]"
name="cat_inside_corner_size_label">Inside corners size (0 – 50%)</string>
<string description="A label for an inner corner size slider [CHAR LIMIT=NONE]"
name="cat_inner_corner_size_label">Inner corners size (0 – 50%)</string>
<string name="cat_button_label_private">Private</string>
<string name="cat_button_label_team">Team</string>
<string name="cat_button_label_everyone">Everyone</string>
Expand Down
14 changes: 7 additions & 7 deletions docs/components/Button.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,13 @@ A toggle button has a shared stroked container, icons and/or text labels.

#### Selection attributes

Element | Attribute | Related method(s) | Default value
------------------------------------- | ----------------------- | --------------------------------------------------------------------------------------- | -------------
**Single selection** | `app:singleSelection` | `setSingleSelection`<br/>`isSingleSelection` | `false`
**Selection required** | `app:selectionRequired` | `setSelectionRequired`<br/>`isSelectionRequired` | `false`
**Enable the group and all children** | `android:enabled` | `setEnabled`<br/>`isEnabled` | `true`
**Radius of inside corners** | `app:insideCornerSize` | `setInsideCornerSizeByPx`<br/>`setInsideCornerSizeByFraction`<br/>`getInsideCornerSize` | `0dp`
**Spacing between buttons** | `android:spacing` | `setSpacing`<br/>`getSpacing` | `0dp`
Element | Attribute | Related method(s) | Default value
------------------------------------- | ----------------------- | ------------------------------------------------ | -------------
**Single selection** | `app:singleSelection` | `setSingleSelection`<br/>`isSingleSelection` | `false`
**Selection required** | `app:selectionRequired` | `setSelectionRequired`<br/>`isSelectionRequired` | `false`
**Enable the group and all children** | `android:enabled` | `setEnabled`<br/>`isEnabled` | `true`
**Size of inner corners** | `app:innerCornerSize` | `setInnerCornerSize`<br/>`getInnerCornerSize` | `0dp`
**Spacing between buttons** | `android:spacing` | `setSpacing`<br/>`getSpacing` | `0dp`

#### Styles

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public int compare(MaterialButton v1, MaterialButton v2) {
private boolean singleSelection;
private boolean selectionRequired;

@NonNull private CornerSize insideCornerSize;
@NonNull private CornerSize innerCornerSize;
@Px private int spacing;

@IdRes private final int defaultCheckId;
Expand Down Expand Up @@ -210,10 +210,10 @@ public MaterialButtonToggleGroup(
attributes.getResourceId(R.styleable.MaterialButtonToggleGroup_checkedButton, View.NO_ID);
selectionRequired =
attributes.getBoolean(R.styleable.MaterialButtonToggleGroup_selectionRequired, false);
insideCornerSize =
innerCornerSize =
ShapeAppearanceModel.getCornerSize(
attributes,
R.styleable.MaterialButtonToggleGroup_insideCornerSize,
R.styleable.MaterialButtonToggleGroup_innerCornerSize,
new AbsoluteCornerSize(0));
spacing =
attributes.getDimensionPixelSize(R.styleable.MaterialButtonToggleGroup_android_spacing, 0);
Expand Down Expand Up @@ -506,25 +506,42 @@ public void setSingleSelection(@BoolRes int id) {
setSingleSelection(getResources().getBoolean(id));
}

/**
* Returns the spacing (in pixels) between each button in the group.
*/
@Px
public int getSpacing() {
return spacing;
}

/**
* Sets the spacing between each button in the group.
*
* @param spacing the spacing (in pixels) between each button in the group
*/
public void setSpacing(@Px int spacing) {
this.spacing = spacing;
invalidate();
requestLayout();
}

public void setInsideCornerSizeInPx(@Px int px) {
insideCornerSize = new AbsoluteCornerSize(px);
updateChildShapes();
invalidate();
/** Returns the inner corner size of the group. */
@NonNull
public CornerSize getInnerCornerSize() {
return innerCornerSize;
}

public void setInsideCornerSizeInFraction(float fraction) {
insideCornerSize = new RelativeCornerSize(fraction);
/**
* Sets the inner corner size of the group.
*
* <p>Can set as an {@link AbsoluteCornerSize} or {@link RelativeCornerSize}. Don't set relative
* corner size larger than 50% or absolute corner size larger than half height to avoid corner
* overlapping.
*
* @param cornerSize the inner corner size of the group
*/
public void setInnerCornerSize(@NonNull CornerSize cornerSize) {
innerCornerSize = cornerSize;
updateChildShapes();
invalidate();
}
Expand Down Expand Up @@ -683,7 +700,7 @@ private int getIndexWithinVisibleButtons(@Nullable View child) {
private CornerData getNewCornerData(
int index, int firstVisibleChildIndex, int lastVisibleChildIndex) {
CornerData cornerData = originalCornerData.get(index);
CornerData insideCornerData = new CornerData(insideCornerSize);
CornerData innerCornerData = new CornerData(innerCornerSize);

// If only one (visible) child exists, use its original corners
if (firstVisibleChildIndex == lastVisibleChildIndex) {
Expand All @@ -693,17 +710,17 @@ private CornerData getNewCornerData(
boolean isHorizontal = getOrientation() == HORIZONTAL;
if (index == firstVisibleChildIndex) {
return isHorizontal
? CornerData.start(cornerData, insideCornerData, this)
: CornerData.top(cornerData, insideCornerData);
? CornerData.start(cornerData, innerCornerData, this)
: CornerData.top(cornerData, innerCornerData);
}

if (index == lastVisibleChildIndex) {
return isHorizontal
? CornerData.end(cornerData, insideCornerData, this)
: CornerData.bottom(cornerData, insideCornerData);
? CornerData.end(cornerData, innerCornerData, this)
: CornerData.bottom(cornerData, innerCornerData);
}

return insideCornerData;
return innerCornerData;
}

private static void updateBuilderWithCornerData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<public name="iconTint" type="attr"/>
<public name="iconTintMode" type="attr"/>
<public name="toggleCheckedStateOnClick" type="attr"/>
<public name="insideCornerSize" type="attr"/>
<public name="innerCornerSize" type="attr"/>
<public name="materialButtonStyle" type="attr"/>
<public name="materialIconButtonStyle" type="attr"/>
<public name="materialIconButtonFilledStyle" type="attr"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
<attr name="checkedButton" format="reference"/>
<!-- Whether this MaterialButtonToggleGroup and all its children are enabled -->
<attr name="android:enabled" />
<!-- The radius of all inside corners between inclusive buttons. -->
<attr name="insideCornerSize" format="dimension|fraction"/>
<!-- The radius of all inner corners between inclusive buttons. -->
<attr name="innerCornerSize" format="dimension|fraction"/>
<!-- The spacing between inclusive buttons. Outlines will overlap, when set to zero or negative. -->
<attr name="android:spacing"/>
</declare-styleable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import android.widget.ToggleButton;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.material.button.MaterialButtonToggleGroup.OnButtonCheckedListener;
import com.google.android.material.shape.AbsoluteCornerSize;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.util.List;
import org.junit.Before;
Expand Down Expand Up @@ -84,13 +85,12 @@ public void correctShapeAppearances_inToggle_afterAdding() {
}

@Test
public void correctShapeAppearances_inToggle_afterAdding_withInsideCorner() {
public void correctShapeAppearances_inToggle_afterAdding_withInnerCorner() {
MaterialButton firstChild = (MaterialButton) toggleGroup.getChildAt(0);
MaterialButton middleChild = (MaterialButton) toggleGroup.getChildAt(1);
MaterialButton lastChild = (MaterialButton) toggleGroup.getChildAt(2);

toggleGroup.setInsideCornerSizeInPx(5);
toggleGroup.updateChildShapes();
toggleGroup.setInnerCornerSize(new AbsoluteCornerSize(5));
assertShapeAppearance(firstChild.getShapeAppearanceModel(), CORNER_SIZE, CORNER_SIZE, 5, 5);
assertShapeAppearance(middleChild.getShapeAppearanceModel(), 5, 5, 5, 5);
assertShapeAppearance(lastChild.getShapeAppearanceModel(), 5, 5, CORNER_SIZE, CORNER_SIZE);
Expand All @@ -110,14 +110,13 @@ public void correctShapeAppearances_inToggle_afterAddingInVertical() {
}

@Test
public void correctShapeAppearances_inToggle_afterAddingInVertical_withInsideCorner() {
public void correctShapeAppearances_inToggle_afterAddingInVertical_withInnerCorner() {
toggleGroup.setOrientation(LinearLayout.VERTICAL);
MaterialButton firstChild = (MaterialButton) toggleGroup.getChildAt(0);
MaterialButton middleChild = (MaterialButton) toggleGroup.getChildAt(1);
MaterialButton lastChild = (MaterialButton) toggleGroup.getChildAt(2);

toggleGroup.setInsideCornerSizeInPx(5);
toggleGroup.updateChildShapes();
toggleGroup.setInnerCornerSize(new AbsoluteCornerSize(5));
assertShapeAppearance(firstChild.getShapeAppearanceModel(), CORNER_SIZE, 5, CORNER_SIZE, 5);
assertShapeAppearance(middleChild.getShapeAppearanceModel(), 5, 5, 5, 5);
assertShapeAppearance(lastChild.getShapeAppearanceModel(), 5, CORNER_SIZE, 5, CORNER_SIZE);
Expand All @@ -139,15 +138,14 @@ public void correctShapeAppearances_inToggle_afterSettingViewToGone() {
}

@Test
public void correctShapeAppearances_inToggle_afterSettingViewToGone_withInsideCorner() {
public void correctShapeAppearances_inToggle_afterSettingViewToGone_withInnerCorner() {
toggleGroup.setOrientation(LinearLayout.VERTICAL);
MaterialButton firstChild = (MaterialButton) toggleGroup.getChildAt(0);
MaterialButton middleChild = (MaterialButton) toggleGroup.getChildAt(1);
MaterialButton lastChild = (MaterialButton) toggleGroup.getChildAt(2);

firstChild.setVisibility(GONE);
toggleGroup.setInsideCornerSizeInPx(5);
toggleGroup.updateChildShapes();
toggleGroup.setInnerCornerSize(new AbsoluteCornerSize(5));

// Now middle and end child has rounded corners.
assertShapeAppearance(middleChild.getShapeAppearanceModel(), CORNER_SIZE, 5, CORNER_SIZE, 5);
Expand All @@ -169,15 +167,14 @@ public void correctShapeAppearances_inToggle_whenOneVisibleButton() {
}

@Test
public void correctShapeAppearances_inToggle_whenOneVisibleButton_withInsideCorner() {
public void correctShapeAppearances_inToggle_whenOneVisibleButton_withInnerCorner() {
MaterialButton firstChild = (MaterialButton) toggleGroup.getChildAt(0);
MaterialButton middleChild = (MaterialButton) toggleGroup.getChildAt(1);
MaterialButton lastChild = (MaterialButton) toggleGroup.getChildAt(2);

firstChild.setVisibility(GONE);
middleChild.setVisibility(GONE);
toggleGroup.setInsideCornerSizeInPx(5);
toggleGroup.updateChildShapes();
toggleGroup.setInnerCornerSize(new AbsoluteCornerSize(5));
// Last child has default shape appearance.
assertShapeAppearance(
lastChild.getShapeAppearanceModel(), CORNER_SIZE, CORNER_SIZE, CORNER_SIZE, CORNER_SIZE);
Expand Down

0 comments on commit 63bb499

Please sign in to comment.