2014-02-21 18:59:47 +08:00
|
|
|
|
using System;
|
2015-11-08 09:44:28 +08:00
|
|
|
|
using System.Collections;
|
2014-02-21 18:59:47 +08:00
|
|
|
|
using System.Collections.Generic;
|
2015-11-07 05:30:38 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Diagnostics;
|
2013-12-27 00:39:07 +08:00
|
|
|
|
using System.Windows;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
using System.Windows.Controls;
|
2014-03-05 22:32:21 +08:00
|
|
|
|
using System.Windows.Input;
|
2014-02-28 23:21:01 +08:00
|
|
|
|
using System.Windows.Media;
|
2015-11-07 05:30:38 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Remoting.Contexts;
|
2015-07-17 15:08:39 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2015-11-07 11:50:26 +08:00
|
|
|
|
using Wox.Helper;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Plugin;
|
2015-02-05 23:29:41 +08:00
|
|
|
|
using Wox.Storage;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
|
2014-01-29 18:33:24 +08:00
|
|
|
|
namespace Wox
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2015-11-07 05:30:38 +08:00
|
|
|
|
[Synchronization]
|
2013-12-22 19:35:21 +08:00
|
|
|
|
public partial class ResultPanel : UserControl
|
|
|
|
|
{
|
2014-10-23 18:39:11 +08:00
|
|
|
|
public event Action<Result> LeftMouseClickEvent;
|
|
|
|
|
public event Action<Result> RightMouseClickEvent;
|
2015-02-03 18:32:16 +08:00
|
|
|
|
public event Action<Result, IDataObject, DragEventArgs> ItemDropEvent;
|
2015-11-08 09:44:28 +08:00
|
|
|
|
private readonly ListBoxItems _results;
|
2015-11-07 05:30:38 +08:00
|
|
|
|
private readonly object _resultsUpdateLock = new object();
|
2014-03-05 22:32:21 +08:00
|
|
|
|
|
2014-10-23 18:39:11 +08:00
|
|
|
|
protected virtual void OnRightMouseClick(Result result)
|
2014-03-05 22:32:21 +08:00
|
|
|
|
{
|
2014-10-23 18:39:11 +08:00
|
|
|
|
Action<Result> handler = RightMouseClickEvent;
|
|
|
|
|
if (handler != null) handler(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnLeftMouseClick(Result result)
|
|
|
|
|
{
|
|
|
|
|
Action<Result> handler = LeftMouseClickEvent;
|
2014-03-05 22:32:21 +08:00
|
|
|
|
if (handler != null) handler(result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
|
2015-07-13 03:13:30 +08:00
|
|
|
|
public int MaxResultsToShow { get { return UserSettingStorage.Instance.MaxResultsToShow * 50; } }
|
|
|
|
|
|
2015-11-07 11:50:26 +08:00
|
|
|
|
internal void RemoveResultsFor(PluginPair plugin)
|
|
|
|
|
{
|
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
|
|
|
|
_results.RemoveAll(r => r.PluginID == plugin.Metadata.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void RemoveResultsExcept(PluginPair plugin)
|
|
|
|
|
{
|
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
|
|
|
|
_results.RemoveAll(r => r.PluginID != plugin.Metadata.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 04:48:18 +08:00
|
|
|
|
public void AddResults(List<Result> newResults, string resultId)
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2015-11-08 04:48:18 +08:00
|
|
|
|
lock (_resultsUpdateLock)
|
2014-01-03 23:52:36 +08:00
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
var resultCopy = _results.ToList();
|
|
|
|
|
var oldResults = resultCopy.Where(r => r.PluginID == resultId).ToList();
|
|
|
|
|
// intersection of A (old results) and B (new newResults)
|
|
|
|
|
var intersection = oldResults.Intersect(newResults).ToList();
|
2015-11-08 04:48:18 +08:00
|
|
|
|
// remove result of relative complement of B in A
|
|
|
|
|
foreach (var result in oldResults.Except(intersection))
|
2015-02-05 23:29:41 +08:00
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
resultCopy.Remove(result);
|
2015-11-08 04:48:18 +08:00
|
|
|
|
}
|
2015-11-07 05:30:38 +08:00
|
|
|
|
|
2015-11-08 04:48:18 +08:00
|
|
|
|
// update scores
|
|
|
|
|
foreach (var result in newResults)
|
|
|
|
|
{
|
|
|
|
|
if (IsTopMostResult(result))
|
2015-11-07 05:30:38 +08:00
|
|
|
|
{
|
2015-11-08 04:48:18 +08:00
|
|
|
|
result.Score = int.MaxValue;
|
2015-11-07 05:30:38 +08:00
|
|
|
|
}
|
2015-11-08 04:48:18 +08:00
|
|
|
|
}
|
2015-11-07 05:30:38 +08:00
|
|
|
|
|
2015-11-08 04:48:18 +08:00
|
|
|
|
// update index for result in intersection of A and B
|
|
|
|
|
foreach (var result in intersection)
|
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
int oldIndex = resultCopy.IndexOf(result);
|
|
|
|
|
int oldScore = resultCopy[oldIndex].Score;
|
2015-11-08 04:48:18 +08:00
|
|
|
|
if (result.Score != oldScore)
|
2015-11-07 05:30:38 +08:00
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
int newIndex = InsertIndexOf(result.Score, resultCopy);
|
2015-11-08 04:48:18 +08:00
|
|
|
|
if (newIndex != oldIndex)
|
2015-11-07 05:30:38 +08:00
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
var item = resultCopy[oldIndex];
|
|
|
|
|
resultCopy.RemoveAt(oldIndex);
|
|
|
|
|
resultCopy.Insert(newIndex, item);
|
2015-11-07 05:30:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-08 04:48:18 +08:00
|
|
|
|
}
|
2015-11-07 05:30:38 +08:00
|
|
|
|
|
2015-11-08 04:48:18 +08:00
|
|
|
|
// insert result in relative complement of A in B
|
|
|
|
|
foreach (var result in newResults.Except(intersection))
|
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
int newIndex = InsertIndexOf(result.Score, resultCopy);
|
|
|
|
|
resultCopy.Insert(newIndex, result);
|
2015-02-05 23:29:41 +08:00
|
|
|
|
}
|
2015-11-08 09:44:28 +08:00
|
|
|
|
|
|
|
|
|
// update UI in one run, so it can avoid UI flickering
|
|
|
|
|
_results.Update(resultCopy);
|
|
|
|
|
|
2015-11-08 04:48:18 +08:00
|
|
|
|
lbResults.Margin = lbResults.Items.Count > 0 ? new Thickness { Top = 8 } : new Thickness { Top = 0 };
|
|
|
|
|
SelectFirst();
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2014-01-04 20:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 23:29:41 +08:00
|
|
|
|
private bool IsTopMostResult(Result result)
|
|
|
|
|
{
|
|
|
|
|
return TopMostRecordStorage.Instance.IsTopMost(result);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 09:44:28 +08:00
|
|
|
|
private int InsertIndexOf(int newScore, IList<Result> list)
|
2014-01-04 20:26:13 +08:00
|
|
|
|
{
|
2015-11-07 05:30:38 +08:00
|
|
|
|
int index = 0;
|
2015-11-08 09:44:28 +08:00
|
|
|
|
for (; index < list.Count; index++)
|
2014-01-04 20:26:13 +08:00
|
|
|
|
{
|
2015-11-08 09:44:28 +08:00
|
|
|
|
var result = list[index];
|
|
|
|
|
if (newScore > result.Score)
|
2014-01-04 20:26:13 +08:00
|
|
|
|
{
|
2015-11-07 05:30:38 +08:00
|
|
|
|
break;
|
2014-01-04 20:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-07 05:30:38 +08:00
|
|
|
|
return index;
|
2014-01-04 20:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
public void SelectNext()
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
int index = lbResults.SelectedIndex;
|
|
|
|
|
if (index == lbResults.Items.Count - 1)
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
|
|
|
|
index = -1;
|
|
|
|
|
}
|
|
|
|
|
Select(index + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrev()
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
int index = lbResults.SelectedIndex;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
index = lbResults.Items.Count;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
Select(index - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 22:50:15 +08:00
|
|
|
|
private void SelectFirst()
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
Select(0);
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 22:50:15 +08:00
|
|
|
|
private void Select(int index)
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
if (index >= 0 && index < lbResults.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
lbResults.SelectedItem = lbResults.Items.GetItemAt(index);
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 18:28:14 +08:00
|
|
|
|
public List<Result> GetVisibleResults()
|
|
|
|
|
{
|
|
|
|
|
List<Result> visibleElements = new List<Result>();
|
2015-02-09 00:13:08 +08:00
|
|
|
|
VirtualizingStackPanel virtualizingStackPanel = GetInnerStackPanel(lbResults);
|
|
|
|
|
for (int i = (int)virtualizingStackPanel.VerticalOffset; i <= virtualizingStackPanel.VerticalOffset + virtualizingStackPanel.ViewportHeight; i++)
|
|
|
|
|
{
|
|
|
|
|
ListBoxItem item = lbResults.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
visibleElements.Add(item.DataContext as Result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return visibleElements;
|
|
|
|
|
}
|
2015-01-23 18:28:14 +08:00
|
|
|
|
|
2015-02-09 00:13:08 +08:00
|
|
|
|
private void UpdateItemNumber()
|
|
|
|
|
{
|
2015-02-09 19:16:13 +08:00
|
|
|
|
//VirtualizingStackPanel virtualizingStackPanel = GetInnerStackPanel(lbResults);
|
|
|
|
|
//int index = 0;
|
|
|
|
|
//for (int i = (int)virtualizingStackPanel.VerticalOffset; i <= virtualizingStackPanel.VerticalOffset + virtualizingStackPanel.ViewportHeight; i++)
|
|
|
|
|
//{
|
|
|
|
|
// index++;
|
|
|
|
|
// ListBoxItem item = lbResults.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
|
|
|
|
|
// if (item != null)
|
|
|
|
|
// {
|
|
|
|
|
// ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item);
|
|
|
|
|
// if (myContentPresenter != null)
|
|
|
|
|
// {
|
|
|
|
|
// DataTemplate dataTemplate = myContentPresenter.ContentTemplate;
|
|
|
|
|
// TextBlock tbItemNumber = (TextBlock)dataTemplate.FindName("tbItemNumber", myContentPresenter);
|
|
|
|
|
// tbItemNumber.Text = index.ToString();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2015-02-09 00:13:08 +08:00
|
|
|
|
}
|
2015-01-23 18:28:14 +08:00
|
|
|
|
|
2015-02-09 00:13:08 +08:00
|
|
|
|
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
|
|
|
|
|
{
|
|
|
|
|
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
|
|
|
|
|
if (child != null && child is childItem)
|
|
|
|
|
return (childItem)child;
|
|
|
|
|
else
|
2015-01-23 18:28:14 +08:00
|
|
|
|
{
|
2015-02-09 00:13:08 +08:00
|
|
|
|
childItem childOfChild = FindVisualChild<childItem>(child);
|
|
|
|
|
if (childOfChild != null)
|
|
|
|
|
return childOfChild;
|
2015-01-23 18:28:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-09 00:13:08 +08:00
|
|
|
|
return null;
|
2015-01-23 18:28:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private VirtualizingStackPanel GetInnerStackPanel(FrameworkElement element)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
|
|
|
|
|
{
|
|
|
|
|
var child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;
|
|
|
|
|
|
|
|
|
|
if (child == null) continue;
|
|
|
|
|
|
|
|
|
|
if (child is VirtualizingStackPanel) return child as VirtualizingStackPanel;
|
|
|
|
|
|
|
|
|
|
var panel = GetInnerStackPanel(child);
|
|
|
|
|
|
|
|
|
|
if (panel != null)
|
|
|
|
|
return panel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-21 22:27:57 +08:00
|
|
|
|
public Result GetActiveResult()
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
int index = lbResults.SelectedIndex;
|
2014-01-07 23:27:05 +08:00
|
|
|
|
if (index < 0) return null;
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
2014-02-22 16:55:15 +08:00
|
|
|
|
return lbResults.Items[index] as Result;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultPanel()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2015-11-07 11:50:26 +08:00
|
|
|
|
_results = new ListBoxItems();
|
2015-11-07 05:30:38 +08:00
|
|
|
|
lbResults.ItemsSource = _results;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2014-01-03 23:52:36 +08:00
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2015-11-07 05:30:38 +08:00
|
|
|
|
lock (_resultsUpdateLock)
|
|
|
|
|
{
|
|
|
|
|
_results.Clear();
|
|
|
|
|
lbResults.Margin = new Thickness { Top = 0 };
|
|
|
|
|
}
|
2014-02-19 22:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void lbResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
|
|
{
|
2014-03-18 00:25:27 +08:00
|
|
|
|
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
|
2014-02-19 22:50:15 +08:00
|
|
|
|
{
|
|
|
|
|
lbResults.ScrollIntoView(e.AddedItems[0]);
|
2015-10-31 07:03:16 +08:00
|
|
|
|
Dispatcher.DelayInvoke("UpdateItemNumber", () =>
|
2015-02-09 00:13:08 +08:00
|
|
|
|
{
|
|
|
|
|
UpdateItemNumber();
|
|
|
|
|
}, TimeSpan.FromMilliseconds(3));
|
2014-02-19 22:50:15 +08:00
|
|
|
|
}
|
2014-01-03 23:52:36 +08:00
|
|
|
|
}
|
2014-03-05 22:32:21 +08:00
|
|
|
|
|
|
|
|
|
private void LbResults_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var item = ItemsControl.ContainerFromElement(lbResults, e.OriginalSource as DependencyObject) as ListBoxItem;
|
2014-10-23 18:39:11 +08:00
|
|
|
|
if (item != null && e.ChangedButton == MouseButton.Left)
|
|
|
|
|
{
|
|
|
|
|
OnLeftMouseClick(item.DataContext as Result);
|
|
|
|
|
}
|
|
|
|
|
if (item != null && e.ChangedButton == MouseButton.Right)
|
2014-03-05 22:32:21 +08:00
|
|
|
|
{
|
2014-10-23 18:39:11 +08:00
|
|
|
|
OnRightMouseClick(item.DataContext as Result);
|
2014-03-05 22:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-17 03:52:52 +08:00
|
|
|
|
|
|
|
|
|
public void SelectNextPage()
|
|
|
|
|
{
|
|
|
|
|
int index = lbResults.SelectedIndex;
|
|
|
|
|
index += 5;
|
|
|
|
|
if (index >= lbResults.Items.Count)
|
|
|
|
|
{
|
|
|
|
|
index = lbResults.Items.Count - 1;
|
|
|
|
|
}
|
|
|
|
|
Select(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrevPage()
|
|
|
|
|
{
|
|
|
|
|
int index = lbResults.SelectedIndex;
|
|
|
|
|
index -= 5;
|
|
|
|
|
if (index < 0)
|
|
|
|
|
{
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
|
|
|
|
Select(index);
|
|
|
|
|
}
|
2015-02-02 23:28:40 +08:00
|
|
|
|
|
|
|
|
|
private void ListBoxItem_OnDrop(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var item = ItemsControl.ContainerFromElement(lbResults, e.OriginalSource as DependencyObject) as ListBoxItem;
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
2015-02-03 18:32:16 +08:00
|
|
|
|
OnItemDropEvent(item.DataContext as Result, e.Data, e);
|
2015-02-02 23:28:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 18:32:16 +08:00
|
|
|
|
protected virtual void OnItemDropEvent(Result obj, IDataObject data, DragEventArgs e)
|
2015-02-02 23:28:40 +08:00
|
|
|
|
{
|
|
|
|
|
var handler = ItemDropEvent;
|
2015-02-03 18:32:16 +08:00
|
|
|
|
if (handler != null) handler(obj, data, e);
|
2015-02-02 23:28:40 +08:00
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2013-12-23 23:53:38 +08:00
|
|
|
|
}
|