From 7a0db78be9ec537345cf487971f5161685078273 Mon Sep 17 00:00:00 2001 From: Matt Lacey Date: Wed, 29 May 2019 01:53:17 +0100 Subject: [PATCH] Issue4879 ImageButton Padding (UWP) (#5837) * Apply ImageButton.Padding on UWP like other platforms For #4879 * Apply ImageButton.Padding on UWP like other platforms For #4879 * Apply ImageButton.Padding on UWP like other platforms For #4879 * don't use obsolete property after rebasing to 4.0 fixes #4879 --- .../Issue4879.cs | 86 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../ImageButtonRenderer.cs | 14 +-- 3 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue4879.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue4879.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue4879.cs new file mode 100644 index 000000000..d7a5997a7 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue4879.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using NUnit.Framework; +using Xamarin.UITest; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 4879, "4879 - ImageButtonPadding", PlatformAffected.UWP)] + public class Issue4879 : TestContentPage + { + protected override async void Init() + { + await Navigation.PushModalAsync(new Issue4879Page()); + } + + public class Issue4879Page : ContentPage + { + public Issue4879Page() + { + Button b = new Button + { + HorizontalOptions = LayoutOptions.End, + VerticalOptions = LayoutOptions.End, + ImageSource = "coffee.png", + Padding = new Thickness(10), + BackgroundColor = Color.Green + }; + // Add BorderWidth to ImageButtons to match border of Button and allow for easier size comparisons + ImageButton ib1 = new ImageButton + { + HorizontalOptions = LayoutOptions.Start, + VerticalOptions = LayoutOptions.End, + BorderWidth = 2, + Source = "coffee.png", + Padding = new Thickness(10), + BackgroundColor = Color.Purple + }; + ImageButton ib2 = new ImageButton + { + HorizontalOptions = LayoutOptions.End, + VerticalOptions = LayoutOptions.Start, + BorderWidth = 2, + Source = "coffee.png", + Padding = new Thickness(10), + BackgroundColor = Color.Red + }; + Grid mainG = new Grid + { + ColumnDefinitions = { + new ColumnDefinition { Width = GridLength.Star }, + new ColumnDefinition { Width = GridLength.Star } + }, + RowDefinitions = { + new RowDefinition { Height = GridLength.Star }, + new RowDefinition { Height = GridLength.Star } + } + }; + + // Green Button top left + // Purple ImageButton top right to compare height + // Red ImageButton bottom left to compare width + mainG.Children.Add(b, 0, 0); + mainG.Children.Add(ib1, 1, 0); + mainG.Children.Add(ib2, 0, 1); + Content = mainG; + } + } + +#if UITEST + [Test] + [Category(UITestCategories.ManualReview)] + public void Issue4879Test() + { + RunningApp.Screenshot("I am at Issue 4879 - All buttons/images should be the same size."); + } +#endif + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 231b5077a..b25fb45ed 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Xamarin.Forms.Platform.UAP/ImageButtonRenderer.cs b/Xamarin.Forms.Platform.UAP/ImageButtonRenderer.cs index 977e81b47..5e950c230 100644 --- a/Xamarin.Forms.Platform.UAP/ImageButtonRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/ImageButtonRenderer.cs @@ -54,13 +54,16 @@ namespace Xamarin.Forms.Platform.UWP _measured = true; - // we have to include the padding, otherwise the image is smaller than expected - var padding = new Size(Element.Padding.HorizontalThickness, Element.Padding.VerticalThickness); + // The size needs to be the entire size needed for the button (including padding, borders, etc.) + // Not just the size of the image. + var btn = Control; + btn.Measure(new Windows.Foundation.Size(widthConstraint, heightConstraint)); - return new SizeRequest(_image.Source.GetImageSourceSize() + padding); + var size = new Size(Math.Ceiling(btn.DesiredSize.Width), Math.Ceiling(btn.DesiredSize.Height)); + + return new SizeRequest(size); } - protected async override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); @@ -205,7 +208,8 @@ namespace Xamarin.Forms.Platform.UWP { _image.Margin = new WThickness(0); - Control.Padding = new WThickness( + // Apply the padding to the containing button, not the image + _formsButton.Padding = new WThickness( Element.Padding.Left, Element.Padding.Top, Element.Padding.Right,