Skip to content

Commit

Permalink
Make sure ImageButton always has a background (dotnet#22717)
Browse files Browse the repository at this point in the history
The OS does not specify a background so we need to make sure there is
a transparent background to get the ripple and border.
  • Loading branch information
mattleibow authored Jun 5, 2024
1 parent 719b41c commit 6d42e7c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/Core/src/Platform/Android/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ internal static void UpdateButtonStroke(this MaterialButton platformView, IButto

internal static void UpdateButtonBackground(this MaterialButton platformView, IButton button)
{
platformView.UpdateMauiRippleDrawableBackground(button,
platformView.UpdateMauiRippleDrawableBackground(
button.Background,
button,
() =>
{
// Copy the tints from a temporary button.
Expand Down
5 changes: 4 additions & 1 deletion src/Core/src/Platform/Android/ImageButtonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Google.Android.Material.ImageView;
using Google.Android.Material.Shape;
using Microsoft.Maui.Graphics;

namespace Microsoft.Maui.Platform
{
Expand Down Expand Up @@ -76,7 +77,9 @@ internal static void UpdateButtonStroke(this ShapeableImageView platformView, IB

internal static void UpdateButtonBackground(this ShapeableImageView platformView, IImageButton button)
{
platformView.UpdateMauiRippleDrawableBackground(button,
platformView.UpdateMauiRippleDrawableBackground(
button.Background ?? new SolidPaint(Colors.Transparent), // transparent to force some background
button,
beforeSet: () =>
{
// We have a background, so we need to remove the things that were set on the
Expand Down
11 changes: 5 additions & 6 deletions src/Core/src/Platform/Android/MauiRippleDrawableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ internal static bool UpdateMauiRippleDrawableStroke(this AView platformView, IBu
return true;
}

internal static void UpdateMauiRippleDrawableBackground<TButton>(this AView platformView, TButton button,
internal static void UpdateMauiRippleDrawableBackground(this AView platformView,
Paint? background,
IButtonStroke stroke,
Func<int?>? getEmptyBackgroundColor = null,
Func<ColorStateList?>? getDefaultRippleColor = null,
Action? beforeSet = null)
where TButton : IButtonStroke, IView
{
var background = button.Background;

// Get the current background
var recreateRipple = false;
if (!platformView.TryGetMauiBackground(out var rippleDrawable, out var layerDrawable, out _, out var strokeDrawable, out _))
Expand All @@ -74,7 +73,7 @@ internal static void UpdateMauiRippleDrawableBackground<TButton>(this AView plat
recreateRipple = true;
}

var (width, color, radius) = button.GetStrokeProperties(platformView.Context!, false);
var (width, color, radius) = stroke.GetStrokeProperties(platformView.Context!, false);

// The previous background may have had transparency or a gradient which cannot
// be un-set, so we need an entirely new drawable.
Expand Down Expand Up @@ -132,7 +131,7 @@ internal static void UpdateMauiRippleDrawableBackground<TButton>(this AView plat
// Create the stroke layer.
strokeDrawable = new GradientDrawable();
strokeDrawable.SetCornerRadius(radius);
var strokeColor = button.StrokeColor ?? Colors.Black;
var strokeColor = stroke.StrokeColor ?? Colors.Black;
var strokeColorList = ColorStateListExtensions.CreateButton(strokeColor.ToPlatform());
strokeDrawable.SetStroke(width, strokeColorList);

Expand Down

0 comments on commit 6d42e7c

Please sign in to comment.