From 8888739867ef813a3cc3bb13e5dd04287a1bd5d7 Mon Sep 17 00:00:00 2001 From: Clint Rutkas Date: Wed, 12 Aug 2020 10:44:58 -0700 Subject: [PATCH] fixing all warnings and info for infra (#5891) --- .../launcher/Wox.Infrastructure/Alphabet.cs | 10 +++---- .../launcher/Wox.Infrastructure/Constant.cs | 7 ++--- .../Exception/ExceptionFormatter.cs | 2 +- .../Wox.Infrastructure/Hotkey/HotkeyModel.cs | 15 ++++++----- .../Wox.Infrastructure/Image/ImageCache.cs | 14 +++++----- .../Wox.Infrastructure/Image/ImageLoader.cs | 26 ++++++++++++------- .../Image/WindowsThumbnailProvider.cs | 15 +++++------ .../launcher/Wox.Infrastructure/Logger/Log.cs | 3 +-- .../Wox.Infrastructure/MatchOption.cs | 2 +- .../Wox.Infrastructure/MatchResult.cs | 2 +- .../Storage/BinaryStorage`1.cs | 6 ++--- .../Storage/JsonStorage`1.cs | 6 ++--- .../Storage/StoragePowerToysVersionInfo.cs | 14 +++++----- .../Wox.Infrastructure/StringMatcher.cs | 2 +- .../Wox.Infrastructure.csproj | 4 +-- 15 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/modules/launcher/Wox.Infrastructure/Alphabet.cs b/src/modules/launcher/Wox.Infrastructure/Alphabet.cs index d8b503a0cc..ba057eb373 100644 --- a/src/modules/launcher/Wox.Infrastructure/Alphabet.cs +++ b/src/modules/launcher/Wox.Infrastructure/Alphabet.cs @@ -88,8 +88,8 @@ namespace Wox.Infrastructure _pinyinStorage.Save(GetPinyinCacheAsDictionary()); } - private static string[] EmptyStringArray = new string[0]; - private static string[][] Empty2DStringArray = new string[0][]; + private static readonly string[] _emptyStringArray = new string[0]; + private static readonly string[][] _empty2DStringArray = new string[0][]; /// /// replace chinese character with pinyin, non chinese character won't be modified @@ -100,7 +100,7 @@ namespace Wox.Infrastructure { if (!_settings.ShouldUsePinyin) { - return EmptyStringArray; + return _emptyStringArray; } var pinyin = word.Select(c => @@ -122,7 +122,7 @@ namespace Wox.Infrastructure { if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters)) { - return Empty2DStringArray; + return _empty2DStringArray; } if (!_pinyinCache.ContainsKey(characters)) @@ -181,7 +181,7 @@ namespace Wox.Infrastructure { if (!_settings.ShouldUsePinyin) { - return EmptyStringArray; + return _emptyStringArray; } var combination = ( diff --git a/src/modules/launcher/Wox.Infrastructure/Constant.cs b/src/modules/launcher/Wox.Infrastructure/Constant.cs index bb96308dad..e0abc4e6c7 100644 --- a/src/modules/launcher/Wox.Infrastructure/Constant.cs +++ b/src/modules/launcher/Wox.Infrastructure/Constant.cs @@ -14,14 +14,15 @@ namespace Wox.Infrastructure public const string ExeFileName = "PowerLauncher"; public const string ModuleLocation = "Microsoft\\PowerToys\\PowerToys Run"; public const string Plugins = "Plugins"; + public const string PortableFolderName = "UserData"; private static readonly Assembly Assembly = Assembly.GetExecutingAssembly(); public static readonly string ProgramDirectory = Directory.GetParent(Assembly.Location.NonNull()).ToString(); public static readonly string ExecutablePath = Path.Combine(ProgramDirectory, ExeFileName + ".exe"); - public static bool IsPortableMode; - public const string PortableFolderName = "UserData"; - public static string PortableDataPath = Path.Combine(ProgramDirectory, PortableFolderName); + public static bool IsPortableMode { get; set; } + + public static string PortableDataPath { get; set; } = Path.Combine(ProgramDirectory, PortableFolderName); public static string DetermineDataDirectory() { diff --git a/src/modules/launcher/Wox.Infrastructure/Exception/ExceptionFormatter.cs b/src/modules/launcher/Wox.Infrastructure/Exception/ExceptionFormatter.cs index caeeb29178..8973f0ead2 100644 --- a/src/modules/launcher/Wox.Infrastructure/Exception/ExceptionFormatter.cs +++ b/src/modules/launcher/Wox.Infrastructure/Exception/ExceptionFormatter.cs @@ -190,7 +190,7 @@ namespace Wox.Infrastructure.Exception return result; } - catch (System.Exception e) + catch (System.Exception) { return new List(); } diff --git a/src/modules/launcher/Wox.Infrastructure/Hotkey/HotkeyModel.cs b/src/modules/launcher/Wox.Infrastructure/Hotkey/HotkeyModel.cs index 7a4f8abba2..c6f66a88b4 100644 --- a/src/modules/launcher/Wox.Infrastructure/Hotkey/HotkeyModel.cs +++ b/src/modules/launcher/Wox.Infrastructure/Hotkey/HotkeyModel.cs @@ -21,7 +21,7 @@ namespace Wox.Infrastructure.Hotkey public Key CharKey { get; set; } - Dictionary specialSymbolDictionary = new Dictionary + private readonly Dictionary _specialSymbolDictionary = new Dictionary { { Key.Space, "Space" }, { Key.Oem3, "~" }, @@ -32,6 +32,7 @@ namespace Wox.Infrastructure.Hotkey get { ModifierKeys modifierKeys = ModifierKeys.None; + if (Alt) { modifierKeys = ModifierKeys.Alt; @@ -39,17 +40,17 @@ namespace Wox.Infrastructure.Hotkey if (Shift) { - modifierKeys = modifierKeys | ModifierKeys.Shift; + modifierKeys |= ModifierKeys.Shift; } if (Win) { - modifierKeys = modifierKeys | ModifierKeys.Windows; + modifierKeys |= ModifierKeys.Windows; } if (Ctrl) { - modifierKeys = modifierKeys | ModifierKeys.Control; + modifierKeys |= ModifierKeys.Control; } return modifierKeys; @@ -109,7 +110,7 @@ namespace Wox.Infrastructure.Hotkey if (keys.Count > 0) { string charKey = keys[0]; - KeyValuePair? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey); + KeyValuePair? specialSymbolPair = _specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey); if (specialSymbolPair.Value.Value != null) { CharKey = specialSymbolPair.Value.Key; @@ -152,8 +153,8 @@ namespace Wox.Infrastructure.Hotkey if (CharKey != Key.None) { - text += specialSymbolDictionary.ContainsKey(CharKey) - ? specialSymbolDictionary[CharKey] + text += _specialSymbolDictionary.ContainsKey(CharKey) + ? _specialSymbolDictionary[CharKey] : CharKey.ToString(); } else if (!string.IsNullOrEmpty(text)) diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs index 834fda62b7..52a428cce0 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageCache.cs @@ -14,9 +14,11 @@ namespace Wox.Infrastructure.Image public class ImageCache { private const int MaxCached = 50; - public ConcurrentDictionary Usage = new ConcurrentDictionary(); + private const int PermissibleFactor = 2; + private readonly ConcurrentDictionary _data = new ConcurrentDictionary(); - private const int permissibleFactor = 2; + + public ConcurrentDictionary Usage { get; set; } = new ConcurrentDictionary(); 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 _); } } } diff --git a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs index 07177eedc3..9988745a51 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs @@ -19,11 +19,14 @@ namespace Wox.Infrastructure.Image public static class ImageLoader { private static readonly ImageCache ImageCache = new ImageCache(); - private static BinaryStorage> _storage; private static readonly ConcurrentDictionary GuidToKey = new ConcurrentDictionary(); + + private static BinaryStorage> _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; } } diff --git a/src/modules/launcher/Wox.Infrastructure/Image/WindowsThumbnailProvider.cs b/src/modules/launcher/Wox.Infrastructure/Image/WindowsThumbnailProvider.cs index b82b0073e6..5e157ffe01 100644 --- a/src/modules/launcher/Wox.Infrastructure/Image/WindowsThumbnailProvider.cs +++ b/src/modules/launcher/Wox.Infrastructure/Image/WindowsThumbnailProvider.cs @@ -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) diff --git a/src/modules/launcher/Wox.Infrastructure/Logger/Log.cs b/src/modules/launcher/Wox.Infrastructure/Logger/Log.cs index b7c8a794da..15cc2ff62a 100644 --- a/src/modules/launcher/Wox.Infrastructure/Logger/Log.cs +++ b/src/modules/launcher/Wox.Infrastructure/Logger/Log.cs @@ -60,8 +60,7 @@ namespace Wox.Infrastructure.Logger ExceptionInternal(classNameWithMethod, message, exception); } - private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, - string methodName) + private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message, string methodName) { if (string.IsNullOrWhiteSpace(className)) { diff --git a/src/modules/launcher/Wox.Infrastructure/MatchOption.cs b/src/modules/launcher/Wox.Infrastructure/MatchOption.cs index 53461a1e4d..80b375f3de 100644 --- a/src/modules/launcher/Wox.Infrastructure/MatchOption.cs +++ b/src/modules/launcher/Wox.Infrastructure/MatchOption.cs @@ -5,7 +5,7 @@ using System; using System.Runtime.CompilerServices; -[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] +[assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")] namespace Wox.Infrastructure { diff --git a/src/modules/launcher/Wox.Infrastructure/MatchResult.cs b/src/modules/launcher/Wox.Infrastructure/MatchResult.cs index 266b2eb44f..0e83fdf82c 100644 --- a/src/modules/launcher/Wox.Infrastructure/MatchResult.cs +++ b/src/modules/launcher/Wox.Infrastructure/MatchResult.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using static Wox.Infrastructure.StringMatcher; -[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] +[assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")] namespace Wox.Infrastructure { diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs b/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs index ef1cc76614..9f1ef73df7 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/BinaryStorage`1.cs @@ -19,7 +19,7 @@ namespace Wox.Infrastructure.Storage public class BinaryStorage : IStorage { // 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; public BinaryStorage(string filename) @@ -36,10 +36,10 @@ namespace Wox.Infrastructure.Storage 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 - if (_storageHelper.clearCache) + if (_storageHelper.ClearCache) { if (File.Exists(FilePath)) { diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage`1.cs b/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage`1.cs index 7f34ecca80..3673dddb61 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage`1.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/JsonStorage`1.cs @@ -27,7 +27,7 @@ namespace Wox.Infrastructure.Storage public string DirectoryPath { get; set; } // 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; internal JsonStorage() @@ -43,10 +43,10 @@ namespace Wox.Infrastructure.Storage 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 - if (_storageHelper.clearCache) + if (_storageHelper.ClearCache) { if (File.Exists(FilePath)) { diff --git a/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs b/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs index dea054c347..0c778f34e9 100644 --- a/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs +++ b/src/modules/launcher/Wox.Infrastructure/Storage/StoragePowerToysVersionInfo.cs @@ -10,9 +10,9 @@ namespace Wox.Infrastructure.Storage public class StoragePowerToysVersionInfo { // 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; @@ -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 cacheSuffix = ".cache"; @@ -96,13 +96,13 @@ namespace Wox.Infrastructure.Storage 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; } - 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 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 if (Lessthan(previousVersion, currentPowerToysVersion)) { - clearCache = true; + ClearCache = true; } } diff --git a/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs b/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs index 1b2d4c5a92..7bd58c8685 100644 --- a/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs +++ b/src/modules/launcher/Wox.Infrastructure/StringMatcher.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; -[assembly: InternalsVisibleToAttribute("Microsoft.Plugin.Program.UnitTests")] +[assembly: InternalsVisibleTo("Microsoft.Plugin.Program.UnitTests")] namespace Wox.Infrastructure { diff --git a/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj b/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj index 18e4cba9c5..e66f847902 100644 --- a/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj +++ b/src/modules/launcher/Wox.Infrastructure/Wox.Infrastructure.csproj @@ -69,7 +69,7 @@ PreserveNewest - + \ No newline at end of file