mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Preload top used images.
This commit is contained in:
@@ -18,7 +18,7 @@ namespace Wox.Converters
|
||||
return null;
|
||||
}
|
||||
|
||||
return ImageLoader.Load(value.ToString());
|
||||
return ImageLoader.ImageLoader.Load(value.ToString());
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
|
||||
54
Wox/ImageLoader/ImageCacheStroage.cs
Normal file
54
Wox/ImageLoader/ImageCacheStroage.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Wox.Infrastructure.Storage;
|
||||
|
||||
namespace Wox.ImageLoader
|
||||
{
|
||||
[Serializable]
|
||||
public class ImageCacheStroage : BinaryStorage<ImageCacheStroage>
|
||||
{
|
||||
public int counter = 0;
|
||||
public const int maxCached = 200;
|
||||
public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>();
|
||||
|
||||
protected override string ConfigName
|
||||
{
|
||||
get { return "ImageCache"; }
|
||||
}
|
||||
|
||||
public void Add(string path)
|
||||
{
|
||||
if (TopUsedImages.ContainsKey(path))
|
||||
{
|
||||
TopUsedImages[path] = TopUsedImages[path] + 1 ;
|
||||
}
|
||||
else
|
||||
{
|
||||
TopUsedImages.Add(path, 1);
|
||||
}
|
||||
|
||||
if (TopUsedImages.Count > maxCached)
|
||||
{
|
||||
TopUsedImages = TopUsedImages.OrderByDescending(o => o.Value)
|
||||
.Take(maxCached)
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
}
|
||||
|
||||
if (++counter == 30)
|
||||
{
|
||||
counter = 0;
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(string path)
|
||||
{
|
||||
if (TopUsedImages.ContainsKey(path))
|
||||
{
|
||||
TopUsedImages.Remove(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Wox.Infrastructure;
|
||||
|
||||
namespace Wox.Helper
|
||||
namespace Wox.ImageLoader
|
||||
{
|
||||
public class ImageLoader
|
||||
{
|
||||
@@ -53,9 +50,42 @@ namespace Wox.Helper
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ImageSource Load(string path)
|
||||
public static void PreloadImages()
|
||||
{
|
||||
//ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy
|
||||
var imageList = new Dictionary<string, int>(ImageCacheStroage.Instance.TopUsedImages);
|
||||
using (new Timeit(string.Format("Preload {0} images",imageList.Count)))
|
||||
{
|
||||
foreach (var image in imageList)
|
||||
{
|
||||
ImageSource img = Load(image.Key, false);
|
||||
if (img != null)
|
||||
{
|
||||
img.Freeze(); //to make it copy to UI thread
|
||||
if (!imageCache.ContainsKey(image.Key))
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (!imageCache.ContainsKey(image.Key))
|
||||
{
|
||||
KeyValuePair<string, int> copyedImg = image;
|
||||
App.Window.Dispatcher.Invoke(new Action(() => imageCache.Add(copyedImg.Key, img)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ImageSource Load(string path,bool addToCache = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path)) return null;
|
||||
if (addToCache)
|
||||
{
|
||||
ImageCacheStroage.Instance.Add(path);
|
||||
}
|
||||
|
||||
if (imageCache.ContainsKey(path))
|
||||
{
|
||||
return imageCache[path];
|
||||
@@ -78,7 +108,7 @@ namespace Wox.Helper
|
||||
}
|
||||
|
||||
|
||||
if (img != null)
|
||||
if (img != null && addToCache)
|
||||
{
|
||||
if (!imageCache.ContainsKey(path))
|
||||
{
|
||||
@@ -16,6 +16,7 @@ using NHotkey;
|
||||
using NHotkey.Wpf;
|
||||
using Wox.Commands;
|
||||
using Wox.Helper;
|
||||
using Wox.ImageLoader;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
@@ -188,9 +189,22 @@ namespace Wox
|
||||
Closing += MainWindow_Closing;
|
||||
//since MainWIndow implement IPublicAPI, so we need to finish ctor MainWindow object before
|
||||
//PublicAPI invoke in plugin init methods. E.g FolderPlugin
|
||||
ThreadPool.QueueUserWorkItem(o => Plugins.Init());
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
Plugins.Init();
|
||||
});
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
PreLoadImages();
|
||||
});
|
||||
ThreadPool.QueueUserWorkItem(o => CheckUpdate());
|
||||
}
|
||||
|
||||
ThreadPool.QueueUserWorkItem(o => checkUpdate());
|
||||
private void PreLoadImages()
|
||||
{
|
||||
ImageLoader.ImageLoader.PreloadImages();
|
||||
}
|
||||
|
||||
void pnlResult_RightMouseClickEvent(Result result)
|
||||
@@ -198,7 +212,7 @@ namespace Wox
|
||||
ShowContextMenu(result);
|
||||
}
|
||||
|
||||
void checkUpdate()
|
||||
void CheckUpdate()
|
||||
{
|
||||
Release release = new UpdateChecker().CheckUpgrade();
|
||||
if (release != null && !UserSettingStorage.Instance.DontPromptUpdateMsg)
|
||||
|
||||
@@ -482,7 +482,7 @@ namespace Wox
|
||||
pluginAuthor.Text = "By: " + pair.Metadata.Author;
|
||||
pluginSubTitle.Text = pair.Metadata.Description;
|
||||
pluginId = pair.Metadata.ID;
|
||||
pluginIcon.Source = ImageLoader.Load(pair.Metadata.FullIcoPath);
|
||||
pluginIcon.Source = ImageLoader.ImageLoader.Load(pair.Metadata.FullIcoPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -499,7 +499,7 @@ namespace Wox
|
||||
tbOpenPluginDirecoty.Visibility = Visibility.Collapsed;
|
||||
pluginActionKeywordTitle.Visibility = Visibility.Collapsed;
|
||||
pluginTitle.Cursor = Cursors.Arrow;
|
||||
pluginIcon.Source = ImageLoader.Load(sys.FullIcoPath);
|
||||
pluginIcon.Source = ImageLoader.ImageLoader.Load(sys.FullIcoPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +104,7 @@
|
||||
<Reference Include="WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ImageLoader\ImageCacheStroage.cs" />
|
||||
<Compile Include="Update\NewVersionWindow.xaml.cs">
|
||||
<DependentUpon>NewVersionWindow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
@@ -136,7 +137,7 @@
|
||||
</Compile>
|
||||
<Compile Include="Helper\FontHelper.cs" />
|
||||
<Compile Include="Helper\HttpProxy.cs" />
|
||||
<Compile Include="Helper\ImageLoader.cs" />
|
||||
<Compile Include="ImageLoader\ImageLoader.cs" />
|
||||
<Compile Include="Helper\SingleInstance.cs" />
|
||||
<Compile Include="Helper\SyntaxSugars.cs" />
|
||||
<Compile Include="Helper\WallpaperPathRetrieval.cs" />
|
||||
|
||||
Reference in New Issue
Block a user