Added fix for closing wox on pressing escape and app execution (#75)

This commit is contained in:
Divyansh Srivastava
2020-04-08 11:32:59 -07:00
committed by GitHub
parent 0648ecf2d2
commit 9d1b8fbd3e
2 changed files with 339 additions and 332 deletions

View File

@@ -1,31 +1,34 @@
<Window x:Class="PowerLauncher.MainWindow" <Window x:Class="PowerLauncher.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wox="clr-namespace:PowerLauncher" xmlns:wox="clr-namespace:PowerLauncher"
xmlns:vm="clr-namespace:Wox.ViewModel;assembly=Wox" xmlns:vm="clr-namespace:Wox.ViewModel;assembly=Wox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" mc:Ignorable="d"
xmlns:xaml="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost" xmlns:xaml="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"
Title="Wox" Title="Wox"
Topmost="True" Topmost="True"
SizeToContent="Height" SizeToContent="Height"
ResizeMode="NoResize" ResizeMode="NoResize"
WindowStyle="None" WindowStyle="None"
WindowStartupLocation="Manual" WindowStartupLocation="Manual"
AllowDrop="True" AllowDrop="True"
ShowInTaskbar="False" ShowInTaskbar="False"
Style="{DynamicResource WindowStyle}" Style="{DynamicResource WindowStyle}"
Icon="Images/app.png" Icon="Images/app.png"
AllowsTransparency="True" AllowsTransparency="True"
Loaded="OnLoaded" Loaded="OnLoaded"
Initialized="OnInitialized" Initialized="OnInitialized"
Closing="OnClosing" Closing="OnClosing"
Drop="OnDrop" Drop="OnDrop"
LocationChanged="OnLocationChanged" LocationChanged="OnLocationChanged"
Deactivated="OnDeactivated" Deactivated="OnDeactivated"
PreviewKeyDown="OnKeyDown" PreviewKeyDown="OnKeyDown"
Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
d:DataContext="{d:DesignInstance vm:MainViewModel}"> d:DataContext="{d:DesignInstance vm:MainViewModel}">
<xaml:WindowsXamlHost InitialTypeName="PowerLauncher.UI.LauncherControl" ChildChanged="WindowsXamlHost_ChildChanged" /> <xaml:WindowsXamlHost InitialTypeName="PowerLauncher.UI.LauncherControl" ChildChanged="WindowsXamlHost_ChildChanged" />
</Window> <Window.InputBindings>
<KeyBinding Key="Escape" Command="{Binding EscCommand}"></KeyBinding>
</Window.InputBindings>
</Window>

View File

@@ -1,302 +1,306 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media.Animation; using System.Windows.Media.Animation;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Forms; using System.Windows.Forms;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Core.Resource; using Wox.Core.Resource;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
using Wox.ViewModel; using Wox.ViewModel;
using Screen = System.Windows.Forms.Screen; using Screen = System.Windows.Forms.Screen;
using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip; using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip;
using DataFormats = System.Windows.DataFormats; using DataFormats = System.Windows.DataFormats;
using DragEventArgs = System.Windows.DragEventArgs; using DragEventArgs = System.Windows.DragEventArgs;
using KeyEventArgs = System.Windows.Input.KeyEventArgs; using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox; using MessageBox = System.Windows.MessageBox;
using Microsoft.Toolkit.Wpf.UI.XamlHost; using Microsoft.Toolkit.Wpf.UI.XamlHost;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using System.Diagnostics; using System.Diagnostics;
using Wox.Plugin; using Wox.Plugin;
using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Input;
namespace PowerLauncher namespace PowerLauncher
{ {
public partial class MainWindow public partial class MainWindow
{ {
#region Private Fields #region Private Fields
private readonly Storyboard _progressBarStoryboard = new Storyboard(); private readonly Storyboard _progressBarStoryboard = new Storyboard();
private Settings _settings; private Settings _settings;
private MainViewModel _viewModel; private MainViewModel _viewModel;
#endregion #endregion
public MainWindow(Settings settings, MainViewModel mainVM) public MainWindow(Settings settings, MainViewModel mainVM)
{ {
DataContext = mainVM; DataContext = mainVM;
_viewModel = mainVM; _viewModel = mainVM;
_settings = settings; _settings = settings;
InitializeComponent(); InitializeComponent();
} }
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
} }
private void OnClosing(object sender, CancelEventArgs e) private void OnClosing(object sender, CancelEventArgs e)
{ {
_viewModel.Save(); _viewModel.Save();
} }
private void OnInitialized(object sender, EventArgs e) private void OnInitialized(object sender, EventArgs e)
{ {
} }
private void OnLoaded(object sender, RoutedEventArgs _) private void OnLoaded(object sender, RoutedEventArgs _)
{ {
// todo is there a way to set blur only once? // todo is there a way to set blur only once?
//ThemeManager.Instance.SetBlurForWindow(); //ThemeManager.Instance.SetBlurForWindow();
//WindowsInteropHelper.DisableControlBox(this); //WindowsInteropHelper.DisableControlBox(this);
//InitProgressbarAnimation(); //InitProgressbarAnimation();
//InitializePosition(); //InitializePosition();
//// since the default main window visibility is visible //// since the default main window visibility is visible
//// so we need set focus during startup //// so we need set focus during startup
//QueryTextBox.Focus(); //QueryTextBox.Focus();
//_viewModel.PropertyChanged += (o, e) => //_viewModel.PropertyChanged += (o, e) =>
//{ //{
// if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility)) // if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility))
// { // {
// if (Visibility == Visibility.Visible) // if (Visibility == Visibility.Visible)
// { // {
// Activate(); // Activate();
// QueryTextBox.Focus(); // QueryTextBox.Focus();
// UpdatePosition(); // UpdatePosition();
// _settings.ActivateTimes++; // _settings.ActivateTimes++;
// if (!_viewModel.LastQuerySelected) // if (!_viewModel.LastQuerySelected)
// { // {
// QueryTextBox.SelectAll(); // QueryTextBox.SelectAll();
// _viewModel.LastQuerySelected = true; // _viewModel.LastQuerySelected = true;
// } // }
// } // }
// } // }
//}; //};
InitializePosition(); InitializePosition();
} }
private void InitializePosition() private void InitializePosition()
{ {
//Top = WindowTop(); //Top = WindowTop();
Left = WindowLeft(); Left = WindowLeft();
//_settings.WindowTop = Top; //_settings.WindowTop = Top;
_settings.WindowLeft = Left; _settings.WindowLeft = Left;
} }
//private void InitProgressbarAnimation() //private void InitProgressbarAnimation()
//{ //{
// var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); // 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))); // 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(da, new PropertyPath("(Line.X2)"));
// Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)")); // Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)"));
// _progressBarStoryboard.Children.Add(da); // _progressBarStoryboard.Children.Add(da);
// _progressBarStoryboard.Children.Add(da1); // _progressBarStoryboard.Children.Add(da1);
// _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever; // _progressBarStoryboard.RepeatBehavior = RepeatBehavior.Forever;
// ProgressBar.BeginStoryboard(_progressBarStoryboard); // ProgressBar.BeginStoryboard(_progressBarStoryboard);
// _viewModel.ProgressBarVisibility = Visibility.Hidden; // _viewModel.ProgressBarVisibility = Visibility.Hidden;
//} //}
private void OnMouseDown(object sender, MouseButtonEventArgs e) private void OnMouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Left) DragMove(); if (e.ChangedButton == MouseButton.Left) DragMove();
} }
//private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e) //private void OnPreviewMouseButtonDown(object sender, MouseButtonEventArgs e)
//{ //{
// if (sender != null && e.OriginalSource != null) // if (sender != null && e.OriginalSource != null)
// { // {
// var r = (ResultListBox)sender; // var r = (ResultListBox)sender;
// var d = (DependencyObject)e.OriginalSource; // var d = (DependencyObject)e.OriginalSource;
// var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem; // var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem;
// var result = (ResultViewModel)item?.DataContext; // var result = (ResultViewModel)item?.DataContext;
// if (result != null) // if (result != null)
// { // {
// if (e.ChangedButton == MouseButton.Left) // if (e.ChangedButton == MouseButton.Left)
// { // {
// _viewModel.OpenResultCommand.Execute(null); // _viewModel.OpenResultCommand.Execute(null);
// } // }
// else if (e.ChangedButton == MouseButton.Right) // else if (e.ChangedButton == MouseButton.Right)
// { // {
// _viewModel.LoadContextMenuCommand.Execute(null); // _viewModel.LoadContextMenuCommand.Execute(null);
// } // }
// } // }
// } // }
//} //}
private void OnDrop(object sender, DragEventArgs e) private void OnDrop(object sender, DragEventArgs e)
{ {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) if (e.Data.GetDataPresent(DataFormats.FileDrop))
{ {
// Note that you can have more than one file. // Note that you can have more than one file.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files[0].ToLower().EndsWith(".wox")) if (files[0].ToLower().EndsWith(".wox"))
{ {
PluginManager.InstallPlugin(files[0]); PluginManager.InstallPlugin(files[0]);
} }
else else
{ {
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidWoxPluginFileFormat")); MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidWoxPluginFileFormat"));
} }
} }
e.Handled = false; e.Handled = false;
} }
private void OnPreviewDragOver(object sender, DragEventArgs e) private void OnPreviewDragOver(object sender, DragEventArgs e)
{ {
e.Handled = true; e.Handled = true;
} }
private void OnContextMenusForSettingsClick(object sender, RoutedEventArgs e) private void OnContextMenusForSettingsClick(object sender, RoutedEventArgs e)
{ {
} }
private void OnDeactivated(object sender, EventArgs e) private void OnDeactivated(object sender, EventArgs e)
{ {
if (_settings.HideWhenDeactive) if (_settings.HideWhenDeactive)
{ {
Hide(); Hide();
} }
} }
private void UpdatePosition() private void UpdatePosition()
{ {
if (_settings.RememberLastLaunchLocation) if (_settings.RememberLastLaunchLocation)
{ {
Left = _settings.WindowLeft; Left = _settings.WindowLeft;
Top = _settings.WindowTop; Top = _settings.WindowTop;
} }
else else
{ {
Left = WindowLeft(); Left = WindowLeft();
//Top = WindowTop(); //Top = WindowTop();
} }
} }
private void OnLocationChanged(object sender, EventArgs e) private void OnLocationChanged(object sender, EventArgs e)
{ {
if (_settings.RememberLastLaunchLocation) if (_settings.RememberLastLaunchLocation)
{ {
_settings.WindowLeft = Left; _settings.WindowLeft = Left;
_settings.WindowTop = Top; _settings.WindowTop = Top;
} }
} }
private double WindowLeft() private double WindowLeft()
{ {
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0); var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0);
var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0); var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0);
var left = (dip2.X - ActualWidth) / 2 + dip1.X; var left = (dip2.X - ActualWidth) / 2 + dip1.X;
return left; return left;
} }
//private double WindowTop() //private double WindowTop()
//{ //{
// var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); // var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
// var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); // var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y);
// var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height); // var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height);
// var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y; // var top = (dip2.Y - QueryTextBox.ActualHeight) / 4 + dip1.Y;
// return top; // return top;
//} //}
/// <summary> /// <summary>
/// Register up and down key /// Register up and down key
/// todo: any way to put this in xaml ? /// todo: any way to put this in xaml ?
/// </summary> /// </summary>
private void OnKeyDown(object sender, KeyEventArgs e) private void OnKeyDown(object sender, KeyEventArgs e)
{ {
if (e.Key == Key.Down) if (e.Key == Key.Down)
{ {
_viewModel.SelectNextItemCommand.Execute(null); _viewModel.SelectNextItemCommand.Execute(null);
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.Up) else if (e.Key == Key.Up)
{ {
_viewModel.SelectPrevItemCommand.Execute(null); _viewModel.SelectPrevItemCommand.Execute(null);
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.PageDown) else if (e.Key == Key.PageDown)
{ {
_viewModel.SelectNextPageCommand.Execute(null); _viewModel.SelectNextPageCommand.Execute(null);
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.PageUp) else if (e.Key == Key.PageUp)
{ {
_viewModel.SelectPrevPageCommand.Execute(null); _viewModel.SelectPrevPageCommand.Execute(null);
e.Handled = true; e.Handled = true;
} }
} }
//private void OnTextChanged(object sender, TextChangedEventArgs e) //private void OnTextChanged(object sender, TextChangedEventArgs e)
//{ //{
// if (_viewModel.QueryTextCursorMovedToEnd) // if (_viewModel.QueryTextCursorMovedToEnd)
// { // {
// QueryTextBox.CaretIndex = QueryTextBox.Text.Length; // QueryTextBox.CaretIndex = QueryTextBox.Text.Length;
// _viewModel.QueryTextCursorMovedToEnd = false; // _viewModel.QueryTextCursorMovedToEnd = false;
// } // }
//} //}
private PowerLauncher.UI.LauncherControl _launcher = null; private PowerLauncher.UI.LauncherControl _launcher = null;
private void WindowsXamlHost_ChildChanged(object sender, EventArgs ev) private void WindowsXamlHost_ChildChanged(object sender, EventArgs ev)
{ {
if (sender == null) return; if (sender == null) return;
var host = (WindowsXamlHost)sender; var host = (WindowsXamlHost)sender;
_launcher = (PowerLauncher.UI.LauncherControl)host.Child; _launcher = (PowerLauncher.UI.LauncherControl)host.Child;
_launcher.DataContext = _viewModel; _launcher.DataContext = _viewModel;
_launcher.SearchBox.TextChanged += QueryTextBox_TextChanged; _launcher.SearchBox.TextChanged += QueryTextBox_TextChanged;
_launcher.SearchBox.QuerySubmitted += AutoSuggestBox_QuerySubmitted; _launcher.SearchBox.QuerySubmitted += AutoSuggestBox_QuerySubmitted;
_launcher.SearchBox.Focus(Windows.UI.Xaml.FocusState.Programmatic); _launcher.SearchBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
_viewModel.PropertyChanged += (o, e) => _viewModel.PropertyChanged += (o, e) =>
{ {
if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility)) if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility))
{ {
if (Visibility == System.Windows.Visibility.Visible) if (Visibility == System.Windows.Visibility.Visible)
{ {
Activate(); Activate();
_launcher.SearchBox.Focus(Windows.UI.Xaml.FocusState.Programmatic); _launcher.SearchBox.Focus(Windows.UI.Xaml.FocusState.Programmatic);
UpdatePosition(); UpdatePosition();
_settings.ActivateTimes++; _settings.ActivateTimes++;
if (!_viewModel.LastQuerySelected) if (!_viewModel.LastQuerySelected)
{ {
_viewModel.LastQuerySelected = true; _viewModel.LastQuerySelected = true;
} }
} }
} }
}; };
} }
private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{ {
if (args != null && args.ChosenSuggestion != null) if (args != null && args.ChosenSuggestion != null)
{ {
ResultViewModel result = (ResultViewModel)args.ChosenSuggestion; ResultViewModel result = (ResultViewModel)args.ChosenSuggestion;
_ = result.Result.Action != null && result.Result.Action(new ActionContext { }); if (result != null)
} {
} _viewModel.Results.SelectedItem = result;
_viewModel.OpenResultCommand.Execute(null);
private void QueryTextBox_TextChanged(Windows.UI.Xaml.Controls.AutoSuggestBox sender, Windows.UI.Xaml.Controls.AutoSuggestBoxTextChangedEventArgs args) }
{ }
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) }
{
_viewModel.QueryText = sender.Text; private void QueryTextBox_TextChanged(Windows.UI.Xaml.Controls.AutoSuggestBox sender, Windows.UI.Xaml.Controls.AutoSuggestBoxTextChangedEventArgs args)
} {
} if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
} {
_viewModel.QueryText = sender.Text;
}
}
}
} }