PowerToys/Wox/ViewModel/ResultsViewModel.cs

293 lines
8.9 KiB
C#
Raw Normal View History

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;
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;
using Wox.Plugin;
namespace Wox.ViewModel
{
public class ResultsViewModel : BaseModel
{
#region Private Fields
public ResultCollection Results { get; }
private readonly object _addResultsLock = new object();
private readonly object _collectionLock = new object();
private readonly Settings _settings;
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
2016-05-23 02:14:59 +08:00
public ResultsViewModel()
{
Results = new ResultCollection();
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
}
public ResultsViewModel(Settings settings) : this()
2016-05-23 02:14:59 +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
}
#endregion
#region Properties
2016-05-23 02:14:59 +08:00
public int MaxHeight => MaxResults * 50;
2016-05-24 05:17:38 +08:00
public int SelectedIndex { get; set; }
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; }
public Visibility Visbility { get; set; } = Visibility.Collapsed;
#endregion
#region Private Methods
private int InsertIndexOf(int newScore, IList<ResultViewModel> list)
{
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)
{
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 SelectFirstResult()
{
SelectedIndex = NewIndex(0);
}
public void Clear()
{
Results.Clear();
}
public void RemoveResultsExcept(PluginMetadata metadata)
{
2016-06-23 07:22:41 +08:00
Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
}
public void RemoveResultsFor(PluginMetadata metadata)
{
2016-06-23 07:22:41 +08:00
Results.RemoveAll(r => r.Result.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 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;
2019-12-03 22:08:21 +08:00
textBlock.Inlines.Add(inline);
}
#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);
}
}
}
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>
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))
2019-12-14 06:17:05 +08:00
{ // result is not the same update it in the current index
this[i] = newResult;
}
2016-06-23 07:22:41 +08:00
else if (oldResult.Result.Score != newResult.Result.Score)
{
2016-06-23 07:22:41 +08:00
this[i].Result.Score = newResult.Result.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);
}
}
}
}
}
2019-12-14 06:17:05 +08:00
}