Async loading item images => huge performance improvements for old machines.

This commit is contained in:
qianlifeng
2014-07-16 18:52:00 +08:00
parent e7f000147b
commit 3a61a7992a
7 changed files with 71 additions and 25 deletions

View File

@@ -0,0 +1,38 @@
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Data;
using System.Windows.Threading;
namespace Wox.Converters
{
public class AsyncTask : INotifyPropertyChanged
{
public AsyncTask(Func<object> valueFunc)
{
LoadValue(valueFunc);
}
private void LoadValue(Func<object> valueFunc)
{
var frame = new DispatcherFrame();
ThreadPool.QueueUserWorkItem(delegate
{
object returnValue =
AsyncValue = valueFunc();
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("AsyncValue"));
});
}
public event PropertyChangedEventHandler PropertyChanged;
public object AsyncValue
{
get;
set;
}
}
}

View File

@@ -1,30 +1,32 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Wox.Helper;
namespace Wox.Converters
{
public class ImagePathConverter : IMultiValueConverter
public class ImagePathConverter : IValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == null) return null;
if (value == null || value == DependencyProperty.UnsetValue) return null;
string relativePath = values[0].ToString();
string pluginDirectory = values[1].ToString();
string fullPath = Path.Combine(pluginDirectory, relativePath);
string fullPath = value.ToString();
if (relativePath.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
if (fullPath.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
return new BitmapImage(new Uri(relativePath));
return new BitmapImage(new Uri(fullPath));
}
return ImageLoader.Load(fullPath);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;

View File

@@ -18,22 +18,14 @@
<DataTemplate>
<!-- a result item height is 50 including margin -->
<Grid HorizontalAlignment="Stretch" Height="40" VerticalAlignment="Stretch" Margin="5" Cursor="Hand">
<Grid.Resources>
<converters:ImagePathConverter x:Key="ImageConverter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"></ColumnDefinition>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left">
<Image.Resources>
<converters:ImagePathConverter x:Key="ImageConverter"/>
</Image.Resources>
<Image.Source>
<MultiBinding Converter="{StaticResource ImageConverter}">
<MultiBinding.Bindings>
<Binding Path="IcoPath" />
<Binding Path="PluginDirectory" />
</MultiBinding.Bindings>
</MultiBinding>
</Image.Source>
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left" Source="{Binding FullIcoPath,Converter={StaticResource ImageConverter},IsAsync=True}" >
</Image>
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>

View File

@@ -34,10 +34,6 @@ namespace Wox
}
foreach (var result in results)
{
//ThreadPool.QueueUserWorkItem(delegate
// {
// ImageLoader.Load(Path.Combine(result.PluginDirectory, result.IcoPath));
// });
int position = GetInsertLocation(result.Score);
lbResults.Items.Insert(position, result);
}

View File

@@ -112,6 +112,7 @@
<Compile Include="Commands\CommandFactory.cs" />
<Compile Include="Commands\PluginCommand.cs" />
<Compile Include="Commands\SystemCommand.cs" />
<Compile Include="Converters\AsyncConverter.cs" />
<Compile Include="Converters\ConvertorBase.cs" />
<Compile Include="Helper\DataWebRequestFactory.cs" />
<Compile Include="Helper\ErrorReporting\ErrorReporting.cs" />