Skip to content

(Windows) ImageButton contents now explicitly stretches #17159

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

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -45,6 +45,18 @@
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#FFFFFF" BaseSize="168,208" />
</ItemGroup>

<ItemGroup>
<Compile Update="Issues\Issue16918.xaml.cs">
<DependentUpon>Issue16918.xaml</DependentUpon>
</Compile>
</ItemGroup>

<Import Project="$(MauiSrcDirectory)Maui.InTree.props" Condition=" '$(UseMaui)' != 'true' " />

<ItemGroup>
<MauiXaml Update="Issues\Issue16918.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.Issue16918"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Image (SVG 40x40):"
HorizontalOptions="Center" />
<Image
Source="menu_entry_settings_40.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="40"
HorizontalOptions="Center" />

<Label
Text="ImageButton (SVG 40x40):"
HorizontalOptions="Center" />
<ImageButton
Source="menu_entry_settings_40.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="40"
BackgroundColor="Transparent"
HorizontalOptions="Center" />

<Label
Text="Image (SVG 210x210):"
HorizontalOptions="Center" />
<Image
Source="menu_entry_settings_210.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="40"
HorizontalOptions="Center" />

<Label
Text="ImageButton (SVG 210x210):"
HorizontalOptions="Center" />
<ImageButton
Source="menu_entry_settings_210.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="40"
BackgroundColor="Transparent"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</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, 16918, "ImageButton is not properly anti-aliased when scaled down", PlatformAffected.UWP)]
public partial class Issue16918 : ContentPage
{
public Issue16918()
{
InitializeComponent();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue16918.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Maui.Appium;
using Microsoft.Maui.AppiumTests;
using NUnit.Framework;

namespace Controls.AppiumTests.Tests.Issues
{
public class Issue16918 : _IssuesUITest
{
public Issue16918(TestDevice device)
: base(device)
{ }

public override string Issue => "Image buttons aliasing";

[Test]
public void Issue16918Test()
{
// https://github.com/dotnet/maui/issues/16918
UITestContext.IgnoreIfPlatforms(new[]
{
TestDevice.Mac,
TestDevice.Android,
TestDevice.iOS
});

App.WaitForElement("WaitForStubControl");
VerifyScreenshot();
}
}
}
33 changes: 3 additions & 30 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,9 @@ public static void UpdateImageSource(this Button platformButton, WImageSource? n
{
nativeImage.Source = nativeImageSource;

if (nativeImageSource is not null)
{
// set the base size if we can
{
var imageSourceSize = nativeImageSource.GetImageSourceSize(platformButton);
nativeImage.Width = imageSourceSize.Width;
nativeImage.Height = imageSourceSize.Height;
}

// BitmapImage is a special case that has an event when the image is loaded
// when this happens, we want to resize the button
if (nativeImageSource is BitmapImage bitmapImage)
{
bitmapImage.ImageOpened += OnImageOpened;

void OnImageOpened(object sender, RoutedEventArgs e)
{
bitmapImage.ImageOpened -= OnImageOpened;

// Check if the image that just loaded is still the current image
var actualImageSource = sender as BitmapImage;

if (actualImageSource is not null && nativeImage.Source == actualImageSource)
nativeImage.Height = nativeImage.Width = Primitives.Dimension.Unset;

if (platformButton.Parent is FrameworkElement frameworkElement)
frameworkElement.InvalidateMeasure();
};
}
}
// Stretch to the size of the button
nativeImage.HorizontalAlignment = UI.Xaml.HorizontalAlignment.Stretch;
nativeImage.VerticalAlignment = UI.Xaml.VerticalAlignment.Stretch;

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