Skip to content

Commit

Permalink
Merge pull request #2862 from Wox-launcher/bao
Browse files Browse the repository at this point in the history
faster image loading
  • Loading branch information
bao-qian authored Apr 24, 2020
2 parents 36d72f1 + 024eebe commit d46882c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Wox.Infrastructure/Image/ImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static void Save()

private static ImageSource LoadInternal(string path)
{
Logger.WoxDebug($"image {path}");
ImageSource image;

if (string.IsNullOrEmpty(path))
Expand Down Expand Up @@ -122,7 +121,9 @@ private static ImageSource GetErrorImage()

public static ImageSource Load(string path)
{
Logger.WoxDebug($"load begin {path}");
var img = LoadInternal(path);
Logger.WoxDebug($"load end {path}");
return img;
}
}
Expand Down
22 changes: 2 additions & 20 deletions Wox.Plugin/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public string IcoPath
public delegate ImageSource IconDelegate();

public IconDelegate Icon;
private IList<int> _titleHighlightData;
private IList<int> _subTitleHighlightData;


/// <summary>
Expand All @@ -55,28 +53,12 @@ public string IcoPath
/// <summary>
/// A list of indexes for the characters to be highlighted in Title
/// </summary>
public IList<int> TitleHighlightData
{
get => _titleHighlightData;
set
{
_titleHighlightData = value;
OnPropertyChanged();
}
}
public IList<int> TitleHighlightData { get; set; }

/// <summary>
/// A list of indexes for the characters to be highlighted in SubTitle
/// </summary>
public IList<int> SubTitleHighlightData
{
get => _subTitleHighlightData;
set
{
_subTitleHighlightData = value;
OnPropertyChanged();
}
}
public IList<int> SubTitleHighlightData { get; set; }

/// <summary>
/// Only results that originQuery match with current query will be displayed in the panel
Expand Down
2 changes: 1 addition & 1 deletion Wox/ResultListBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<ColumnDefinition Width="0" />
</Grid.ColumnDefinitions>
<Image x:Name="imgIco" Width="32" Height="32" HorizontalAlignment="Left"
Source="{Binding Image ,IsAsync=True}" />
Source="{Binding Image.Value, IsAsync=True}" />
<Grid Margin="5 0 5 0" Grid.Column="1" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition />
Expand Down
2 changes: 0 additions & 2 deletions Wox/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,11 @@ private void QueryResults()
// nothing to do here
}

Logger.WoxInfo($"canceled {query} {token.IsCancellationRequested}");
if (!token.IsCancellationRequested)
{ // update to hidden if this is still the current query
ProgressBarVisibility = Visibility.Hidden;
updateSource.Cancel();
updateSource.Dispose();
Logger.WoxInfo($"progressbard hidden {query} {token.IsCancellationRequested} {ProgressBarVisibility}");
}
}, token);
}
Expand Down
49 changes: 22 additions & 27 deletions Wox/ViewModel/ResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,44 @@ public class ResultViewModel : BaseModel
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private Result _result;

public ResultViewModel(Result result)
{
if (result != null)
{
Result = result;
Image = new Lazy<ImageSource>(() => {
return SetImage(result);
});
}
}

public ImageSource Image
private ImageSource SetImage(Result result)
{
get
string imagePath = result.IcoPath;
if (string.IsNullOrEmpty(imagePath) && result.Icon != null)
{
var imagePath = Result.IcoPath;
if (string.IsNullOrEmpty(imagePath) && Result.Icon != null)
try
{
return result.Icon();
}
catch (Exception e)
{
try
{
return Result.Icon();
}
catch (Exception e)
{
Logger.WoxError($"IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e);
imagePath = Constant.ErrorIcon;
}
Logger.WoxError($"IcoPath is empty and exception when calling Icon() for result <{Result.Title}> of plugin <{Result.PluginDirectory}>", e);
imagePath = Constant.ErrorIcon;
}

// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
}
}

public Result Result
{
get => _result;
set
{
_result = value;
OnPropertyChanged();
}
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
}

// directly binding will cause unnecessory image load
// only binding get will cause load twice or more
// so use lazy binding
public Lazy<ImageSource> Image { get; set; }

public Result Result { get; set; }

public override bool Equals(object obj)
{
var r = obj as ResultViewModel;
Expand Down

0 comments on commit d46882c

Please sign in to comment.