2016-02-18 19:31:15 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
2019-12-03 22:08:21 +08:00
|
|
|
|
using System.Windows.Controls;
|
2016-05-06 04:15:13 +08:00
|
|
|
|
using System.Windows.Data;
|
2019-12-03 22:08:21 +08:00
|
|
|
|
using System.Windows.Documents;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace Wox.ViewModel
|
|
|
|
|
{
|
2016-05-24 05:08:13 +08:00
|
|
|
|
public class ResultsViewModel : BaseModel
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
#region Private Fields
|
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
public ResultCollection Results { get; }
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-05-06 04:15:13 +08:00
|
|
|
|
private readonly object _addResultsLock = new object();
|
|
|
|
|
private readonly object _collectionLock = new object();
|
2016-05-24 05:08:13 +08:00
|
|
|
|
private readonly Settings _settings;
|
|
|
|
|
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
2016-05-23 02:14:59 +08:00
|
|
|
|
public ResultsViewModel()
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Results = new ResultCollection();
|
|
|
|
|
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
|
2016-03-28 10:09:57 +08:00
|
|
|
|
}
|
2016-05-24 05:08:13 +08:00
|
|
|
|
public ResultsViewModel(Settings settings) : this()
|
2016-05-23 02:14:59 +08:00
|
|
|
|
{
|
2016-05-24 05:08:13 +08:00
|
|
|
|
_settings = settings;
|
|
|
|
|
_settings.PropertyChanged += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.PropertyName == nameof(_settings.MaxResultsToShow))
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(nameof(MaxHeight));
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-23 02:14:59 +08:00
|
|
|
|
}
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2016-06-23 07:26:57 +08:00
|
|
|
|
#region Properties
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-05-23 02:14:59 +08:00
|
|
|
|
public int MaxHeight => MaxResults * 50;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-05-24 05:17:38 +08:00
|
|
|
|
public int SelectedIndex { get; set; }
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2016-05-06 10:24:14 +08:00
|
|
|
|
public ResultViewModel SelectedItem { get; set; }
|
2016-05-24 05:17:38 +08:00
|
|
|
|
public Thickness Margin { get; set; }
|
2016-06-23 07:26:57 +08:00
|
|
|
|
public Visibility Visbility { get; set; } = Visibility.Collapsed;
|
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];
|
2016-06-23 07:22:41 +08:00
|
|
|
|
if (newScore > result.Result.Score)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 22:57:21 +08:00
|
|
|
|
public void SelectFirstResult()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(0);
|
|
|
|
|
}
|
|
|
|
|
|
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-06-23 07:22:41 +08:00
|
|
|
|
Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsFor(PluginMetadata metadata)
|
|
|
|
|
{
|
2016-06-23 07:22:41 +08:00
|
|
|
|
Results.RemoveAll(r => r.Result.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 results = Results.ToList();
|
2019-12-14 06:17:05 +08:00
|
|
|
|
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
|
2016-06-23 07:22:41 +08:00
|
|
|
|
var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList();
|
2016-05-08 03:08:27 +08:00
|
|
|
|
|
2019-08-31 14:58:15 +08:00
|
|
|
|
// Find the same results in A (old results) and B (new newResults)
|
|
|
|
|
var sameResults = oldResults
|
|
|
|
|
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result)))
|
|
|
|
|
.ToList();
|
2019-12-14 06:17:05 +08:00
|
|
|
|
|
2016-05-08 03:08:27 +08:00
|
|
|
|
// remove result of relative complement of B in A
|
2019-08-31 14:58:15 +08:00
|
|
|
|
foreach (var result in oldResults.Except(sameResults))
|
2016-05-08 03:08:27 +08:00
|
|
|
|
{
|
|
|
|
|
results.Remove(result);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 14:58:15 +08:00
|
|
|
|
// update result with B's score and index position
|
|
|
|
|
foreach (var sameResult in sameResults)
|
2016-05-08 03:08:27 +08:00
|
|
|
|
{
|
2019-08-31 14:58:15 +08:00
|
|
|
|
int oldIndex = results.IndexOf(sameResult);
|
2016-06-23 07:22:41 +08:00
|
|
|
|
int oldScore = results[oldIndex].Result.Score;
|
2019-08-31 14:58:15 +08:00
|
|
|
|
var newResult = newResults[newResults.IndexOf(sameResult)];
|
2016-06-23 07:22:41 +08:00
|
|
|
|
int newScore = newResult.Result.Score;
|
2016-05-08 03:08:27 +08:00
|
|
|
|
if (newScore != oldScore)
|
|
|
|
|
{
|
|
|
|
|
var oldResult = results[oldIndex];
|
|
|
|
|
|
2016-06-23 07:22:41 +08:00
|
|
|
|
oldResult.Result.Score = newScore;
|
|
|
|
|
oldResult.Result.OriginQuery = newResult.Result.OriginQuery;
|
2016-05-08 03:08:27 +08:00
|
|
|
|
|
|
|
|
|
results.RemoveAt(oldIndex);
|
|
|
|
|
int newIndex = InsertIndexOf(newScore, results);
|
|
|
|
|
results.Insert(newIndex, oldResult);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// insert result in relative complement of A in B
|
2019-08-31 14:58:15 +08:00
|
|
|
|
foreach (var result in newResults.Except(sameResults))
|
2016-05-08 03:08:27 +08:00
|
|
|
|
{
|
2016-06-23 07:22:41 +08:00
|
|
|
|
int newIndex = InsertIndexOf(result.Result.Score, results);
|
2016-05-08 03:08:27 +08:00
|
|
|
|
results.Insert(newIndex, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2019-12-03 22:08:21 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region FormattedText Dependency Property
|
|
|
|
|
public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
|
|
|
|
|
"FormattedText",
|
|
|
|
|
typeof(Inline),
|
|
|
|
|
typeof(ResultsViewModel),
|
|
|
|
|
new PropertyMetadata(null, FormattedTextPropertyChanged));
|
|
|
|
|
|
|
|
|
|
public static void SetFormattedText(DependencyObject textBlock, IList<int> value)
|
|
|
|
|
{
|
|
|
|
|
textBlock.SetValue(FormattedTextProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Inline GetFormattedText(DependencyObject textBlock)
|
|
|
|
|
{
|
|
|
|
|
return (Inline)textBlock.GetValue(FormattedTextProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var textBlock = d as TextBlock;
|
|
|
|
|
if (textBlock == null) return;
|
|
|
|
|
|
|
|
|
|
var inline = (Inline)e.NewValue;
|
2016-05-08 03:08:27 +08:00
|
|
|
|
|
2019-12-03 22:08:21 +08:00
|
|
|
|
textBlock.Inlines.Clear();
|
|
|
|
|
if (inline == null) return;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
|
2019-12-03 22:08:21 +08:00
|
|
|
|
textBlock.Inlines.Add(inline);
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 06:17:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the results collection with new results, try to keep identical results
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="newItems"></param>
|
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))
|
2019-12-14 06:17:05 +08:00
|
|
|
|
{ // result is not the same update it in the current index
|
2016-02-21 23:19:42 +08:00
|
|
|
|
this[i] = newResult;
|
2016-02-18 19:31:15 +08:00
|
|
|
|
}
|
2016-06-23 07:22:41 +08:00
|
|
|
|
else if (oldResult.Result.Score != newResult.Result.Score)
|
2016-02-18 19:31:15 +08:00
|
|
|
|
{
|
2016-06-23 07:22:41 +08:00
|
|
|
|
this[i].Result.Score = newResult.Result.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-14 06:17:05 +08:00
|
|
|
|
}
|