PowerToys/Wox/MainWindow.xaml.cs

437 lines
14 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2014-03-02 11:04:30 +08:00
using System.Runtime.CompilerServices;
2014-01-29 18:33:24 +08:00
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Animation;
2014-02-22 11:55:48 +08:00
using WindowsInput;
using WindowsInput.Native;
2014-02-20 23:15:03 +08:00
using NHotkey;
using NHotkey.Wpf;
2014-01-29 18:33:24 +08:00
using Wox.Commands;
2014-03-02 11:04:30 +08:00
using Wox.Helper;
using Wox.Infrastructure;
2014-02-22 15:52:20 +08:00
using Wox.Infrastructure.UserSettings;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
using Wox.PluginLoader;
using Application = System.Windows.Application;
2014-02-22 11:55:48 +08:00
using ContextMenu = System.Windows.Forms.ContextMenu;
2014-01-29 18:33:24 +08:00
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
2014-02-22 11:55:48 +08:00
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;
2014-02-28 23:21:01 +08:00
using ToolTip = System.Windows.Controls.ToolTip;
2014-01-29 18:33:24 +08:00
namespace Wox
{
public partial class MainWindow
{
2014-02-22 11:55:48 +08:00
private static readonly object locker = new object();
2014-03-11 23:54:37 +08:00
public static bool Initialized = false;
2014-01-29 18:33:24 +08:00
2014-02-22 16:28:23 +08:00
private static readonly List<Result> waitShowResultList = new List<Result>();
private readonly GloablHotkey globalHotkey = new GloablHotkey();
2014-02-22 11:55:48 +08:00
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly Storyboard progressBarStoryboard = new Storyboard();
private bool WinRStroked;
private NotifyIcon notifyIcon;
private bool queryHasReturn;
2014-02-28 23:21:01 +08:00
private ToolTip toolTip = new ToolTip();
2014-01-29 18:33:24 +08:00
public MainWindow()
{
InitializeComponent();
2014-03-11 23:54:37 +08:00
Initialized = true;
2014-03-11 21:52:29 +08:00
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
2014-02-28 23:21:01 +08:00
progressBar.ToolTip = toolTip;
InitialTray();
resultCtrl.OnMouseClickItem += AcceptSelect;
2014-02-22 16:28:23 +08:00
2014-01-29 18:33:24 +08:00
ThreadPool.SetMaxThreads(30, 10);
try
{
SetTheme(CommonStorage.Instance.UserSetting.Theme);
}
catch (IOException)
{
SetTheme(CommonStorage.Instance.UserSetting.Theme = "Default");
}
2014-03-09 17:01:39 +08:00
SetHotkey(CommonStorage.Instance.UserSetting.Hotkey, OnHotkey);
SetCustomPluginHotkey();
2014-03-11 23:54:37 +08:00
globalHotkey.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2;
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3;
Plugins.Init();
2014-03-15 00:17:37 +08:00
InitProgressbarAnimation();
2014-02-22 11:55:48 +08:00
}
2014-03-11 21:52:29 +08:00
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
string error = "Wox has an error that can't be handled. " + e.ExceptionObject;
Log.Error(error);
if (e.IsTerminating)
{
2014-03-11 23:54:37 +08:00
notifyIcon.Visible = false;
2014-03-11 21:52:29 +08:00
MessageBox.Show(error);
}
}
}
2014-02-22 16:28:23 +08:00
public void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
2014-02-22 11:55:48 +08:00
{
2014-02-22 16:28:23 +08:00
var hotkey = new HotkeyModel(hotkeyStr);
2014-02-22 11:55:48 +08:00
try
{
2014-02-22 16:28:23 +08:00
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
2014-02-22 11:55:48 +08:00
}
catch (Exception)
{
MessageBox.Show("Registe hotkey: " + hotkeyStr + " failed.");
2014-02-22 11:55:48 +08:00
}
2014-02-22 15:52:20 +08:00
}
2014-02-22 11:55:48 +08:00
2014-02-22 15:52:20 +08:00
public void RemoveHotkey(string hotkeyStr)
{
2014-02-22 16:28:23 +08:00
if (!string.IsNullOrEmpty(hotkeyStr))
{
HotkeyManager.Current.Remove(hotkeyStr);
}
2014-02-22 15:52:20 +08:00
}
private void SetCustomPluginHotkey()
{
2014-02-22 16:28:23 +08:00
if (CommonStorage.Instance.UserSetting.CustomPluginHotkeys == null) return;
2014-02-22 15:52:20 +08:00
foreach (CustomPluginHotkey hotkey in CommonStorage.Instance.UserSetting.CustomPluginHotkeys)
{
CustomPluginHotkey hotkey1 = hotkey;
2014-02-22 16:28:23 +08:00
SetHotkey(hotkey.Hotkey, delegate
2014-02-22 15:52:20 +08:00
{
ShowApp();
2014-02-28 23:21:01 +08:00
ChangeQuery(hotkey1.ActionKeyword, true);
2014-02-22 15:52:20 +08:00
});
}
}
2014-02-20 23:15:03 +08:00
private void OnHotkey(object sender, HotkeyEventArgs e)
{
if (!IsVisible)
{
ShowWox();
}
else
{
HideWox();
}
e.Handled = true;
}
2014-01-29 18:33:24 +08:00
private void InitProgressbarAnimation()
{
2014-03-15 00:17:37 +08:00
var da = new DoubleAnimation(progressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
var da1 = new DoubleAnimation(progressBar.X1, ActualWidth, new Duration(new TimeSpan(0, 0, 0, 0, 1600)));
2014-01-29 18:33:24 +08:00
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
progressBarStoryboard.Children.Add(da);
progressBarStoryboard.Children.Add(da1);
progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
progressBar.Visibility = Visibility.Hidden;
progressBar.BeginStoryboard(progressBarStoryboard);
}
private void InitialTray()
{
2014-02-22 16:55:15 +08:00
notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
2014-01-29 18:33:24 +08:00
notifyIcon.Click += (o, e) => ShowWox();
2014-02-22 11:55:48 +08:00
var open = new MenuItem("Open");
2014-01-29 18:33:24 +08:00
open.Click += (o, e) => ShowWox();
2014-02-22 11:55:48 +08:00
var exit = new MenuItem("Exit");
2014-01-29 18:33:24 +08:00
exit.Click += (o, e) => CloseApp();
2014-02-22 16:55:15 +08:00
MenuItem[] childen = { open, exit };
2014-02-22 11:55:48 +08:00
notifyIcon.ContextMenu = new ContextMenu(childen);
2014-01-29 18:33:24 +08:00
}
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
2014-02-28 23:21:01 +08:00
toolTip.IsOpen = false;
2014-01-29 18:33:24 +08:00
resultCtrl.Dirty = true;
Dispatcher.DelayInvoke("UpdateSearch",
2014-02-22 11:55:48 +08:00
o =>
{
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);
var q = new Query(tbQuery.Text);
CommandFactory.DispatchCommand(q);
queryHasReturn = false;
if (Plugins.HitThirdpartyKeyword(q))
{
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
{
if (!queryHasReturn && originQuery == tbQuery.Text)
{
StartProgress();
}
}, TimeSpan.FromSeconds(1), tbQuery.Text);
}
}, TimeSpan.FromMilliseconds(150));
2014-01-29 18:33:24 +08:00
}
private void StartProgress()
{
progressBar.Visibility = Visibility.Visible;
}
private void StopProgress()
{
progressBar.Visibility = Visibility.Hidden;
}
private void HideWox()
{
Hide();
}
private void ShowWox(bool selectAll = true)
{
Show();
Activate();
Focus();
tbQuery.Focus();
if (selectAll) tbQuery.SelectAll();
}
public void ParseArgs(string[] args)
{
if (args != null && args.Length > 0)
{
switch (args[0].ToLower())
2014-01-29 18:33:24 +08:00
{
case "reloadplugin":
2014-01-29 18:33:24 +08:00
Plugins.Init();
break;
case "query":
if (args.Length > 1)
{
string query = args[1];
tbQuery.Text = query;
tbQuery.SelectAll();
}
break;
case "hidestart":
HideApp();
break;
2014-03-11 22:17:10 +08:00
case "installplugin":
var path = args[1];
if (!File.Exists(path))
{
MessageBox.Show("Plugin " + path + " didn't exist");
return;
}
PluginInstaller.Install(path);
break;
2014-01-29 18:33:24 +08:00
}
}
}
2014-03-09 17:01:39 +08:00
2014-01-29 18:33:24 +08:00
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
{
if (CommonStorage.Instance.UserSetting.ReplaceWinR)
{
//todo:need refatoring. move those codes to CMD file or expose events
2014-02-22 16:55:15 +08:00
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
2014-01-29 18:33:24 +08:00
{
WinRStroked = true;
Dispatcher.BeginInvoke(new Action(OnWinRPressed));
return false;
}
2014-02-22 16:55:15 +08:00
if (keyevent == KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin)
2014-01-29 18:33:24 +08:00
{
WinRStroked = false;
2014-02-22 11:55:48 +08:00
keyboardSimulator.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL);
2014-01-29 18:33:24 +08:00
return false;
}
}
return true;
}
private void OnWinRPressed()
{
ShowWox(false);
if (tbQuery.Text != ">")
{
resultCtrl.Clear();
ChangeQuery(">");
}
tbQuery.CaretIndex = tbQuery.Text.Length;
}
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
//when alt is pressed, the real key should be e.SystemKey
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
switch (key)
2014-01-29 18:33:24 +08:00
{
case Key.Escape:
HideWox();
e.Handled = true;
break;
case Key.Down:
resultCtrl.SelectNext();
2014-02-28 23:21:01 +08:00
toolTip.IsOpen = false;
2014-01-29 18:33:24 +08:00
e.Handled = true;
break;
case Key.Up:
resultCtrl.SelectPrev();
2014-02-28 23:21:01 +08:00
toolTip.IsOpen = false;
2014-01-29 18:33:24 +08:00
e.Handled = true;
break;
case Key.Enter:
AcceptSelect(resultCtrl.AcceptSelect());
2014-01-29 18:33:24 +08:00
e.Handled = true;
break;
}
}
private void AcceptSelect(Result result)
2014-01-29 18:33:24 +08:00
{
if (result != null)
{
2014-02-22 16:55:15 +08:00
if (result.Action != null)
{
2014-02-28 23:21:01 +08:00
bool hideWindow = result.Action(new ActionContext()
2014-02-22 16:55:15 +08:00
{
SpecialKeyState = new GloablHotkey().CheckModifiers()
});
2014-02-28 23:21:01 +08:00
if (hideWindow)
{
HideWox();
}
2014-02-22 16:55:15 +08:00
CommonStorage.Instance.UserSelectedRecords.Add(result);
}
2014-01-29 18:33:24 +08:00
}
}
public void OnUpdateResultView(List<Result> list)
{
queryHasReturn = true;
progressBar.Dispatcher.Invoke(new Action(StopProgress));
if (list.Count > 0)
{
//todo:this should be opened to users, it's their choise to use it or not in thier workflows
2014-02-22 11:55:48 +08:00
list.ForEach(
o =>
{
if (o.AutoAjustScore) o.Score += CommonStorage.Instance.UserSelectedRecords.GetSelectedCount(o);
});
lock (locker)
2014-01-29 18:33:24 +08:00
{
2014-02-20 21:46:23 +08:00
waitShowResultList.AddRange(list);
}
2014-02-20 21:46:23 +08:00
Dispatcher.DelayInvoke("ShowResult", k => resultCtrl.Dispatcher.Invoke(new Action(() =>
2014-01-29 18:33:24 +08:00
{
2014-02-28 23:21:01 +08:00
List<Result> l = waitShowResultList.Where(o => o.OriginQuery != null && o.OriginQuery.RawQuery == tbQuery.Text).ToList();
2014-02-20 21:46:23 +08:00
waitShowResultList.Clear();
2014-01-29 18:33:24 +08:00
resultCtrl.AddResults(l);
2014-02-20 21:46:23 +08:00
})), TimeSpan.FromMilliseconds(50));
2014-01-29 18:33:24 +08:00
}
}
public void SetTheme(string themeName)
{
2014-02-22 11:55:48 +08:00
var dict = new ResourceDictionary
2014-01-29 18:33:24 +08:00
{
2014-03-15 00:17:37 +08:00
Source = new Uri(Path.Combine(Directory.GetCurrentDirectory(),"Themes\\"+themeName+".xaml") , UriKind.Absolute)
2014-01-29 18:33:24 +08:00
};
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(dict);
}
#region Public API
2014-02-28 23:21:01 +08:00
public void ChangeQuery(string query, bool requery = false)
2014-01-29 18:33:24 +08:00
{
tbQuery.Text = query;
tbQuery.CaretIndex = tbQuery.Text.Length;
2014-02-28 23:21:01 +08:00
if (requery)
{
TextBoxBase_OnTextChanged(null, null);
}
2014-01-29 18:33:24 +08:00
}
public void CloseApp()
{
notifyIcon.Visible = false;
2014-02-20 21:46:23 +08:00
Close();
2014-01-29 18:33:24 +08:00
Environment.Exit(0);
}
public void HideApp()
{
HideWox();
}
public void ShowApp()
{
ShowWox();
}
public void ShowMsg(string title, string subTitle, string iconPath)
{
2014-02-22 16:55:15 +08:00
var m = new Msg { Owner = GetWindow(this) };
2014-01-29 18:33:24 +08:00
m.Show(title, subTitle, iconPath);
}
public void OpenSettingDialog()
{
2014-02-22 15:52:20 +08:00
new SettingWidow(this).Show();
2014-01-29 18:33:24 +08:00
}
2014-02-28 23:21:01 +08:00
public void ShowCurrentResultItemTooltip(string text)
{
toolTip.Content = text;
toolTip.IsOpen = true;
}
2014-03-13 22:31:44 +08:00
public void StartLoadingBar()
{
Dispatcher.Invoke(new Action(StartProgress));
}
2014-02-28 23:21:01 +08:00
2014-03-13 22:31:44 +08:00
public void StopLoadingBar()
{
Dispatcher.Invoke(new Action(StopProgress));
}
#endregion
2014-02-28 23:21:01 +08:00
2014-03-13 22:31:44 +08:00
2014-01-29 18:33:24 +08:00
}
2013-12-22 19:35:21 +08:00
}