From 93af4fc6b0fb60ddb30ba4486f38ecc2fb45ed2b Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Tue, 9 Jun 2020 09:16:20 -0700 Subject: [PATCH] Fixed initial focus on WPF issue (#4113) * Fixed initial focus on WPF issue * Added comment for property change * Updated function naming for visibility callback --- .../launcher/PowerLauncher/MainWindow.xaml | 1 + .../launcher/PowerLauncher/MainWindow.xaml.cs | 57 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml b/src/modules/launcher/PowerLauncher/MainWindow.xaml index 122a6301f7..2f0240408e 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml @@ -21,6 +21,7 @@ Background="Transparent" LocationChanged="OnLocationChanged" Deactivated="OnDeactivated" + IsVisibleChanged="OnVisibilityChanged" Visibility="{Binding MainWindowVisibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" d:DataContext="{d:DesignInstance vm:MainViewModel}"> diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index c829c1c4bc..67e3df34f4 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -78,6 +78,8 @@ namespace PowerLauncher ListBox.SuggestionsList.SelectionChanged += SuggestionsList_SelectionChanged; ListBox.SuggestionsList.PreviewMouseLeftButtonUp += SuggestionsList_PreviewMouseLeftButtonUp; _viewModel.PropertyChanged += ViewModel_PropertyChanged; + + Activate(); } private void SuggestionsList_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) @@ -98,32 +100,16 @@ namespace PowerLauncher private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(MainViewModel.MainWindowVisibility)) + if (Visibility == System.Windows.Visibility.Visible) { - if (Visibility == System.Windows.Visibility.Visible) + // Not called on first launch + // Additionally called when deactivated by clicking on screen + UpdatePosition(); + Activate(); + + if (!_viewModel.LastQuerySelected) { - _deletePressed = false; - _firstDeleteTimer.Start(); - Activate(); - //(this.FindResource("IntroStoryboard") as Storyboard).Begin(); - - UpdatePosition(); - SearchBox.QueryTextBox.Focus(); - _settings.ActivateTimes++; - - if (!_viewModel.LastQuerySelected) - { - _viewModel.LastQuerySelected = true; - } - - if (!String.IsNullOrEmpty(SearchBox.QueryTextBox.Text)) - { - SearchBox.QueryTextBox.SelectAll(); - } - } - else - { - _firstDeleteTimer.Stop(); + _viewModel.LastQuerySelected = true; } } else if (e.PropertyName == nameof(MainViewModel.SystemQueryText)) @@ -331,9 +317,28 @@ namespace PowerLauncher } } - private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) + private void OnVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e) { - System.Diagnostics.Debug.WriteLine("Changed"); + if (Visibility == Visibility.Visible) + { + _deletePressed = false; + _firstDeleteTimer.Start(); + // (this.FindResource("IntroStoryboard") as Storyboard).Begin(); + + SearchBox.QueryTextBox.Focus(); + Keyboard.Focus(SearchBox.QueryTextBox); + + _settings.ActivateTimes++; + + if (!String.IsNullOrEmpty(SearchBox.QueryTextBox.Text)) + { + SearchBox.QueryTextBox.SelectAll(); + } + } + else + { + _firstDeleteTimer.Stop(); + } } private void OutroStoryboard_Completed(object sender, EventArgs e)