diff --git a/Library/ContentProviders/GCodeContentProvider.cs b/Library/ContentProviders/GCodeContentProvider.cs index 86178e530..e5ce4e82a 100644 --- a/Library/ContentProviders/GCodeContentProvider.cs +++ b/Library/ContentProviders/GCodeContentProvider.cs @@ -70,10 +70,9 @@ namespace MatterHackers.MatterControl return null; } - public Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback) + public Task GetThumbnail(ILibraryItem item, int width, int height) { - imageCallback(thumbnailImage, false); - return Task.CompletedTask; + return Task.FromResult(thumbnailImage); } } } diff --git a/Library/ContentProviders/IContentProvider.cs b/Library/ContentProviders/IContentProvider.cs index f54c4b356..983d6f17d 100644 --- a/Library/ContentProviders/IContentProvider.cs +++ b/Library/ContentProviders/IContentProvider.cs @@ -39,7 +39,7 @@ namespace MatterHackers.MatterControl.Library public interface IContentProvider { - Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback); + Task GetThumbnail(ILibraryItem item, int width, int height); ImageBuffer DefaultImage { get; } } diff --git a/Library/ContentProviders/ImageContentProvider.cs b/Library/ContentProviders/ImageContentProvider.cs index 43eef0459..0b74684fb 100644 --- a/Library/ContentProviders/ImageContentProvider.cs +++ b/Library/ContentProviders/ImageContentProvider.cs @@ -96,10 +96,9 @@ namespace MatterHackers.MatterControl.DesignTools return null; } - public async Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback) + public async Task GetThumbnail(ILibraryItem item, int width, int height) { - var imageBuffer = await LoadImage(item); - imageCallback(imageBuffer, raytracedImage: true); + return await LoadImage(item); } public ImageBuffer DefaultImage => AggContext.StaticData.LoadIcon("140.png"); diff --git a/Library/ContentProviders/MeshContentProvider.cs b/Library/ContentProviders/MeshContentProvider.cs index adcfb37ea..d2468dba4 100644 --- a/Library/ContentProviders/MeshContentProvider.cs +++ b/Library/ContentProviders/MeshContentProvider.cs @@ -71,7 +71,7 @@ namespace MatterHackers.MatterControl loadedItem = Object3D.Load(contentStream.Stream, Path.GetExtension(streamInterface.FileName), CancellationToken.None, null /*itemCache*/, progressReporter); // Set MeshPath for non-mcx content. Avoid on mcx to ensure serialization of children - if (item is FileSystemFileItem fileItem + if (item is FileSystemFileItem fileItem && !string.Equals(Path.GetExtension(fileItem.FileName), ".mcx", StringComparison.OrdinalIgnoreCase)) { loadedItem.MeshPath = fileItem.Path; @@ -101,7 +101,7 @@ namespace MatterHackers.MatterControl } - public async Task GetThumbnail(ILibraryItem libraryItem, int width, int height, ThumbnailSetter imageCallback) + public async Task GetThumbnail(ILibraryItem libraryItem, int width, int height) { IObject3D object3D = null; @@ -122,15 +122,14 @@ namespace MatterHackers.MatterControl string thumbnailId = libraryItem.ID; - var thumbnail = GetThumbnail(object3D, thumbnailId, width, height, false); - imageCallback?.Invoke(thumbnail, true); + return GetThumbnail(object3D, thumbnailId, width, height, false); } public ImageBuffer GetThumbnail(IObject3D item, string thumbnailId, int width, int height, bool onlyUseCache) { if (item == null) { - return DefaultImage.CreateScaledImage(width, height); + return DefaultImage; } var image = LoadCachedImage(thumbnailId, width, height); @@ -148,7 +147,7 @@ namespace MatterHackers.MatterControl if(onlyUseCache) { - return DefaultImage.CreateScaledImage(width, height); + return DefaultImage; } int estimatedMemorySize = item.EstimatedMemory(); @@ -175,7 +174,7 @@ namespace MatterHackers.MatterControl if (thumbnail != null) { // Cache at requested size - string cachePath = ApplicationController.Instance.ThumbnailCachePath(thumbnailId, width, height); + string cachePath = ApplicationController.Instance.ThumbnailCachePath(item.MeshRenderId().ToString(), width, height); // TODO: Lookup best large image and downscale if required if (false) @@ -184,15 +183,9 @@ namespace MatterHackers.MatterControl } AggContext.ImageIO.SaveImageData(cachePath, thumbnail); - var meshCachePath = ApplicationController.Instance.ThumbnailCachePath(item.MeshRenderId().ToString(), width, height); - if (meshCachePath != cachePath) - { - // also save it to the mesh cache - AggContext.ImageIO.SaveImageData(meshCachePath, thumbnail); - } } - return thumbnail ?? DefaultImage.CreateScaledImage(width, height); + return thumbnail ?? DefaultImage; } internal static ImageBuffer LoadCachedImage(string cacheId, int width, int height) @@ -206,7 +199,7 @@ namespace MatterHackers.MatterControl if (width < 100 && height < 100) { - // check for a 100x100 image + // check for a 100x100 image var cachedAt100x100 = LoadImage(ApplicationController.Instance.ThumbnailCachePath(cacheId, 100, 100)); if (cachedAt100x100 != null) { diff --git a/Library/Widgets/ListView/IconListView.cs b/Library/Widgets/ListView/IconListView.cs index 6543a8741..ae6d1d7b1 100644 --- a/Library/Widgets/ListView/IconListView.cs +++ b/Library/Widgets/ListView/IconListView.cs @@ -262,7 +262,7 @@ namespace MatterHackers.MatterControl.CustomWidgets container.AddChild(text); } - this.SetItemThumbnail(loadingImage, raytracedImage: false); + this.SetItemThumbnail(loadingImage); } public override string ToolTipText diff --git a/Library/Widgets/ListView/ListViewItemBase.cs b/Library/Widgets/ListView/ListViewItemBase.cs index e333c93fa..9a9b70092 100644 --- a/Library/Widgets/ListView/ListViewItemBase.cs +++ b/Library/Widgets/ListView/ListViewItemBase.cs @@ -75,7 +75,7 @@ namespace MatterHackers.MatterControl.CustomWidgets var thumbnail = MeshContentProvider.LoadCachedImage(thumbnailId, thumbWidth, thumbHeight); if (thumbnail != null) { - this.SetItemThumbnail(thumbnail, raytracedImage: false); + this.SetItemThumbnail(thumbnail); return; } @@ -111,14 +111,10 @@ namespace MatterHackers.MatterControl.CustomWidgets else { // Show processing image - this.SetItemThumbnail(generatingThumbnailIcon, raytracedImage: false); + this.SetItemThumbnail(generatingThumbnailIcon); // Ask the provider for a content specific thumbnail - await contentProvider.GetThumbnail( - libraryItem, - thumbWidth, - thumbHeight, - this.SetItemThumbnail); + thumbnail = await contentProvider.GetThumbnail(libraryItem, thumbWidth, thumbHeight); } } } @@ -129,7 +125,7 @@ namespace MatterHackers.MatterControl.CustomWidgets thumbnail = ((libraryItem is ILibraryContainerLink) ? defaultFolderIcon : defaultItemIcon).AlphaToPrimaryAccent(); } - this.SetItemThumbnail(thumbnail, raytracedImage: false); + this.SetItemThumbnail(thumbnail); } private void ScheduleRaytraceOperation() @@ -141,7 +137,7 @@ namespace MatterHackers.MatterControl.CustomWidgets ApplicationController.Instance.QueueForGeneration(async () => { - // When this widget is dequeued for generation, validate before processing. Off-screen widgets should be skipped and will requeue next time they become visible + // When dequeued for generation, ensure visible before raytracing. Off-screen widgets are dequeue and will reschedule if redrawn if (!this.ActuallyVisibleOnScreen()) { // Skip raytracing operation, requeue on next draw @@ -155,14 +151,17 @@ namespace MatterHackers.MatterControl.CustomWidgets requeueRaytraceOnDraw = false; // Show processing image - this.SetItemThumbnail(generatingThumbnailIcon, raytracedImage: false); + this.SetItemThumbnail(generatingThumbnailIcon); // Ask the MeshContentProvider to RayTrace the image - await meshContentProvider.GetThumbnail( - listViewItem.Model, - thumbWidth, - thumbHeight, - this.SetItemThumbnail); + var thumbnail = await meshContentProvider.GetThumbnail(listViewItem.Model, thumbWidth, thumbHeight); + if (thumbnail != null) + { + requeueRaytraceOnDraw = false; + raytracePending = false; + + this.SetItemThumbnail(thumbnail); + } } }); } @@ -219,7 +218,7 @@ namespace MatterHackers.MatterControl.CustomWidgets public event EventHandler ImageSet; - protected void SetItemThumbnail(ImageBuffer thumbnail, bool raytracedImage) + protected void SetItemThumbnail(ImageBuffer thumbnail) { if (thumbnail != null) { @@ -233,11 +232,6 @@ namespace MatterHackers.MatterControl.CustomWidgets thumbnail = LibraryProviderHelpers.ResizeImage(thumbnail, thumbWidth, thumbHeight); } - if (raytracedImage) - { - this.raytracePending = false; - } - if (GuiWidget.DeviceScale != 1) { thumbnail = thumbnail.CreateScaledImage(GuiWidget.DeviceScale); diff --git a/Tests/MatterControl.AutomationTests/MatterControlTests.cs b/Tests/MatterControl.AutomationTests/MatterControlTests.cs index 4ffaaac50..c5c5d84ef 100644 --- a/Tests/MatterControl.AutomationTests/MatterControlTests.cs +++ b/Tests/MatterControl.AutomationTests/MatterControlTests.cs @@ -50,7 +50,7 @@ namespace MatterHackers.MatterControl.Tests.Automation // Generate thumbnail var stopWatch = Stopwatch.StartNew(); - await provider.GetThumbnail(item, 400, 400, (imageBuffer, _) => { }); + await provider.GetThumbnail(item, 400, 400); Assert.Less(stopWatch.ElapsedMilliseconds, 2000, "Elapsed thumbnail generation for Rook.amf should be less than 2 seconds for expected orthographic mode"); });