2014-01-29 18:33:24 +08:00
|
|
|
|
using System;
|
2015-01-08 22:49:42 +08:00
|
|
|
|
using System.ComponentModel;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media.Animation;
|
2016-03-26 09:20:42 +08:00
|
|
|
|
using System.Windows.Controls;
|
2014-12-26 19:36:43 +08:00
|
|
|
|
using Wox.Core.Plugin;
|
2016-01-08 04:04:37 +08:00
|
|
|
|
using Wox.Core.Resource;
|
2014-03-02 11:04:30 +08:00
|
|
|
|
using Wox.Helper;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
2016-02-23 05:43:37 +08:00
|
|
|
|
using Wox.ViewModel;
|
2016-05-25 08:00:10 +08:00
|
|
|
|
using Screen = System.Windows.Forms.Screen;
|
2016-05-10 06:35:20 +08:00
|
|
|
|
using ContextMenu = System.Windows.Forms.ContextMenu;
|
|
|
|
|
using MenuItem = System.Windows.Forms.MenuItem;
|
2016-05-25 08:00:10 +08:00
|
|
|
|
using NotifyIcon = System.Windows.Forms.NotifyIcon;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
|
2014-07-24 04:46:19 +08:00
|
|
|
|
namespace Wox
|
|
|
|
|
{
|
2016-02-18 19:30:36 +08:00
|
|
|
|
public partial class MainWindow
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
#region Private Fields
|
2014-07-24 04:46:19 +08:00
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private readonly Storyboard _progressBarStoryboard = new Storyboard();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private Settings _settings;
|
2016-05-10 06:35:20 +08:00
|
|
|
|
private NotifyIcon _notifyIcon;
|
2016-05-25 08:00:10 +08:00
|
|
|
|
private MainViewModel _viewModel;
|
2015-02-04 23:16:41 +08:00
|
|
|
|
|
2014-07-24 04:46:19 +08:00
|
|
|
|
#endregion
|
2016-02-27 07:43:57 +08:00
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public MainWindow(Settings settings, MainViewModel mainVM)
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2016-05-25 08:00:10 +08:00
|
|
|
|
DataContext = mainVM;
|
|
|
|
|
_viewModel = mainVM;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings = settings;
|
|
|
|
|
}
|
2014-07-24 04:46:19 +08:00
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2016-02-18 19:30:36 +08:00
|
|
|
|
}
|
2016-05-25 08:00:10 +08:00
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private void OnClosing(object sender, CancelEventArgs e)
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
2016-05-10 06:35:20 +08:00
|
|
|
|
_notifyIcon.Visible = false;
|
2016-05-25 08:00:10 +08:00
|
|
|
|
_viewModel.Save();
|
2014-07-24 04:46:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 07:24:28 +08:00
|
|
|
|
private void OnInitialized(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// show notify icon when wox is hided
|
|
|
|
|
InitializeNotifyIcon();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs _)
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
2016-08-01 21:12:53 +08:00
|
|
|
|
WindowsInteropHelper.DisableControlBox(this);
|
2016-05-10 05:56:05 +08:00
|
|
|
|
ThemeManager.Instance.ChangeTheme(_settings.Theme);
|
2016-06-16 08:49:15 +08:00
|
|
|
|
InitProgressbarAnimation();
|
|
|
|
|
|
|
|
|
|
_viewModel.PropertyChanged += (o, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility))
|
|
|
|
|
{
|
2016-06-23 07:26:57 +08:00
|
|
|
|
if (Visibility == Visibility.Visible)
|
2016-06-16 08:49:15 +08:00
|
|
|
|
{
|
|
|
|
|
Activate();
|
|
|
|
|
QueryTextBox.Focus();
|
|
|
|
|
SetWindowPosition();
|
|
|
|
|
_settings.ActivateTimes++;
|
|
|
|
|
if (_viewModel.QueryTextSelected)
|
|
|
|
|
{
|
|
|
|
|
QueryTextBox.SelectAll();
|
|
|
|
|
_viewModel.QueryTextSelected = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// since the default main window visibility is visible
|
|
|
|
|
// so we need set focus during startup
|
|
|
|
|
QueryTextBox.Focus();
|
2016-05-10 06:35:20 +08:00
|
|
|
|
}
|
2016-02-12 17:20:46 +08:00
|
|
|
|
|
2016-05-10 06:35:20 +08:00
|
|
|
|
private void InitializeNotifyIcon()
|
|
|
|
|
{
|
2016-05-19 02:38:43 +08:00
|
|
|
|
_notifyIcon = new NotifyIcon { Text = Infrastructure.Constant.Wox, Icon = Properties.Resources.app, Visible = true };
|
2016-05-25 08:00:10 +08:00
|
|
|
|
_notifyIcon.Click += (o, e) => Visibility = Visibility.Visible;
|
2016-05-10 06:35:20 +08:00
|
|
|
|
var open = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayOpen"));
|
2016-05-25 08:00:10 +08:00
|
|
|
|
open.Click += (o, e) => Visibility = Visibility.Visible;
|
2016-05-10 06:35:20 +08:00
|
|
|
|
var setting = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTraySettings"));
|
|
|
|
|
setting.Click += (o, e) => App.API.OpenSettingDialog();
|
|
|
|
|
var exit = new MenuItem(InternationalizationManager.Instance.GetTranslation("iconTrayExit"));
|
|
|
|
|
exit.Click += (o, e) => Close();
|
2016-05-23 02:16:39 +08:00
|
|
|
|
MenuItem[] childen = { open, setting, exit };
|
2016-05-10 06:35:20 +08:00
|
|
|
|
_notifyIcon.ContextMenu = new ContextMenu(childen);
|
2015-02-20 21:45:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-24 04:46:19 +08:00
|
|
|
|
private void InitProgressbarAnimation()
|
|
|
|
|
{
|
2016-03-28 10:09:57 +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-07-24 04:46:19 +08:00
|
|
|
|
Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)"));
|
|
|
|
|
Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
|
2016-02-27 07:43:57 +08:00
|
|
|
|
_progressBarStoryboard.Children.Add(da);
|
|
|
|
|
_progressBarStoryboard.Children.Add(da1);
|
|
|
|
|
_progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
ProgressBar.BeginStoryboard(_progressBarStoryboard);
|
2016-05-25 08:00:10 +08:00
|
|
|
|
_viewModel.ProgressBarVisibility = Visibility.Hidden;
|
2014-07-24 04:46:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private void OnMouseDown(object sender, MouseButtonEventArgs e)
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
|
|
|
|
if (e.ChangedButton == MouseButton.Left) DragMove();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 09:20:42 +08:00
|
|
|
|
private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (sender != null && e.OriginalSource != null)
|
|
|
|
|
{
|
|
|
|
|
var r = (ResultListBox)sender;
|
|
|
|
|
var d = (DependencyObject)e.OriginalSource;
|
|
|
|
|
var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem;
|
|
|
|
|
var result = (ResultViewModel)item?.DataContext;
|
|
|
|
|
if (result != null)
|
|
|
|
|
{
|
2016-05-25 08:00:10 +08:00
|
|
|
|
if (e.ChangedButton == MouseButton.Left)
|
|
|
|
|
{
|
|
|
|
|
_viewModel.OpenResultCommand.Execute(null);
|
|
|
|
|
}
|
|
|
|
|
else if (e.ChangedButton == MouseButton.Right)
|
2016-03-26 09:20:42 +08:00
|
|
|
|
{
|
2016-05-25 08:00:10 +08:00
|
|
|
|
_viewModel.LoadContextMenuCommand.Execute(null);
|
2016-03-26 09:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-12 14:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-18 19:30:36 +08:00
|
|
|
|
|
2016-03-26 09:20:42 +08:00
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private void OnDrop(object sender, DragEventArgs e)
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
2015-01-08 22:49:42 +08:00
|
|
|
|
if (e.Data.GetDataPresent(DataFormats.FileDrop))
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
|
|
|
|
// Note that you can have more than one file.
|
2015-01-08 22:49:42 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 07:43:57 +08:00
|
|
|
|
private void OnPreviewDragOver(object sender, DragEventArgs e)
|
2014-07-24 04:46:19 +08:00
|
|
|
|
{
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
2016-03-11 12:23:46 +08:00
|
|
|
|
|
|
|
|
|
private void OnContextMenusForSettingsClick(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
App.API.OpenSettingDialog();
|
|
|
|
|
}
|
2016-05-25 08:00:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDeactivated(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_settings.HideWhenDeactive)
|
|
|
|
|
{
|
|
|
|
|
Hide();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _startup = true;
|
|
|
|
|
private void SetWindowPosition()
|
|
|
|
|
{
|
|
|
|
|
if (!_settings.RememberLastLaunchLocation && !_startup)
|
|
|
|
|
{
|
|
|
|
|
Left = WindowLeft();
|
|
|
|
|
Top = WindowTop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_startup = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
// used to set correct position on windows first startup
|
|
|
|
|
// since the actual width and actual height will be avaiable after this event
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Left = WindowLeft();
|
|
|
|
|
Top = WindowTop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double WindowLeft()
|
|
|
|
|
{
|
|
|
|
|
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
|
2016-08-01 21:12:53 +08:00
|
|
|
|
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
|
|
|
|
|
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
|
2016-05-25 08:00:10 +08:00
|
|
|
|
var left = (dip2.X - ActualWidth) / 2 + dip1.X;
|
|
|
|
|
return left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double WindowTop()
|
|
|
|
|
{
|
|
|
|
|
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
|
2016-08-01 21:12:53 +08:00
|
|
|
|
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
|
|
|
|
|
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
|
2016-05-25 08:00:10 +08:00
|
|
|
|
var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y;
|
|
|
|
|
return top;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Register up and down key
|
|
|
|
|
/// todo: any way to put this in xaml ?
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnKeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == Key.Down)
|
|
|
|
|
{
|
|
|
|
|
_viewModel.SelectNextItemCommand.Execute(null);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
else if (e.Key == Key.Up)
|
|
|
|
|
{
|
|
|
|
|
_viewModel.SelectPrevItemCommand.Execute(null);
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-23 07:26:57 +08:00
|
|
|
|
|
|
|
|
|
private void OnTextChanged(object sender, TextChangedEventArgs e)
|
|
|
|
|
{
|
2016-07-21 06:37:06 +08:00
|
|
|
|
if (_viewModel.QueryTextCursorMovedToEnd)
|
|
|
|
|
{
|
|
|
|
|
QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
|
|
|
|
|
_viewModel.QueryTextCursorMovedToEnd = false;
|
|
|
|
|
}
|
2016-06-23 07:26:57 +08:00
|
|
|
|
}
|
2014-07-24 04:46:19 +08:00
|
|
|
|
}
|
2013-12-22 19:35:21 +08:00
|
|
|
|
}
|