42
42
import com .google .android .material .ripple .RippleUtils ;
43
43
import com .google .android .material .shape .MaterialShapeDrawable ;
44
44
import com .google .android .material .shape .MaterialShapeDrawable .OnCornerSizeChangeListener ;
45
+ import com .google .android .material .shape .ShapeAppearance ;
45
46
import com .google .android .material .shape .ShapeAppearanceModel ;
46
47
import com .google .android .material .shape .Shapeable ;
47
48
import com .google .android .material .shape .StateListShapeAppearanceModel ;
51
52
class MaterialButtonHelper {
52
53
53
54
private final MaterialButton materialButton ;
54
- @ NonNull private ShapeAppearanceModel shapeAppearanceModel ;
55
- @ Nullable private StateListShapeAppearanceModel stateListShapeAppearanceModel ;
55
+ @ NonNull private ShapeAppearance shapeAppearance ;
56
56
@ Nullable private SpringForce cornerSpringForce ;
57
57
@ Nullable private OnCornerSizeChangeListener onCornerSizeChangeListener ;
58
58
@@ -77,9 +77,9 @@ class MaterialButtonHelper {
77
77
private LayerDrawable rippleDrawable ;
78
78
private int elevation ;
79
79
80
- MaterialButtonHelper (MaterialButton button , @ NonNull ShapeAppearanceModel shapeAppearanceModel ) {
80
+ MaterialButtonHelper (MaterialButton button , @ NonNull ShapeAppearance shapeAppearance ) {
81
81
materialButton = button ;
82
- this .shapeAppearanceModel = shapeAppearanceModel ;
82
+ this .shapeAppearance = shapeAppearance ;
83
83
}
84
84
85
85
void loadFromAttributes (@ NonNull TypedArray attributes ) {
@@ -93,7 +93,7 @@ void loadFromAttributes(@NonNull TypedArray attributes) {
93
93
// cornerRadius should override whatever corner radius is set in shapeAppearanceModel
94
94
if (attributes .hasValue (R .styleable .MaterialButton_cornerRadius )) {
95
95
cornerRadius = attributes .getDimensionPixelSize (R .styleable .MaterialButton_cornerRadius , -1 );
96
- setShapeAppearanceModel ( shapeAppearanceModel .withCornerSize (cornerRadius ));
96
+ setShapeAppearance ( shapeAppearance .withCornerSize (cornerRadius ));
97
97
cornerRadiusSet = true ;
98
98
}
99
99
@@ -208,10 +208,7 @@ void setShouldDrawSurfaceColorStroke(boolean shouldDrawSurfaceColorStroke) {
208
208
* @return Drawable representing background for this button.
209
209
*/
210
210
private Drawable createBackground () {
211
- MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable (shapeAppearanceModel );
212
- if (stateListShapeAppearanceModel != null ) {
213
- backgroundDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
214
- }
211
+ MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable (shapeAppearance );
215
212
if (cornerSpringForce != null ) {
216
213
backgroundDrawable .setCornerSpringForce (cornerSpringForce );
217
214
}
@@ -226,11 +223,7 @@ private Drawable createBackground() {
226
223
}
227
224
backgroundDrawable .setStroke (strokeWidth , strokeColor );
228
225
229
- MaterialShapeDrawable surfaceColorStrokeDrawable =
230
- new MaterialShapeDrawable (shapeAppearanceModel );
231
- if (stateListShapeAppearanceModel != null ) {
232
- surfaceColorStrokeDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
233
- }
226
+ MaterialShapeDrawable surfaceColorStrokeDrawable = new MaterialShapeDrawable (shapeAppearance );
234
227
if (cornerSpringForce != null ) {
235
228
surfaceColorStrokeDrawable .setCornerSpringForce (cornerSpringForce );
236
229
}
@@ -241,11 +234,7 @@ private Drawable createBackground() {
241
234
? MaterialColors .getColor (materialButton , R .attr .colorSurface )
242
235
: Color .TRANSPARENT );
243
236
244
- maskDrawable = new MaterialShapeDrawable (shapeAppearanceModel );
245
- if (stateListShapeAppearanceModel != null ) {
246
- ((MaterialShapeDrawable ) maskDrawable )
247
- .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
248
- }
237
+ maskDrawable = new MaterialShapeDrawable (shapeAppearance );
249
238
if (cornerSpringForce != null ) {
250
239
((MaterialShapeDrawable ) maskDrawable ).setCornerSpringForce (cornerSpringForce );
251
240
}
@@ -332,7 +321,7 @@ void setCornerRadius(int cornerRadius) {
332
321
this .cornerRadius = cornerRadius ;
333
322
cornerRadiusSet = true ;
334
323
335
- setShapeAppearanceModel ( shapeAppearanceModel .withCornerSize (cornerRadius ));
324
+ setShapeAppearance ( shapeAppearance .withCornerSize (cornerRadius ));
336
325
}
337
326
}
338
327
@@ -402,37 +391,28 @@ private void updateButtonShape() {
402
391
} else {
403
392
MaterialShapeDrawable backgroundDrawable = getMaterialShapeDrawable ();
404
393
if (backgroundDrawable != null ) {
405
- if (stateListShapeAppearanceModel != null ) {
406
- backgroundDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
407
- } else {
408
- backgroundDrawable .setShapeAppearanceModel (shapeAppearanceModel );
409
- }
394
+ backgroundDrawable .setShapeAppearance (shapeAppearance );
410
395
if (cornerSpringForce != null ) {
411
396
backgroundDrawable .setCornerSpringForce (cornerSpringForce );
412
397
}
413
398
}
414
399
MaterialShapeDrawable strokeDrawable = getSurfaceColorStrokeDrawable ();
415
400
if (strokeDrawable != null ) {
416
- if (stateListShapeAppearanceModel != null ) {
417
- strokeDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
418
- } else {
419
- strokeDrawable .setShapeAppearanceModel (shapeAppearanceModel );
420
- }
401
+ strokeDrawable .setShapeAppearance (shapeAppearance );
421
402
if (cornerSpringForce != null ) {
422
403
strokeDrawable .setCornerSpringForce (cornerSpringForce );
423
404
}
424
405
}
425
406
Shapeable animatedShapeable = getMaskDrawable ();
426
407
if (animatedShapeable != null ) {
427
- animatedShapeable .setShapeAppearanceModel (shapeAppearanceModel );
428
408
if (animatedShapeable instanceof MaterialShapeDrawable ) {
429
409
MaterialShapeDrawable maskDrawable = (MaterialShapeDrawable ) animatedShapeable ;
430
- if (stateListShapeAppearanceModel != null ) {
431
- maskDrawable .setStateListShapeAppearanceModel (stateListShapeAppearanceModel );
432
- }
410
+ maskDrawable .setShapeAppearance (shapeAppearance );
433
411
if (cornerSpringForce != null ) {
434
412
maskDrawable .setCornerSpringForce (cornerSpringForce );
435
413
}
414
+ } else {
415
+ animatedShapeable .setShapeAppearanceModel (shapeAppearance .getDefaultShape ());
436
416
}
437
417
}
438
418
}
@@ -455,7 +435,7 @@ public Shapeable getMaskDrawable() {
455
435
void setCornerSpringForce (@ NonNull SpringForce springForce ) {
456
436
this .cornerSpringForce = springForce ;
457
437
// We don't want to set unused spring objects.
458
- if (stateListShapeAppearanceModel != null ) {
438
+ if (shapeAppearance instanceof StateListShapeAppearanceModel ) {
459
439
updateButtonShape ();
460
440
}
461
441
}
@@ -465,26 +445,19 @@ SpringForce getCornerSpringForce() {
465
445
return this .cornerSpringForce ;
466
446
}
467
447
468
- void setStateListShapeAppearanceModel (
469
- @ NonNull StateListShapeAppearanceModel stateListShapeAppearanceModel ) {
470
- this .stateListShapeAppearanceModel = stateListShapeAppearanceModel ;
448
+ void setShapeAppearance (@ NonNull ShapeAppearance shapeAppearanceModel ) {
449
+ this .shapeAppearance = shapeAppearanceModel ;
471
450
updateButtonShape ();
472
451
}
473
452
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 ;
483
456
}
484
457
485
458
@ NonNull
486
459
ShapeAppearanceModel getShapeAppearanceModel () {
487
- return this . shapeAppearanceModel ;
460
+ return shapeAppearance . getDefaultShape () ;
488
461
}
489
462
490
463
public void setInsetBottom (@ Dimension int newInsetBottom ) {
0 commit comments