PowerToys/Wox/MainWindow.xaml.cs

343 lines
12 KiB
C#
Raw Normal View History

2014-01-29 18:33:24 +08:00
using System;
using System.ComponentModel;
2014-01-29 18:33:24 +08:00
using System.Windows;
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-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;
2014-12-21 22:03:03 +08:00
using Wox.Infrastructure.Hotkey;
2016-02-23 05:43:37 +08:00
using Wox.ViewModel;
2016-01-07 05:34:42 +08:00
using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
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
2016-02-12 15:22:01 +08:00
private readonly Storyboard progressBarStoryboard = new Storyboard();
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();
Closing += MainWindow_Closing;
}
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
InitProgressbarAnimation();
WindowIntelopHelper.DisableControlBox(this);
CheckUpdate();
2016-02-12 17:20:46 +08:00
var vm = DataContext as MainViewModel;
2016-02-12 17:20:46 +08:00
vm.PropertyChanged += (o, eve) =>
{
if(eve.PropertyName == "SelectAllText")
{
if (vm.SelectAllText)
{
2016-02-27 05:34:27 +08:00
QueryTextBox.SelectAll();
2016-02-12 17:20:46 +08:00
}
}
else if(eve.PropertyName == "CaretIndex")
{
2016-02-27 05:34:27 +08:00
QueryTextBox.CaretIndex = vm.CaretIndex;
2016-02-12 17:20:46 +08:00
}
else if(eve.PropertyName == "Left")
{
Left = vm.Left;
2016-02-12 17:20:46 +08:00
}
else if(eve.PropertyName == "Top")
{
Top = vm.Top;
2016-02-12 17:20:46 +08:00
}
2016-02-27 05:34:27 +08:00
else if(eve.PropertyName == "MainWindowVisibility")
2016-02-12 17:20:46 +08:00
{
2016-02-26 20:05:32 +08:00
if (vm.MainWindowVisibility.IsVisible())
2016-02-12 17:20:46 +08:00
{
2016-02-27 05:34:27 +08:00
Activate();
QueryTextBox.Focus();
2016-02-12 17:20:46 +08:00
}
}
};
vm.Left = GetWindowsLeft();
vm.Top = GetWindowsTop();
2016-02-27 05:34:27 +08:00
vm.MainWindowVisibility = Visibility.Visible;
2015-02-20 21:45:42 +08:00
}
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);
2016-02-27 05:34:27 +08:00
UserSettingStorage.Instance.WindowTop = (dipPoint.Y - QueryTextBox.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 Border_OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left) DragMove();
}
private void MainWindow_OnDeactivated(object sender, EventArgs e)
{
if (UserSettingStorage.Instance.HideWhenDeactive)
{
2016-02-12 17:20:46 +08:00
App.API.HideApp();
2014-07-24 04:46:19 +08:00
}
}
2016-02-12 14:21:12 +08:00
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
var vm = DataContext as MainViewModel;
if (null == vm) return;
2016-02-12 14:21:12 +08:00
//when alt is pressed, the real key should be e.SystemKey
var key = (e.Key == Key.System ? e.SystemKey : e.Key);
2016-02-12 14:21:12 +08:00
switch (key)
{
case Key.Escape:
vm.EscCommand.Execute(null);
e.Handled = true;
break;
case Key.Tab:
if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
{
vm.SelectPrevItemCommand.Execute(null);
}
else
{
vm.SelectNextItemCommand.Execute(null);
}
e.Handled = true;
break;
case Key.N:
case Key.J:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.SelectNextItemCommand.Execute(null);
}
break;
case Key.P:
case Key.K:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.SelectPrevItemCommand.Execute(null);
}
break;
case Key.O:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.CtrlOCommand.Execute(null);
}
break;
2016-02-12 14:21:12 +08:00
case Key.Down:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.DisplayNextQueryCommand.Execute(null);
}
else
{
vm.SelectNextItemCommand.Execute(null);
}
e.Handled = true;
break;
case Key.Up:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.DisplayPrevQueryCommand.Execute(null);
}
else
{
vm.SelectPrevItemCommand.Execute(null);
}
e.Handled = true;
break;
case Key.D:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.SelectNextPageCommand.Execute(null);
}
break;
2016-02-12 14:21:12 +08:00
case Key.PageDown:
vm.SelectNextPageCommand.Execute(null);
e.Handled = true;
break;
case Key.U:
if (GlobalHotkey.Instance.CheckModifiers().CtrlPressed)
{
vm.SelectPrevPageCommand.Execute(null);
}
break;
2016-02-12 14:21:12 +08:00
case Key.PageUp:
vm.SelectPrevPageCommand.Execute(null);
e.Handled = true;
break;
2016-02-12 17:20:46 +08:00
case Key.Back:
vm.BackCommand.Execute(e);
break;
case Key.F1:
vm.StartHelpCommand.Execute(null);
break;
case Key.Enter:
if (GlobalHotkey.Instance.CheckModifiers().ShiftPressed)
{
vm.ShiftEnterCommand.Execute(null);
}
else
{
vm.OpenResultCommand.Execute(null);
}
e.Handled = true;
break;
case Key.D1:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(0);
}
break;
case Key.D2:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(1);
}
break;
case Key.D3:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(2);
}
break;
case Key.D4:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(3);
}
break;
case Key.D5:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(4);
}
break;
case Key.D6:
if (GlobalHotkey.Instance.CheckModifiers().AltPressed)
{
vm.OpenResultCommand.Execute(5);
}
break;
2016-02-12 14:21:12 +08:00
}
}
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
}
}
2016-02-12 14:21:12 +08:00
e.Handled = false;
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
}