2013-12-22 19:35:21 +08:00
|
|
|
|
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;
|
|
|
|
|
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];
|
2013-12-22 19:35:21 +08:00
|
|
|
|
ResultItem control = new ResultItem(result);
|
2013-12-23 23:53:38 +08:00
|
|
|
|
control.SetIndex(i + 1);
|
2013-12-22 19:35:21 +08:00
|
|
|
|
pnlContainer.Children.Add(control);
|
|
|
|
|
}
|
2013-12-23 00:10:46 +08:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
2013-12-23 23:53:38 +08:00
|
|
|
|
pnlContainer.Height = results.Count * resultItemHeight;
|
2013-12-22 19:35:21 +08:00
|
|
|
|
OnResultItemChangedEvent();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
public int GetCurrentResultCount()
|
|
|
|
|
{
|
|
|
|
|
return pnlContainer.Children.Count;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-22 19:35:21 +08:00
|
|
|
|
private int GetCurrentSelectedResultIndex()
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AcceptSelect()
|
|
|
|
|
{
|
|
|
|
|
int index = GetCurrentSelectedResultIndex();
|
|
|
|
|
var resultItemControl = pnlContainer.Children[index] as ResultItem;
|
|
|
|
|
if (resultItemControl != null)
|
|
|
|
|
{
|
2013-12-27 20:06:49 +08:00
|
|
|
|
if (resultItemControl.Result.Action != null) resultItemControl.Result.Action();
|
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
|
|
|
|
}
|