Fix issues#13. Add image cache.

This commit is contained in:
qianlifeng
2014-03-10 07:21:26 +08:00
parent 8922cc72fc
commit fe7afe3dec

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
@@ -11,43 +12,57 @@ namespace Wox
{ {
public class ImagePathConverter : IMultiValueConverter public class ImagePathConverter : IMultiValueConverter
{ {
private static Dictionary<string, object> imageCache = new Dictionary<string, object>();
private static ImageSource GetIcon(string fileName) private static ImageSource GetIcon(string fileName)
{ {
Icon icon = Icon.ExtractAssociatedIcon(fileName); Icon icon = Icon.ExtractAssociatedIcon(fileName);
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( if (icon != null)
icon.Handle, {
new Int32Rect(0, 0, icon.Width, icon.Height), return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
BitmapSizeOptions.FromEmptyOptions()); }
return null;
} }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
object img = null;
if (values[0] == null) return null; if (values[0] == null) return null;
string path = values[0].ToString(); string path = values[0].ToString();
string pluginDirectory = values[1].ToString(); string pluginDirectory = values[1].ToString();
string fullPath = Path.Combine(pluginDirectory, path);
if (imageCache.ContainsKey(fullPath))
{
return imageCache[fullPath];
}
string resolvedPath = string.Empty; string resolvedPath = string.Empty;
if (!string.IsNullOrEmpty(path) && path.Contains(":\\") && File.Exists(path)) if (!string.IsNullOrEmpty(path) && path.Contains(":\\") && File.Exists(path))
{ {
resolvedPath = path; resolvedPath = path;
} }
else if (!string.IsNullOrEmpty(path) && File.Exists(Path.Combine(pluginDirectory,path))) else if (!string.IsNullOrEmpty(path) && File.Exists(fullPath))
{ {
resolvedPath = Path.Combine(pluginDirectory, path); resolvedPath = fullPath;
} }
if (resolvedPath.ToLower().EndsWith(".exe") || resolvedPath.ToLower().EndsWith(".lnk")) if (resolvedPath.ToLower().EndsWith(".exe") || resolvedPath.ToLower().EndsWith(".lnk"))
{ {
return GetIcon(resolvedPath); img = GetIcon(resolvedPath);
} }
else if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath))
if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath))
{ {
return new BitmapImage(new Uri(resolvedPath)); img = new BitmapImage(new Uri(resolvedPath));
} }
return null; if (img != null)
{
imageCache.Add(fullPath, img);
}
return img;
} }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)