4242import com .google .android .material .ripple .RippleUtils ;
4343import com .google .android .material .shape .MaterialShapeDrawable ;
4444import com .google .android .material .shape .MaterialShapeDrawable .OnCornerSizeChangeListener ;
45+ import com .google .android .material .shape .ShapeAppearance ;
4546import com .google .android .material .shape .ShapeAppearanceModel ;
4647import com .google .android .material .shape .Shapeable ;
4748import com .google .android .material .shape .StateListShapeAppearanceModel ;
5152class MaterialButtonHelper {
5253
5354 private final MaterialButton materialButton ;
54- @ NonNull private ShapeAppearanceModel shapeAppearanceModel ;
55- @ Nullable private StateListShapeAppearanceModel stateListShapeAppearanceModel ;
55+ @ NonNull private ShapeAppearance shapeAppearance ;
5656 @ Nullable private SpringForce cornerSpringForce ;
5757 @ Nullable private OnCornerSizeChangeListener onCornerSizeChangeListener ;
5858
@@ -77,9 +77,9 @@ class MaterialButtonHelper {
7777 private LayerDrawable rippleDrawable ;
7878 private int elevation ;
7979
80- MaterialButtonHelper (MaterialButton button , @ NonNull ShapeAppearanceModel shapeAppearanceModel ) {
80+ MaterialButtonHelper (MaterialButton button , @ NonNull ShapeAppearance shapeAppearance ) {
8181 materialButton = button ;
82- this .shapeAppearanceModel = shapeAppearanceModel ;
82+ this .shapeAppearance = shapeAppearance ;
8383 }
8484
8585 void loadFromAttributes (@ NonNull TypedArray attributes ) {
@@ -93,7 +93,7 @@ void loadFromAttributes(@NonNull TypedArray attributes) {
9393 // cornerRadius should override whatever corner radius is set in shapeAppearanceModel
9494 if (attributes .hasValue (R .styleable .MaterialButton_cornerRadius )) {
9595 cornerRadius = attributes .getDimensionPixelSize (R .styleable .MaterialButton_cornerRadius , -1 );
96- setShapeAppearanceModel ( shapeAppearanceModel .withCornerSize (cornerRadius ));
96+ setShapeAppearance ( shapeAppearance .withCornerSize (cornerRadius ));
9797 cornerRadiusSet = true ;
9898 }
9999
@@ -208,10 +208,7 @@ void setShouldDrawSurfaceColorStroke(boolean shouldDrawSurfaceColorStroke) {
208208 * @return Drawable representing background for this button.
209209 */
210210 private Drawable createBackground () {
211- MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable (shapeAppearanceModel );
212- if (stateListShapeAppearanceModel != null ) {
213- backgroundDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
214- }
211+ MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable (shapeAppearance );
215212 if (cornerSpringForce != null ) {
216213 backgroundDrawable .setCornerSpringForce (cornerSpringForce );
217214 }
@@ -226,11 +223,7 @@ private Drawable createBackground() {
226223 }
227224 backgroundDrawable .setStroke (strokeWidth , strokeColor );
228225
229- MaterialShapeDrawable surfaceColorStrokeDrawable =
230- new MaterialShapeDrawable (shapeAppearanceModel );
231- if (stateListShapeAppearanceModel != null ) {
232- surfaceColorStrokeDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
233- }
226+ MaterialShapeDrawable surfaceColorStrokeDrawable = new MaterialShapeDrawable (shapeAppearance );
234227 if (cornerSpringForce != null ) {
235228 surfaceColorStrokeDrawable .setCornerSpringForce (cornerSpringForce );
236229 }
@@ -241,11 +234,7 @@ private Drawable createBackground() {
241234 ? MaterialColors .getColor (materialButton , R .attr .colorSurface )
242235 : Color .TRANSPARENT );
243236
244- maskDrawable = new MaterialShapeDrawable (shapeAppearanceModel );
245- if (stateListShapeAppearanceModel != null ) {
246- ((MaterialShapeDrawable ) maskDrawable )
247- .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
248- }
237+ maskDrawable = new MaterialShapeDrawable (shapeAppearance );
249238 if (cornerSpringForce != null ) {
250239 ((MaterialShapeDrawable ) maskDrawable ).setCornerSpringForce (cornerSpringForce );
251240 }
@@ -332,7 +321,7 @@ void setCornerRadius(int cornerRadius) {
332321 this .cornerRadius = cornerRadius ;
333322 cornerRadiusSet = true ;
334323
335- setShapeAppearanceModel ( shapeAppearanceModel .withCornerSize (cornerRadius ));
324+ setShapeAppearance ( shapeAppearance .withCornerSize (cornerRadius ));
336325 }
337326 }
338327
@@ -402,37 +391,28 @@ private void updateButtonShape() {
402391 } else {
403392 MaterialShapeDrawable backgroundDrawable = getMaterialShapeDrawable ();
404393 if (backgroundDrawable != null ) {
405- if (stateListShapeAppearanceModel != null ) {
406- backgroundDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
407- } else {
408- backgroundDrawable .setShapeAppearanceModel (shapeAppearanceModel );
409- }
394+ backgroundDrawable .setShapeAppearance (shapeAppearance );
410395 if (cornerSpringForce != null ) {
411396 backgroundDrawable .setCornerSpringForce (cornerSpringForce );
412397 }
413398 }
414399 MaterialShapeDrawable strokeDrawable = getSurfaceColorStrokeDrawable ();
415400 if (strokeDrawable != null ) {
416- if (stateListShapeAppearanceModel != null ) {
417- strokeDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
418- } else {
419- strokeDrawable .setShapeAppearanceModel (shapeAppearanceModel );
420- }
401+ strokeDrawable .setShapeAppearance (shapeAppearance );
421402 if (cornerSpringForce != null ) {
422403 strokeDrawable .setCornerSpringForce (cornerSpringForce );
423404 }
424405 }
425406 Shapeable animatedShapeable = getMaskDrawable ();
426407 if (animatedShapeable != null ) {
427- animatedShapeable .setShapeAppearanceModel (shapeAppearanceModel );
428408 if (animatedShapeable instanceof MaterialShapeDrawable ) {
429409 MaterialShapeDrawable maskDrawable = (MaterialShapeDrawable ) animatedShapeable ;
430- if (stateListShapeAppearanceModel != null ) {
431- maskDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
432- }
410+ maskDrawable .setShapeAppearance (shapeAppearance );
433411 if (cornerSpringForce != null ) {
434412 maskDrawable .setCornerSpringForce (cornerSpringForce );
435413 }
414+ } else {
415+ animatedShapeable .setShapeAppearanceModel (shapeAppearance .getDefaultShape ());
436416 }
437417 }
438418 }
@@ -455,7 +435,7 @@ public Shapeable getMaskDrawable() {
455435 void setCornerSpringForce (@ NonNull SpringForce springForce ) {
456436 this .cornerSpringForce = springForce ;
457437 // We don't want to set unused spring objects.
458- if (stateListShapeAppearanceModel != null ) {
438+ if (shapeAppearance instanceof StateListShapeAppearanceModel ) {
459439 updateButtonShape ();
460440 }
461441 }
@@ -465,26 +445,19 @@ SpringForce getCornerSpringForce() {
465445 return this .cornerSpringForce ;
466446 }
467447
468- void setStateListShapeAppearanceModel (
469- @ NonNull StateListShapeAppearanceModel stateListShapeAppearanceModel ) {
470- this .stateListShapeAppearanceModel = stateListShapeAppearanceModel ;
448+ void setShapeAppearance (@ NonNull ShapeAppearance shapeAppearanceModel ) {
449+ this .shapeAppearance = shapeAppearanceModel ;
471450 updateButtonShape ();
472451 }
473452
474- @ Nullable
475- StateListShapeAppearanceModel getStateListShapeAppearanceModel () {
476- return this .stateListShapeAppearanceModel ;
477- }
478-
479- void setShapeAppearanceModel (@ NonNull ShapeAppearanceModel shapeAppearanceModel ) {
480- this .shapeAppearanceModel = shapeAppearanceModel ;
481- this .stateListShapeAppearanceModel = null ;
482- updateButtonShape ();
453+ @ NonNull
454+ ShapeAppearance getShapeAppearance () {
455+ return shapeAppearance ;
483456 }
484457
485458 @ NonNull
486459 ShapeAppearanceModel getShapeAppearanceModel () {
487- return this . shapeAppearanceModel ;
460+ return shapeAppearance . getDefaultShape () ;
488461 }
489462
490463 public void setInsetBottom (@ Dimension int newInsetBottom ) {
0 commit comments