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

@@ -88,8 +88,8 @@ namespace Wox.Infrastructure
_pinyinStorage.Save(GetPinyinCacheAsDictionary()); _pinyinStorage.Save(GetPinyinCacheAsDictionary());
} }
private static string[] EmptyStringArray = new string[0]; private static readonly string[] _emptyStringArray = new string[0];
private static string[][] Empty2DStringArray = new string[0][]; private static readonly string[][] _empty2DStringArray = new string[0][];
/// <summary> /// <summary>
/// replace chinese character with pinyin, non chinese character won't be modified /// replace chinese character with pinyin, non chinese character won't be modified
@@ -100,7 +100,7 @@ namespace Wox.Infrastructure
{ {
if (!_settings.ShouldUsePinyin) if (!_settings.ShouldUsePinyin)
{ {
return EmptyStringArray; return _emptyStringArray;
} }
var pinyin = word.Select(c => var pinyin = word.Select(c =>
@@ -122,7 +122,7 @@ namespace Wox.Infrastructure
{ {
if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters)) if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters))
{ {
return Empty2DStringArray; return _empty2DStringArray;
} }
if (!_pinyinCache.ContainsKey(characters)) if (!_pinyinCache.ContainsKey(characters))
@@ -181,7 +181,7 @@ namespace Wox.Infrastructure
{ {
if (!_settings.ShouldUsePinyin) if (!_settings.ShouldUsePinyin)
{ {
return EmptyStringArray; return _emptyStringArray;
} }
var combination = ( var combination = (

View File

@@ -14,14 +14,15 @@ namespace Wox.Infrastructure
public const string ExeFileName = "PowerLauncher"; public const string ExeFileName = "PowerLauncher";
public const string ModuleLocation = "Microsoft\\PowerToys\\PowerToys Run"; public const string ModuleLocation = "Microsoft\\PowerToys\\PowerToys Run";
public const string Plugins = "Plugins"; public const string Plugins = "Plugins";
public const string PortableFolderName = "UserData";
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString();
public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, ExeFileName + ".exe"); public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, ExeFileName + ".exe");
public static bool IsPortableMode; public static bool IsPortableMode { get; set; }
public const string PortableFolderName = "UserData";
public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName); public static string PortableDataPath { get; set; } = Path.Combine(ProgramDirectory, PortableFolderName);
public static string DetermineDataDirectory() public static string DetermineDataDirectory()
{ {

View File

@@ -190,7 +190,7 @@ namespace Wox.Infrastructure.Exception
return result; return result;
} }
catch (System.Exception e) catch (System.Exception)
{ {
return new List<string>(); return new List<string>();
} }

View File

@@ -21,7 +21,7 @@ namespace Wox.Infrastructure.Hotkey
public Key CharKey { get; set; } public Key CharKey { get; set; }
Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string> private readonly Dictionary<Key, string> _specialSymbolDictionary = new Dictionary<Key, string>
{ {
{ Key.Space, "Space" }, { Key.Space, "Space" },
{ Key.Oem3, "~" }, { Key.Oem3, "~" },
@@ -32,6 +32,7 @@ namespace Wox.Infrastructure.Hotkey
get get
{ {
ModifierKeys modifierKeys = ModifierKeys.None; ModifierKeys modifierKeys = ModifierKeys.None;
if (Alt) if (Alt)
{ {
modifierKeys = ModifierKeys.Alt; modifierKeys = ModifierKeys.Alt;
@@ -39,17 +40,17 @@ namespace Wox.Infrastructure.Hotkey
if (Shift) if (Shift)
{ {
modifierKeys = modifierKeys | ModifierKeys.Shift; modifierKeys |= ModifierKeys.Shift;
} }
if (Win) if (Win)
{ {
modifierKeys = modifierKeys | ModifierKeys.Windows; modifierKeys |= ModifierKeys.Windows;
} }
if (Ctrl) if (Ctrl)
{ {
modifierKeys = modifierKeys | ModifierKeys.Control; modifierKeys |= ModifierKeys.Control;
} }
return modifierKeys; return modifierKeys;
@@ -109,7 +110,7 @@ namespace Wox.Infrastructure.Hotkey
if (keys.Count > 0) if (keys.Count > 0)
{ {
string charKey = keys[0]; string charKey = keys[0];
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey); KeyValuePair<Key, string>? specialSymbolPair = _specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
if (specialSymbolPair.Value.Value != null) if (specialSymbolPair.Value.Value != null)
{ {
CharKey = specialSymbolPair.Value.Key; CharKey = specialSymbolPair.Value.Key;
@@ -152,8 +153,8 @@ namespace Wox.Infrastructure.Hotkey
if (CharKey != Key.None) if (CharKey != Key.None)
{ {
text += specialSymbolDictionary.ContainsKey(CharKey) text += _specialSymbolDictionary.ContainsKey(CharKey)
? specialSymbolDictionary[CharKey] ? _specialSymbolDictionary[CharKey]
: CharKey.ToString(); : CharKey.ToString();
} }
else if (!string.IsNullOrEmpty(text)) else if (!string.IsNullOrEmpty(text))

View File

@@ -14,9 +14,11 @@ namespace Wox.Infrastructure.Image
public class ImageCache public class ImageCache
{ {
private const int MaxCached = 50; 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 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] 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 // 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 // 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. // This function resizes the Usage dictionary, taking the top 'maxCached' number of items and filtering the image icons that are not accessed frequently.
Cleanup(); 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. // To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
foreach (var key in _data.Keys) foreach (var key in _data.Keys)
{ {
int dictValue; if (!Usage.TryGetValue(key, out _) && !(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon) || key.Equals(Constant.LightThemedErrorIcon) || key.Equals(Constant.LightThemedDefaultIcon)))
if (!Usage.TryGetValue(key, out dictValue) && !(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon) || key.Equals(Constant.LightThemedErrorIcon) || key.Equals(Constant.LightThemedDefaultIcon)))
{ {
ImageSource imgSource; _data.TryRemove(key, out _);
_data.TryRemove(key, out imgSource);
} }
} }
} }

View File

@@ -19,11 +19,14 @@ namespace Wox.Infrastructure.Image
public static class ImageLoader public static class ImageLoader
{ {
private static readonly ImageCache ImageCache = new ImageCache(); 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 readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
private static BinaryStorage<Dictionary<string, int>> _storage;
private static IImageHashGenerator _hashGenerator; 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 = private static readonly string[] ImageExtensions =
{ {
@@ -194,7 +197,7 @@ namespace Wox.Infrastructure.Image
return new ImageResult(image, type); 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) public static ImageSource Load(string path, bool loadFullImage = false)
{ {
@@ -202,20 +205,23 @@ namespace Wox.Infrastructure.Image
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)
{ // 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) if (hash != null)
{ {
int ImageCacheValue;
if (GuidToKey.TryGetValue(hash, out string key)) 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]; img = ImageCache[key];
} }
} }
else else
{ // new guid {
// new guid
GuidToKey[hash] = path; GuidToKey[hash] = path;
} }
} }

View File

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

View File

@@ -60,8 +60,7 @@ namespace Wox.Infrastructure.Logger
ExceptionInternal(classNameWithMethod, message, exception); ExceptionInternal(classNameWithMethod, message, exception);
} }
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, string methodName)
string methodName)
{ {
if (string.IsNullOrWhiteSpace(className)) if (string.IsNullOrWhiteSpace(className))
{ {

View File

@@ -5,7 +5,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] [assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")]
namespace Wox.Infrastructure namespace Wox.Infrastructure
{ {

View File

@@ -6,7 +6,7 @@ using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using static Wox.Infrastructure.StringMatcher; using static Wox.Infrastructure.StringMatcher;
[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] [assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")]
namespace Wox.Infrastructure namespace Wox.Infrastructure
{ {

View File

@@ -19,7 +19,7 @@ namespace Wox.Infrastructure.Storage
public class BinaryStorage<T> : IStorage<T> public class BinaryStorage<T> : IStorage<T>
{ {
// This storage helper returns whether or not to delete the binary storage items // This storage helper returns whether or not to delete the binary storage items
private static readonly int BINARY_STORAGE = 0; private static readonly int _binaryStorage = 0;
private StoragePowerToysVersionInfo _storageHelper; private StoragePowerToysVersionInfo _storageHelper;
public BinaryStorage(string filename) public BinaryStorage(string filename)
@@ -36,10 +36,10 @@ namespace Wox.Infrastructure.Storage
public T TryLoad(T defaultData) public T TryLoad(T defaultData)
{ {
_storageHelper = new StoragePowerToysVersionInfo(FilePath, BINARY_STORAGE); _storageHelper = new StoragePowerToysVersionInfo(FilePath, _binaryStorage);
// Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible // Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible
if (_storageHelper.clearCache) if (_storageHelper.ClearCache)
{ {
if (File.Exists(FilePath)) if (File.Exists(FilePath))
{ {

View File

@@ -27,7 +27,7 @@ namespace Wox.Infrastructure.Storage
public string DirectoryPath { get; set; } public string DirectoryPath { get; set; }
// This storage helper returns whether or not to delete the json storage items // This storage helper returns whether or not to delete the json storage items
private static readonly int JSON_STORAGE = 1; private static readonly int _jsonStorage = 1;
private StoragePowerToysVersionInfo _storageHelper; private StoragePowerToysVersionInfo _storageHelper;
internal JsonStorage() internal JsonStorage()
@@ -43,10 +43,10 @@ namespace Wox.Infrastructure.Storage
public T Load() public T Load()
{ {
_storageHelper = new StoragePowerToysVersionInfo(FilePath, JSON_STORAGE); _storageHelper = new StoragePowerToysVersionInfo(FilePath, _jsonStorage);
// Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible // Depending on the version number of the previously installed PT Run, delete the cache if it is found to be incompatible
if (_storageHelper.clearCache) if (_storageHelper.ClearCache)
{ {
if (File.Exists(FilePath)) if (File.Exists(FilePath))
{ {

View File

@@ -10,9 +10,9 @@ namespace Wox.Infrastructure.Storage
public class StoragePowerToysVersionInfo public class StoragePowerToysVersionInfo
{ {
// This detail is accessed by the storage items and is used to decide if the cache must be deleted or not // This detail is accessed by the storage items and is used to decide if the cache must be deleted or not
public bool clearCache = false; public bool ClearCache { get; set; } = false;
private string currentPowerToysVersion = string.Empty; private readonly string currentPowerToysVersion = string.Empty;
private string FilePath { get; set; } = string.Empty; private string FilePath { get; set; } = string.Empty;
@@ -81,7 +81,7 @@ namespace Wox.Infrastructure.Storage
} }
} }
private string GetFilePath(string AssociatedFilePath, int type) private string GetFilePath(string associatedFilePath, int type)
{ {
string suffix = string.Empty; string suffix = string.Empty;
string cacheSuffix = ".cache"; string cacheSuffix = ".cache";
@@ -96,13 +96,13 @@ namespace Wox.Infrastructure.Storage
suffix = jsonSuffix; suffix = jsonSuffix;
} }
string filePath = AssociatedFilePath.Substring(0, AssociatedFilePath.Length - suffix.Length) + "_version.txt"; string filePath = associatedFilePath.Substring(0, associatedFilePath.Length - suffix.Length) + "_version.txt";
return filePath; return filePath;
} }
public StoragePowerToysVersionInfo(string AssociatedFilePath, int type) public StoragePowerToysVersionInfo(string associatedFilePath, int type)
{ {
FilePath = GetFilePath(AssociatedFilePath, type); FilePath = GetFilePath(associatedFilePath, type);
// Get the previous version of PowerToys and cache Storage details from the CacheDetails.json storage file // Get the previous version of PowerToys and cache Storage details from the CacheDetails.json storage file
string previousVersion = GetPreviousVersion(); string previousVersion = GetPreviousVersion();
@@ -112,7 +112,7 @@ namespace Wox.Infrastructure.Storage
// However, we do not want to delete the cache if the same version of powerToys is being launched // However, we do not want to delete the cache if the same version of powerToys is being launched
if (Lessthan(previousVersion, currentPowerToysVersion)) if (Lessthan(previousVersion, currentPowerToysVersion))
{ {
clearCache = true; ClearCache = true;
} }
} }

View File

@@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] [assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")]
namespace Wox.Infrastructure namespace Wox.Infrastructure
{ {

View File

@@ -69,7 +69,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<!--<ItemGroup> <ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs"> <Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link> <Link>GlobalSuppressions.cs</Link>
</Compile> </Compile>
@@ -83,5 +83,5 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup>--> </ItemGroup>
</Project> </Project>