Make sure we are thread safe

This commit is contained in:
LarsBrubaker 2020-09-10 09:50:33 -07:00
parent 54c3cdedea
commit 26bb26884f
5 changed files with 37 additions and 19 deletions

View file

@ -101,7 +101,7 @@ namespace MatterHackers.MatterControl.DesignTools
var thumbnail = await LoadImage(item);
if (thumbnail != null)
{
LibraryProviderHelpers.ResizeImage(thumbnail, width, height);
thumbnail = LibraryProviderHelpers.ResizeImage(thumbnail, width, height);
// Cache library thumbnail
AggContext.ImageIO.SaveImageData(

View file

@ -140,30 +140,35 @@ namespace MatterHackers.MatterControl.Library
public ILibraryContainer RootLibaryContainer { get; }
public ImageBuffer EnsureCorrectThumbnailSizing(ImageBuffer thumbnail, int thumbWidth, int thumbHeight)
public ImageBuffer EnsureCorrectThumbnailSizing(ImageBuffer originalThumbnail, int thumbWidth, int thumbHeight, Action<ImageBuffer> resizedImage)
{
var processingImage = originalThumbnail;
// Resize canvas to target as fallback
if (thumbnail.Width < thumbWidth || thumbnail.Height < thumbHeight)
if (processingImage.Width < thumbWidth || processingImage.Height < thumbHeight)
{
LibraryListView.ResizeCanvas(thumbnail, thumbWidth, thumbHeight);
processingImage = LibraryListView.ResizeCanvas(processingImage, thumbWidth, thumbHeight);
}
else if (thumbnail.Width > thumbWidth || thumbnail.Height > thumbHeight)
else if (processingImage.Width > thumbWidth || processingImage.Height > thumbHeight)
{
LibraryProviderHelpers.ResizeImage(thumbnail, thumbWidth, thumbHeight);
processingImage = LibraryProviderHelpers.ResizeImage(processingImage, thumbWidth, thumbHeight);
}
if (GuiWidget.DeviceScale != 1
&& thumbnail.Width != thumbWidth * GuiWidget.DeviceScale)
&& processingImage.Width != thumbWidth * GuiWidget.DeviceScale)
{
thumbnail.ScaleImage(GuiWidget.DeviceScale);
processingImage = processingImage.CreateScaledImage(GuiWidget.DeviceScale);
}
thumbnail.ImageChanged += (s, e) =>
originalThumbnail.ImageChanged += (s, e) =>
{
thumbnail.ScaleImage(GuiWidget.DeviceScale);
// this happens when we get an updated image from a web request
UiThread.RunOnIdle(() =>
{
resizedImage?.Invoke(originalThumbnail.CreateScaledImage(GuiWidget.DeviceScale));
});
};
return thumbnail;
return processingImage;
}
public IContentProvider GetContentProvider(ILibraryItem item)
@ -212,7 +217,10 @@ namespace MatterHackers.MatterControl.Library
return;
}
icon = await Task.Run(() => this.EnsureCorrectThumbnailSizing(icon, thumbWidth, thumbHeight));
icon = await Task.Run(() => this.EnsureCorrectThumbnailSizing(icon, thumbWidth, thumbHeight, (image) =>
{
setItemThumbnail(image);
}));
thumbnailListener?.Invoke(icon);
}
}

View file

@ -56,7 +56,7 @@ namespace MatterHackers.MatterControl.Library
/// <param name="targetWidth">The target width</param>
/// <param name="targetHeight">The target height</param>
/// <returns>A resized ImageBuffer constrained to the given bounds and centered on the new surface</returns>
public static void ResizeImage(ImageBuffer imageBuffer, int targetWidth, int targetHeight)
public static ImageBuffer ResizeImage(ImageBuffer imageBuffer, int targetWidth, int targetHeight)
{
var expectedSize = new Vector2((int)(targetWidth * GuiWidget.DeviceScale), (int)(targetHeight * GuiWidget.DeviceScale));
@ -70,8 +70,10 @@ namespace MatterHackers.MatterControl.Library
var scaledImageBuffer = imageBuffer.CreateScaledImage(targetWidth, targetHeight);
scaledImageBuffer.SetRecieveBlender(new BlenderPreMultBGRA());
imageBuffer.CopyFrom(scaledImageBuffer);
return scaledImageBuffer;
}
return imageBuffer;
}
}
}

View file

@ -401,7 +401,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
}
}
public static void ResizeCanvas(ImageBuffer originalImage, int width, int height)
public static ImageBuffer ResizeCanvas(ImageBuffer originalImage, int width, int height)
{
var destImage = new ImageBuffer(width, height, 32, originalImage.GetRecieveBlender());
@ -419,9 +419,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
renderGraphics.FillRectangle(center, Color.Transparent);
originalImage.Allocate(width, height, 32, originalImage.GetRecieveBlender());
originalImage.CopyFrom(destImage);
return destImage;
}
private void ListViewItem_DoubleClick(object sender, MouseEventArgs e)

View file

@ -121,6 +121,12 @@ namespace MatterHackers.MatterControl.CustomWidgets
requeueRaytraceOnDraw = false;
raytracePending = false;
if (GuiWidget.DeviceScale != 1
&& thumbnail.Width != thumbWidth * GuiWidget.DeviceScale)
{
thumbnail = thumbnail.CreateScaledImage(GuiWidget.DeviceScale);
}
if (thumbnail.Width != thumbWidth
|| thumbnail.Height != thumbHeight)
{
@ -199,7 +205,11 @@ namespace MatterHackers.MatterControl.CustomWidgets
ApplicationController.Instance.Library.EnsureCorrectThumbnailSizing(
thumbnail,
thumbWidth,
thumbHeight));
thumbHeight,
(image) =>
{
SetSizedThumbnail(image);
}));
}
private void SetSizedThumbnail(ImageBuffer thumbnail)