PowerToys/Wox/ViewModel/ResultsViewModel.cs

264 lines
7.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using Wox.Core.UserSettings;
using Wox.Plugin;
namespace Wox.ViewModel
{
public class ResultsViewModel : BaseViewModel
{
#region Private Fields
2016-05-06 10:24:14 +08:00
private int _selectedIndex;
public ResultCollection Results { get; }
private Thickness _margin;
private readonly object _addResultsLock = new object();
private readonly object _collectionLock = new object();
2016-05-23 02:14:59 +08:00
public int MaxResults = 6;
2016-05-23 02:14:59 +08:00
public ResultsViewModel()
{
Results = new ResultCollection();
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
}
2016-05-23 02:14:59 +08:00
public ResultsViewModel(int maxResults) : this()
{
MaxResults = maxResults;
}
#endregion
#region ViewModel Properties
2016-05-23 02:14:59 +08:00
public int MaxHeight => MaxResults * 50;
2016-05-06 10:24:14 +08:00
public int SelectedIndex
{
2016-05-06 10:24:14 +08:00
get { return _selectedIndex; }
set
{
2016-05-06 10:24:14 +08:00
_selectedIndex = value;
OnPropertyChanged();
}
}
2016-05-06 10:24:14 +08:00
public ResultViewModel SelectedItem { get; set; }
public Thickness Margin
{
get
{
return _margin;
}
set
{
_margin = value;
OnPropertyChanged();
}
}
#endregion
#region Private Methods
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
{
int index = 0;
for (; index < list.Count; index++)
{
var result = list[index];
if (newScore > result.RawResult.Score)
{
break;
}
}
return index;
}
2016-05-06 10:24:14 +08:00
private int NewIndex(int i)
2016-02-12 14:21:12 +08:00
{
2016-05-06 10:24:14 +08:00
var n = Results.Count;
if (n > 0)
{
i = (n + i) % n;
return i;
}
else
2016-02-12 14:21:12 +08:00
{
2016-05-06 10:24:14 +08:00
// SelectedIndex returns -1 if selection is empty.
return -1;
2016-02-12 14:21:12 +08:00
}
}
2016-05-06 10:24:14 +08:00
#endregion
#region Public Methods
2016-02-12 14:21:12 +08:00
public void SelectNextResult()
{
2016-05-06 10:24:14 +08:00
SelectedIndex = NewIndex(SelectedIndex + 1);
2016-02-12 14:21:12 +08:00
}
public void SelectPrevResult()
{
2016-05-06 10:24:14 +08:00
SelectedIndex = NewIndex(SelectedIndex - 1);
2016-02-12 14:21:12 +08:00
}
public void SelectNextPage()
{
2016-05-23 02:14:59 +08:00
SelectedIndex = NewIndex(SelectedIndex + MaxResults);
2016-02-12 14:21:12 +08:00
}
public void SelectPrevPage()
{
2016-05-23 02:14:59 +08:00
SelectedIndex = NewIndex(SelectedIndex - MaxResults);
2016-02-12 14:21:12 +08:00
}
public void Clear()
{
Results.Clear();
}
public void RemoveResultsExcept(PluginMetadata metadata)
{
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
}
public void RemoveResultsFor(PluginMetadata metadata)
{
Results.RemoveAll(r => r.PluginID == metadata.ID);
}
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </summary>
public void AddResults(List<Result> newRawResults, string resultId)
{
lock (_addResultsLock)
{
2016-05-08 03:08:27 +08:00
var newResults = NewResults(newRawResults, resultId);
// update UI in one run, so it can avoid UI flickering
2016-05-08 03:08:27 +08:00
Results.Update(newResults);
if (Results.Count > 0)
{
Margin = new Thickness { Top = 8 };
2016-05-06 10:24:14 +08:00
SelectedIndex = 0;
}
else
{
Margin = new Thickness { Top = 0 };
}
}
}
2016-05-08 03:08:27 +08:00
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
{
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
var results = Results.ToList();
var oldResults = results.Where(r => r.PluginID == resultId).ToList();
// intersection of A (old results) and B (new newResults)
var intersection = oldResults.Intersect(newResults).ToList();
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(intersection))
{
results.Remove(result);
}
// update index for result in intersection of A and B
foreach (var commonResult in intersection)
{
int oldIndex = results.IndexOf(commonResult);
int oldScore = results[oldIndex].Score;
var newResult = newResults[newResults.IndexOf(commonResult)];
int newScore = newResult.Score;
if (newScore != oldScore)
{
var oldResult = results[oldIndex];
oldResult.Score = newScore;
oldResult.OriginQuery = newResult.OriginQuery;
results.RemoveAt(oldIndex);
int newIndex = InsertIndexOf(newScore, results);
results.Insert(newIndex, oldResult);
}
}
// insert result in relative complement of A in B
foreach (var result in newResults.Except(intersection))
{
int newIndex = InsertIndexOf(result.Score, results);
results.Insert(newIndex, result);
}
return results;
}
#endregion
public class ResultCollection : ObservableCollection<ResultViewModel>
{
public void RemoveAll(Predicate<ResultViewModel> predicate)
{
CheckReentrancy();
2016-05-08 03:08:27 +08:00
for (int i = Count - 1; i >= 0; i--)
{
2016-05-08 03:08:27 +08:00
if (predicate(this[i]))
{
RemoveAt(i);
}
}
}
public void Update(List<ResultViewModel> newItems)
{
int newCount = newItems.Count;
int oldCount = Items.Count;
int location = newCount > oldCount ? oldCount : newCount;
2016-05-08 03:08:27 +08:00
for (int i = 0; i < location; i++)
{
2016-05-08 03:08:27 +08:00
ResultViewModel oldResult = this[i];
ResultViewModel newResult = newItems[i];
if (!oldResult.Equals(newResult))
{
this[i] = newResult;
}
else if (oldResult.Score != newResult.Score)
{
this[i].Score = newResult.Score;
}
}
2016-05-08 03:08:27 +08:00
if (newCount >= oldCount)
{
for (int i = oldCount; i < newCount; i++)
{
Add(newItems[i]);
}
}
else
{
2016-05-08 03:08:27 +08:00
for (int i = oldCount - 1; i >= newCount; i--)
{
2016-05-08 03:08:27 +08:00
RemoveAt(i);
}
}
}
}
}
}