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

Implement Padding property in ImageButtonHandler #4665

Merged
merged 14 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,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 @@ -210,6 +210,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 @@ -91,6 +91,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 @@ -64,6 +64,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 as UIButton)?.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
17 changes: 17 additions & 0 deletions src/Core/src/Platform/Android/ImageButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,22 @@ public static void UpdateCornerRadius(this ShapeableImageView platformButton, IB
.SetBottomRightCorner(CornerFamily.Rounded, buttonStroke.CornerRadius)
.Build();
}

public static void UpdatePadding(this ShapeableImageView nativeButton, IImageButton imageButton)
{
var context = nativeButton.Context;

if (context == null)
{
return;
}

nativeButton.SetContentPadding(
(int)context.ToPixels(imageButton.Padding.Left),
(int)context.ToPixels(imageButton.Padding.Top),
(int)context.ToPixels(imageButton.Padding.Right),
(int)context.ToPixels(imageButton.Padding.Bottom)
);
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@ Task PerformClick(IButton 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(IButton button)
GetPlatformImageButton(CreateHandler(button)).SendActionForControlEvents(UIControlEvent.TouchUpInside);
});
}

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