2014-02-21 18:59:47 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
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;
|
2015-07-13 03:13:30 +08:00
|
|
|
|
using Wox.Infrastructure.Storage.UserSettings;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Plugin;
|
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
|
|
|
|
{
|
|
|
|
|
public partial class ResultPanel : UserControl
|
|
|
|
|
{
|
2014-10-23 18:39:11 +08:00
|
|
|
|
public event Action<Result> LeftMouseClickEvent;
|
|
|
|
|
public event Action<Result> RightMouseClickEvent;
|
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
|
|
|
|
public bool Dirty { get; set; }
|
|
|
|
|
|
2015-07-13 03:13:30 +08:00
|
|
|
|
public int MaxResultsToShow { get { return UserSettingStorage.Instance.MaxResultsToShow * 50; } }
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
public void AddResults(List<Result> results)
|
|
|
|
|
{
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
if (Dirty)
|
|
|
|
|
{
|
|
|
|
|
Dirty = false;
|
2014-02-19 22:50:15 +08:00
|
|
|
|
lbResults.Items.Clear();
|
2014-01-03 23:52:36 +08:00
|
|
|
|
}
|
2014-02-19 22:50:15 +08:00
|
|
|
|
foreach (var result in results)
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
int position = GetInsertLocation(result.Score);
|
|
|
|
|
lbResults.Items.Insert(position, result);
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2014-03-20 21:41:30 +08:00
|
|
|
|
lbResults.Margin = lbResults.Items.Count > 0 ? new Thickness { Top = 8 } : new Thickness { Top = 0 };
|
2014-01-04 20:26:13 +08:00
|
|
|
|
SelectFirst();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetInsertLocation(int currentScore)
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
int location = lbResults.Items.Count;
|
|
|
|
|
if (lbResults.Items.Count == 0) return 0;
|
|
|
|
|
if (currentScore > ((Result)lbResults.Items[0]).Score) return 0;
|
2014-01-04 20:26:13 +08:00
|
|
|
|
|
2014-02-19 22:50:15 +08:00
|
|
|
|
for (int index = 1; index < lbResults.Items.Count; index++)
|
2014-01-04 20:26:13 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
Result next = lbResults.Items[index] as Result;
|
|
|
|
|
Result prev = lbResults.Items[index - 1] as Result;
|
2014-01-04 20:26:13 +08:00
|
|
|
|
if (next != null && prev != null)
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
if ((currentScore >= next.Score && currentScore <= prev.Score))
|
2014-01-04 20:26:13 +08:00
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
if (currentScore == next.Score)
|
2014-01-14 23:31:24 +08:00
|
|
|
|
{
|
|
|
|
|
location = index + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
location = index;
|
|
|
|
|
}
|
2014-01-04 20:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2014-01-03 23:52:36 +08:00
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2014-02-19 22:50:15 +08:00
|
|
|
|
lbResults.Items.Clear();
|
2014-03-20 21:41:30 +08:00
|
|
|
|
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]);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2013-12-23 23:53:38 +08:00
|
|
|
|
}
|