PowerToys/Wox/MainWindow.xaml.cs

478 lines
16 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
2015-02-07 16:53:33 +08:00
using System.IO;
2014-01-29 18:33:24 +08:00
using System.Linq;
using System.Net;
2015-02-05 23:29:41 +08:00
using System.Reflection;
2014-01-29 18:33:24 +08:00
using System.Threading;
2016-01-06 14:31:17 +08:00
using System.Threading.Tasks;
2014-01-29 18:33:24 +08:00
using System.Windows;
using System.Windows.Controls;
2016-01-07 05:34:42 +08:00
using System.Windows.Forms;
2014-01-29 18:33:24 +08:00
using System.Windows.Input;
using System.Windows.Media.Animation;
2014-02-20 23:15:03 +08:00
using NHotkey;
using NHotkey.Wpf;
2014-12-26 19:36:43 +08:00
using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Core.Updater;
using Wox.Core.UserSettings;
2014-03-02 11:04:30 +08:00
using Wox.Helper;
using Wox.Infrastructure;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Hotkey;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
2014-12-26 22:51:04 +08:00
using Wox.Storage;
2016-01-07 05:34:42 +08:00
using Application = System.Windows.Application;
2014-02-22 11:55:48 +08:00
using ContextMenu = System.Windows.Forms.ContextMenu;
2016-01-07 05:34:42 +08:00
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
using IDataObject = System.Windows.IDataObject;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
2014-02-22 11:55:48 +08:00
using MenuItem = System.Windows.Forms.MenuItem;
2016-01-07 05:34:42 +08:00
using MessageBox = System.Windows.MessageBox;
using Stopwatch = Wox.Infrastructure.Stopwatch;
2016-01-07 05:34:42 +08:00
using ToolTip = System.Windows.Controls.ToolTip;
using Wox.ViewModel;
2014-01-29 18:33:24 +08:00
2014-07-24 04:46:19 +08:00
namespace Wox
{
public partial class MainWindow
2014-07-24 04:46:19 +08:00
{
#region Properties
private readonly Storyboard progressBarStoryboard = new Storyboard();
private NotifyIcon notifyIcon;
2015-02-04 23:16:41 +08:00
2014-07-24 04:46:19 +08:00
#endregion
2014-07-24 04:46:19 +08:00
public MainWindow()
{
InitializeComponent();
2015-02-21 21:57:00 +08:00
InitialTray();
2015-02-02 23:28:40 +08:00
//pnlResult.ItemDropEvent += pnlResult_ItemDropEvent;
Closing += MainWindow_Closing;
}
//void pnlResult_ItemDropEvent(Result result, IDataObject dropDataObject, DragEventArgs args)
//{
// PluginPair pluginPair = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ID == result.PluginID);
// if (ResultItemDropEvent != null && pluginPair != null)
// {
// foreach (var delegateHandler in ResultItemDropEvent.GetInvocationList())
// {
// if (delegateHandler.Target == pluginPair.Plugin)
// {
// delegateHandler.DynamicInvoke(result, dropDataObject, args);
// }
// }
// }
//}
2014-10-23 18:39:11 +08:00
void MainWindow_Closing(object sender, CancelEventArgs e)
2014-07-24 04:46:19 +08:00
{
UserSettingStorage.Instance.WindowLeft = Left;
UserSettingStorage.Instance.WindowTop = Top;
UserSettingStorage.Instance.Save();
e.Cancel = true;
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme);
InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language);
2015-02-20 21:45:42 +08:00
Left = GetWindowsLeft();
Top = GetWindowsTop();
InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this);
CheckUpdate();
}
private double GetWindowsLeft()
{
if (UserSettingStorage.Instance.RememberLastLaunchLocation) return UserSettingStorage.Instance.WindowLeft;
2014-07-24 04:46:19 +08:00
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipPoint = WindowIntelopHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
UserSettingStorage.Instance.WindowLeft = (dipPoint.X - ActualWidth)/2;
2015-02-20 21:45:42 +08:00
return UserSettingStorage.Instance.WindowLeft;
}
private double GetWindowsTop()
{
if (UserSettingStorage.Instance.RememberLastLaunchLocation) return UserSettingStorage.Instance.WindowTop;
2015-02-20 21:45:42 +08:00
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dipPoint = WindowIntelopHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
UserSettingStorage.Instance.WindowTop = (dipPoint.Y - tbQuery.ActualHeight)/4;
2015-02-20 21:45:42 +08:00
return UserSettingStorage.Instance.WindowTop;
2015-02-01 22:46:56 +08:00
}
private void CheckUpdate()
{
2015-02-04 23:16:41 +08:00
UpdaterManager.Instance.PrepareUpdateReady += OnPrepareUpdateReady;
2015-02-01 22:46:56 +08:00
UpdaterManager.Instance.UpdateError += OnUpdateError;
UpdaterManager.Instance.CheckUpdate();
2014-07-24 04:46:19 +08:00
}
2015-02-01 22:46:56 +08:00
void OnUpdateError(object sender, EventArgs e)
{
string updateError = InternationalizationManager.Instance.GetTranslation("update_wox_update_error");
MessageBox.Show(updateError);
}
private void OnPrepareUpdateReady(object sender, EventArgs e)
{
2016-01-07 05:34:42 +08:00
Dispatcher.Invoke(() =>
2015-02-01 22:46:56 +08:00
{
new WoxUpdate().ShowDialog();
2016-01-07 05:34:42 +08:00
});
2015-02-01 22:46:56 +08:00
}
2014-07-24 04:46:19 +08:00
private void InitProgressbarAnimation()
{
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)));
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()
{
//notifyIcon = new NotifyIcon { Text = "Wox", Icon = Properties.Resources.app, Visible = true };
//notifyIcon.Click += (o, e) => ShowWox();
//var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
//open.Click += (o, e) => ShowWox();
//var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
//setting.Click += (o, e) => OpenSettingDialog();
//var about = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayAbout"));
//about.Click += (o, e) => OpenSettingDialog("about");
//var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
//exit.Click += (o, e) => CloseApp();
//MenuItem[] childen = { open, setting, about, exit };
//notifyIcon.ContextMenu = new ContextMenu(childen);
2014-10-23 18:39:11 +08:00
}
2014-07-24 04:46:19 +08:00
private void Border_OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) DragMove();
}
private void ShowWox(bool selectAll = true)
{
//UserSettingStorage.Instance.IncreaseActivateTimes();
//Left = GetWindowsLeft();
//Top = GetWindowsTop();
2014-07-24 04:46:19 +08:00
//Show();
//Activate();
//Focus();
//tbQuery.Focus();
//ResetQueryHistoryIndex();
//if (selectAll) tbQuery.SelectAll();
2014-07-24 04:46:19 +08:00
}
private void MainWindow_OnDeactivated(object sender, EventArgs e)
{
if (UserSettingStorage.Instance.HideWhenDeactive)
{
//TODO:Hide the window when deactivated
//HideWox();
2014-07-24 04:46:19 +08:00
}
}
//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)
// {
// case Key.Escape:
// if (IsInContextMenuMode)
// {
// BackToResultMode();
// }
// else
// {
// HideWox();
// }
// e.Handled = true;
// break;
// case Key.Tab:
// if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
// {
// SelectPrevItem();
// }
// else
// {
// SelectNextItem();
// }
// e.Handled = true;
// break;
// case Key.N:
// case Key.J:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// SelectNextItem();
// }
// break;
// case Key.P:
// case Key.K:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// SelectPrevItem();
// }
// break;
// case Key.O:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// if (IsInContextMenuMode)
// {
// BackToResultMode();
// }
// else
// {
// ShowContextMenu(GetActiveResult());
// }
// }
// break;
// case Key.Down:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// DisplayNextQuery();
// }
// else
// {
// SelectNextItem();
// }
// e.Handled = true;
// break;
// case Key.Up:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// DisplayPrevQuery();
// }
// else
// {
// SelectPrevItem();
// }
// e.Handled = true;
// break;
// case Key.D:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// pnlResult.SelectNextPage();
// }
// break;
// case Key.PageDown:
// pnlResult.SelectNextPage();
// e.Handled = true;
// break;
// case Key.U:
// if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
// {
// pnlResult.SelectPrevPage();
// }
// break;
// case Key.PageUp:
// pnlResult.SelectPrevPage();
// e.Handled = true;
// break;
// case Key.Back:
// if (BackKeyDownEvent != null)
// {
// BackKeyDownEvent(new WoxKeyDownEventArgs
// {
// Query = tbQuery.Text,
// keyEventArgs = e
// });
// }
// break;
// case Key.F1:
// Process.Start("http://doc.getwox.com");
// break;
// case Key.Enter:
// Result activeResult = GetActiveResult();
// if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
// {
// ShowContextMenu(activeResult);
// }
// else
// {
// SelectResult(activeResult);
// }
// e.Handled = true;
// break;
// case Key.D1:
// SelectItem(1);
// break;
// case Key.D2:
// SelectItem(2);
// break;
// case Key.D3:
// SelectItem(3);
// break;
// case Key.D4:
// SelectItem(4);
// break;
// case Key.D5:
// SelectItem(5);
// break;
// case Key.D6:
// SelectItem(6);
// break;
// }
//}
//private void DisplayPrevQuery()
//{
// var prev = QueryHistoryStorage.Instance.Previous();
// DisplayQueryHistory(prev);
//}
//private void DisplayNextQuery()
//{
// var nextQuery = QueryHistoryStorage.Instance.Next();
// DisplayQueryHistory(nextQuery);
//}
//private void DisplayQueryHistory(HistoryItem history)
//{
// if (history != null)
// {
// var historyMetadata = QueryHistoryStorage.MetaData;
// ChangeQueryText(history.Query, true);
// var executeQueryHistoryTitle = GetTranslation("executeQuery");
// var lastExecuteTime = GetTranslation("lastExecuteTime");
// pnlResult.RemoveResultsExcept(historyMetadata);
// UpdateResultViewInternal(new List<Result>
// {
// new Result
// {
// Title = string.Format(executeQueryHistoryTitle,history.Query),
// SubTitle = string.Format(lastExecuteTime,history.ExecutedDateTime),
// IcoPath = "Images\\history.png",
// PluginDirectory = WoxDirectroy.Executable,
// Action = _ =>{
// ChangeQuery(history.Query,true);
// return false;
// }
// }
// }, historyMetadata);
// }
//}
//private void SelectItem(int index)
//{
// int zeroBasedIndex = index - 1;
// SpecialKeyState keyState = GlobalHotkey.Instance.CheckModifiers();
// if (keyState.AltPressed || keyState.CtrlPressed)
// {
// List<Result> visibleResults = pnlResult.GetVisibleResults();
// if (zeroBasedIndex < visibleResults.Count)
// {
// SelectResult(visibleResults[zeroBasedIndex]);
// }
// }
//}
//private bool IsInContextMenuMode
//{
// get { return pnlContextMenu.Visibility == Visibility.Visible; }
//}
//private Result GetActiveResult()
//{
// if (IsInContextMenuMode)
// {
// return pnlContextMenu.GetActiveResult();
// }
// else
// {
// return pnlResult.GetActiveResult();
// }
//}
//private void SelectPrevItem()
//{
// if (IsInContextMenuMode)
// {
// pnlContextMenu.SelectPrev();
// }
// else
// {
// pnlResult.SelectPrev();
// }
// toolTip.IsOpen = false;
//}
//private void SelectNextItem()
//{
// if (IsInContextMenuMode)
// {
// pnlContextMenu.SelectNext();
// }
// else
// {
// pnlResult.SelectNext();
// }
// toolTip.IsOpen = false;
//}
2014-07-24 04:46:19 +08:00
private void MainWindow_OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
2014-07-24 04:46:19 +08:00
{
// Note that you can have more than one file.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
2014-07-24 04:46:19 +08:00
if (files[0].ToLower().EndsWith(".wox"))
{
2014-12-27 12:34:51 +08:00
PluginManager.InstallPlugin(files[0]);
2014-07-24 04:46:19 +08:00
}
else
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidWoxPluginFileFormat"));
2014-07-24 04:46:19 +08:00
}
}
}
private void TbQuery_OnPreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
}
2013-12-22 19:35:21 +08:00
}