Skip to content

Commit

Permalink
Core(Gtk): fix image resize
Browse files Browse the repository at this point in the history
Implement WidthRequest and HeightRequest mappings for Image.
  • Loading branch information
parhamsaremi authored and aarani committed Apr 24, 2023
1 parent a42fc12 commit bb36698
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/Core/src/Handlers/Image/ImageHandler.Gtk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ void OnSetImageSource(Pixbuf? obj)
PlatformView.Image = obj;
}

static int? Request(double viewSize) => viewSize >= 0 ? (int)Math.Round(viewSize) : null;
private void UpdateWidthRequest(IImage image)
{
var widthRequest = Request(image.Width * PlatformView.ScaleFactor);

if (widthRequest is not null && widthRequest != PlatformView.WidthRequest && widthRequest != PlatformView.AllocatedWidth)
{
PlatformView.ChangeWidth(widthRequest.Value);
PlatformView.QueueResize();
}
}

private void UpdateHeightRequest(IImage image)
{
var heightRequest = Request(image.Height * PlatformView.ScaleFactor);

if (heightRequest is not null && heightRequest != PlatformView.WidthRequest && heightRequest != PlatformView.AllocatedWidth)
{
PlatformView.ChangeHeight(heightRequest.Value);
PlatformView.QueueResize();
}
}

public static void MapWidthRequest(IImageHandler handler, IImage view)
{
((ImageHandler)handler).UpdateWidthRequest(view);
}

public static void MapHeightRequest(IImageHandler handler, IImage view)
{
((ImageHandler)handler).UpdateHeightRequest(view);
}

}

}
4 changes: 4 additions & 0 deletions src/Core/src/Handlers/Image/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public partial class ImageHandler : IImageHandler
[nameof(IImage.Aspect)] = MapAspect,
[nameof(IImage.IsAnimationPlaying)] = MapIsAnimationPlaying,
[nameof(IImage.Source)] = MapSource,
#if GTK
["WidthRequest"] = MapWidthRequest,
["HeightRequest"] = MapHeightRequest,
#endif
};

public static CommandMapper<IImage, IImageHandler> CommandMapper = new(ViewHandler.ViewCommandMapper)
Expand Down
55 changes: 53 additions & 2 deletions src/Core/src/Platform/Gtk/ImageView.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Gdk;

namespace Microsoft.Maui.Platform
{

Expand All @@ -9,11 +11,60 @@ namespace Microsoft.Maui.Platform

public class ImageView : Gtk.Image
{
// OriginalSizeBuf is added so when we want to resize the image using Pixbuf we can access the
// initial Pixbuf and resize that.
Pixbuf? _originalSizeBuf;
// Image might be null because it is possible to have a WidthRequest before Pixbuf has a value.
// In this case, we'll just save the value, and once Pixbuf is initialized we'll update its size
// to have the saved values
int? _lastRequestedWidth;
int? _lastRequestedHeight;

public Gdk.Pixbuf? Image
public Pixbuf? Image
{
get => Pixbuf;
set => Pixbuf = value;
set
{
_originalSizeBuf = value;
if (_lastRequestedHeight is not null || _lastRequestedWidth is not null)
{
Pixbuf = _originalSizeBuf?.ScaleSimple(
_lastRequestedWidth ?? _originalSizeBuf.Width,
_lastRequestedHeight ?? _originalSizeBuf.Height,
InterpType.Bilinear
);
}
else
{
Pixbuf = value;
}
}
}

internal void ChangeHeight(int height)
{
if (Image is null)
{
_lastRequestedHeight = height;
}
else
{
var newBuf = _originalSizeBuf?.ScaleSimple(Pixbuf.Width, height, InterpType.Bilinear);
Pixbuf = newBuf;
}
}

internal void ChangeWidth(int width)
{
if (Image is null)
{
_lastRequestedWidth = width;
}
else
{
var newBuf = _originalSizeBuf?.ScaleSimple(width, Pixbuf.Height, InterpType.Bilinear);
Pixbuf = newBuf;
}
}

}
Expand Down

0 comments on commit bb36698

Please sign in to comment.