Skip to content

Commit

Permalink
Implement Padding property in ImageButtonHandler (#4665)
Browse files Browse the repository at this point in the history
* Implement Padding property in ImageButtonHandler

* Added Device test

* Renamed NativeView to PlatformView

* More renaming from Native to Platform

* Renamed NativeView by PlatformView in tests

* Update ImageButton Padding update logic in Android

* Fix device tests

Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
jsuarezruiz and rmarinho authored Mar 28, 2022
1 parent 5c0a286 commit a270ebd
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public override bool OnTouchEvent(MotionEvent e)
return base.OnTouchEvent(e);
}


[PortHandler]
void UpdatePadding()
{
SetPadding(
Expand Down
2 changes: 2 additions & 0 deletions src/Compatibility/Core/src/Windows/ImageButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ protected override async void OnElementPropertyChanged(object sender, PropertyCh
else if (e.PropertyName == ImageButton.SourceProperty.PropertyName)
await TryUpdateSource().ConfigureAwait(false);
}

[PortHandler]
void UpdatePadding()
{
_image.Margin = WinUIHelpers.CreateThickness(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected async override void OnElementChanged(ElementChangedEventArgs<ImageButt
}
}

[PortHandler]
void UpdatePadding(UIButton button = null)
{
var uiElement = button ?? Control;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static void MapCornerRadius(IImageButtonHandler handler, IButtonStroke bu
(handler.PlatformView as ShapeableImageView)?.UpdateCornerRadius(buttonStroke);
}

public static void MapPadding(IImageButtonHandler handler, IImageButton imageButton)
{
(handler.PlatformView as ShapeableImageView)?.UpdatePadding(imageButton);
}

void OnFocusChange(object? sender, View.FocusChangeEventArgs e)
{
if (VirtualView != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed partial class ImageButtonHandler : ViewHandler<IImageButton, objec
public static void MapStrokeColor(IImageButtonHandler handler, IButtonStroke buttonStroke) { }
public static void MapStrokeThickness(IImageButtonHandler handler, IButtonStroke buttonStroke) { }
public static void MapCornerRadius(IImageButtonHandler handler, IButtonStroke buttonStroke) { }
public static void MapPadding(IImageButtonHandler handler, IImageButton imageButton) { }

void OnSetImageSource(object? obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public static void MapBackground(IImageButtonHandler handler, IImageButton image
(handler.PlatformView as Button)?.UpdateBackground(imageButton);
}

public static void MapPadding(IImageButtonHandler handler, IImageButton imageButton)
{
(handler.PlatformView as Button)?.UpdatePadding(imageButton);
}

void OnSetImageSource(ImageSource? nativeImageSource)
{
PlatformView.UpdateImageSource(nativeImageSource);
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class ImageButtonHandler : IImageButtonHandler
[nameof(IButtonStroke.StrokeThickness)] = MapStrokeThickness,
[nameof(IButtonStroke.StrokeColor)] = MapStrokeColor,
[nameof(IButtonStroke.CornerRadius)] = MapCornerRadius,
[nameof(IImageButton.Padding)] = MapPadding,
#if WINDOWS
[nameof(IImageButton.Background)] = MapBackground,
#endif
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public static void MapCornerRadius(IImageButtonHandler handler, IButtonStroke bu
handler.PlatformView?.UpdateCornerRadius(buttonStroke);
}

public static void MapPadding(IImageButtonHandler handler, IImageButton imageButton)
{
(handler.PlatformView as UIButton)?.UpdatePadding(imageButton);
}

void OnButtonTouchUpInside(object? sender, EventArgs e)
{
VirtualView?.Released();
Expand Down
24 changes: 24 additions & 0 deletions src/Core/src/Platform/Android/ImageButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,29 @@ public static void UpdateCornerRadius(this ShapeableImageView platformButton, IB
.SetBottomRightCorner(CornerFamily.Rounded, radius)
.Build();
}

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);
});
}

internal static void SetContentPadding(this ShapeableImageView platformButton, IImageButton imageButton)
{
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)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ Task PerformClick(IImageButton button)
GetPlatformImageButton(CreateHandler(button)).PerformClick();
});
}

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

return new Thickness(
shapeableImageView.ContentPaddingLeft,
shapeableImageView.ContentPaddingTop,
shapeableImageView.ContentPaddingRight,
shapeableImageView.ContentPaddingBottom);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Microsoft.Maui.DeviceTests
[Category(TestCategory.ImageButton)]
public partial class ImageButtonHandlerTests : HandlerTestBase<ImageButtonHandler, ImageButtonStub>
{
const int Precision = 4;

[Fact(DisplayName = "Click event fires Correctly")]
public async Task ClickEventFires()
{
Expand All @@ -29,6 +31,38 @@ public async Task ClickEventFires()
Assert.True(clicked);
}

[Theory(DisplayName = "Padding Initializes Correctly")]
[InlineData(0, 0, 0, 0)]
[InlineData(1, 1, 1, 1)]
[InlineData(10, 10, 10, 10)]
[InlineData(5, 10, 15, 20)]
public async Task PaddingInitializesCorrectly(double left, double top, double right, double bottom)
{
var user = new Thickness(left, top, right, bottom);

var button = new ImageButtonStub
{
Padding = user
};

var (expected, native) = await GetValueAsync(button, handler =>
{
var native = GetNativePadding(handler);
var scaled = user;

#if __ANDROID__
scaled = handler.PlatformView.Context!.ToPixels(scaled);
#endif

return (scaled, native);
});

Assert.Equal(expected.Left, native.Left, Precision);
Assert.Equal(expected.Top, native.Top, Precision);
Assert.Equal(expected.Right, native.Right, Precision);
Assert.Equal(expected.Bottom, native.Bottom, Precision);
}

[Category(TestCategory.ImageButton)]
public partial class ImageButtonImageHandlerTests : ImageHandlerTests<ImageButtonHandler, ImageButtonStub>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public async Task StrokeColorInitializesCorrectly()
Assert.Equal(expectedValue, values.PlatformViewValue);
}

UIButton GetPlatformImageButton(ImageButtonHandler imageButtonHandler) =>
imageButtonHandler.PlatformView;
UIButton GetPlatformImageButton(ImageButtonHandler imageButtonHandler) =>
imageButtonHandler.PlatformView;

UIColor GetNativeStrokeColor(ImageButtonHandler imageButtonHandler)
{
Expand All @@ -60,5 +60,8 @@ Task PerformClick(IImageButton button)
GetPlatformImageButton(CreateHandler(button)).SendActionForControlEvents(UIControlEvent.TouchUpInside);
});
}

UIEdgeInsets GetNativePadding(ImageButtonHandler imageButtonHandler) =>
GetPlatformImageButton(imageButtonHandler).ContentEdgeInsets;
}
}

0 comments on commit a270ebd

Please sign in to comment.