2013-12-22 11:35:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using WinAlfred.Helper;
|
|
|
|
|
using WinAlfred.Plugin;
|
|
|
|
|
using WinAlfred.PluginLoader;
|
|
|
|
|
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
|
|
|
|
|
|
|
|
|
namespace WinAlfred
|
|
|
|
|
{
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
|
|
|
|
private KeyboardHook hook = new KeyboardHook();
|
|
|
|
|
public List<PluginPair> plugins = new List<PluginPair>();
|
|
|
|
|
private List<Result> results = new List<Result>();
|
2013-12-22 16:10:46 +00:00
|
|
|
|
private NotifyIcon notifyIcon = null;
|
2013-12-22 11:35:21 +00:00
|
|
|
|
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
hook.KeyPressed += OnHotKey;
|
|
|
|
|
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
|
|
|
|
|
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-22 16:10:46 +00:00
|
|
|
|
private void InitialTray()
|
|
|
|
|
{
|
|
|
|
|
notifyIcon = new NotifyIcon {Text = "WinAlfred", Icon = Properties.Resources.app, Visible = true};
|
|
|
|
|
notifyIcon.Click += (o, e) => ShowWinAlfred();
|
|
|
|
|
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
|
|
|
|
|
open.Click += (o, e) => ShowWinAlfred();
|
|
|
|
|
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
|
|
|
|
|
exit.Click += (o, e) =>
|
|
|
|
|
{
|
|
|
|
|
notifyIcon.Visible = false;
|
|
|
|
|
Close();
|
|
|
|
|
};
|
|
|
|
|
System.Windows.Forms.MenuItem[] childen = { open, exit };
|
|
|
|
|
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-22 11:35:21 +00:00
|
|
|
|
private void resultCtrl_resultItemChangedEvent()
|
|
|
|
|
{
|
|
|
|
|
Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
|
2013-12-22 16:10:46 +00:00
|
|
|
|
resultCtrl.Margin = results.Count > 0 ? new Thickness{ Bottom = 10,Left = 10,Right = 10} : new Thickness { Bottom = 0,Left = 10,Right = 10 };
|
2013-12-22 11:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnHotKey(object sender, KeyPressedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!IsVisible)
|
|
|
|
|
{
|
|
|
|
|
ShowWinAlfred();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
HideWinAlfred();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
results.Clear();
|
|
|
|
|
foreach (PluginPair pair in plugins)
|
|
|
|
|
{
|
|
|
|
|
var q = new Query(tbQuery.Text);
|
|
|
|
|
if (pair.Metadata.ActionKeyword == q.ActionName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
results.AddRange(pair.Plugin.Query(q));
|
|
|
|
|
results.ForEach(o => o.PluginDirectory = pair.Metadata.PluginDirecotry);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception queryException)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name,
|
|
|
|
|
queryException.Message));
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
|
|
|
|
|
resultCtrl.SelectFirst();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 15:53:38 +00:00
|
|
|
|
private void HideWinAlfred()
|
2013-12-22 11:35:21 +00:00
|
|
|
|
{
|
|
|
|
|
Hide();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 15:53:38 +00:00
|
|
|
|
private void ShowWinAlfred()
|
2013-12-22 11:35:21 +00:00
|
|
|
|
{
|
|
|
|
|
tbQuery.SelectAll();
|
|
|
|
|
Focus();
|
|
|
|
|
tbQuery.Focus();
|
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
plugins.AddRange(new PythonPluginLoader().LoadPlugin());
|
|
|
|
|
plugins.AddRange(new CSharpPluginLoader().LoadPlugin());
|
|
|
|
|
|
|
|
|
|
ShowWinAlfred();
|
2013-12-22 16:10:46 +00:00
|
|
|
|
InitialTray();
|
2013-12-22 11:35:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
switch (e.Key)
|
|
|
|
|
{
|
|
|
|
|
case Key.Escape:
|
|
|
|
|
HideWinAlfred();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Key.Down:
|
|
|
|
|
resultCtrl.SelectNext();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Key.Up:
|
|
|
|
|
resultCtrl.SelectPrev();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Key.Enter:
|
|
|
|
|
resultCtrl.AcceptSelect();
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|