fixing all warnings and info for infra (#5891)

This commit is contained in:
Clint Rutkas
2020-08-12 10:44:58 -07:00
committed by GitHub
parent db6e9b6962
commit 8888739867
15 changed files with 67 additions and 61 deletions

View File

@@ -14,9 +14,11 @@ namespace Wox.Infrastructure.Image
public class ImageCache
{
private const int MaxCached = 50;
public ConcurrentDictionary<string, int> Usage = new ConcurrentDictionary<string, int>();
private const int PermissibleFactor = 2;
private readonly ConcurrentDictionary<string, ImageSource> _data = new ConcurrentDictionary<string, ImageSource>();
private const int permissibleFactor = 2;
public ConcurrentDictionary<string, int> Usage { get; set; } = new ConcurrentDictionary<string, int>();
public ImageSource this[string path]
{
@@ -33,7 +35,7 @@ namespace Wox.Infrastructure.Image
// To prevent the dictionary from drastically increasing in size by caching images, the dictionary size is not allowed to grow more than the permissibleFactor * maxCached size
// This is done so that we don't constantly perform this resizing operation and also maintain the image cache size at the same time
if (_data.Count > permissibleFactor * MaxCached)
if (_data.Count > PermissibleFactor * MaxCached)
{
// This function resizes the Usage dictionary, taking the top 'maxCached' number of items and filtering the image icons that are not accessed frequently.
Cleanup();
@@ -41,11 +43,9 @@ namespace Wox.Infrastructure.Image
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
foreach (var key in _data.Keys)
{
int dictValue;
if (!Usage.TryGetValue(key, out dictValue) && !(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon) || key.Equals(Constant.LightThemedErrorIcon) || key.Equals(Constant.LightThemedDefaultIcon)))
if (!Usage.TryGetValue(key, out _) && !(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon) || key.Equals(Constant.LightThemedErrorIcon) || key.Equals(Constant.LightThemedDefaultIcon)))
{
ImageSource imgSource;
_data.TryRemove(key, out imgSource);
_data.TryRemove(key, out _);
}
}
}

View File

@@ -19,11 +19,14 @@ namespace Wox.Infrastructure.Image
public static class ImageLoader
{
private static readonly ImageCache ImageCache = new ImageCache();
private static BinaryStorage<Dictionary<string, int>> _storage;
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
private static BinaryStorage<Dictionary<string, int>> _storage;
private static IImageHashGenerator _hashGenerator;
public static string ErrorIconPath;
public static string DefaultIconPath;
public static string ErrorIconPath { get; set; }
public static string DefaultIconPath { get; set; }
private static readonly string[] ImageExtensions =
{
@@ -194,7 +197,7 @@ namespace Wox.Infrastructure.Image
return new ImageResult(image, type);
}
private static bool EnableImageHash = true;
private static readonly bool _enableImageHash = true;
public static ImageSource Load(string path, bool loadFullImage = false)
{
@@ -202,20 +205,23 @@ namespace Wox.Infrastructure.Image
var img = imageResult.ImageSource;
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
{ // we need to get image hash
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
{
// we need to get image hash
string hash = _enableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
if (hash != null)
{
int ImageCacheValue;
if (GuidToKey.TryGetValue(hash, out string key))
{ // image already exists
if (ImageCache.Usage.TryGetValue(path, out ImageCacheValue))
{
// image already exists
if (ImageCache.Usage.TryGetValue(path, out _))
{
img = ImageCache[key];
}
}
else
{ // new guid
{
// new guid
GuidToKey[hash] = path;
}
}

View File

@@ -43,7 +43,8 @@ namespace Wox.Infrastructure.Image
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
internal interface IShellItem
{
void BindToHandler(IntPtr pbc,
void BindToHandler(
IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)] Guid bhid,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
out IntPtr ppv);
@@ -87,9 +88,9 @@ namespace Wox.Infrastructure.Image
AccessDenied = unchecked((int)0x80030005),
}
[ComImportAttribute()]
[GuidAttribute("bcc18b79-ba16-442f-80c4-8a59c30c463b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
[Guid("bcc18b79-ba16-442f-80c4-8a59c30c463b")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IShellItemImageFactory
{
[PreserveSig]
@@ -133,9 +134,8 @@ namespace Wox.Infrastructure.Image
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
{
IShellItem nativeShellItem;
Guid shellItem2Guid = new Guid(IShellItem2Guid);
int retCode = SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out nativeShellItem);
int retCode = SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out IShellItem nativeShellItem);
if (retCode != 0)
{
@@ -148,8 +148,7 @@ namespace Wox.Infrastructure.Image
Height = height,
};
IntPtr hBitmap;
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out IntPtr hBitmap);
// if extracting image thumbnail and failed, extract shell icon
if (options == ThumbnailOptions.ThumbnailOnly && hr == HResult.ExtractionFailed)