2016-02-18 19:31:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using Wox.Core.UserSettings;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.Storage;
|
|
|
|
|
|
|
|
|
|
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-02-21 23:19:42 +08:00
|
|
|
|
private ResultViewModel _selectedResult;
|
2016-02-21 23:42:37 +08:00
|
|
|
|
public ResultCollection Results { get; } = new ResultCollection();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
private Thickness _margin;
|
|
|
|
|
|
|
|
|
|
private readonly object _resultsUpdateLock = new object();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ViewModel Properties
|
|
|
|
|
|
2016-02-21 23:42:37 +08:00
|
|
|
|
public int MaxHeight => UserSettingStorage.Instance.MaxResultsToShow * 50;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-02-21 23:19:42 +08:00
|
|
|
|
public ResultViewModel SelectedResult
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
return _selectedResult;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (value != null)
|
2016-02-19 21:36:44 +08:00
|
|
|
|
{
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (_selectedResult != null)
|
2016-02-19 21:36:44 +08:00
|
|
|
|
{
|
|
|
|
|
_selectedResult.IsSelected = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectedResult = value;
|
|
|
|
|
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (_selectedResult != null)
|
2016-02-19 21:36:44 +08:00
|
|
|
|
{
|
|
|
|
|
_selectedResult.IsSelected = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-02-19 22:55:58 +08:00
|
|
|
|
|
2016-02-27 08:09:12 +08:00
|
|
|
|
OnPropertyChanged();
|
2016-02-19 22:55:58 +08:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
private bool IsTopMostResult(Result result)
|
|
|
|
|
{
|
|
|
|
|
return TopMostRecordStorage.Instance.IsTopMost(result);
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Public Methods
|
|
|
|
|
|
2016-02-12 14:21:12 +08:00
|
|
|
|
public void SelectResult(int index)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
if (index <= Results.Count - 1)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
SelectedResult = Results[index];
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextResult()
|
|
|
|
|
{
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (SelectedResult != null)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
var index = Results.IndexOf(SelectedResult);
|
|
|
|
|
if (index == Results.Count - 1)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
|
|
|
|
index = -1;
|
|
|
|
|
}
|
2016-02-21 23:42:37 +08:00
|
|
|
|
SelectedResult = Results.ElementAt(index + 1);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevResult()
|
|
|
|
|
{
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (SelectedResult != null)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
var index = Results.IndexOf(SelectedResult);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
index = Results.Count;
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
2016-02-21 23:42:37 +08:00
|
|
|
|
SelectedResult = Results.ElementAt(index - 1);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextPage()
|
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (SelectedResult != null)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
index = Results.IndexOf(SelectedResult);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
index += 5;
|
2016-02-21 23:42:37 +08:00
|
|
|
|
if (index > Results.Count - 1)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
index = Results.Count - 1;
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
2016-02-21 23:42:37 +08:00
|
|
|
|
SelectedResult = Results.ElementAt(index);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevPage()
|
|
|
|
|
{
|
|
|
|
|
var index = 0;
|
2016-02-23 05:47:10 +08:00
|
|
|
|
if (SelectedResult != null)
|
2016-02-12 14:21:12 +08:00
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
index = Results.IndexOf(SelectedResult);
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
index -= 5;
|
|
|
|
|
if (index < 0)
|
|
|
|
|
{
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
2016-02-21 23:42:37 +08:00
|
|
|
|
SelectedResult = Results.ElementAt(index);
|
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)
|
|
|
|
|
{
|
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.RemoveAll(r => r.RawResult.PluginID != metadata.ID);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsFor(PluginMetadata metadata)
|
|
|
|
|
{
|
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.RemoveAll(r => r.RawResult.PluginID == metadata.ID);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddResults(List<Result> newRawResults, string resultId)
|
|
|
|
|
{
|
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
2016-02-21 23:19:42 +08:00
|
|
|
|
var newResults = new List<ResultViewModel>();
|
|
|
|
|
newRawResults.ForEach((re) => { newResults.Add(new ResultViewModel(re)); });
|
2016-02-18 19:31:15 +08:00
|
|
|
|
// todo use async to do new result calculation
|
2016-02-21 23:42:37 +08:00
|
|
|
|
var resultsCopy = Results.ToList();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
var oldResults = resultsCopy.Where(r => r.RawResult.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))
|
|
|
|
|
{
|
|
|
|
|
resultsCopy.Remove(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update scores
|
|
|
|
|
foreach (var result in newResults)
|
|
|
|
|
{
|
|
|
|
|
if (IsTopMostResult(result.RawResult))
|
|
|
|
|
{
|
|
|
|
|
result.RawResult.Score = int.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update index for result in intersection of A and B
|
|
|
|
|
foreach (var commonResult in intersection)
|
|
|
|
|
{
|
|
|
|
|
int oldIndex = resultsCopy.IndexOf(commonResult);
|
|
|
|
|
int oldScore = resultsCopy[oldIndex].RawResult.Score;
|
|
|
|
|
int newScore = newResults[newResults.IndexOf(commonResult)].RawResult.Score;
|
|
|
|
|
if (newScore != oldScore)
|
|
|
|
|
{
|
|
|
|
|
var oldResult = resultsCopy[oldIndex];
|
|
|
|
|
oldResult.RawResult.Score = newScore;
|
|
|
|
|
resultsCopy.RemoveAt(oldIndex);
|
|
|
|
|
int newIndex = InsertIndexOf(newScore, resultsCopy);
|
|
|
|
|
resultsCopy.Insert(newIndex, oldResult);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert result in relative complement of A in B
|
|
|
|
|
foreach (var result in newResults.Except(intersection))
|
|
|
|
|
{
|
|
|
|
|
int newIndex = InsertIndexOf(result.RawResult.Score, resultsCopy);
|
|
|
|
|
resultsCopy.Insert(newIndex, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update UI in one run, so it can avoid UI flickering
|
2016-02-21 23:42:37 +08:00
|
|
|
|
Results.Update(resultsCopy);
|
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 };
|
|
|
|
|
SelectedResult = Results[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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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-02-21 23:19:42 +08:00
|
|
|
|
List<ResultViewModel> itemsToRemove = Items.Where(x => predicate(x)).ToList();
|
2016-02-18 19:31:15 +08:00
|
|
|
|
if (itemsToRemove.Count > 0)
|
|
|
|
|
{
|
2016-02-21 23:42:37 +08:00
|
|
|
|
itemsToRemove.ForEach(item => { Items.Remove(item); });
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
|
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
|
|
|
|
|
// fuck ms
|
|
|
|
|
// http://blogs.msdn.com/b/nathannesbit/archive/2009/04/20/addrange-and-observablecollection.aspx
|
|
|
|
|
// http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx
|
|
|
|
|
// PS: don't use Reset for other data updates, it will cause UI flickering
|
|
|
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
for (int i = 0; i < location; i++)
|
|
|
|
|
{
|
2016-02-21 23:19:42 +08:00
|
|
|
|
ResultViewModel oldResult = Items[i];
|
|
|
|
|
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-02-21 23:19:42 +08:00
|
|
|
|
else if (oldResult.RawResult.Score != newResult.RawResult.Score)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-02-21 23:19:42 +08:00
|
|
|
|
this[i].RawResult.Score = newResult.RawResult.Score;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newCount > oldCount)
|
|
|
|
|
{
|
|
|
|
|
for (int i = oldCount; i < newCount; i++)
|
|
|
|
|
{
|
|
|
|
|
Add(newItems[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int removeIndex = newCount;
|
|
|
|
|
for (int i = newCount; i < oldCount; i++)
|
|
|
|
|
{
|
|
|
|
|
RemoveAt(removeIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-02-21 23:42:37 +08:00
|
|
|
|
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|