Add Program Plugin

This commit is contained in:
qianlifeng
2014-01-04 20:26:13 +08:00
parent dc51bc39ab
commit 07d002da48
8 changed files with 213 additions and 34 deletions

View File

@@ -1,27 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using WinAlfred.Commands;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using Timer = System.Threading.Timer;
namespace WinAlfred
{
public partial class MainWindow : Window
public partial class MainWindow
{
private KeyboardHook hook = new KeyboardHook();
private List<Result> results = new List<Result>();
private NotifyIcon notifyIcon = null;
private NotifyIcon notifyIcon;
private Command cmdDispatcher;
public MainWindow()
@@ -31,7 +29,7 @@ namespace WinAlfred
hook.KeyPressed += OnHotKey;
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
ThreadPool.SetMaxThreads(10, 5);
ThreadPool.SetMaxThreads(30, 10);
}
private void InitialTray()
@@ -54,11 +52,6 @@ namespace WinAlfred
{
Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Bottom = 10, Left = 10, Right = 10 } : new Thickness { Bottom = 0, Left = 10, Right = 10 };
if (resultCtrl.GetCurrentResultCount() == 1)
{
resultCtrl.SelectFirst();
}
}
private void OnHotKey(object sender, KeyPressedEventArgs e)
@@ -75,17 +68,18 @@ namespace WinAlfred
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
//MessageBox.Show("s");
resultCtrl.Dirty = true;
//auto clear results after 50ms if there are any results returned by plugins
//why we do this? because if we clear resulsts in the start of the text changed event
//we will see the splash. The more closer that clear and addResult method, the less splash we will see.
////auto clear results after 50ms if there are any results returned by plugins
////why we do this? because if we clear resulsts in the start of the text changed event
////we will see the splash. The more closer that clear and addResult method, the less splash we will see.
new Timer(o =>
{
if (resultCtrl.Dirty)
{
resultCtrl.Dispatcher.Invoke(new Action(() => resultCtrl.Clear()));
}
}, null, TimeSpan.FromMilliseconds(50),TimeSpan.FromMilliseconds(-1));
}, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(-1));
if (string.IsNullOrEmpty(tbQuery.Text)) return;
var q = new Query(tbQuery.Text);
@@ -99,17 +93,16 @@ namespace WinAlfred
public void ShowWinAlfred()
{
tbQuery.SelectAll();
Show();
Focus();
FocusManager.SetFocusedElement(this, tbQuery);
//FocusManager.SetFocusedElement(this, tbQuery);
Keyboard.Focus(tbQuery);
tbQuery.SelectAll();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
Plugins.Init(this);
cmdDispatcher = new Command(this);
ShowWinAlfred();
InitialTray();
}
@@ -145,7 +138,7 @@ namespace WinAlfred
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).OrderByDescending(o => o.Score).ToList();
resultCtrl.AddResults(l);
resultCtrl.AddResults(l);
}));
}
}

View File

@@ -1,9 +1,12 @@
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WinAlfred.Plugin;
using Brush = System.Windows.Media.Brush;
namespace WinAlfred
{
@@ -40,10 +43,36 @@ namespace WinAlfred
tbTitle.Text = result.Title;
tbSubTitle.Text = result.SubTitle;
if (!string.IsNullOrEmpty(result.IcoPath) && File.Exists(result.PluginDirectory + result.IcoPath))
string path = string.Empty;
if (!string.IsNullOrEmpty(result.IcoPath) && result.IcoPath.Contains(":\\") && File.Exists(result.IcoPath))
{
imgIco.Source = new BitmapImage(new Uri(result.PluginDirectory + result.IcoPath));
path = result.IcoPath;
}
else if (!string.IsNullOrEmpty(result.IcoPath) && File.Exists(result.PluginDirectory + result.IcoPath))
{
path = result.PluginDirectory + result.IcoPath;
}
if (!string.IsNullOrEmpty(path))
{
if (path.ToLower().EndsWith(".exe") || path.ToLower().EndsWith(".lnk"))
{
imgIco.Source = GetIcon(path);
}
else
{
imgIco.Source = new BitmapImage(new Uri(path));
}
}
}
public static ImageSource GetIcon(string fileName)
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
new Int32Rect(0, 0, icon.Width, icon.Height),
BitmapSizeOptions.FromEmptyOptions());
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using WinAlfred.Plugin;
@@ -32,11 +33,15 @@ namespace WinAlfred
for (int i = 0; i < results.Count; i++)
{
Result result = results[i];
ResultItem control = new ResultItem(result);
control.SetIndex(i + 1);
pnlContainer.Children.Add(control);
if (!CheckExisted(result))
{
ResultItem control = new ResultItem(result);
control.SetIndex(i + 1);
pnlContainer.Children.Insert(GetInsertLocation(result.Score), control);
}
}
SelectFirst();
pnlContainer.UpdateLayout();
double resultItemHeight = 0;
@@ -46,16 +51,43 @@ namespace WinAlfred
if (resultItem != null)
resultItemHeight = resultItem.ActualHeight;
}
pnlContainer.Height = results.Count * resultItemHeight;
pnlContainer.Height = pnlContainer.Children.Count * resultItemHeight;
OnResultItemChangedEvent();
}
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))
{
location = index;
}
}
}
return location;
}
public int GetCurrentResultCount()
{
return pnlContainer.Children.Count;
}
private int GetCurrentSelectedResultIndex()
public int GetCurrentSelectedResultIndex()
{
for (int i = 0; i < pnlContainer.Children.Count; i++)
{