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

[Windows] Correctly size the ImageButton BitmapImage #6663

Merged
merged 4 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Button/ButtonHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void MapImageSource(IButtonHandler handler, IImage image) =>

void OnSetImageSource(ImageSource? platformImageSource)
{
PlatformView.UpdateImageSource(platformImageSource);
PlatformView.UpdateImageSource(VirtualView, platformImageSource);
}

void OnClick(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void MapPadding(IImageButtonHandler handler, IImageButton imageBut

void OnSetImageSource(ImageSource? nativeImageSource)
{
PlatformView.UpdateImageSource(nativeImageSource);
PlatformView.UpdateImageSource(VirtualView, nativeImageSource);
}

void OnClick(object sender, RoutedEventArgs e)
Expand Down
10 changes: 7 additions & 3 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void UpdateCharacterSpacing(this ButtonBase platformButton, ITextS
textBlock.CharacterSpacing = characterSpacing;
}

public static void UpdateImageSource(this Button platformButton, WImageSource? nativeImageSource)
public static void UpdateImageSource(this Button platformButton, IButton button, WImageSource? nativeImageSource)
{
if (platformButton.GetContent<WImage>() is WImage nativeImage)
{
Expand Down Expand Up @@ -171,9 +171,13 @@ void OnImageOpened(object sender, RoutedEventArgs e)
{
// do the actual resize
var imageSourceSize = actualImageSource.GetImageSourceSize(platformButton);
nativeImage.Width = imageSourceSize.Width;
nativeImage.Height = imageSourceSize.Height;

nativeImage.Width = button.Width > 0 ? platformButton.ActualWidth : imageSourceSize.Width;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If ImageButton HeightRequest and WidthRequest properties have been set we use that values on the image. Otherwise, the height and width of the original image size is used.

Copy link
Member

Choose a reason for hiding this comment

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

I believe EZ added a special value for this: Primitives.Dimension.Unset
The Width/Height properties are already massaged into either Unset or a positive value, so we should probably use that to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied changes to use Primitives.Dimension.Unset, also added a sample:
fix-imagebutton-size

Is working correctly and also resize as expected.

nativeImage.Height = button.Height > 0 ? platformButton.ActualHeight : imageSourceSize.Height;
}

if (platformButton.Parent is FrameworkElement frameworkElement)
frameworkElement.InvalidateMeasure();
};
}
}
Expand Down