ImageLoader now loads everything through IShellItemImageFactory::GetImage (#1836)

* Added thumbnail loader

* Deleted old shell icon extraction logic.
Refactored ImageLoader.Load to improve readibility.

* Moved error handling down into the API call itself

* Minor renamings in ImageLoader

* Load icons only for files that are not images. Fixes stutters when loading folders.

* Added the ability to load a full image through ImageLoader.
ImageLoader.Load now also has a "loadFullImage" parameter.

* Max image cache is now 5000 instead of 200.

* Added some commentaries on how thumbnails are loaded
This commit is contained in:
Boris Makogonyuk
2018-03-31 09:19:55 +02:00
committed by CHU Zhaowei
parent 553a6e8ff6
commit 343b904607
5 changed files with 218 additions and 99 deletions

View File

@@ -2,10 +2,7 @@
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Infrastructure.Logger;
@@ -19,7 +16,7 @@ namespace Wox.Infrastructure.Image
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
private static readonly string[] ImageExtions =
private static readonly string[] ImageExtensions =
{
".png",
".jpg",
@@ -64,128 +61,94 @@ namespace Wox.Infrastructure.Image
ImageCache.Cleanup();
_storage.Save(ImageCache.Usage);
}
private static ImageSource ShellIcon(string fileName)
{
try
{
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx
var shfi = new SHFILEINFO();
var himl = SHGetFileInfo(
fileName,
FILE_ATTRIBUTE_NORMAL,
ref shfi,
(uint)Marshal.SizeOf(shfi),
SHGFI_SYSICONINDEX
);
if (himl != IntPtr.Zero)
{
var hIcon = ImageList_GetIcon(himl, shfi.iIcon, ILD_NORMAL);
// http://stackoverflow.com/questions/1325625/how-do-i-display-a-windows-file-icon-in-wpf
var img = Imaging.CreateBitmapSourceFromHIcon(
hIcon,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
DestroyIcon(hIcon);
return img;
}
else
{
return new BitmapImage(new Uri(Constant.ErrorIcon));
}
}
catch (System.Exception e)
{
Log.Exception($"|ImageLoader.ShellIcon|can't get shell icon for <{fileName}>", e);
return ImageCache[Constant.ErrorIcon];
}
}
public static ImageSource Load(string path)
public static ImageSource Load(string path, bool loadFullImage = false)
{
ImageSource image;
if (string.IsNullOrEmpty(path))
{
image = ImageCache[Constant.ErrorIcon];
}
else if (ImageCache.ContainsKey(path))
{
image = ImageCache[path];
}
else
try
{
if (string.IsNullOrEmpty(path))
{
return ImageCache[Constant.ErrorIcon];
}
if (ImageCache.ContainsKey(path))
{
return ImageCache[path];
}
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
image = new BitmapImage(new Uri(path));
return new BitmapImage(new Uri(path));
}
else if (Path.IsPathRooted(path))
if (!Path.IsPathRooted(path))
{
if (Directory.Exists(path))
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
}
if (Directory.Exists(path))
{
/* Directories can also have thumbnails instead of shell icons.
* Generating thumbnails for a bunch of folders while scrolling through
* results from Everything makes a big impact on performance and
* Wox responsibility.
* - Solution: just load the icon
*/
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
}
else if (File.Exists(path))
{
var extension = Path.GetExtension(path).ToLower();
if (ImageExtensions.Contains(extension))
{
image = ShellIcon(path);
}
else if (File.Exists(path))
{
var externsion = Path.GetExtension(path).ToLower();
if (ImageExtions.Contains(externsion))
if (loadFullImage)
{
image = new BitmapImage(new Uri(path));
image = LoadFullImage(path);
}
else
{
image = ShellIcon(path);
/* Although the documentation for GetImage on MSDN indicates that
* if a thumbnail is available it will return one, this has proved to not
* be the case in many situations while testing.
* - Solution: explicitly pass the ThumbnailOnly flag
*/
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly);
}
}
else
{
image = ImageCache[Constant.ErrorIcon];
path = Constant.ErrorIcon;
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
Constant.ThumbnailSize, ThumbnailOptions.None);
}
}
else
{
var defaultDirectoryPath = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
if (File.Exists(defaultDirectoryPath))
{
image = new BitmapImage(new Uri(defaultDirectoryPath));
}
else
{
image = ImageCache[Constant.ErrorIcon];
path = Constant.ErrorIcon;
}
image = ImageCache[Constant.ErrorIcon];
path = Constant.ErrorIcon;
}
ImageCache[path] = image;
image.Freeze();
}
catch (System.Exception e)
{
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
image = ImageCache[Constant.ErrorIcon];
ImageCache[path] = image;
}
return image;
}
private const int NAMESIZE = 80;
private const int MAX_PATH = 256;
private const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
private const uint ILD_NORMAL = 0x00000000;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SHFILEINFO
private static BitmapImage LoadFullImage(string path)
{
readonly IntPtr hIcon;
internal readonly int iIcon;
readonly uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] readonly string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)] readonly string szTypeName;
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
image.EndInit();
return image;
}
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("User32.dll")]
private static extern int DestroyIcon(IntPtr hIcon);
[DllImport("comctl32.dll")]
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
}
}