2020-08-06 05:06:55 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-12 01:24:56 +08:00
|
|
|
|
using System.Linq;
|
2020-08-01 06:09:23 +08:00
|
|
|
|
using System.Threading;
|
2020-08-06 05:06:55 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
using PowerLauncher.Helper;
|
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace PowerLauncher.ViewModel
|
|
|
|
|
{
|
|
|
|
|
public class ResultsViewModel : BaseModel
|
|
|
|
|
{
|
|
|
|
|
private readonly object _collectionLock = new object();
|
2020-08-12 01:24:56 +08:00
|
|
|
|
|
2020-08-06 05:06:55 +08:00
|
|
|
|
private readonly Settings _settings;
|
|
|
|
|
|
|
|
|
|
public ResultsViewModel()
|
|
|
|
|
{
|
|
|
|
|
Results = new ResultCollection();
|
|
|
|
|
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 06:32:12 +08:00
|
|
|
|
public ResultsViewModel(Settings settings)
|
|
|
|
|
: this()
|
2020-08-06 05:06:55 +08:00
|
|
|
|
{
|
|
|
|
|
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
|
|
|
|
_settings.PropertyChanged += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.PropertyName == nameof(_settings.MaxResultsToShow))
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(nameof(MaxHeight));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int MaxHeight
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _settings.MaxResultsToShow * 75;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int SelectedIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
private ResultViewModel _selectedItem;
|
|
|
|
|
|
|
|
|
|
public ResultViewModel SelectedItem
|
|
|
|
|
{
|
2020-08-14 06:32:12 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _selectedItem;
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
if (_selectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
_selectedItem.DeactivateContextButtons(ResultViewModel.ActivationType.Selection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_selectedItem = value;
|
|
|
|
|
_selectedItem.ActivateContextButtons(ResultViewModel.ActivationType.Selection);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_selectedItem = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Thickness Margin { get; set; }
|
|
|
|
|
|
|
|
|
|
public Visibility Visibility { get; set; } = Visibility.Hidden;
|
|
|
|
|
|
2020-08-12 01:24:56 +08:00
|
|
|
|
public ResultCollection Results { get; }
|
|
|
|
|
|
2020-08-06 05:06:55 +08:00
|
|
|
|
private static int InsertIndexOf(int newScore, IList<ResultViewModel> list)
|
|
|
|
|
{
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (; index < list.Count; index++)
|
|
|
|
|
{
|
|
|
|
|
var result = list[index];
|
|
|
|
|
if (newScore > result.Result.Score)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-07 11:38:49 +08:00
|
|
|
|
|
2020-08-06 05:06:55 +08:00
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int NewIndex(int i)
|
|
|
|
|
{
|
|
|
|
|
var n = Results.Count;
|
|
|
|
|
if (n > 0)
|
|
|
|
|
{
|
|
|
|
|
i = (n + i) % n;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// SelectedIndex returns -1 if selection is empty.
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextResult()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevResult()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextPage()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex + _settings.MaxResultsToShow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevPage()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(SelectedIndex - _settings.MaxResultsToShow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectFirstResult()
|
|
|
|
|
{
|
|
|
|
|
SelectedIndex = NewIndex(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
Results.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsExcept(PluginMetadata metadata)
|
|
|
|
|
{
|
|
|
|
|
Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveResultsFor(PluginMetadata metadata)
|
|
|
|
|
{
|
|
|
|
|
Results.RemoveAll(r => r.Result.PluginID == metadata.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextTabItem()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing if there is no selected item or we've selected the next context button
|
|
|
|
|
if (!SelectedItem?.SelectNextContextButton() ?? true)
|
|
|
|
|
{
|
|
|
|
|
SelectNextResult();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevTabItem()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing if there is no selected item or we've selected the previous context button
|
|
|
|
|
if (!SelectedItem?.SelectPrevContextButton() ?? true)
|
|
|
|
|
{
|
|
|
|
|
// Tabbing backwards should highlight the last item of the previous row
|
|
|
|
|
SelectPrevResult();
|
|
|
|
|
SelectedItem.SelectLastContextButton();
|
|
|
|
|
}
|
2020-07-23 03:48:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNextContextMenuItem()
|
|
|
|
|
{
|
2020-08-07 11:38:49 +08:00
|
|
|
|
if (SelectedItem != null)
|
2020-07-23 03:48:42 +08:00
|
|
|
|
{
|
2020-08-07 11:38:49 +08:00
|
|
|
|
if (!SelectedItem.SelectNextContextButton())
|
2020-07-23 03:48:42 +08:00
|
|
|
|
{
|
|
|
|
|
SelectedItem.SelectLastContextButton();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 03:48:42 +08:00
|
|
|
|
public void SelectPreviousContextMenuItem()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedItem != null)
|
|
|
|
|
{
|
|
|
|
|
SelectedItem.SelectPrevContextButton();
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 03:48:42 +08:00
|
|
|
|
public bool IsContextMenuItemSelected()
|
|
|
|
|
{
|
|
|
|
|
if (SelectedItem != null && SelectedItem.ContextMenuSelectedIndex != ResultViewModel.NoSelectionIndex)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add new results to ResultCollection
|
|
|
|
|
/// </summary>
|
2020-08-08 10:27:52 +08:00
|
|
|
|
public void AddResults(List<Result> newRawResults, CancellationToken ct)
|
2020-07-28 09:13:47 +08:00
|
|
|
|
{
|
2020-08-01 06:09:23 +08:00
|
|
|
|
if (newRawResults == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(newRawResults));
|
|
|
|
|
}
|
2020-07-23 03:48:42 +08:00
|
|
|
|
|
2020-08-01 06:09:23 +08:00
|
|
|
|
List<ResultViewModel> newResults = new List<ResultViewModel>(newRawResults.Count);
|
2020-08-07 11:38:49 +08:00
|
|
|
|
foreach (Result r in newRawResults)
|
2020-08-01 06:09:23 +08:00
|
|
|
|
{
|
|
|
|
|
newResults.Add(new ResultViewModel(r));
|
|
|
|
|
ct.ThrowIfCancellationRequested();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 05:06:55 +08:00
|
|
|
|
Results.AddRange(newResults);
|
|
|
|
|
}
|
2020-08-12 01:24:56 +08:00
|
|
|
|
|
|
|
|
|
public void Sort()
|
|
|
|
|
{
|
|
|
|
|
var sorted = Results.OrderByDescending(x => x.Result.Score).ToList();
|
|
|
|
|
Clear();
|
|
|
|
|
Results.AddRange(sorted);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 05:06:55 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (textBlock != null)
|
|
|
|
|
{
|
|
|
|
|
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;
|
2020-08-14 06:32:12 +08:00
|
|
|
|
if (textBlock == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
|
|
|
|
|
var inline = (Inline)e.NewValue;
|
|
|
|
|
|
|
|
|
|
textBlock.Inlines.Clear();
|
2020-08-14 06:32:12 +08:00
|
|
|
|
if (inline == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-06 05:06:55 +08:00
|
|
|
|
|
|
|
|
|
textBlock.Inlines.Add(inline);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|