[stylecop] Launcher (#5968)

* Should be last stylecop change!

* two more tweaks
This commit is contained in:
Clint Rutkas
2020-08-14 13:35:06 -07:00
committed by GitHub
parent e0a1b478a1
commit 6514712054
9 changed files with 594 additions and 597 deletions

View File

@@ -13,15 +13,13 @@ namespace Wox
public partial class ActionKeywords : Window public partial class ActionKeywords : Window
{ {
private readonly Internationalization _translater = InternationalizationManager.Instance; private readonly Internationalization _translater = InternationalizationManager.Instance;
private readonly PluginPair _plugin;
private PluginPair _plugin; public ActionKeywords(string pluginId)
private Settings _settings;
public ActionKeywords(string pluginId, Settings settings)
{ {
InitializeComponent(); InitializeComponent();
_plugin = PluginManager.GetPluginForId(pluginId); _plugin = PluginManager.GetPluginForId(pluginId);
_settings = settings;
if (_plugin == null) if (_plugin == null)
{ {
MessageBox.Show(_translater.GetTranslation("cannotFindSpecifiedPlugin")); MessageBox.Show(_translater.GetTranslation("cannotFindSpecifiedPlugin"));

View File

@@ -16,7 +16,9 @@ namespace PowerLauncher.Helper
{ {
public static class WindowsInteropHelper public static class WindowsInteropHelper
{ {
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Matching COM")]
private const int GWL_STYLE = -16; // WPF's Message code for Title Bar's Style private const int GWL_STYLE = -16; // WPF's Message code for Title Bar's Style
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Matching COM")]
private const int WS_SYSMENU = 0x80000; // WPF's Message code for System Menu private const int WS_SYSMENU = 0x80000; // WPF's Message code for System Menu
private static IntPtr _hwnd_shell; private static IntPtr _hwnd_shell;
private static IntPtr _hwnd_desktop; private static IntPtr _hwnd_desktop;
@@ -42,8 +44,8 @@ namespace PowerLauncher.Helper
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct INPUT internal struct INPUT
{ {
public INPUTTYPE type; public INPUTTYPE Type;
public InputUnion data; public InputUnion Data;
public static int Size public static int Size
{ {
@@ -52,6 +54,7 @@ namespace PowerLauncher.Helper
} }
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching COM")]
internal struct InputUnion internal struct InputUnion
{ {
[FieldOffset(0)] [FieldOffset(0)]
@@ -63,6 +66,7 @@ namespace PowerLauncher.Helper
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching COM")]
internal struct MOUSEINPUT internal struct MOUSEINPUT
{ {
internal int dx; internal int dx;
@@ -74,6 +78,7 @@ namespace PowerLauncher.Helper
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching COM")]
internal struct KEYBDINPUT internal struct KEYBDINPUT
{ {
internal short wVk; internal short wVk;
@@ -84,6 +89,7 @@ namespace PowerLauncher.Helper
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Matching COM")]
internal struct HARDWAREINPUT internal struct HARDWAREINPUT
{ {
internal int uMsg; internal int uMsg;
@@ -98,10 +104,10 @@ namespace PowerLauncher.Helper
INPUTHARDWARE = 2, INPUTHARDWARE = 2,
} }
private const string WINDOW_CLASS_CONSOLE = "ConsoleWindowClass"; private const string WindowClassConsole = "ConsoleWindowClass";
private const string WINDOW_CLASS_WINTAB = "Flip3D"; private const string WindowClassWinTab = "Flip3D";
private const string WINDOW_CLASS_PROGMAN = "Progman"; private const string WindowClassProgman = "Progman";
private const string WINDOW_CLASS_WORKERW = "WorkerW"; private const string WindowClassWorkerW = "WorkerW";
public static bool IsWindowFullscreen() public static bool IsWindowFullscreen()
{ {
@@ -118,7 +124,7 @@ namespace PowerLauncher.Helper
string windowClass = sb.ToString(); string windowClass = sb.ToString();
// for Win+Tab (Flip3D) // for Win+Tab (Flip3D)
if (windowClass == WINDOW_CLASS_WINTAB) if (windowClass == WindowClassWinTab)
{ {
return false; return false;
} }
@@ -126,13 +132,13 @@ namespace PowerLauncher.Helper
_ = NativeMethods.GetWindowRect(hWnd, out RECT appBounds); _ = NativeMethods.GetWindowRect(hWnd, out RECT appBounds);
// for console (ConsoleWindowClass), we have to check for negative dimensions // for console (ConsoleWindowClass), we have to check for negative dimensions
if (windowClass == WINDOW_CLASS_CONSOLE) if (windowClass == WindowClassConsole)
{ {
return appBounds.Top < 0 && appBounds.Bottom < 0; return appBounds.Top < 0 && appBounds.Bottom < 0;
} }
// for desktop (Progman or WorkerW, depends on the system), we have to check // for desktop (Progman or WorkerW, depends on the system), we have to check
if (windowClass == WINDOW_CLASS_PROGMAN || windowClass == WINDOW_CLASS_WORKERW) if (windowClass == WindowClassProgman || windowClass == WindowClassWorkerW)
{ {
IntPtr hWndDesktop = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null); IntPtr hWndDesktop = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
hWndDesktop = NativeMethods.FindWindowEx(hWndDesktop, IntPtr.Zero, "SysListView32", "FolderView"); hWndDesktop = NativeMethods.FindWindowEx(hWndDesktop, IntPtr.Zero, "SysListView32", "FolderView");

View File

@@ -1,437 +1,437 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Timers; using System.Timers;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.PowerLauncher.Telemetry; using Microsoft.PowerLauncher.Telemetry;
using Microsoft.PowerToys.Telemetry; using Microsoft.PowerToys.Telemetry;
using PowerLauncher.Helper; using PowerLauncher.Helper;
using PowerLauncher.ViewModel; using PowerLauncher.ViewModel;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
using KeyEventArgs = System.Windows.Input.KeyEventArgs; using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using Screen = System.Windows.Forms.Screen; using Screen = System.Windows.Forms.Screen;
namespace PowerLauncher namespace PowerLauncher
{ {
public partial class MainWindow : IDisposable public partial class MainWindow : IDisposable
{ {
private Settings _settings; private readonly Settings _settings;
private MainViewModel _viewModel; private readonly MainViewModel _viewModel;
private bool _isTextSetProgrammatically; private bool _isTextSetProgrammatically;
private bool _deletePressed = false; private bool _deletePressed = false;
private Timer _firstDeleteTimer = new Timer(); private Timer _firstDeleteTimer = new Timer();
private bool _coldStateHotkeyPressed = false; private bool _coldStateHotkeyPressed = false;
public MainWindow(Settings settings, MainViewModel mainVM) public MainWindow(Settings settings, MainViewModel mainVM)
: this() : this()
{ {
DataContext = mainVM; DataContext = mainVM;
_viewModel = mainVM; _viewModel = mainVM;
_settings = settings; _settings = settings;
InitializeComponent(); InitializeComponent();
_firstDeleteTimer.Elapsed += CheckForFirstDelete; _firstDeleteTimer.Elapsed += CheckForFirstDelete;
_firstDeleteTimer.Interval = 1000; _firstDeleteTimer.Interval = 1000;
} }
private void CheckForFirstDelete(object sender, ElapsedEventArgs e) private void CheckForFirstDelete(object sender, ElapsedEventArgs e)
{ {
if (_firstDeleteTimer != null) if (_firstDeleteTimer != null)
{ {
_firstDeleteTimer.Stop(); _firstDeleteTimer.Stop();
if (_deletePressed) if (_deletePressed)
{ {
PowerToysTelemetry.Log.WriteEvent(new LauncherFirstDeleteEvent()); PowerToysTelemetry.Log.WriteEvent(new LauncherFirstDeleteEvent());
} }
} }
} }
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 BringProcessToForeground() private void BringProcessToForeground()
{ {
// Use SendInput hack to allow Activate to work - required to resolve focus issue https://github.com/microsoft/PowerToys/issues/4270 // Use SendInput hack to allow Activate to work - required to resolve focus issue https://github.com/microsoft/PowerToys/issues/4270
WindowsInteropHelper.INPUT input = new WindowsInteropHelper.INPUT { type = WindowsInteropHelper.INPUTTYPE.INPUTMOUSE, data = { } }; WindowsInteropHelper.INPUT input = new WindowsInteropHelper.INPUT { Type = WindowsInteropHelper.INPUTTYPE.INPUTMOUSE, Data = { } };
WindowsInteropHelper.INPUT[] inputs = new WindowsInteropHelper.INPUT[] { input }; WindowsInteropHelper.INPUT[] inputs = new WindowsInteropHelper.INPUT[] { input };
// Send empty mouse event. This makes this thread the last to send input, and hence allows it to pass foreground permission checks // Send empty mouse event. This makes this thread the last to send input, and hence allows it to pass foreground permission checks
_ = NativeMethods.SendInput(1, inputs, WindowsInteropHelper.INPUT.Size); _ = NativeMethods.SendInput(1, inputs, WindowsInteropHelper.INPUT.Size);
Activate(); Activate();
} }
private void OnLoaded(object sender, RoutedEventArgs e) private void OnLoaded(object sender, RoutedEventArgs e)
{ {
WindowsInteropHelper.DisableControlBox(this); WindowsInteropHelper.DisableControlBox(this);
InitializePosition(); InitializePosition();
SearchBox.QueryTextBox.DataContext = _viewModel; SearchBox.QueryTextBox.DataContext = _viewModel;
SearchBox.QueryTextBox.PreviewKeyDown += Launcher_KeyDown; SearchBox.QueryTextBox.PreviewKeyDown += Launcher_KeyDown;
SearchBox.QueryTextBox.TextChanged += QueryTextBox_TextChanged; SearchBox.QueryTextBox.TextChanged += QueryTextBox_TextChanged;
// Set initial language flow direction // Set initial language flow direction
SearchBox_UpdateFlowDirection(); SearchBox_UpdateFlowDirection();
// Register language changed event // Register language changed event
InputLanguageManager.Current.InputLanguageChanged += SearchBox_InputLanguageChanged; InputLanguageManager.Current.InputLanguageChanged += SearchBox_InputLanguageChanged;
SearchBox.QueryTextBox.Focus(); SearchBox.QueryTextBox.Focus();
ListBox.DataContext = _viewModel; ListBox.DataContext = _viewModel;
ListBox.SuggestionsList.SelectionChanged += SuggestionsList_SelectionChanged; ListBox.SuggestionsList.SelectionChanged += SuggestionsList_SelectionChanged;
ListBox.SuggestionsList.PreviewMouseLeftButtonUp += SuggestionsList_PreviewMouseLeftButtonUp; ListBox.SuggestionsList.PreviewMouseLeftButtonUp += SuggestionsList_PreviewMouseLeftButtonUp;
_viewModel.PropertyChanged += ViewModel_PropertyChanged; _viewModel.PropertyChanged += ViewModel_PropertyChanged;
BringProcessToForeground(); BringProcessToForeground();
} }
private void SuggestionsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) private void SuggestionsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{ {
var result = ((FrameworkElement)e.OriginalSource).DataContext; var result = ((FrameworkElement)e.OriginalSource).DataContext;
if (result != null) if (result != null)
{ {
// This may be null if the tapped item was one of the context buttons (run as admin etc). // This may be null if the tapped item was one of the context buttons (run as admin etc).
if (result is ResultViewModel resultVM) if (result is ResultViewModel resultVM)
{ {
_viewModel.Results.SelectedItem = resultVM; _viewModel.Results.SelectedItem = resultVM;
_viewModel.OpenResultCommand.Execute(null); _viewModel.OpenResultCommand.Execute(null);
} }
} }
} }
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs 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)
{ {
// Not called on first launch // Not called on first launch
// Additionally called when deactivated by clicking on screen // Additionally called when deactivated by clicking on screen
UpdatePosition(); UpdatePosition();
BringProcessToForeground(); BringProcessToForeground();
if (!_viewModel.LastQuerySelected) if (!_viewModel.LastQuerySelected)
{ {
_viewModel.LastQuerySelected = true; _viewModel.LastQuerySelected = true;
} }
} }
} }
else if (e.PropertyName == nameof(MainViewModel.SystemQueryText)) else if (e.PropertyName == nameof(MainViewModel.SystemQueryText))
{ {
_isTextSetProgrammatically = true; _isTextSetProgrammatically = true;
if (_viewModel.Results != null) if (_viewModel.Results != null)
{ {
SearchBox.QueryTextBox.Text = MainViewModel.GetSearchText( SearchBox.QueryTextBox.Text = MainViewModel.GetSearchText(
_viewModel.Results.SelectedIndex, _viewModel.Results.SelectedIndex,
_viewModel.SystemQueryText, _viewModel.SystemQueryText,
_viewModel.QueryText); _viewModel.QueryText);
} }
} }
} }
private void OnMouseDown(object sender, MouseButtonEventArgs e) private void OnMouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Left) if (e.ChangedButton == MouseButton.Left)
{ {
DragMove(); DragMove();
} }
} }
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 OnActivated(object sender, EventArgs e) private void OnActivated(object sender, EventArgs e)
{ {
if (_settings.ClearInputOnLaunch) if (_settings.ClearInputOnLaunch)
{ {
_viewModel.ClearQueryCommand.Execute(null); _viewModel.ClearQueryCommand.Execute(null);
} }
} }
private void OnDeactivated(object sender, EventArgs e) private void OnDeactivated(object sender, EventArgs e)
{ {
if (_settings.HideWhenDeactivated) if (_settings.HideWhenDeactivated)
{ {
// (this.FindResource("OutroStoryboard") as Storyboard).Begin(); // (this.FindResource("OutroStoryboard") as Storyboard).Begin();
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
{ {
Top = WindowTop(); Top = WindowTop();
Left = WindowLeft(); Left = WindowLeft();
} }
} }
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;
} }
} }
/// <summary> /// <summary>
/// Calculates X co-ordinate of main window top left corner. /// Calculates X co-ordinate of main window top left corner.
/// </summary> /// </summary>
/// <returns>X co-ordinate of main window top left corner</returns> /// <returns>X co-ordinate of main window top left corner</returns>
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 - SearchBox.ActualHeight) / 4) + dip1.Y; var top = ((dip2.Y - SearchBox.ActualHeight) / 4) + dip1.Y;
return top; return top;
} }
private void Launcher_KeyDown(object sender, KeyEventArgs e) private void Launcher_KeyDown(object sender, KeyEventArgs e)
{ {
if (e.Key == Key.Tab && Keyboard.IsKeyDown(Key.LeftShift)) if (e.Key == Key.Tab && Keyboard.IsKeyDown(Key.LeftShift))
{ {
_viewModel.SelectPrevTabItemCommand.Execute(null); _viewModel.SelectPrevTabItemCommand.Execute(null);
UpdateTextBoxToSelectedItem(); UpdateTextBoxToSelectedItem();
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.Tab) else if (e.Key == Key.Tab)
{ {
_viewModel.SelectNextTabItemCommand.Execute(null); _viewModel.SelectNextTabItemCommand.Execute(null);
UpdateTextBoxToSelectedItem(); UpdateTextBoxToSelectedItem();
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.Down) else if (e.Key == Key.Down)
{ {
_viewModel.SelectNextItemCommand.Execute(null); _viewModel.SelectNextItemCommand.Execute(null);
UpdateTextBoxToSelectedItem(); UpdateTextBoxToSelectedItem();
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);
UpdateTextBoxToSelectedItem(); UpdateTextBoxToSelectedItem();
e.Handled = true; e.Handled = true;
} }
else if (e.Key == Key.Right) else if (e.Key == Key.Right)
{ {
if (SearchBox.QueryTextBox.CaretIndex == SearchBox.QueryTextBox.Text.Length) if (SearchBox.QueryTextBox.CaretIndex == SearchBox.QueryTextBox.Text.Length)
{ {
_viewModel.SelectNextContextMenuItemCommand.Execute(null); _viewModel.SelectNextContextMenuItemCommand.Execute(null);
e.Handled = true; e.Handled = true;
} }
} }
else if (e.Key == Key.Left) else if (e.Key == Key.Left)
{ {
if (SearchBox.QueryTextBox.CaretIndex == SearchBox.QueryTextBox.Text.Length) if (SearchBox.QueryTextBox.CaretIndex == SearchBox.QueryTextBox.Text.Length)
{ {
if (_viewModel.Results != null && _viewModel.Results.IsContextMenuItemSelected()) if (_viewModel.Results != null && _viewModel.Results.IsContextMenuItemSelected())
{ {
_viewModel.SelectPreviousContextMenuItemCommand.Execute(null); _viewModel.SelectPreviousContextMenuItemCommand.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;
} }
else if (e.Key == Key.Back) else if (e.Key == Key.Back)
{ {
_deletePressed = true; _deletePressed = true;
} }
else else
{ {
_viewModel.HandleContextMenu(e.Key, Keyboard.Modifiers); _viewModel.HandleContextMenu(e.Key, Keyboard.Modifiers);
} }
} }
private void UpdateTextBoxToSelectedItem() private void UpdateTextBoxToSelectedItem()
{ {
var itemText = _viewModel?.Results?.SelectedItem?.ToString() ?? null; var itemText = _viewModel?.Results?.SelectedItem?.ToString() ?? null;
if (!string.IsNullOrEmpty(itemText)) if (!string.IsNullOrEmpty(itemText))
{ {
_viewModel.ChangeQueryText(itemText); _viewModel.ChangeQueryText(itemText);
} }
} }
private void SuggestionsList_SelectionChanged(object sender, SelectionChangedEventArgs e) private void SuggestionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ {
ListView listview = (ListView)sender; ListView listview = (ListView)sender;
_viewModel.Results.SelectedItem = (ResultViewModel)listview.SelectedItem; _viewModel.Results.SelectedItem = (ResultViewModel)listview.SelectedItem;
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) if (e.AddedItems.Count > 0 && e.AddedItems[0] != null)
{ {
listview.ScrollIntoView(e.AddedItems[0]); listview.ScrollIntoView(e.AddedItems[0]);
} }
// To populate the AutoCompleteTextBox as soon as the selection is changed or set. // To populate the AutoCompleteTextBox as soon as the selection is changed or set.
// Setting it here instead of when the text is changed as there is a delay in executing the query and populating the result // Setting it here instead of when the text is changed as there is a delay in executing the query and populating the result
if (_viewModel.Results != null) if (_viewModel.Results != null)
{ {
SearchBox.AutoCompleteTextBlock.Text = MainViewModel.GetAutoCompleteText( SearchBox.AutoCompleteTextBlock.Text = MainViewModel.GetAutoCompleteText(
_viewModel.Results.SelectedIndex, _viewModel.Results.SelectedIndex,
_viewModel.Results.SelectedItem?.ToString(), _viewModel.Results.SelectedItem?.ToString(),
_viewModel.QueryText); _viewModel.QueryText);
} }
} }
private bool disposedValue = false; private bool disposedValue = false;
private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e) private void QueryTextBox_TextChanged(object sender, TextChangedEventArgs e)
{ {
if (_isTextSetProgrammatically) if (_isTextSetProgrammatically)
{ {
var textBox = (TextBox)sender; var textBox = (TextBox)sender;
textBox.SelectionStart = textBox.Text.Length; textBox.SelectionStart = textBox.Text.Length;
_isTextSetProgrammatically = false; _isTextSetProgrammatically = false;
} }
else else
{ {
var text = ((TextBox)sender).Text; var text = ((TextBox)sender).Text;
if (string.IsNullOrEmpty(text)) if (string.IsNullOrEmpty(text))
{ {
SearchBox.AutoCompleteTextBlock.Text = string.Empty; SearchBox.AutoCompleteTextBlock.Text = string.Empty;
} }
_viewModel.QueryText = text; _viewModel.QueryText = text;
_viewModel.Query(); _viewModel.Query();
} }
} }
private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{ {
if (e.ChangedButton == MouseButton.Right) if (e.ChangedButton == MouseButton.Right)
{ {
e.Handled = true; e.Handled = true;
} }
} }
private void OnVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e) private void OnVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
{ {
if (Visibility == Visibility.Visible) if (Visibility == Visibility.Visible)
{ {
_deletePressed = false; _deletePressed = false;
if (_firstDeleteTimer != null) if (_firstDeleteTimer != null)
{ {
_firstDeleteTimer.Start(); _firstDeleteTimer.Start();
} }
// (this.FindResource("IntroStoryboard") as Storyboard).Begin(); // (this.FindResource("IntroStoryboard") as Storyboard).Begin();
SearchBox.QueryTextBox.Focus(); SearchBox.QueryTextBox.Focus();
Keyboard.Focus(SearchBox.QueryTextBox); Keyboard.Focus(SearchBox.QueryTextBox);
_settings.ActivateTimes++; _settings.ActivateTimes++;
if (!string.IsNullOrEmpty(SearchBox.QueryTextBox.Text)) if (!string.IsNullOrEmpty(SearchBox.QueryTextBox.Text))
{ {
SearchBox.QueryTextBox.SelectAll(); SearchBox.QueryTextBox.SelectAll();
} }
// Log the time taken from pressing the hotkey till launcher is visible as separate events depending on if it's the first hotkey invoke or second // Log the time taken from pressing the hotkey till launcher is visible as separate events depending on if it's the first hotkey invoke or second
if (!_coldStateHotkeyPressed) if (!_coldStateHotkeyPressed)
{ {
PowerToysTelemetry.Log.WriteEvent(new LauncherColdStateHotkeyEvent() { HotkeyToVisibleTimeMs = _viewModel.GetHotkeyEventTimeMs() }); PowerToysTelemetry.Log.WriteEvent(new LauncherColdStateHotkeyEvent() { HotkeyToVisibleTimeMs = _viewModel.GetHotkeyEventTimeMs() });
_coldStateHotkeyPressed = true; _coldStateHotkeyPressed = true;
} }
else else
{ {
PowerToysTelemetry.Log.WriteEvent(new LauncherWarmStateHotkeyEvent() { HotkeyToVisibleTimeMs = _viewModel.GetHotkeyEventTimeMs() }); PowerToysTelemetry.Log.WriteEvent(new LauncherWarmStateHotkeyEvent() { HotkeyToVisibleTimeMs = _viewModel.GetHotkeyEventTimeMs() });
} }
} }
else else
{ {
if (_firstDeleteTimer != null) if (_firstDeleteTimer != null)
{ {
_firstDeleteTimer.Stop(); _firstDeleteTimer.Stop();
} }
} }
} }
private void OutroStoryboard_Completed(object sender, EventArgs e) private void OutroStoryboard_Completed(object sender, EventArgs e)
{ {
Hide(); Hide();
} }
private void SearchBox_UpdateFlowDirection() private void SearchBox_UpdateFlowDirection()
{ {
SearchBox.QueryTextBox.FlowDirection = MainViewModel.GetLanguageFlowDirection(); SearchBox.QueryTextBox.FlowDirection = MainViewModel.GetLanguageFlowDirection();
SearchBox.AutoCompleteTextBlock.FlowDirection = MainViewModel.GetLanguageFlowDirection(); SearchBox.AutoCompleteTextBlock.FlowDirection = MainViewModel.GetLanguageFlowDirection();
} }
private void SearchBox_InputLanguageChanged(object sender, InputLanguageEventArgs e) private void SearchBox_InputLanguageChanged(object sender, InputLanguageEventArgs e)
{ {
SearchBox_UpdateFlowDirection(); SearchBox_UpdateFlowDirection();
} }
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!disposedValue) if (!disposedValue)
{ {
if (disposing) if (disposing)
{ {
if (_firstDeleteTimer != null) if (_firstDeleteTimer != null)
{ {
_firstDeleteTimer.Dispose(); _firstDeleteTimer.Dispose();
} }
} }
// TODO: free unmanaged resources (unmanaged objects) and override finalizer // TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null // TODO: set large fields to null
_firstDeleteTimer = null; _firstDeleteTimer = null;
disposedValue = true; disposedValue = true;
} }
} }
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~MainWindow() // ~MainWindow()
// { // {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false); // Dispose(disposing: false);
// } // }
public void Dispose() public void Dispose()
{ {
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true); Dispose(disposing: true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
} }
} }

View File

@@ -259,7 +259,7 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
</ItemGroup> </ItemGroup>
<!--<ItemGroup> <ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs"> <Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link> <Link>GlobalSuppressions.cs</Link>
</Compile> </Compile>
@@ -273,5 +273,5 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup>--> </ItemGroup>
</Project> </Project>

View File

@@ -1,119 +1,119 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.PowerToys.Settings.UI.Lib; using Microsoft.PowerToys.Settings.UI.Lib;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Infrastructure.Hotkey; using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.UserSettings; using Wox.Infrastructure.UserSettings;
using Wox.Plugin; using Wox.Plugin;
namespace PowerLauncher namespace PowerLauncher
{ {
// Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes // Watch for /Local/Microsoft/PowerToys/Launcher/Settings.json changes
public class SettingsWatcher : BaseModel public class SettingsWatcher : BaseModel
{ {
private static int _maxRetries = 10; private const int MaxRetries = 10;
private static object _watcherSyncObject = new object(); private static readonly object _watcherSyncObject = new object();
private FileSystemWatcher _watcher; private readonly FileSystemWatcher _watcher;
private Settings _settings; private readonly Settings _settings;
public SettingsWatcher(Settings settings) public SettingsWatcher(Settings settings)
{ {
_settings = settings; _settings = settings;
// Set up watcher // Set up watcher
_watcher = Microsoft.PowerToys.Settings.UI.Lib.Utilities.Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", OverloadSettings); _watcher = Microsoft.PowerToys.Settings.UI.Lib.Utilities.Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", OverloadSettings);
// Load initial settings file // Load initial settings file
OverloadSettings(); OverloadSettings();
} }
public void OverloadSettings() public void OverloadSettings()
{ {
Monitor.Enter(_watcherSyncObject); Monitor.Enter(_watcherSyncObject);
var retry = true; var retry = true;
var retryCount = 0; var retryCount = 0;
while (retry) while (retry)
{ {
try try
{ {
retryCount++; retryCount++;
if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName)) if (!SettingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
{ {
Debug.WriteLine("PT Run settings.json was missing, creating a new one"); Debug.WriteLine("PT Run settings.json was missing, creating a new one");
var defaultSettings = new PowerLauncherSettings(); var defaultSettings = new PowerLauncherSettings();
defaultSettings.Save(); defaultSettings.Save();
} }
var overloadSettings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName); var overloadSettings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher); var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher);
if (_settings.Hotkey != openPowerlauncher) if (_settings.Hotkey != openPowerlauncher)
{ {
_settings.Hotkey = openPowerlauncher; _settings.Hotkey = openPowerlauncher;
} }
var shell = PluginManager.AllPlugins.Find(pp => pp.Metadata.Name == "Shell"); var shell = PluginManager.AllPlugins.Find(pp => pp.Metadata.Name == "Shell");
if (shell != null) if (shell != null)
{ {
var shellSettings = shell.Plugin as ISettingProvider; var shellSettings = shell.Plugin as ISettingProvider;
shellSettings.UpdateSettings(overloadSettings); shellSettings.UpdateSettings(overloadSettings);
} }
if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults) if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults)
{ {
_settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults; _settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults;
} }
if (_settings.IgnoreHotkeysOnFullscreen != overloadSettings.Properties.IgnoreHotkeysInFullscreen) if (_settings.IgnoreHotkeysOnFullscreen != overloadSettings.Properties.IgnoreHotkeysInFullscreen)
{ {
_settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen; _settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen;
} }
var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer Plugin", StringComparison.OrdinalIgnoreCase)); var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer Plugin", StringComparison.OrdinalIgnoreCase));
if (indexer != null) if (indexer != null)
{ {
var indexerSettings = indexer.Plugin as ISettingProvider; var indexerSettings = indexer.Plugin as ISettingProvider;
indexerSettings.UpdateSettings(overloadSettings); indexerSettings.UpdateSettings(overloadSettings);
} }
if (_settings.ClearInputOnLaunch != overloadSettings.Properties.ClearInputOnLaunch) if (_settings.ClearInputOnLaunch != overloadSettings.Properties.ClearInputOnLaunch)
{ {
_settings.ClearInputOnLaunch = overloadSettings.Properties.ClearInputOnLaunch; _settings.ClearInputOnLaunch = overloadSettings.Properties.ClearInputOnLaunch;
} }
retry = false; retry = false;
} }
// the settings application can hold a lock on the settings.json file which will result in a IOException. // the settings application can hold a lock on the settings.json file which will result in a IOException.
// This should be changed to properly synch with the settings app instead of retrying. // This should be changed to properly synch with the settings app instead of retrying.
catch (IOException e) catch (IOException e)
{ {
if (retryCount > _maxRetries) if (retryCount > MaxRetries)
{ {
retry = false; retry = false;
} }
Thread.Sleep(1000); Thread.Sleep(1000);
Debug.WriteLine(e.Message); Debug.WriteLine(e.Message);
} }
} }
Monitor.Exit(_watcherSyncObject); Monitor.Exit(_watcherSyncObject);
} }
private static string ConvertHotkey(HotkeySettings hotkey) private static string ConvertHotkey(HotkeySettings hotkey)
{ {
Key key = KeyInterop.KeyFromVirtualKey(hotkey.Code); Key key = KeyInterop.KeyFromVirtualKey(hotkey.Code);
HotkeyModel model = new HotkeyModel(hotkey.Alt, hotkey.Shift, hotkey.Win, hotkey.Ctrl, key); HotkeyModel model = new HotkeyModel(hotkey.Alt, hotkey.Shift, hotkey.Win, hotkey.Ctrl, key);
return model.ToString(); return model.ToString();
} }
} }
} }

View File

@@ -12,7 +12,7 @@ namespace PowerLauncher.Storage
{ {
public List<HistoryItem> Items { get; } = new List<HistoryItem>(); public List<HistoryItem> Items { get; } = new List<HistoryItem>();
private int _maxHistory = 300; private readonly int _maxHistory = 300;
public void Add(string query) public void Add(string query)
{ {

View File

@@ -12,7 +12,7 @@ namespace PowerLauncher.Storage
public class UserSelectedRecord public class UserSelectedRecord
{ {
[JsonProperty] [JsonProperty]
private Dictionary<string, int> records = new Dictionary<string, int>(); private readonly Dictionary<string, int> records = new Dictionary<string, int>();
public void Add(Result result) public void Add(Result result)
{ {

View File

@@ -29,7 +29,7 @@ namespace PowerLauncher.ViewModel
{ {
public class MainViewModel : BaseModel, ISavable, IDisposable public class MainViewModel : BaseModel, ISavable, IDisposable
{ {
private static Query _emptyQuery = new Query(); private static readonly Query _emptyQuery = new Query();
private static bool _disposed; private static bool _disposed;
private readonly WoxJsonStorage<QueryHistory> _historyItemsStorage; private readonly WoxJsonStorage<QueryHistory> _historyItemsStorage;
@@ -41,6 +41,7 @@ namespace PowerLauncher.ViewModel
private readonly TopMostRecord _topMostRecord; private readonly TopMostRecord _topMostRecord;
private readonly object _addResultsLock = new object(); private readonly object _addResultsLock = new object();
private readonly Internationalization _translator = InternationalizationManager.Instance; private readonly Internationalization _translator = InternationalizationManager.Instance;
private readonly System.Diagnostics.Stopwatch _hotkeyTimer = new System.Diagnostics.Stopwatch();
private Query _currentQuery; private Query _currentQuery;
private string _queryTextBeforeLeaveResults; private string _queryTextBeforeLeaveResults;
@@ -49,15 +50,13 @@ namespace PowerLauncher.ViewModel
private CancellationToken _updateToken; private CancellationToken _updateToken;
private bool _saved; private bool _saved;
private HotkeyManager _hotkeyManager { get; set; }
private ushort _hotkeyHandle; private ushort _hotkeyHandle;
private System.Diagnostics.Stopwatch hotkeyTimer = new System.Diagnostics.Stopwatch();
internal HotkeyManager HotkeyManager { get; set; }
public MainViewModel(Settings settings) public MainViewModel(Settings settings)
{ {
_hotkeyManager = new HotkeyManager(); HotkeyManager = new HotkeyManager();
_saved = false; _saved = false;
_queryTextBeforeLeaveResults = string.Empty; _queryTextBeforeLeaveResults = string.Empty;
_currentQuery = _emptyQuery; _currentQuery = _emptyQuery;
@@ -88,7 +87,7 @@ namespace PowerLauncher.ViewModel
{ {
if (!string.IsNullOrEmpty(_settings.PreviousHotkey)) if (!string.IsNullOrEmpty(_settings.PreviousHotkey))
{ {
_hotkeyManager.UnregisterHotkey(_hotkeyHandle); HotkeyManager.UnregisterHotkey(_hotkeyHandle);
} }
if (!string.IsNullOrEmpty(_settings.Hotkey)) if (!string.IsNullOrEmpty(_settings.Hotkey))
@@ -603,12 +602,6 @@ namespace PowerLauncher.ViewModel
return selected; return selected;
} }
private bool ContextMenuSelected()
{
var selected = SelectedResults == ContextMenu;
return selected;
}
private bool HistorySelected() private bool HistorySelected()
{ {
var selected = SelectedResults == History; var selected = SelectedResults == History;
@@ -632,10 +625,10 @@ namespace PowerLauncher.ViewModel
Shift = hotkeyModel.Shift, Shift = hotkeyModel.Shift,
Ctrl = hotkeyModel.Ctrl, Ctrl = hotkeyModel.Ctrl,
Win = hotkeyModel.Win, Win = hotkeyModel.Win,
Key = (byte)KeyInterop.VirtualKeyFromKey(hotkeyModel.CharKey) Key = (byte)KeyInterop.VirtualKeyFromKey(hotkeyModel.CharKey),
}; };
_hotkeyHandle = _hotkeyManager.RegisterHotkey(hotkey, action); _hotkeyHandle = HotkeyManager.RegisterHotkey(hotkey, action);
} }
#pragma warning disable CA1031 // Do not catch general exception types #pragma warning disable CA1031 // Do not catch general exception types
catch (Exception) catch (Exception)
@@ -799,7 +792,7 @@ namespace PowerLauncher.ViewModel
{ {
if (!plugin.Metadata.Disabled && plugin.Metadata.Name != "Window Walker") if (!plugin.Metadata.Disabled && plugin.Metadata.Name != "Window Walker")
{ {
var _ = PluginManager.QueryForPlugin(plugin, query); _ = PluginManager.QueryForPlugin(plugin, query);
} }
} }
} }
@@ -877,10 +870,10 @@ namespace PowerLauncher.ViewModel
{ {
if (_hotkeyHandle != 0) if (_hotkeyHandle != 0)
{ {
_hotkeyManager?.UnregisterHotkey(_hotkeyHandle); HotkeyManager?.UnregisterHotkey(_hotkeyHandle);
} }
_hotkeyManager?.Dispose(); HotkeyManager?.Dispose();
_updateSource?.Dispose(); _updateSource?.Dispose();
_disposed = true; _disposed = true;
} }
@@ -895,16 +888,16 @@ namespace PowerLauncher.ViewModel
public void StartHotkeyTimer() public void StartHotkeyTimer()
{ {
hotkeyTimer.Start(); _hotkeyTimer.Start();
} }
public long GetHotkeyEventTimeMs() public long GetHotkeyEventTimeMs()
{ {
hotkeyTimer.Stop(); _hotkeyTimer.Stop();
long recordedTime = hotkeyTimer.ElapsedMilliseconds; long recordedTime = _hotkeyTimer.ElapsedMilliseconds;
// Reset the stopwatch and return the time elapsed // Reset the stopwatch and return the time elapsed
hotkeyTimer.Reset(); _hotkeyTimer.Reset();
return recordedTime; return recordedTime;
} }
} }

View File

@@ -9,7 +9,7 @@ namespace PowerLauncher.ViewModel
{ {
public class RelayCommand : ICommand public class RelayCommand : ICommand
{ {
private Action<object> _action; private readonly Action<object> _action;
public RelayCommand(Action<object> action) public RelayCommand(Action<object> action)
{ {