mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[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:
@@ -5,6 +5,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using PowerLauncher.Helper;
|
using PowerLauncher.Helper;
|
||||||
@@ -38,6 +39,9 @@ namespace PowerLauncher.ViewModel
|
|||||||
|
|
||||||
private bool _areContextButtonsActive;
|
private bool _areContextButtonsActive;
|
||||||
|
|
||||||
|
private ImageSource _image;
|
||||||
|
private volatile bool _imageLoaded;
|
||||||
|
|
||||||
public bool AreContextButtonsActive
|
public bool AreContextButtonsActive
|
||||||
{
|
{
|
||||||
get => _areContextButtonsActive;
|
get => _areContextButtonsActive;
|
||||||
@@ -191,23 +195,49 @@ namespace PowerLauncher.ViewModel
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var imagePath = Result.IcoPath;
|
if (!_imageLoaded)
|
||||||
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
|
{
|
||||||
|
_imageLoaded = true;
|
||||||
|
_ = LoadImageAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
try
|
||||||
{
|
{
|
||||||
return Result.Icon();
|
var image = icon();
|
||||||
|
return image;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.Exception($"IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e, GetType());
|
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;
|
imagePath = ImageLoader.ErrorIconPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
|
return await ImageLoader.LoadAsync(imagePath, _settings.GenerateThumbnailsFromFiles, loadFullImage).ConfigureAwait(false);
|
||||||
return ImageLoader.Load(imagePath, _settings.GenerateThumbnailsFromFiles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
// Returns false if we've already reached the last item.
|
||||||
|
|||||||
@@ -64,12 +64,12 @@ namespace Wox.Infrastructure.Image
|
|||||||
UpdateIconPath(theme);
|
UpdateIconPath(theme);
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
Stopwatch.Normal("ImageLoader.Initialize - Preload images cost", () =>
|
Stopwatch.Normal("ImageLoader.Initialize - Preload images cost", async () =>
|
||||||
{
|
{
|
||||||
ImageCache.Usage.AsParallel().ForAll(x =>
|
foreach (var (path, _) in ImageCache.Usage)
|
||||||
{
|
{
|
||||||
Load(x.Key, true);
|
await LoadAsync(path, true);
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Log.Info($"Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}", MethodBase.GetCurrentMethod().DeclaringType);
|
Log.Info($"Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}", MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
@@ -120,10 +120,9 @@ namespace Wox.Infrastructure.Image
|
|||||||
Cache,
|
Cache,
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ImageResult LoadInternal(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
|
private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
|
||||||
{
|
{
|
||||||
ImageSource image;
|
ImageResult imageResult;
|
||||||
ImageType type = ImageType.Error;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
@@ -144,6 +143,24 @@ namespace Wox.Infrastructure.Image
|
|||||||
return new ImageResult(imageSource, ImageType.Data);
|
return new ImageResult(imageSource, ImageType.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageResult = await Task.Run(() => GetThumbnailResult(ref path, generateThumbnailsFromFiles, loadFullImage));
|
||||||
|
}
|
||||||
|
catch (System.Exception e)
|
||||||
|
{
|
||||||
|
Log.Exception($"Failed to get thumbnail for {path}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
|
ImageSource image = ImageCache[ErrorIconPath];
|
||||||
|
ImageCache[path] = image;
|
||||||
|
imageResult = new ImageResult(image, ImageType.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ImageResult GetThumbnailResult(ref string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
|
||||||
|
{
|
||||||
|
ImageSource image;
|
||||||
|
ImageType type = ImageType.Error;
|
||||||
|
|
||||||
if (!Path.IsPathRooted(path))
|
if (!Path.IsPathRooted(path))
|
||||||
{
|
{
|
||||||
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
||||||
@@ -213,23 +230,15 @@ namespace Wox.Infrastructure.Image
|
|||||||
{
|
{
|
||||||
image.Freeze();
|
image.Freeze();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (System.Exception e)
|
|
||||||
{
|
|
||||||
Log.Exception($"Failed to get thumbnail for {path}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
|
||||||
type = ImageType.Error;
|
|
||||||
image = ImageCache[ErrorIconPath];
|
|
||||||
ImageCache[path] = image;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ImageResult(image, type);
|
return new ImageResult(image, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
private const bool _enableImageHash = true;
|
private const bool _enableImageHash = true;
|
||||||
|
|
||||||
public static ImageSource Load(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
|
public static async ValueTask<ImageSource> LoadAsync(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
|
||||||
{
|
{
|
||||||
var imageResult = LoadInternal(path, generateThumbnailsFromFiles, loadFullImage);
|
var imageResult = await LoadInternalAsync(path, generateThumbnailsFromFiles, loadFullImage);
|
||||||
|
|
||||||
var img = imageResult.ImageSource;
|
var img = imageResult.ImageSource;
|
||||||
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
|
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
|
||||||
|
|||||||
Reference in New Issue
Block a user