Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure ImageButton always has a background #22717

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same default color applied to Button?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this ever was the case, so it may be a breaking change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it always Transparent ? Should we grab the color from the theme here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me test OG net8 and see. I am sure I have always seen transparent because this is not a button, it is an image view. I will also test net7.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested OG 8.0.3 and there is no background. Same with 7.0.101+4.

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
Loading