Skip to content

Commit

Permalink
[Windows] Fix image button scaling (dotnet#20949)
Browse files Browse the repository at this point in the history
* Cherry pick Windows only changes for fix-18242

* Revert change to csproj

---------

Co-authored-by: Mike Corsaro <[email protected]>
  • Loading branch information
Foda and Mike Corsaro authored Mar 1, 2024
1 parent d77b18f commit 5ad30b6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue18242"
Title="Issue 18242">
<VerticalStackLayout
Padding="12">
<Label
AutomationId="WaitForStubControl"
Text="1. The image must adapt to the size of the Button and be in the position determined by the ContentLayout property."/>
<Label
Text="Button with Height"/>
<Button
HeightRequest="50"
ImageSource="dotnet_bot.png"
ContentLayout="Left,10"
Text="Button"/>
<Label
Text="Button without Height"/>
<Button
ImageSource="dotnet_bot.png"
ContentLayout="Left,10"
Text="Button"/>
<Label
Text="2. The image must adapt to the size of the ImageButton."/>
<Label
Text="ImageButton with Height"/>
<ImageButton
HeightRequest="30"
WidthRequest="50"
Source="dotnet_bot.png"/>
<Label
Text="ImageButton without Height"/>
<ImageButton
WidthRequest="50"
Source="dotnet_bot.png"/>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 18242, "Button ImageSource not Scaling as expected", PlatformAffected.UWP)]
public partial class Issue18242 : ContentPage
{
public Issue18242()
{
InitializeComponent();
}
}
}
25 changes: 25 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue18242.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue18242 : _IssuesUITest
{
public Issue18242(TestDevice device) : base(device)
{
}

public override string Issue => "Button ImageSource not Scaling as expected";

[Test]
public void Issue18242Test()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.iOS }, "Only Windows for now");

App.WaitForElement("WaitForStubControl");

VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ public static void UpdateImageSource(this Button platformButton, WImageSource? n
{
if (platformButton.GetContent<WImage>() is WImage nativeImage)
{
nativeImage.Source = nativeImageSource;

// Stretch to the size of the button
nativeImage.HorizontalAlignment = UI.Xaml.HorizontalAlignment.Stretch;
nativeImage.VerticalAlignment = UI.Xaml.VerticalAlignment.Stretch;
nativeImage.Stretch = UI.Xaml.Media.Stretch.Uniform;
nativeImage.Source = nativeImageSource;

nativeImage.Visibility = nativeImageSource == null
? UI.Xaml.Visibility.Collapsed
Expand Down

0 comments on commit 5ad30b6

Please sign in to comment.