PowerToys/WinAlfred/MainWindow.xaml.cs

145 lines
4.8 KiB
C#
Raw Normal View History

2013-12-22 19:35:21 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2013-12-25 00:21:54 +08:00
using System.Threading;
2013-12-22 19:35:21 +08:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
2014-01-03 18:16:05 +08:00
using WinAlfred.Commands;
2013-12-22 19:35:21 +08:00
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
2014-01-04 20:26:13 +08:00
using MessageBox = System.Windows.MessageBox;
2014-01-03 23:52:36 +08:00
using Timer = System.Threading.Timer;
2013-12-22 19:35:21 +08:00
namespace WinAlfred
{
2014-01-04 20:26:13 +08:00
public partial class MainWindow
2013-12-22 19:35:21 +08:00
{
private KeyboardHook hook = new KeyboardHook();
2014-01-04 20:26:13 +08:00
private NotifyIcon notifyIcon;
2014-01-03 23:52:36 +08:00
private Command cmdDispatcher;
2013-12-22 19:35:21 +08:00
public MainWindow()
{
InitializeComponent();
2014-01-03 23:52:36 +08:00
2013-12-22 19:35:21 +08:00
hook.KeyPressed += OnHotKey;
hook.RegisterHotKey(XModifierKeys.Alt, Keys.Space);
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
2014-01-04 20:26:13 +08:00
ThreadPool.SetMaxThreads(30, 10);
2013-12-22 19:35:21 +08:00
}
2013-12-23 00:10:46 +08:00
private void InitialTray()
{
2013-12-25 00:21:54 +08:00
notifyIcon = new NotifyIcon { Text = "WinAlfred", Icon = Properties.Resources.app, Visible = true };
2013-12-23 00:10:46 +08:00
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 19:35:21 +08:00
private void resultCtrl_resultItemChangedEvent()
{
Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
2014-01-03 23:52:36 +08:00
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Bottom = 10, Left = 10, Right = 10 } : new Thickness { Bottom = 0, Left = 10, Right = 10 };
2013-12-22 19:35:21 +08:00
}
private void OnHotKey(object sender, KeyPressedEventArgs e)
{
if (!IsVisible)
{
ShowWinAlfred();
}
else
{
HideWinAlfred();
}
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
2014-01-04 20:26:13 +08:00
//MessageBox.Show("s");
2014-01-03 23:52:36 +08:00
resultCtrl.Dirty = true;
2014-01-04 20:26:13 +08:00
////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.
2014-01-03 23:52:36 +08:00
new Timer(o =>
{
if (resultCtrl.Dirty)
{
resultCtrl.Dispatcher.Invoke(new Action(() => resultCtrl.Clear()));
}
2014-01-04 20:26:13 +08:00
}, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(-1));
2014-01-03 23:52:36 +08:00
if (string.IsNullOrEmpty(tbQuery.Text)) return;
2014-01-03 18:16:05 +08:00
var q = new Query(tbQuery.Text);
cmdDispatcher.DispatchCommand(q);
2013-12-22 19:35:21 +08:00
}
2013-12-23 23:53:38 +08:00
private void HideWinAlfred()
2013-12-22 19:35:21 +08:00
{
Hide();
}
2014-01-03 23:52:36 +08:00
public void ShowWinAlfred()
2013-12-22 19:35:21 +08:00
{
Show();
2014-01-04 20:26:13 +08:00
//FocusManager.SetFocusedElement(this, tbQuery);
Keyboard.Focus(tbQuery);
tbQuery.SelectAll();
2013-12-22 19:35:21 +08:00
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
2014-01-03 23:52:36 +08:00
Plugins.Init(this);
cmdDispatcher = new Command(this);
2013-12-23 00:10:46 +08:00
InitialTray();
2013-12-22 19:35:21 +08: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();
2013-12-26 20:18:08 +08:00
HideWinAlfred();
2013-12-22 19:35:21 +08:00
e.Handled = true;
break;
}
}
2014-01-03 23:52:36 +08:00
public void OnUpdateResultView(List<Result> list)
{
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).OrderByDescending(o => o.Score).ToList();
2014-01-04 20:26:13 +08:00
resultCtrl.AddResults(l);
2014-01-03 23:52:36 +08:00
}));
}
2013-12-22 19:35:21 +08:00
}
}