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

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
namespace Wox.Plugin { namespace Wox.Plugin {
@@ -9,6 +10,19 @@ namespace Wox.Plugin {
public string SubTitle { get; set; } public string SubTitle { get; set; }
public string IcoPath { get; set; } public string IcoPath { get; set; }
public string FullIcoPath
{
get
{
if (IcoPath.StartsWith("data:"))
{
return IcoPath;
}
return Path.Combine(PluginDirectory, IcoPath);
}
}
/// <summary> /// <summary>
/// return true to hide wox after select result /// return true to hide wox after select result
/// </summary> /// </summary>
@@ -38,6 +52,8 @@ namespace Wox.Plugin {
return r.Title == Title && r.SubTitle == SubTitle; return r.Title == Title && r.SubTitle == SubTitle;
} }
public override string ToString() { public override string ToString() {
return Title + SubTitle; return Title + SubTitle;
} }

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;
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Windows;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using Wox.Helper; using Wox.Helper;
namespace Wox.Converters 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 fullPath = value.ToString();
string pluginDirectory = values[1].ToString();
string fullPath = Path.Combine(pluginDirectory, relativePath);
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); 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; return null;
} }

View File

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

View File

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

View File

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

View File

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