2016-02-18 19:31:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Windows.Data;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.ViewModel
|
|
|
|
|
{
|
2016-02-21 22:33:18 +08:00
|
|
|
|
public class ResultsViewModel : BaseViewModel
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
#region Private Fields
|
|
|
|
|
|
2016-05-06 10:24:14 +08:00
|
|
|
|
private int _selectedIndex;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
public ResultCollection Results { get; }
|
2016-02-18 19:31:15 +08:00
|
|
|
|
private Thickness _margin;
|
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
private readonly object _addResultsLock = new object();
|
|
|
|
|
private readonly object _collectionLock = new object();
|
|
|
|
|
private readonly Settings _settings;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
public ResultsViewModel(Settings settings)
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Results = new ResultCollection();
|
|
|
|
|
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
|
2016-03-28 10:09:57 +08:00
|
|
|
|
}
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ViewModel Properties
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
public int MaxHeight => _settings.MaxResultsToShow * 50;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-05-06 10:24:14 +08:00
|
|
|
|
public int SelectedIndex
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-05-06 10:24:14 +08:00
|
|
|
|
get { return _selectedIndex; }
|
2016-02-18 19:31:15 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2016-05-06 10:24:14 +08:00
|
|
|
|
_selectedIndex = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 10:24:14 +08:00
|
|
|
|
public ResultViewModel SelectedItem { get; set; }
|
2016-02-18 19:31:15 +08:00
|
|
|
|
public Thickness Margin
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
return _margin;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
_margin = value;
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
|
2016-02-21 23:19:42 +08:00
|
|
|
|
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
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-03-26 09:20:42 +08:00
|
|
|
|
|
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-06 10:24:14 +08:00
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex + _settings.MaxResultsToShow);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevPage()
|
|
|
|
|
{
|
2016-05-06 10:24:14 +08:00
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex - _settings.MaxResultsToShow);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 19:31:15 +08:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.Clear();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsExcept(PluginMetadata metadata)
|
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsFor(PluginMetadata metadata)
|
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Results.RemoveAll(r => r.PluginID == metadata.ID);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// To avoid deadlock, this method should not called from main thread
|
|
|
|
|
/// </summary>
|
2016-02-18 19:31:15 +08:00
|
|
|
|
public void AddResults(List<Result> newRawResults, string resultId)
|
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
lock (_addResultsLock)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-05-08 03:08:27 +08:00
|
|
|
|
var newResults = NewResults(newRawResults, resultId);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
|
|
|
|
// update UI in one run, so it can avoid UI flickering
|
2016-05-08 03:08:27 +08:00
|
|
|
|
Results.Update(newResults);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-02-21 23:42:37 +08:00
|
|
|
|
if (Results.Count > 0)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Margin = new Thickness { Top = 8 };
|
2016-05-06 10:24:14 +08:00
|
|
|
|
SelectedIndex = 0;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Margin = new Thickness { Top = 0 };
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2016-02-21 23:19:42 +08:00
|
|
|
|
public class ResultCollection : ObservableCollection<ResultViewModel>
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
|
2016-02-21 23:19:42 +08:00
|
|
|
|
public void RemoveAll(Predicate<ResultViewModel> predicate)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
CheckReentrancy();
|
|
|
|
|
|
2016-05-08 03:08:27 +08:00
|
|
|
|
for (int i = Count - 1; i >= 0; i--)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-05-08 03:08:27 +08:00
|
|
|
|
if (predicate(this[i]))
|
|
|
|
|
{
|
|
|
|
|
RemoveAt(i);
|
|
|
|
|
}
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 23:19:42 +08:00
|
|
|
|
public void Update(List<ResultViewModel> newItems)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
int newCount = newItems.Count;
|
|
|
|
|
int oldCount = Items.Count;
|
|
|
|
|
int location = newCount > oldCount ? oldCount : newCount;
|
2016-05-08 03:08:27 +08:00
|
|
|
|
|
2016-02-18 19:31:15 +08:00
|
|
|
|
for (int i = 0; i < location; i++)
|
|
|
|
|
{
|
2016-05-08 03:08:27 +08:00
|
|
|
|
ResultViewModel oldResult = this[i];
|
2016-02-21 23:19:42 +08:00
|
|
|
|
ResultViewModel newResult = newItems[i];
|
|
|
|
|
if (!oldResult.Equals(newResult))
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-02-21 23:19:42 +08:00
|
|
|
|
this[i] = newResult;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
2016-04-21 08:53:21 +08:00
|
|
|
|
else if (oldResult.Score != newResult.Score)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
this[i].Score = newResult.Score;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
|
2016-05-08 03:08:27 +08:00
|
|
|
|
if (newCount >= oldCount)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
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-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-05-08 03:08:27 +08:00
|
|
|
|
RemoveAt(i);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|