2013-12-22 19:35:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-01-04 20:26:13 +08:00
|
|
|
|
using System.Linq;
|
2013-12-27 00:39:07 +08:00
|
|
|
|
using System.Windows;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using WinAlfred.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace WinAlfred
|
|
|
|
|
{
|
|
|
|
|
public partial class ResultPanel : UserControl
|
|
|
|
|
{
|
2014-01-03 23:52:36 +08:00
|
|
|
|
public bool Dirty { get; set; }
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
public delegate void ResultItemsChanged();
|
|
|
|
|
|
|
|
|
|
public event ResultItemsChanged resultItemChangedEvent;
|
|
|
|
|
|
|
|
|
|
protected virtual void OnResultItemChangedEvent()
|
|
|
|
|
{
|
|
|
|
|
ResultItemsChanged handler = resultItemChangedEvent;
|
|
|
|
|
if (handler != null) handler();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddResults(List<Result> results)
|
|
|
|
|
{
|
2014-01-03 23:52:36 +08:00
|
|
|
|
if (results.Count == 0) return;
|
|
|
|
|
|
|
|
|
|
if (Dirty)
|
|
|
|
|
{
|
|
|
|
|
Dirty = false;
|
|
|
|
|
pnlContainer.Children.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 00:10:46 +08:00
|
|
|
|
for (int i = 0; i < results.Count; i++)
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
2013-12-23 00:10:46 +08:00
|
|
|
|
Result result = results[i];
|
2014-01-04 20:26:13 +08:00
|
|
|
|
if (!CheckExisted(result))
|
|
|
|
|
{
|
|
|
|
|
ResultItem control = new ResultItem(result);
|
|
|
|
|
control.SetIndex(i + 1);
|
|
|
|
|
pnlContainer.Children.Insert(GetInsertLocation(result.Score), control);
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2013-12-23 00:10:46 +08:00
|
|
|
|
|
2014-01-04 20:26:13 +08:00
|
|
|
|
SelectFirst();
|
2013-12-22 19:35:21 +08:00
|
|
|
|
pnlContainer.UpdateLayout();
|
|
|
|
|
|
|
|
|
|
double resultItemHeight = 0;
|
|
|
|
|
if (pnlContainer.Children.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var resultItem = pnlContainer.Children[0] as ResultItem;
|
|
|
|
|
if (resultItem != null)
|
|
|
|
|
resultItemHeight = resultItem.ActualHeight;
|
|
|
|
|
}
|
2014-01-04 20:26:13 +08:00
|
|
|
|
pnlContainer.Height = pnlContainer.Children.Count * resultItemHeight;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
OnResultItemChangedEvent();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 20:26:13 +08:00
|
|
|
|
private bool CheckExisted(Result result)
|
|
|
|
|
{
|
|
|
|
|
return pnlContainer.Children.Cast<ResultItem>().Any(child => child.Result.Equals(result));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetInsertLocation(int currentScore)
|
|
|
|
|
{
|
|
|
|
|
int location = pnlContainer.Children.Count;
|
|
|
|
|
if (pnlContainer.Children.Count == 0) return 0;
|
|
|
|
|
if (currentScore > ((ResultItem)pnlContainer.Children[0]).Result.Score) return 0;
|
|
|
|
|
|
|
|
|
|
for (int index = 1; index < pnlContainer.Children.Count; index++)
|
|
|
|
|
{
|
|
|
|
|
ResultItem next = pnlContainer.Children[index] as ResultItem;
|
|
|
|
|
ResultItem prev = pnlContainer.Children[index - 1] as ResultItem;
|
|
|
|
|
if (next != null && prev != null)
|
|
|
|
|
{
|
|
|
|
|
if ((currentScore >= next.Result.Score && currentScore <= prev.Result.Score))
|
|
|
|
|
{
|
2014-01-14 23:31:24 +08:00
|
|
|
|
if (currentScore == next.Result.Score)
|
|
|
|
|
{
|
|
|
|
|
location = index + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
location = index;
|
|
|
|
|
}
|
2014-01-04 20:26:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return location;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
public int GetCurrentResultCount()
|
|
|
|
|
{
|
|
|
|
|
return pnlContainer.Children.Count;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 20:26:13 +08:00
|
|
|
|
public int GetCurrentSelectedResultIndex()
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < pnlContainer.Children.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var resultItemControl = pnlContainer.Children[i] as ResultItem;
|
|
|
|
|
if (resultItemControl != null && resultItemControl.Selected)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnSelectAll()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < pnlContainer.Children.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var resultItemControl = pnlContainer.Children[i] as ResultItem;
|
|
|
|
|
if (resultItemControl != null && resultItemControl.Selected)
|
|
|
|
|
{
|
|
|
|
|
resultItemControl.Selected = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNext()
|
|
|
|
|
{
|
|
|
|
|
int index = GetCurrentSelectedResultIndex();
|
|
|
|
|
if (index == pnlContainer.Children.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
index = -1;
|
|
|
|
|
}
|
|
|
|
|
Select(index + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectPrev()
|
|
|
|
|
{
|
|
|
|
|
int index = GetCurrentSelectedResultIndex();
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
index = pnlContainer.Children.Count;
|
|
|
|
|
}
|
|
|
|
|
Select(index - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Select(int index)
|
|
|
|
|
{
|
|
|
|
|
if (pnlContainer.Children.Count > 0)
|
|
|
|
|
{
|
2013-12-27 00:39:07 +08:00
|
|
|
|
int oldIndex = GetCurrentSelectedResultIndex();
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
UnSelectAll();
|
|
|
|
|
var resultItemControl = pnlContainer.Children[index] as ResultItem;
|
2013-12-27 00:39:07 +08:00
|
|
|
|
if (resultItemControl != null)
|
|
|
|
|
{
|
|
|
|
|
resultItemControl.Selected = true;
|
|
|
|
|
|
|
|
|
|
double scrollPosition = 0;
|
|
|
|
|
Point newItemBottomPoint = resultItemControl.TranslatePoint(new Point(0, resultItemControl.ActualHeight), pnlContainer);
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
sv.ScrollToTop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (index == pnlContainer.Children.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
sv.ScrollToBottom();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index < oldIndex)
|
|
|
|
|
{
|
|
|
|
|
//move up and old item is at the top of the scroll view
|
2014-01-03 23:52:36 +08:00
|
|
|
|
if (newItemBottomPoint.Y - sv.VerticalOffset == 0)
|
2013-12-27 00:39:07 +08:00
|
|
|
|
{
|
|
|
|
|
scrollPosition = sv.VerticalOffset - resultItemControl.ActualHeight;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//move down and old item is at the bottom of scroll view
|
|
|
|
|
if (sv.ActualHeight + sv.VerticalOffset == newItemBottomPoint.Y - resultItemControl.ActualHeight)
|
|
|
|
|
{
|
|
|
|
|
scrollPosition = newItemBottomPoint.Y - sv.ActualHeight;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sv.ScrollToVerticalOffset(scrollPosition);
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectFirst()
|
|
|
|
|
{
|
|
|
|
|
Select(0);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 23:27:05 +08:00
|
|
|
|
public Result AcceptSelect()
|
2013-12-22 19:35:21 +08:00
|
|
|
|
{
|
|
|
|
|
int index = GetCurrentSelectedResultIndex();
|
2014-01-07 23:27:05 +08:00
|
|
|
|
if (index < 0) return null;
|
2014-01-06 21:25:24 +08:00
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
var resultItemControl = pnlContainer.Children[index] as ResultItem;
|
|
|
|
|
if (resultItemControl != null)
|
|
|
|
|
{
|
2014-01-05 17:56:02 +08:00
|
|
|
|
if (resultItemControl.Result.Action != null)
|
|
|
|
|
{
|
|
|
|
|
resultItemControl.Result.Action();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 23:27:05 +08:00
|
|
|
|
return resultItemControl.Result;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2014-01-05 17:56:02 +08:00
|
|
|
|
|
2014-01-07 23:27:05 +08:00
|
|
|
|
return null;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResultPanel()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2014-01-03 23:52:36 +08:00
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
pnlContainer.Children.Clear();
|
|
|
|
|
pnlContainer.Height = 0;
|
|
|
|
|
OnResultItemChangedEvent();
|
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|
2013-12-23 23:53:38 +08:00
|
|
|
|
}
|