[PTRun]Asynchronously load image and thumbnails (#24736)

* [PTRun]Asynchronously load image and thumbnails

* Bring back check for Adobe PDF generated thumbnails

---------

Co-authored-by: Stefan Markovic <stefan@janeasystems.com>
This commit is contained in:
Jaime Bernardo
2023-03-20 13:22:51 +00:00
committed by GitHub
parent 7d4e804cec
commit 35e8d04f66
2 changed files with 132 additions and 93 deletions

View File

@@ -5,6 +5,7 @@
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;
using PowerLauncher.Helper;
@@ -38,6 +39,9 @@ namespace PowerLauncher.ViewModel
private bool _areContextButtonsActive;
private ImageSource _image;
private volatile bool _imageLoaded;
public bool AreContextButtonsActive
{
get => _areContextButtonsActive;
@@ -191,23 +195,49 @@ namespace PowerLauncher.ViewModel
{
get
{
var imagePath = Result.IcoPath;
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
if (!_imageLoaded)
{
try
{
return Result.Icon();
}
catch (Exception e)
{
Log.Exception($"IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e, GetType());
imagePath = ImageLoader.ErrorIconPath;
}
_imageLoaded = true;
_ = LoadImageAsync();
}
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath, _settings.GenerateThumbnailsFromFiles);
return _image;
}
private set
{
_image = value;
OnPropertyChanged(nameof(Image));
}
}
private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.IconDelegate icon, bool loadFullImage)
{
if (string.IsNullOrEmpty(imagePath) && icon != null)
{
try
{
var image = icon();
return image;
}
catch (Exception e)
{
Log.Exception(
$"IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
e,
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
imagePath = ImageLoader.ErrorIconPath;
}
}
return await ImageLoader.LoadAsync(imagePath, _settings.GenerateThumbnailsFromFiles, loadFullImage).ConfigureAwait(false);
}
private async Task LoadImageAsync()
{
var imagePath = Result.IcoPath;
var iconDelegate = Result.Icon;
Image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
}
// Returns false if we've already reached the last item.