Skip to content

Commit

Permalink
[Android] Changes updating ImageButton Padding to avoid size issues (#…
Browse files Browse the repository at this point in the history
…14905)

* Changes updating Android ImageButton Padding

* Updated tests

* Auto-format source code

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
  • Loading branch information
jsuarezruiz and GitHub Actions Autoformatter committed May 9, 2023
1 parent 1ab0c63 commit 1bbe79d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>
<Label
Text="Padding"
Style="{StaticResource Headline}"/>
<ImageButton
HorizontalOptions="Center"
Clicked="OnImageButtonClicked"
Aspect="AspectFit"
Background="Green"
BorderColor="Red"
BorderWidth="10"
Source="cog.png"
Padding="0" />
<ImageButton
HorizontalOptions="Center"
Clicked="OnImageButtonClicked"
Aspect="AspectFit"
Background="Green"
BorderColor="Red"
BorderWidth="10"
Source="cog.png"
Padding="{Binding Source={x:Reference PaddingSlider}, Path=Value}" />
<Slider
x:Name="PaddingSlider"
Minimum="0"
Maximum="60"
Value =" 10"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
Expand Down
35 changes: 24 additions & 11 deletions src/Core/src/Platform/Android/ImageButtonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Google.Android.Material.ImageView;
using Android.Graphics.Drawables;
using Android.Widget;
using Google.Android.Material.ImageView;
using Google.Android.Material.Shape;
using Microsoft.Maui.Graphics;

Expand Down Expand Up @@ -42,25 +44,36 @@ public static void UpdateCornerRadius(this ShapeableImageView platformButton, IB
public static void UpdatePadding(this ShapeableImageView platformButton, IImageButton imageButton)
{
platformButton.SetContentPadding(imageButton);

// NOTE(jpr): post on handler to get around an Android Framework bug.
// see: https://github.com/material-components/material-components-android/issues/2063
platformButton.Post(() =>
{
platformButton.SetContentPadding(imageButton);
});
platformButton.SetContentPadding(imageButton);
}

internal static void SetContentPadding(this ShapeableImageView platformButton, IImageButton imageButton)
{
var padding = imageButton.Padding;
var imageView = platformButton as ImageView;

if (imageView is not null)
{
var bitmapDrawable = imageView.Drawable as BitmapDrawable;

// Without ImageSource we do not apply Padding, although since there is no content
// there are no differences.
if (bitmapDrawable is null)
return;

var backgroundBounds = bitmapDrawable.Bounds;

var padding = imageButton.Padding;

platformButton.SetContentPadding(
(int)platformButton.Context.ToPixels(padding.Left),
(int)platformButton.Context.ToPixels(padding.Top),
(int)platformButton.Context.ToPixels(padding.Right),
(int)platformButton.Context.ToPixels(padding.Bottom)
);
bitmapDrawable.SetBounds(
backgroundBounds.Left + (int)platformButton.Context.ToPixels(padding.Left),
backgroundBounds.Top + (int)platformButton.Context.ToPixels(padding.Top),
backgroundBounds.Right - (int)platformButton.Context.ToPixels(padding.Right),
backgroundBounds.Bottom - (int)platformButton.Context.ToPixels(padding.Bottom));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ Task PerformClick(IImageButton button)
});
}

Thickness GetNativePadding(ImageButtonHandler imageButtonHandler)
{
var shapeableImageView = GetPlatformImageButton(imageButtonHandler);

return new Thickness(
shapeableImageView.ContentPaddingLeft,
shapeableImageView.ContentPaddingTop,
shapeableImageView.ContentPaddingRight,
shapeableImageView.ContentPaddingBottom);
}

bool ImageSourceLoaded(ImageButtonHandler imageButtonHandler) =>
imageButtonHandler.PlatformView.Drawable != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public async Task LoadingCompletedEventFires()
Assert.True(loadingCompleted);
}

#if IOS || MACCATALYST
[Theory(DisplayName = "Padding Initializes Correctly")]
[InlineData(0, 0, 0, 0)]
[InlineData(1, 1, 1, 1)]
Expand All @@ -114,10 +115,6 @@ public async Task PaddingInitializesCorrectly(double left, double top, double ri
var native = GetNativePadding(handler);
var scaled = user;
#if __ANDROID__
scaled = handler.PlatformView.Context!.ToPixels(scaled);
#endif
return (scaled, native);
});

Expand All @@ -126,6 +123,7 @@ public async Task PaddingInitializesCorrectly(double left, double top, double ri
Assert.Equal(expected.Right, native.Right, Precision);
Assert.Equal(expected.Bottom, native.Bottom, Precision);
}
#endif

[Category(TestCategory.ImageButton)]
public partial class ImageButtonImageHandlerTests : ImageHandlerTests<ImageButtonHandler, ImageButtonStub>
Expand Down

0 comments on commit 1bbe79d

Please sign in to comment.