PowerToys/WinAlfred/MainWindow.xaml.cs

362 lines
13 KiB
C#
Raw Normal View History

2013-12-22 19:35:21 +08:00
using System;
using System.Collections.Generic;
2014-01-26 00:37:15 +08:00
using System.Diagnostics;
2013-12-22 19:35:21 +08:00
using System.Linq;
2014-01-26 00:37:15 +08:00
using System.Runtime.InteropServices;
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-07 19:27:51 +08:00
using System.Windows.Media.Animation;
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 Application = System.Windows.Application;
2013-12-22 19:35:21 +08:00
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
2014-01-04 20:26:13 +08:00
using MessageBox = System.Windows.MessageBox;
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;
2014-01-07 19:27:51 +08:00
Storyboard progressBarStoryboard = new Storyboard();
2014-01-07 23:27:05 +08:00
private bool queryHasReturn = false;
SelectedRecords selectedRecords = new SelectedRecords();
2013-12-22 19:35:21 +08:00
2014-01-26 00:37:15 +08:00
private KeyboardListener keyboardListener = new KeyboardListener();
private bool WinRStroked = false;
private WindowsInput.KeyboardSimulator keyboardSimulator = new WindowsInput.KeyboardSimulator(new WindowsInput.InputSimulator());
2014-01-26 00:37:15 +08:00
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);
InitProgressbarAnimation();
try
{
ChangeStyles(Settings.Instance.Theme);
2014-01-26 00:37:15 +08:00
}
catch (System.IO.IOException)
{
ChangeStyles(Settings.Instance.Theme = "Default");
}
2014-01-07 19:27:51 +08:00
}
2014-01-13 18:43:44 +08:00
private void WakeupApp()
{
//After hide winalfred in the background for a long time. It will become very slow in the next show.
2014-01-13 19:32:27 +08:00
//This is caused by the Virtual Mermory Page Mechanisam. So, our solution is execute some codes in every min
2014-01-13 18:43:44 +08:00
//which may prevent sysetem uninstall memory from RAM to disk.
2014-01-26 00:37:15 +08:00
System.Timers.Timer t = new System.Timers.Timer(1000 * 60 * 5) { AutoReset = true, Enabled = true };
2014-01-13 18:43:44 +08:00
t.Elapsed += (o, e) => Dispatcher.Invoke(new Action(() =>
{
if (Visibility != Visibility.Visible)
{
double oldLeft = Left;
Left = 20000;
ShowWinAlfred();
cmdDispatcher.DispatchCommand(new Query("qq"), false);
2014-01-13 18:43:44 +08:00
HideWinAlfred();
Left = oldLeft;
}
}));
2014-01-07 19:27:51 +08:00
}
private void InitProgressbarAnimation()
{
2014-01-07 23:27:05 +08:00
DoubleAnimation da = new DoubleAnimation(progressBar.X2, Width + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
DoubleAnimation da1 = new DoubleAnimation(progressBar.X1, Width, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
2014-01-07 19:27:51 +08:00
progressBarStoryboard.Children.Add(da);
progressBarStoryboard.Children.Add(da1);
progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
progressBar.Visibility = Visibility.Hidden;
progressBar.BeginStoryboard(progressBarStoryboard);
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 };
2014-01-12 21:02:39 +08:00
notifyIcon.Click += (o, e) => ShowWinAlfred();
2013-12-23 00:10:46 +08:00
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("Open");
2014-01-12 21:02:39 +08:00
open.Click += (o, e) => ShowWinAlfred();
2013-12-23 00:10:46 +08:00
System.Windows.Forms.MenuItem exit = new System.Windows.Forms.MenuItem("Exit");
2014-01-05 17:56:02 +08:00
exit.Click += (o, e) => CloseApp();
2013-12-23 00:10:46 +08:00
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()
{
2014-01-26 00:37:15 +08:00
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Top = grid.Margin.Top } : new Thickness { Top = 0 };
2013-12-22 19:35:21 +08:00
}
private void OnHotKey(object sender, KeyPressedEventArgs e)
{
if (!IsVisible)
{
2014-01-12 21:02:39 +08:00
ShowWinAlfred();
2013-12-22 19:35:21 +08:00
}
else
{
HideWinAlfred();
}
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
2014-01-03 23:52:36 +08:00
resultCtrl.Dirty = true;
2014-01-07 19:27:51 +08:00
Dispatcher.DelayInvoke("UpdateSearch",
2014-01-07 23:27:05 +08:00
o =>
2014-01-07 19:27:51 +08:00
{
2014-01-07 23:27:05 +08:00
Dispatcher.DelayInvoke("ClearResults", i =>
{
// first try to use clear method inside resultCtrl, which is more closer to the add new results
// and this will not bring splash issues.After waiting 30ms, if there still no results added, we
// must clear the result. otherwise, it will be confused why the query changed, but the results
// didn't.
if (resultCtrl.Dirty) resultCtrl.Clear();
}, TimeSpan.FromMilliseconds(30), null);
2014-01-07 19:27:51 +08:00
var q = new Query(tbQuery.Text);
cmdDispatcher.DispatchCommand(q);
2014-01-07 23:27:05 +08:00
queryHasReturn = false;
if (Plugins.HitThirdpartyKeyword(q))
{
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
{
if (!queryHasReturn && originQuery == tbQuery.Text)
{
StartProgress();
}
}, TimeSpan.FromSeconds(1), tbQuery.Text);
}
2014-01-07 19:27:51 +08:00
2014-01-07 23:27:05 +08:00
}, TimeSpan.FromMilliseconds(300));
2014-01-07 19:27:51 +08:00
}
private void StartProgress()
{
progressBar.Visibility = Visibility.Visible;
}
private void StopProgress()
{
progressBar.Visibility = Visibility.Hidden;
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-12 21:02:39 +08:00
private void ShowWinAlfred()
{
Show();
Activate();
tbQuery.SelectAll();
2014-01-26 00:37:15 +08:00
Focus();
tbQuery.Focus();
2014-01-12 21:02:39 +08:00
}
public void ParseArgs(string[] args)
2013-12-22 19:35:21 +08:00
{
2014-01-12 18:15:30 +08:00
if (args != null && args.Length > 0)
{
switch (args[0])
{
case "reloadWorkflows":
Plugins.Init(this);
break;
case "query":
if (args.Length > 1)
{
string query = args[1];
tbQuery.Text = query;
2014-01-12 21:02:39 +08:00
tbQuery.SelectAll();
2014-01-12 18:15:30 +08:00
}
break;
}
}
2013-12-22 19:35:21 +08:00
}
2014-01-07 23:27:05 +08:00
private void SetAutoStart(bool IsAtuoRun)
2014-01-06 22:21:08 +08:00
{
2014-01-12 16:28:11 +08:00
//string LnkPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "//WinAlfred.lnk";
//if (IsAtuoRun)
//{
// WshShell shell = new WshShell();
// IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(LnkPath);
// shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
// shortcut.WorkingDirectory = Environment.CurrentDirectory;
// shortcut.WindowStyle = 1; //normal window
// shortcut.Description = "WinAlfred";
// shortcut.Save();
//}
//else
//{
// System.IO.File.Delete(LnkPath);
//}
2014-01-06 22:21:08 +08:00
}
2013-12-22 19:35:21 +08:00
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
2014-01-26 00:37:15 +08:00
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2;
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3;
2014-01-03 23:52:36 +08:00
Plugins.Init(this);
cmdDispatcher = new Command(this);
2013-12-23 00:10:46 +08:00
InitialTray();
2014-01-07 23:27:05 +08:00
selectedRecords.LoadSelectedRecords();
2014-01-06 22:21:08 +08:00
SetAutoStart(true);
2014-01-13 19:32:27 +08:00
WakeupApp();
2014-01-06 19:03:20 +08:00
//var engine = new Jurassic.ScriptEngine();
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
2014-01-26 00:37:15 +08:00
keyboardListener.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
}
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
{
if (Settings.Instance.ReplaceWinR)
{
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
{
Dispatcher.BeginInvoke(new Action(() =>
{
resultCtrl.Clear();
ShowWinAlfred();
ChangeQuery(">");
WinRStroked = true;
}));
return false;
}
if (keyevent == KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin)
{
WinRStroked = false;
keyboardSimulator.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.LWIN, WindowsInput.Native.VirtualKeyCode.CONTROL);
2014-01-26 00:37:15 +08:00
return false;
}
}
return true;
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:
2014-01-07 23:27:05 +08:00
Result result = resultCtrl.AcceptSelect();
if (result != null)
{
selectedRecords.AddSelect(result);
2014-01-15 22:45:02 +08:00
if (!result.DontHideWinAlfredAfterSelect)
2014-01-07 23:27:05 +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)
{
2014-01-07 23:27:05 +08:00
queryHasReturn = true;
progressBar.Dispatcher.Invoke(new Action(StopProgress));
if (list.Count > 0)
2014-01-03 23:52:36 +08:00
{
2014-01-26 00:37:15 +08:00
//todo:this should be opened to users, it's their choise to use it or not in thier workflows
2014-01-07 23:27:05 +08:00
list.ForEach(o =>
{
if (o.AutoAjustScore) o.Score += selectedRecords.GetSelectedCount(o);
2014-01-07 23:27:05 +08:00
});
resultCtrl.Dispatcher.Invoke(new Action(() =>
{
2014-01-14 23:31:24 +08:00
List<Result> l = list.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).ToList();
2014-01-07 23:27:05 +08:00
resultCtrl.AddResults(l);
}));
}
2014-01-03 23:52:36 +08:00
}
2014-01-05 17:56:02 +08:00
public void ChangeStyles(string themeName)
{
ResourceDictionary dict = new ResourceDictionary
{
Source = new Uri("pack://application:,,,/Themes/" + themeName + ".xaml")
};
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(dict);
}
2014-01-05 17:56:02 +08:00
#region Public API
//Those method can be invoked by plugins
public void ChangeQuery(string query)
{
tbQuery.Text = query;
tbQuery.CaretIndex = tbQuery.Text.Length;
}
public void CloseApp()
{
notifyIcon.Visible = false;
2014-01-13 19:32:27 +08:00
Environment.Exit(0);
2014-01-05 17:56:02 +08:00
}
public void HideApp()
{
HideWinAlfred();
}
2014-01-12 21:02:39 +08:00
public void ShowApp()
2014-01-05 17:56:02 +08:00
{
2014-01-12 21:02:39 +08:00
ShowWinAlfred();
2014-01-05 17:56:02 +08:00
}
public void ShowMsg(string title, string subTitle, string iconPath)
{
Msg m = new Msg { Owner = GetWindow(this) };
m.Show(title, subTitle, iconPath);
}
public void OpenSettingDialog()
{
SettingWidow s = new SettingWidow(this);
s.Show();
}
2014-01-05 17:56:02 +08:00
#endregion
2014-01-12 16:28:11 +08:00
2013-12-22 19:35:21 +08:00
}
}