diff --git a/Plugins/Wox.Plugin.Everything/Main.cs b/Plugins/Wox.Plugin.Everything/Main.cs index 2b79949eb9..8ee17eafa9 100644 --- a/Plugins/Wox.Plugin.Everything/Main.cs +++ b/Plugins/Wox.Plugin.Everything/Main.cs @@ -16,8 +16,6 @@ namespace Wox.Plugin.Everything public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable { private readonly EverythingAPI _api = new EverythingAPI(); - private static readonly List ImageExts = new List { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" }; - private static readonly List ExecutableExts = new List { ".exe" }; private const string EverythingProcessName = "Everything"; private const string PortableEverything = "PortableEverything"; @@ -74,7 +72,7 @@ namespace Wox.Plugin.Everything Result r = new Result(); r.Title = Path.GetFileName(path); r.SubTitle = path; - r.IcoPath = GetIconPath(s); + r.IcoPath = path; r.Action = c => { bool hide; @@ -131,28 +129,6 @@ namespace Wox.Plugin.Everything return results; } - private string GetIconPath(SearchResult s) - { - var ext = Path.GetExtension(s.FullPath); - if (s.Type == ResultType.Folder) - { - return "Images\\folder.png"; - } - else if (!string.IsNullOrEmpty(ext)) - { - if (ImageExts.Contains(ext.ToLower())) - { - return "Images\\image.png"; - } - else if (ExecutableExts.Contains(ext.ToLower())) - { - return s.FullPath; - } - } - - return "Images\\file.png"; - } - [DllImport("kernel32.dll")] private static extern int LoadLibrary(string name); diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index bbea213101..ae5aab337a 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -17,9 +17,11 @@ namespace Wox.Infrastructure.Image { public static class ImageLoader { - private static readonly ConcurrentDictionary _imageSources = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary ImageSources = new ConcurrentDictionary(); + private static readonly string DefaultIcon = Path.Combine(Wox.ProgramPath, "Images", "app.png"); + private static readonly string ErrorIcon = Path.Combine(Wox.ProgramPath, "Images", "app_error.png"); - private static readonly List ImageExts = new List + private static readonly string[] ImageExtions = { ".png", ".jpg", @@ -30,16 +32,6 @@ namespace Wox.Infrastructure.Image ".ico" }; - private static readonly List SelfExts = new List - { - ".exe", - ".lnk", - ".ani", - ".cur", - ".sln", - ".appref-ms" - }; - private static readonly ImageCache _cache; private static readonly BinaryStorage _storage; @@ -54,34 +46,67 @@ namespace Wox.Infrastructure.Image _storage.Save(); } - private static ImageSource GetIcon(string fileName) + private static ImageSource ShellIcon(string fileName) { try { - Icon icon = GetFileIcon(fileName) ?? Icon.ExtractAssociatedIcon(fileName); + Icon icon = GetFileIcon(fileName); if (icon != null) { - return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, - new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions()); + var image = ImageFromIcon(icon); + return image; + } + else + { + return ImageSources[ErrorIcon]; } } catch (System.Exception e) { Log.Error(e); + return ImageSources[ErrorIcon]; } - - return null; } + private static ImageSource ImageFromIcon(Icon icon) + { + var image = Imaging.CreateBitmapSourceFromHIcon( + icon.Handle, + new Int32Rect(0, 0, icon.Width, icon.Height), + BitmapSizeOptions.FromEmptyOptions()); + return image; + } + + private static ImageSource AssociatedIcon(string path) + { + try + { + Icon icon = Icon.ExtractAssociatedIcon(path); + if (icon != null) + { + var image = ImageFromIcon(icon); + return image; + } + else + { + return ImageSources[ErrorIcon]; + } + } + catch (System.Exception e) + { + Log.Error(e); + return ImageSources[ErrorIcon]; + } + } public static void PreloadImages() { - var path = Path.Combine(Wox.ProgramPath, "Images", "app.png"); - _imageSources[path] = new BitmapImage(new Uri(path)); + ImageSources[DefaultIcon] = new BitmapImage(new Uri(DefaultIcon)); + ImageSources[ErrorIcon] = new BitmapImage(new Uri(ErrorIcon)); Task.Factory.StartNew(() => { Stopwatch.Debug("Preload images from cache", () => { - _cache.TopUsedImages.AsParallel().Where(i => !_imageSources.ContainsKey(i.Key)).ForAll(i => + _cache.TopUsedImages.AsParallel().Where(i => !ImageSources.ContainsKey(i.Key)).ForAll(i => { var img = Load(i.Key); if (img != null) @@ -91,7 +116,7 @@ namespace Wox.Infrastructure.Image // this line made it possible // should be changed the Dispatcher.InvokeAsync in the future img.Freeze(); - _imageSources[i.Key] = img; + ImageSources[i.Key] = img; } }); }); @@ -100,16 +125,15 @@ namespace Wox.Infrastructure.Image } public static ImageSource Load(string path) - { + { ImageSource image; if (string.IsNullOrEmpty(path)) { - path = Path.Combine(Wox.ProgramPath, "Images", "app.png"); - image = _imageSources[path]; + image = ImageSources[ErrorIcon]; } - else if (_imageSources.ContainsKey(path)) + else if (ImageSources.ContainsKey(path)) { - image = _imageSources[path]; + image = ImageSources[path]; } else { @@ -118,37 +142,44 @@ namespace Wox.Infrastructure.Image if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) { image = new BitmapImage(new Uri(path)); - _imageSources[path] = image; + ImageSources[path] = image; } - else if (File.Exists(path) && Path.IsPathRooted(path)) + else if (Path.IsPathRooted(path)) { - if (SelfExts.Contains(ext)) + if (Directory.Exists(path)) { - image = GetIcon(path); - _imageSources[path] = image; + image = ShellIcon(path); + ImageSources[path] = image; } - else if (ImageExts.Contains(ext)) + else if (File.Exists(path)) { - image = new BitmapImage(new Uri(path)); - _imageSources[path] = image; + if (ImageExtions.Contains(ext)) + { + image = new BitmapImage(new Uri(path)); + ImageSources[path] = image; + } + else + { + image = AssociatedIcon(path); + ImageSources[path] = image; + } } else { - path = Path.Combine(Wox.ProgramPath, "Images", "app.png"); - image = _imageSources[path]; + image = ImageSources[ErrorIcon]; } } else { - path = Path.Combine(Wox.ProgramPath, "Images", Path.GetFileName(path)); - if (File.Exists(path)) + var defaultDirectoryPath = Path.Combine(Wox.ProgramPath, "Images", Path.GetFileName(path)); + if (File.Exists(defaultDirectoryPath)) { - image = new BitmapImage(new Uri(path)); + image = new BitmapImage(new Uri(defaultDirectoryPath)); + ImageSources[path] = image; } else { - path = Path.Combine(Wox.ProgramPath, "Images", "app.png"); - image = _imageSources[path]; + image = ImageSources[ErrorIcon]; } } } diff --git a/Wox.Plugin/Result.cs b/Wox.Plugin/Result.cs index aba637b01a..3f550c9b59 100644 --- a/Wox.Plugin/Result.cs +++ b/Wox.Plugin/Result.cs @@ -49,7 +49,7 @@ namespace Wox.Plugin set { _pluginDirectory = value; - if (!string.IsNullOrEmpty(IcoPath) && Path.IsPathRooted(IcoPath)) + if (!string.IsNullOrEmpty(IcoPath) && !Path.IsPathRooted(IcoPath)) { IcoPath = Path.Combine(value, IcoPath); } diff --git a/Wox/Images/app_error.png b/Wox/Images/app_error.png new file mode 100644 index 0000000000..5106d6e8a9 Binary files /dev/null and b/Wox/Images/app_error.png differ diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index c9f4be0224..170bcfc817 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -198,6 +198,9 @@ PreserveNewest + + PreserveNewest + Designer MSBuild:Compile