Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue30403.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 30403, "Image under WinUI does not respect VerticalOptions and HorizontalOptions with AspectFit", PlatformAffected.UWP)]
public class Issue30403 : TestContentPage
{
protected override void Init()
{
Title = "Issue 30403";

Content = new Grid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could modify a little bit this test? Could you include a StackLayout with 2-3 images?
Leave the current one, but could also validate:

  • Image with extreme aspect ratio
  • Really small image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a separate issue to create the tests #31686

{
BackgroundColor = Colors.LightGray,
Children =
{
new Image
{
AutomationId = "TestImage",
Source = "dotnet_bot.png",
Aspect = Aspect.AspectFit,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
}
}
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.TestCases.Tests.Issues;

public class Issue30403 : _IssuesUITest
{
public Issue30403(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Image under WinUI does not respect VerticalOptions and HorizontalOptions with AspectFit";

[Test]
[Category(UITestCategories.Image)]
public void ImageRespectsVerticalAndHorizontalOptionsWithAspectFit()
{
App.WaitForElement("TestImage");

// Verify that the image is positioned correctly according to VerticalOptions.Center and HorizontalOptions.Center
// with AspectFit aspect ratio. The image should appear in the center of the Grid.
VerifyScreenshot();
}
}
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.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 95 additions & 3 deletions src/Core/src/Handlers/Image/ImageHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using WImage = Microsoft.UI.Xaml.Controls.Image;

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -55,6 +57,33 @@ protected override void RemoveContainer()
UpdateValue(nameof(IView.Width));
}

/// <inheritdoc/>
public override Graphics.Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
// Compute a possible size without mutating platform properties during measure.
var possibleSize = base.GetDesiredSize(widthConstraint, heightConstraint);

// For AspectFit we want each non-Fill axis (independently) to not exceed intrinsic bitmap size
// so that alignment (Center, Start, End) has space to operate. A Fill axis should remain
// unconstrained here and rely on layout constraints.
if (VirtualView.Aspect == Aspect.AspectFit)
{
var imageSize = GetImageSize();
double w = possibleSize.Width;
double h = possibleSize.Height;

if (VirtualView.HorizontalLayoutAlignment != Primitives.LayoutAlignment.Fill && imageSize.Width > 0)
w = Math.Min(w, imageSize.Width);

if (VirtualView.VerticalLayoutAlignment != Primitives.LayoutAlignment.Fill && imageSize.Height > 0)
h = Math.Min(h, imageSize.Height);

return new Graphics.Size(w, h);
}

return possibleSize;
}

/// <summary>
/// Maps the abstract <see cref="IView.Height"/> property to the platform-specific implementations.
/// </summary>
Expand Down Expand Up @@ -112,6 +141,9 @@ public static void MapAspect(IImageHandler handler, IImage image)
{
handler.UpdateValue(nameof(IViewHandler.ContainerView));
handler.PlatformView?.UpdateAspect(image);
// Aspect changes may affect whether we cap to intrinsic size
if (handler is ImageHandler ih)
ih.UpdatePlatformMaxConstraints();
}

/// <summary>
Expand All @@ -135,15 +167,75 @@ public static void MapSource(IImageHandler handler, IImage image) =>
/// </summary>
/// <param name="handler">The associated handler.</param>
/// <param name="image">The associated <see cref="Image"/> instance.</param>
public static Task MapSourceAsync(IImageHandler handler, IImage image) =>
handler.SourceLoader.UpdateImageSourceAsync();
public static Task MapSourceAsync(IImageHandler handler, IImage image)
{
// Reset platform caps so we don't keep stale values between sources
if (handler is ImageHandler ih && ih.PlatformView is not null)
{
ih.PlatformView.MaxWidth = double.PositiveInfinity;
ih.PlatformView.MaxHeight = double.PositiveInfinity;
}

return handler.SourceLoader.UpdateImageSourceAsync();
}

void OnImageOpened(object sender, RoutedEventArgs e)
{
// Because this resolves from a task we should validate that the
// handler hasn't been disconnected
if (this.IsConnected())
{
UpdateValue(nameof(IImage.IsAnimationPlaying));
// Apply platform constraints when the decoded size is available
UpdatePlatformMaxConstraints();
}
}

/// <summary>
/// Updates platform MaxWidth/MaxHeight based on current aspect/alignment and decoded image size.
/// Avoids doing this during GetDesiredSize to prevent side effects across layout passes.
/// </summary>
private void UpdatePlatformMaxConstraints()
{
if (PlatformView is null || VirtualView is null)
return;

if (VirtualView.Aspect == Aspect.AspectFit)
{
var sz = GetImageSize();

// Width: cap to intrinsic only if horizontal alignment isn't Fill
if (VirtualView.HorizontalLayoutAlignment != Primitives.LayoutAlignment.Fill && sz.Width > 0)
PlatformView.MaxWidth = Math.Min(sz.Width, VirtualView.MaximumWidth);
else
PlatformView.MaxWidth = VirtualView.MaximumWidth;

// Height: cap to intrinsic only if vertical alignment isn't Fill
if (VirtualView.VerticalLayoutAlignment != Primitives.LayoutAlignment.Fill && sz.Height > 0)
PlatformView.MaxHeight = Math.Min(sz.Height, VirtualView.MaximumHeight);
else
PlatformView.MaxHeight = VirtualView.MaximumHeight;

return;
}

// Non AspectFit: mirror the view's declared maximums
PlatformView.MaxWidth = VirtualView.MaximumWidth;
PlatformView.MaxHeight = VirtualView.MaximumHeight;
}

private Graphics.Size GetImageSize()
{
if (PlatformView.Source is BitmapSource bitmap)
{
// BitmapSource may not have PixelWidth/PixelHeight set until image is loaded
if (bitmap.PixelWidth > 0 && bitmap.PixelHeight > 0)
{
return new Graphics.Size(bitmap.PixelWidth, bitmap.PixelHeight);
}
// If not available, return zero
}
return Graphics.Size.Zero;
}

partial class ImageImageSourcePartSetter
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ override Microsoft.Maui.Converters.ThicknessTypeConverter.CanConvertFrom(System.
override Microsoft.Maui.Converters.ThicknessTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
override Microsoft.Maui.Converters.ThicknessTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object! value) -> object?
override Microsoft.Maui.Converters.ThicknessTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type! destinationType) -> object?
override Microsoft.Maui.Handlers.ImageHandler.GetDesiredSize(double widthConstraint, double heightConstraint) -> Microsoft.Maui.Graphics.Size
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.Equals(object? obj) -> bool
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.GetHashCode() -> int
override Microsoft.Maui.Handlers.ContextFlyoutItemHandlerUpdate.ToString() -> string!
Expand Down
Loading